Skip to content
Browse files

world,player,gamestate_play

  • Loading branch information...
1 parent 57cfacf commit 79ef7c79b8af48f78bb279601933e0b845e4aff0 Dmytro Lytovchenko committed Sep 29, 2011
Showing with 220 additions and 11 deletions.
  1. +9 −0 src/game.cpp
  2. +9 −4 src/game.h
  3. +45 −7 src/game_state.cpp
  4. +27 −0 src/game_state.h
  5. +34 −0 src/player.h
  6. +30 −0 src/world.cpp
  7. +66 −0 src/world.h
View
9 src/game.cpp
@@ -119,6 +119,7 @@ bool MyGame::Startup()
m_state_mainmenu = new GameState_MainMenu( this );
m_state_instructions = new GameState_Instructions( this );
m_state_credits = new GameState_Credits( this );
+ m_state_play = new GameState_Play();
ShowMainMenuScreen();
@@ -132,6 +133,7 @@ void MyGame::Shutdown()
delete m_state_mainmenu;
delete m_state_instructions;
delete m_state_credits;
+ delete m_state_play;
// Delete created objects and free loaded resources
delete m_font;
@@ -171,6 +173,13 @@ void MyGame::ShowCreditsScreen()
}
+void MyGame::ShowPlayScreen()
+{
+ m_state = m_state_play;
+ m_state_play->OnEnterState( this );
+}
+
+
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
MyGame game;
View
13 src/game.h
@@ -1,6 +1,9 @@
// to prevent multiple compiling of this header
#pragma once
+// this is for standard integer types like uint32_t (very useful)
+#include <stdint.h>
+
#include <hge.h>
#include <hgefont.h>
@@ -41,10 +44,11 @@ class MyGame
// premade game states to change to. Since its bad idea to delete them on the
// fly while state change code is called from the running game state, we just
// premake states and store them here.
- GameState * m_state_options;
- GameState * m_state_mainmenu;
- GameState * m_state_instructions;
- GameState * m_state_credits;
+ GameState * m_state_options; // shows options menu
+ GameState * m_state_mainmenu; // shows main menu
+ GameState * m_state_instructions; // shows 2 pages of instructions
+ GameState * m_state_credits; // shows scrolling credits text
+ GameState * m_state_play; // shows game world and allows to play
public:
@@ -65,4 +69,5 @@ class MyGame
void ShowOptionsScreen();
void ShowInstructionsScreen();
void ShowCreditsScreen();
+ void ShowPlayScreen();
};
View
52 src/game_state.cpp
@@ -4,6 +4,8 @@
#include "game_state.h"
#include "game.h"
#include "menuitem.h"
+#include "world.h"
+#include "player.h"
#include <hgeguictrls.h>
@@ -110,25 +112,22 @@ bool GameState_MainMenu::Think( MyGame * game )
switch(lastid)
{
case MAINMENU_ELEMENT_PLAY:
-// m_gui->SetFocus(MAINMENU_ELEMENT_PLAY);
+ game->ShowPlayScreen();
m_gui->Enter();
break;
case MAINMENU_ELEMENT_OPTIONS:
game->ShowOptionsScreen();
-// m_gui->SetFocus(MAINMENU_ELEMENT_PLAY);
m_gui->Enter();
break;
case MAINMENU_ELEMENT_INSTRUCTIONS:
game->ShowInstructionsScreen();
-// m_gui->SetFocus(MAINMENU_ELEMENT_PLAY);
m_gui->Enter();
break;
case MAINMENU_ELEMENT_CREDITS:
game->ShowCreditsScreen();
-// m_gui->SetFocus(MAINMENU_ELEMENT_PLAY);
m_gui->Enter();
break;
@@ -473,7 +472,46 @@ bool GameState_Options::Think( MyGame * game )
}
+void GameState_Play::Render( MyGame * game )
+{
// as we are not using GUI in this state, we have to draw cursor ourself
- //float mx, my;
- //game->m_hge->Input_GetMousePos( & mx, & my );
- //game->m_mouse_cursor_sprite->Render( mx, my );
+ float mx, my;
+ game->m_hge->Input_GetMousePos( & mx, & my );
+ game->m_mouse_cursor_sprite->Render( mx, my );
+}
+
+
+bool GameState_Play::Think( MyGame * game )
+{
+ // give the World a chance to play its internal logic, like move monsters and animate things
+ m_world->Think();
+
+ // TODO: Design a way to return events from the World::Think, like inform about player taking damage/dying
+
+ if( m_world->Victory() ) {
+ }
+
+ return false;
+}
+
+
+void GameState_Play::OnEnterState( MyGame * game )
+{
+ delete m_player;
+ delete m_world;
+ m_player = new Player();
+ m_world = new World( m_player, "level_01.txt" );
+}
+
+
+GameState_Play::GameState_Play()
+ : m_world(NULL), m_player(NULL)
+{
+}
+
+
+GameState_Play::~GameState_Play()
+{
+ delete m_world;
+ delete m_player;
+}
View
27 src/game_state.h
@@ -24,6 +24,11 @@ class GameState
// Please no drawing in this code, only logic
// Think returns bool like HGE framefunc does, returning true will stop the game
virtual bool Think( MyGame * game ) = 0;
+
+ // This is called when mygame enters the state. Not used anywhere except the GameState_Play
+ // class, where it is used to reset game to level 1
+ // (initial lives count and reset the score, stuff like that)
+ void OnEnterState( MyGame * game ) {}
};
@@ -133,3 +138,25 @@ class GameState_Options: public virtual GameState
void Render( MyGame * game );
bool Think( MyGame * game );
};
+
+
+class World;
+class Player;
+
+// This state is activated when player starts the game
+class GameState_Play: virtual public GameState
+{
+protected:
+ // We create and load world using this class. World is responsible for its
+ // playing rules. World defines conditions when player wins.
+ World * m_world;
+ Player * m_player;
+
+public:
+ GameState_Play();
+ virtual ~GameState_Play();
+
+ virtual void Render( MyGame * game );
+ virtual bool Think( MyGame * game );
+ void OnEnterState( MyGame * game );
+};
View
34 src/player.h
@@ -1,5 +1,39 @@
// to prevent multiple compiling of this header
#pragma once
+#include <hge.h>
+#include <hgerect.h>
+
// Player class
// controls keyboard interaction and game rules
+class Player
+{
+protected:
+ // A bounding box for character sprite used for rendering and collisions
+ hgeRect m_position;
+
+ // Horizontal and vertical speeds of player.
+ // You can use acceleration to affect this value when player presses buttons
+ // to simulate inertia. Or you can write exact speeds when player presses buttons
+ // to do instant turns and direction changes.
+ hgeVertex m_speed;
+
+ // Hack this value for infinite lives
+ int m_lives;
+
+public:
+ const static int INITIAL_LIVES_COUNT = 3;
+
+ Player(): m_lives(INITIAL_LIVES_COUNT)
+ {
+ }
+
+ // player's vertical speed will be instantly set to this value on jump, and then
+ // will be gradually reduced by World::GravityAccel every frame to simulate proper
+ // jumping.
+ // This can be modified to return different values depending if player picks up
+ // some bonus for higher jumps or eats some weakening mushroom
+ float JumpAccel() {
+ return 50.0f;
+ }
+};
View
30 src/world.cpp
@@ -2,3 +2,33 @@
// contains the world and its physical rules (collisions, gravity etc)
// basically its a level of the game, other levels can be different child of world
// class and have e.g. negative gravity, or different set of monsters, or no light, etc.
+#include "world.h"
+#include "player.h"
+
+
+World::World( Player * plr, const std::string & filename )
+ : m_player(plr), m_world_width(0), m_world_height(0)
+{
+ LoadWorld( filename );
+}
+
+
+void World::LoadWorld( const std::string & filename )
+{
+ m_world_width = 0;
+ m_world_height = 0;
+}
+
+
+void World::Think()
+{
+
+}
+
+
+World::CellType & World::At( uint32_t row, uint32_t col )
+{
+ _ASSERTE( row >= 0 && row < m_world_height );
+ _ASSERTE( col >= 0 && col < m_world_width );
+ return m_world_cells[ row * m_world_width + col ];
+}
View
66 src/world.h
@@ -1,7 +1,73 @@
// to prevent multiple compiling of this header
#pragma once
+#include <string>
+#include <vector>
+
+// this is for standard integer types like uint32_t (very useful)
+#include <stdint.h>
+
+// this is for _ASSERT(_ASSERTE) macros which crash the program if error condition is detected
+// showing message box with file and line of the problem (for debugging purposes)
+#include <crtdbg.h>
+
+class Player;
+
// World class
// contains the world and its physical rules (collisions, gravity etc)
// basically its a level of the game, other levels can be different child of world
// class and have e.g. negative gravity, or different set of monsters, or no light, etc.
+//
+// We create and load world using this class. World is responsible for its
+// playing rules. World defines conditions when player wins.
+class World
+{
+protected:
+ // this is the player, World does not own the Character and will not delete it on world's end
+ Player * m_player;
+
+ // we define world cell as unsigned integer. But in future this may be changed to a
+ // more complicated struct or class etc. to have more control over world structure
+ typedef uint32_t CellType;
+
+ // Rectangular array of the world lined up row after row, this is for memory storage
+ // simplicity, to allow worlds of arbitrary size and to avoid more complicated memory
+ // management - we let std::vector do the memory job. Use At() function to access cells
+ // by (Row,Col) coord
+ std::vector <CellType> m_world_cells;
+
+ uint32_t m_world_width;
+ uint32_t m_world_height;
+
+public:
+ // Loads the default world from the filename provided
+ World( Player * plr, const std::string & filename );
+ virtual ~World() {}
+
+ // Returns a read/writable reference to a world cell. You can read and write to it
+ // like if it was a real array element
+ CellType & At( uint32_t row, uint32_t col );
+
+ // default world can never be "won", you have to inherit the world class
+ // and override Victory function to define own rules when player wins
+ virtual bool Victory() { return false; }
+
+ // Animates the world, moves monsters, etc.
+ virtual void Think();
+
+ virtual void LoadWorld( const std::string & filename );
+
+ // Returns pixels per second of free fall acceleration - adjust this to have things
+ // fall harder or slower, make it negative to change gravity vector up
+ // This can be modified to return different values depending if player picks up something
+ // or flips a gravity switch (if you wish)
+ virtual float GravityAccel() { return 20.0f; }
+};
+
+
+// Normal world for my game, normal rules apply
+class WorldType1: virtual public World
+{
+public:
+ bool Victory();
+};

0 comments on commit 79ef7c7

Please sign in to comment.
Something went wrong with that request. Please try again.