Commits

stqn committed 878ee10 Merge

Merge.

  • Participants
  • Parent commits d6c030b, 41b1335

Comments (0)

Files changed (10)

data/background.png

Old
Old image
New
New image

data/bg_1.png

Added
New image

data/bg_2.png

Added
New image
 import gameobjectmanager;
 import time;
 import map;
-import menu;
+import menu; // pour menu de pause
 import texturemanager;
 import screen;
 import inputmanager;
 import gameconstants;
 import gamepart;
 
-
 Game gGame;
 
-
 class Game : GamePart {
 
-    enum GameState {
-        MENU_MAIN,
-        MENU_OPTIONS,
-        MENU_MAP,
-        MENU_PAUSE,
-        GAME,
-        EXIT
+    bool mRunning=true;
+
+    GAMEPART_STATE mState = GAMEPART_STATE.GAME;
+    SDL_Surface* mBg;
+    GamePart mSubPart;
+
+    this() {
+        gGame = this;
+        mBg = gTextureManager.get( "background.png" );
+        new Chrono;
+		gSoundManager.play( gSoundManager.get("start.ogg"), MIX_MAX_VOLUME/2 );
     }
 
-    bool mRunning=true;
-    GameState mState;
-    SDL_Surface* mBg;
-    Menu mMenu;
-
-    this() {
-
-        gGame = this;
-
-        mBg = gTextureManager.get( "background.png" );
-
-        mMenu = new Menu(Menu.MenuType.MAIN);
-        mState = GameState.MENU_MAIN;
-
-        gInputManager.bind( IMA_UP, "up" );
-        gInputManager.bind( IMA_DOWN, "down" );
-        gInputManager.bind( IMA_LEFT, "left" );
-        gInputManager.bind( IMA_RIGHT, "right" );
-        gInputManager.bind( IMA_CROUCH, "crouch" );
-        gInputManager.bind( IMA_BACK, "escape" );
-        gInputManager.bind( IMA_FIRE, "fire" );
-        gInputManager.bind( IMA_ACTION, "enter" );
-        gInputManager.bind( IMA_PAUSE, "pause" );
-        gInputManager.bind( IMA_VOLUME_UP, "volume_up" );
-        gInputManager.bind( IMA_VOLUME_DOWN, "volume_down" );
-
-        gInputManager.loadConfig();
-
+    ~this() {
+        delete(gChrono);
+        gChrono = null;
     }
 
 
 	void update() {
-		while( mRunning ) {
 
-			gInputManager.update();
-
-			processInput();
-
-			if( mMenu ) {
-				if( mState != GameState.MENU_PAUSE ) {
-					SDL_BlitSurface( mBg, null, gScreen.mSurface, null );
-				}
-				mMenu.draw();
-			} else {
-				gameplay();
-			}
-
-			gScreen.vsync();
-
-		}
+	    if( mSubPart is null  ) {
+	        processInput();
+            gameplay();
+            render();
+	    }
+	    else {
+	        render();
+	        inPause();
+	    }
 	}
 
     void processInput() {
 
-        if( gInputManager.isReleased(IMA_BACK) ) {
-
-            switch( mState ) {
-				case GameState.MENU_MAIN:
-					// Exit game
-					mRunning = false;
-					break;
-				case GameState.MENU_OPTIONS:
-					SetState( GameState.MENU_MAIN );
-					break;
-				case GameState.MENU_MAP:
-					SetState( GameState.MENU_MAIN );
-					break;
-				case GameState.MENU_PAUSE:
-					SetState( GameState.MENU_MAIN );
-					break;
-				case GameState.GAME:
-					SetState( GameState.MENU_PAUSE );
-					break;
-				default:
-					break;
-            }
-
-        }
-
-        if( gInputManager.isReleased(IMA_PAUSE) ) {
-            switch( mState ) {
-				case GameState.GAME:
-					SetState( GameState.MENU_PAUSE );
-					break;
-				case GameState.MENU_PAUSE:
-					SetState( GameState.GAME );
-					break;
-				default:
-					break;
-            }
+        if( gInputManager.isReleased(IMA_BACK) ||
+            gInputManager.isReleased(IMA_PAUSE) ) {
+                mSubPart = new PauseMenu;
         }
 
         if( gInputManager.isReleased(IMA_VOLUME_UP)) gSoundManager.volumeUp();
         if( gInputManager.isReleased(IMA_VOLUME_DOWN)) gSoundManager.volumeDown();
 
-        //   if( gInputManager.isReleased( IMA_ESCAPE )) mRunning = false;
+    }
 
-        if( gInputManager.isReleased( IMA_UP ) && mMenu ) mMenu.move(-1);
-        if( gInputManager.isReleased( IMA_DOWN ) && mMenu ) mMenu.move(1);
-        if( gInputManager.isReleased( IMA_ACTION ) && mMenu ) mMenu.action();
+    void inPause() {
+
+        mSubPart.update();
+
+        switch( mSubPart.getState() ) {
+
+            case GAMEPART_STATE.UNPAUSE:
+                delete( mSubPart);
+            break;
+
+            case GAMEPART_STATE.EXIT:
+                delete( mSubPart);
+                mState = GAMEPART_STATE.MAIN_MENU;
+            break;
+
+            default:
+            break;
+        }
+
     }
 
 
         gView.update();
         gChrono.update();
         Messages.update();
+    }
 
+    void render() {
         SDL_BlitSurface( mBg, null, gScreen.mSurface, null );
         gView.draw();
         gChrono.draw();
         Messages.draw();
-
-    }
-
-
-    void SetState( GameState state ) {
-        switch( state ) {
-			case GameState.MENU_MAIN:
-				mMenu = new Menu(Menu.MenuType.MAIN);
-				break;
-			case GameState.MENU_OPTIONS:
-				mMenu = new Menu(Menu.MenuType.OPTIONS);
-				break;
-			case GameState.MENU_MAP:
-				gMapList = new MapList;
-				mMenu = new Menu(Menu.MenuType.MAP);
-				break;
-			case GameState.MENU_PAUSE:
-				mMenu = new Menu(Menu.MenuType.PAUSE);
-				break;
-			case GameState.GAME:
-				mMenu = null;
-				switch( mState ) {
-					case GameState.MENU_MAP:
-						new Chrono;
-						gSoundManager.play( gSoundManager.get("start.ogg"), MIX_MAX_VOLUME/2 );
-						break;
-					case GameState.MENU_PAUSE:
-						mMenu = null;
-						break;
-					default:
-						break;
-				}
-	            break;
-			case GameState.EXIT:
-				mRunning = false;
-				break;
-			default:
-				break;
-		}
-		mState = state;
     }
 
 	GAMEPART_STATE getState() {
-		if( !mRunning ) return( GAMEPART_STATE.MAIN_MENU );
-		else return( GAMEPART_STATE.GAME );
+        return( mState );
 	}
 
-
 }

src/gamemanager.d

 
 import gamepart;
 import game;
+import menu;
+import inputmanager;
+import gameconstants;
+import screen;
 
 class GameManager {
 
 	GamePart mGamePart;
 
 	this() {
-
-		mGamePart = new Game(); //new MainMenu();
-
+		mGamePart = new MainMenu();
 	}
 
 	~this() {
 
 	bool update() {
 
+        gInputManager.update();
 		mGamePart.update();
 
 		switch( mGamePart.getState()  ) {
 
-			case GAMEPART_STATE.GAME_WON:
+			case GAMEPART_STATE.MENU_MAP:
 				delete( mGamePart );
-				mGamePart = new Game(); // new GameWon();
+				mGamePart = new MapMenu();
 			break;
 
-			case GAMEPART_STATE.RESTART:
+			case GAMEPART_STATE.NEW_GAME:
 				delete( mGamePart );
 				mGamePart = new Game();
 			break;
 
+			case GAMEPART_STATE.GAME_WON:
+				delete( mGamePart );
+				mGamePart = new MainMenu(); // new GameWon();
+			break;
+
 			case GAMEPART_STATE.MAIN_MENU:
 				delete( mGamePart );
-				mGamePart = new Game(); // new MainMenu();
+				mGamePart = new MainMenu();
 			break;
 
 			case GAMEPART_STATE.EXIT:
 
 			default:
 			break;
-
+
 		}
-
+        gScreen.vsync();
 		return( true );
 	}
 
 
 module gamepart;
 
-enum GAMEPART_STATE { GAME, GAME_WON, MAIN_MENU, RESTART, EXIT };
+enum GAMEPART_STATE { GAME, GAME_WON, MAIN_MENU, EXIT, MENU_MAP, MENU_OPTIONS, NEW_GAME, UNPAUSE };
 
 interface GamePart {
 

src/inputmanager.d

 	void loadConfig() {
 		auto ic = new InputConfiguration();
 		if( ! ic.load( mConfigFilename ))
-			throw new Error("Error while laoding \"" ~ mConfigFilename ~ "\"");
+			throw new Error("Error while loading \"" ~ mConfigFilename ~ "\"");
 	}
 
 	void update() {
 import view;
 import inputmanager;
 import gamemanager;
+import gameconstants;
 
 void main() {
     new Screen;
     //new Map;
     //new Chrono;
 
-
-
 	try {
 	    new InputManager("rwkc.conf");
+	    bind_actions();
+	    gInputManager.loadConfig();
+
+
 		GameManager gm;
 		gm = new GameManager();
 		while( gm.update() ) {};
 
 
 
+void bind_actions() {
+
+    gInputManager.bind( IMA_UP, "up" );
+    gInputManager.bind( IMA_DOWN, "down" );
+    gInputManager.bind( IMA_LEFT, "left" );
+    gInputManager.bind( IMA_RIGHT, "right" );
+    gInputManager.bind( IMA_CROUCH, "crouch" );
+    gInputManager.bind( IMA_BACK, "escape" );
+    gInputManager.bind( IMA_FIRE, "fire" );
+    gInputManager.bind( IMA_ACTION, "enter" );
+    gInputManager.bind( IMA_PAUSE, "pause" );
+    gInputManager.bind( IMA_VOLUME_UP, "volume_up" );
+    gInputManager.bind( IMA_VOLUME_DOWN, "volume_down" );
+}
+

src/mainmenu.d

-
-module mainmenu;
-
 module menu;
 
 import derelict.sdl.sdl;
-import game;
 import map;
 import screen;
 import texturemanager;
+import gamepart;
+import inputmanager;
+import gameconstants;
+
+// ============ base Menu class ===============
+public static Menu gMenu;
 
-Menu gMenu;
-
-class Menu {
-
-	enum MenuType {
-		MAIN,
-		OPTIONS,
-		PAUSE,
-		LANGUAGE,
-		MAP
-	}
+class Menu : public GamePart {
 
 	char[][] mText;
-	int mCurrentItem = 0;
-	MenuType mType;
-
-	this(MenuType type) {
-
-		mType = type;
-
-		switch( type ) {
-			case MenuType.MAIN:
-				mText ~= "Play";
-				mText ~= "Options";
-				mText ~= "Exit";
-				break;
-			case MenuType.OPTIONS:
-				mText ~= "option1";
-				mText ~= "option2";
-				mText ~= "option3";
-				break;
-			case MenuType.PAUSE:
-				mText ~= "        ***  Paused ***";
-				mText ~= "";
-				mText ~= "Continue |: Space";
-				mText ~= "Volume Up |: keypad +";
-				mText ~= "Volume Down |: keypad -";
-				mText ~= "Quit |: Escape";
-				break;
-			case MenuType.LANGUAGE:
-				break;
-			case MenuType.MAP:
-				mText = gMapList.getMenuText();
-				break;
-			default:
-				break;
-		}
-
-		overlay( true );
-
-		//draw();
-
-	}
-
+	int mCurrentItem = 0;
+	GAMEPART_STATE mState = GAMEPART_STATE.GAME;
+
+    void update() {
+        overlay(false);
+        draw();
+        processInput();
+    }
+
+    // comportement de base
+    void processInput() {
+        if( gInputManager.isReleased( IMA_UP )) move(-1);
+        if( gInputManager.isReleased( IMA_DOWN )) move(1);
+        if( gInputManager.isReleased( IMA_ACTION )) action();
+    }
 
 	void overlay( bool fade ) {
 
 		SDL_Surface* surf;
 
 		if( highlight ) {
-			x += 32;
+			x -= 32;
+			s = "=> " ~ s;
 		}
 		SDL_Rect rect = { cast(short)x, cast(short)y, gScreen.WIDTH, gScreen.HEIGHT };
 
 		surf = gTextureManager.getText( s );
 		SDL_BlitSurface( surf, null, gScreen.mSurface, &rect );
 	}
-
-
-	void action() {
-		switch( mType ) {
-			case MenuType.MAIN:
-				switch( mCurrentItem ) {
-					case 0:
-						gGame.SetState(Game.GameState.MENU_MAP);
-						break;
-					case 1:
-						gGame.SetState(Game.GameState.MENU_OPTIONS);
-						break;
-					case 2:
-						gGame.SetState(Game.GameState.EXIT);
-						break;
-					default:
-						break;
-				}
-				break;
-			case MenuType.MAP:
-				new Map( gMapList.getFileName(mCurrentItem) );
-				gGame.SetState(Game.GameState.GAME);
-				break;
-//			case MenuType.WON_LEVEL:
-//				switch( mCurrentItem ) {
-//					case 2:
-//						// Retry
-//						new Map(gMap.mMapFileName);
-//						break;
-//					case 3:
-//						// Next level
-//						new Map("map2.txt");
-//						break;
-//					case 4:
-//						// End game
-//						new Map("map3.txt");
-//						break;
-//					default:
-//						break;
-//				}
-//				gGame.SetState(Game.GameState.GAME);
-//				break;
-			default:
-				break;
-		}
-	}
-
+
+    void action() {
+    }
 
 	void move(int direction) {
 		mCurrentItem = (mCurrentItem+mText.length+direction) % mText.length;
 	}
+
+    void addItem( string s ) {
+        mText ~= s;
+    }
+
+    GAMEPART_STATE getState() {
+        return( mState );
+    }
 
 }
+
+// ============================================================
+
+// Les différents menus :
+
+class MainMenu : public Menu {
+
+    this() {
+        addItem("Play");
+        addItem("Options");
+        addItem("Exit");
+
+        overlay(true);
+    }
+
+    void action() {
+            switch( mCurrentItem ) {
+            case 0:
+                mState = GAMEPART_STATE.MENU_MAP;
+                break;
+            case 1:
+                mState = GAMEPART_STATE.MENU_OPTIONS;
+                break;
+            case 2:
+                mState = GAMEPART_STATE.EXIT;
+                break;
+            default:
+                break;
+        }
+    }
+
+    void processInput() {
+        Menu.processInput();
+        if( gInputManager.isReleased( IMA_BACK )) { mCurrentItem = 2; action(); }
+    }
+
+}
+
+
+class OptionsMenu : public Menu {
+    this() {
+        addItem("option 1");
+        addItem("option 2");
+        addItem("option 3");
+    }
+
+    void action() {
+        mState = GAMEPART_STATE.MAIN_MENU;
+    }
+}
+
+
+// Menu spécial utilisé par Game
+class PauseMenu : public Menu {
+    this() {
+        addItem("        ***  Paused ***");
+        addItem("");
+        addItem("Continue |: RETURN");
+        addItem("Volume Up |: keypad +");
+        addItem("Volume Down |: keypad -");
+        addItem("Quit |: Escape");
+    }
+
+    // override d'update() pour gestion spéciale de la pause
+    void processInput() {
+        if( gInputManager.isReleased( IMA_ACTION )) mState = GAMEPART_STATE.UNPAUSE;
+        if( gInputManager.isReleased( IMA_BACK )) mState = GAMEPART_STATE.EXIT;
+    }
+}
+
+// Liste des maps
+class MapMenu : public Menu {
+    this() {
+
+        if( gMapList is null ) new MapList(); // on devrait vraiment avoir des singletons
+
+        foreach( map; gMapList.getMenuText() ) {
+            addItem( map );
+        }
+    }
+
+    void processInput() {
+        Menu.processInput();
+        if( gInputManager.isReleased( IMA_BACK )) { mState = GAMEPART_STATE.MAIN_MENU; }
+    }
+
+    void action() {
+        if( gMap !is null ) delete(gMap);  // vraiment
+        new Map( gMapList.getFileName(mCurrentItem) );
+		mState = GAMEPART_STATE.NEW_GAME;
+    }
+}