view states are now managed by a stack + bugfixing
- when one defines DECL_ENUM_LAST it is possible to check whether a string for a give enum exists - making sure ships are alive when they are spawned - added warning concerning QueueEvent and TriggerEvent (one shall avoid the latter)
This commit is contained in:
@@ -15,7 +15,7 @@ BEGIN_ENUM(GameEntityType)
|
||||
DECL_ENUM_ELEMENT(GameEntityTypeRocket),
|
||||
DECL_ENUM_ELEMENT(GameEntityTypeAsteroid),
|
||||
DECL_ENUM_ELEMENT(GameEntityTypeShipPart),
|
||||
DECL_ENUM_ELEMENT(GameEntityTypeLast)
|
||||
DECL_ENUM_LAST(GameEntityType)
|
||||
}
|
||||
END_ENUM(GameEntityType)
|
||||
|
||||
@@ -30,7 +30,8 @@ BEGIN_ENUM(ViewState)
|
||||
DECL_ENUM_ELEMENT(ViewStateShowHighscore),
|
||||
DECL_ENUM_ELEMENT(ViewStateEnterPlayername),
|
||||
DECL_ENUM_ELEMENT(ViewStateOptions),
|
||||
DECL_ENUM_ELEMENT(ViewStateGameOver)
|
||||
DECL_ENUM_ELEMENT(ViewStateGameOver),
|
||||
DECL_ENUM_LAST(ViewState)
|
||||
}
|
||||
END_ENUM(ViewState)
|
||||
|
||||
@@ -38,6 +39,7 @@ BEGIN_ENUM(GameState)
|
||||
{
|
||||
DECL_ENUM_ELEMENT(GameStateRunning),
|
||||
DECL_ENUM_ELEMENT(GameStatePaused),
|
||||
DECL_ENUM_LAST(GameState)
|
||||
}
|
||||
END_ENUM(GameState)
|
||||
|
||||
|
||||
@@ -12,7 +12,8 @@ BEGIN_ENUM(Event)
|
||||
DECL_ENUM_ELEMENT(EventLevelComplete),
|
||||
DECL_ENUM_ELEMENT(EventChangeGameState),
|
||||
DECL_ENUM_ELEMENT(EventGameOver),
|
||||
DECL_ENUM_ELEMENT(EventShipExplode)
|
||||
DECL_ENUM_ELEMENT(EventShipExplode),
|
||||
DECL_ENUM_LAST (Event)
|
||||
}
|
||||
END_ENUM(Event)
|
||||
|
||||
|
||||
@@ -132,13 +132,18 @@ bool Cmd_ControllerAttack (std::vector<std::string> args) {
|
||||
assert (ControllerInstance);
|
||||
|
||||
// only continue if the game is running
|
||||
if (GetModel()->GetGameState() != GameStateRunning)
|
||||
if (GetModel()->GetGameState() != GameStateRunning) {
|
||||
Engine::LogDebug ("Not running attack as game is not running");
|
||||
return false;
|
||||
}
|
||||
|
||||
ShipEntity* player_entity = (ShipEntity*) Engine::GetEntity (Engine::GetPlayerEntityId ());
|
||||
if (player_entity) {
|
||||
player_entity->Attack ();
|
||||
return true;
|
||||
} else {
|
||||
Engine::LogError ("Could not attack: player entity not found");
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
+18
-13
@@ -287,8 +287,10 @@ int Model::DoLoadLevel (const char* filename) {
|
||||
bool is_player;
|
||||
level_file >> is_player;
|
||||
|
||||
if (is_player)
|
||||
if (is_player) {
|
||||
mPlayerEntityId = entity->mId;
|
||||
Engine::LogDebug ("Entity with id '%d' is player", entity->mId);
|
||||
}
|
||||
|
||||
level_file >> entity->mPhysicState->mPosition[0];
|
||||
level_file >> entity->mPhysicState->mPosition[1];
|
||||
@@ -352,6 +354,19 @@ int Model::DoSaveLevel (const char* filename) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Model::ProceedToNextLevel () {
|
||||
Engine::LogDebug ("Proceeding to next level %d", mCurrentLevelIndex + 1);
|
||||
mCurrentLevelIndex++;
|
||||
|
||||
if (mCurrentLevelIndex + 1 == mLevelList.size()) {
|
||||
Engine::EventBasePtr gameover_event (new Engine::EventBase());
|
||||
gameover_event->mEventType = EventGameOver;
|
||||
QueueEvent (gameover_event);
|
||||
} else {
|
||||
DoLoadLevel(mLevelList[mCurrentLevelIndex].c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void Model::SetGameState (const unsigned int &state) {
|
||||
mLastGameState = mGameState;
|
||||
|
||||
@@ -365,17 +380,7 @@ void Model::SetGameState (const unsigned int &state) {
|
||||
bool Model::OnLevelComplete() {
|
||||
Engine::LogMessage ("Level complete!");
|
||||
|
||||
mCurrentLevelIndex++;
|
||||
|
||||
if (mCurrentLevelIndex + 1 == mLevelList.size()) {
|
||||
Engine::EventBasePtr gameover_event (new Engine::EventBase());
|
||||
gameover_event->mEventType = EventGameOver;
|
||||
QueueEvent (gameover_event);
|
||||
} else {
|
||||
DoLoadLevel(mLevelList[mCurrentLevelIndex].c_str());
|
||||
}
|
||||
|
||||
SetGameState(GameStatePaused);
|
||||
ProceedToNextLevel();
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -444,7 +449,7 @@ void Model::OnKillEntity (const Engine::EntityBase *entity) {
|
||||
if (mAsteroids.size() == 0) {
|
||||
Engine::EventBasePtr level_complete_event (new Engine::EventBase());
|
||||
level_complete_event->mEventType = EventLevelComplete;
|
||||
TriggerEvent (level_complete_event);
|
||||
QueueEvent (level_complete_event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+10
-7
@@ -10,16 +10,22 @@ class Model : public Engine::ModelBase {
|
||||
public:
|
||||
virtual ~Model() {};
|
||||
virtual void Process();
|
||||
int DoLoadLevel (const char* filename);
|
||||
int DoSaveLevel (const char* filename);
|
||||
virtual void SetGameState (const unsigned int &state);
|
||||
float GetWorldWidth ();
|
||||
float GetWorldHeight ();
|
||||
|
||||
/* Event handler */
|
||||
bool OnLevelComplete();
|
||||
bool OnGameOver();
|
||||
/** \brief Resets values from a previous game */
|
||||
void OnNewGame ();
|
||||
void OnShipExplode ();
|
||||
|
||||
virtual void SetGameState (const unsigned int &state);
|
||||
/* Level loading etc. */
|
||||
int DoLoadLevel (const char* filename);
|
||||
int DoSaveLevel (const char* filename);
|
||||
|
||||
void ProceedToNextLevel ();
|
||||
int GetPlayerLives () { return mPlayerLives; };
|
||||
unsigned int GetPoints () { return mPoints; };
|
||||
std::string GetPlayerName() { return mPlayerName; };
|
||||
@@ -28,9 +34,7 @@ class Model : public Engine::ModelBase {
|
||||
mPlayerName = name;
|
||||
};
|
||||
|
||||
float GetWorldWidth ();
|
||||
float GetWorldHeight ();
|
||||
|
||||
/* Highscore */
|
||||
struct HighscoreEntry {
|
||||
HighscoreEntry(): name ("unknown"), points (0) { };
|
||||
HighscoreEntry (const char *e_name, const unsigned int e_points):
|
||||
@@ -40,7 +44,6 @@ class Model : public Engine::ModelBase {
|
||||
unsigned int points;
|
||||
};
|
||||
|
||||
/* Highscore */
|
||||
void LoadHighscoreList ();
|
||||
void SaveHighscoreList ();
|
||||
unsigned int AddHighscoreEntry(const std::string &name, const unsigned int points);
|
||||
|
||||
@@ -11,7 +11,7 @@ struct ShipEntityPhysicState : public Engine::EntityPhysicState {
|
||||
ShipEntityPhysicState () {
|
||||
mType = GameEntityTypeShip;
|
||||
mBaseType = Engine::EntityBaseTypeActor;
|
||||
|
||||
|
||||
mAcceleration = 10.;
|
||||
mMaxSpeed = 10.;
|
||||
mRotationSpeed = 180.;
|
||||
@@ -37,6 +37,7 @@ struct ShipEntity: public Engine::EntityBase {
|
||||
mType = GameEntityTypeShip;
|
||||
mBaseType = Engine::EntityBaseTypeActor;
|
||||
|
||||
mAlive = true;
|
||||
mState = Idle;
|
||||
mAttackTimer = 0.;
|
||||
}
|
||||
|
||||
+13
-8
@@ -523,17 +523,22 @@ void View::DrawUiGamePaused() {
|
||||
|
||||
if (Engine::GUI::Button (1, "Resume Game", screen_right * 0.5 - 100, 200, button_width, button_height)) {
|
||||
PopViewState();
|
||||
}
|
||||
|
||||
if (Engine::GUI::Button (2, "Abort Game", screen_right * 0.5 - 100, 250, button_width, button_height)) {
|
||||
while (mViewStateStack.size())
|
||||
PopViewState();
|
||||
|
||||
PushViewState(ViewStateGameRunning);
|
||||
GetModel()->SetGameState(GameStateRunning);
|
||||
}
|
||||
|
||||
if (Engine::GUI::Button (3, "Quit", screen_right * 0.5 - 100, 330, button_width, button_height)) {
|
||||
if (Engine::GUI::Button (2, "Options", screen_right * 0.5 - 100, 250, button_width, button_height)) {
|
||||
PushViewState (ViewStateOptions);
|
||||
}
|
||||
|
||||
if (Engine::GUI::Button (3, "Abort Game", screen_right * 0.5 - 100, 300, button_width, button_height)) {
|
||||
while (mViewStateStack.size())
|
||||
PopViewState();
|
||||
|
||||
PushViewState(ViewStateMainMenu);
|
||||
GetModel()->SetGameState(GameStatePaused);
|
||||
}
|
||||
|
||||
if (Engine::GUI::Button (4, "Quit", screen_right * 0.5 - 100, 380, button_width, button_height)) {
|
||||
Engine::RunCommand("quit");
|
||||
}
|
||||
}
|
||||
|
||||
+4
-1
@@ -76,8 +76,11 @@ class View : public Engine::ViewBase {
|
||||
return mViewStateStack.top();
|
||||
}
|
||||
void PopViewState () {
|
||||
// Warning: you must not query for an invalid enum with
|
||||
// GetStringENUM_NAME!
|
||||
Engine::LogDebug("Popping ViewState: %s remainging: %u", GetStringViewState(mViewStateStack.top()), mViewStateStack.size());
|
||||
|
||||
mViewStateStack.pop();
|
||||
Engine::LogDebug ("Popped ViewState top is now:%s", GetStringViewState(mViewStateStack.top()));
|
||||
}
|
||||
|
||||
// \todo [high] add Resource Manager!
|
||||
|
||||
Reference in New Issue
Block a user