intermediate commit: restructuring view states

This commit is contained in:
2010-11-22 20:00:44 +01:00
parent 652367b824
commit 3cbbedde7e
11 changed files with 285 additions and 69 deletions
+14 -6
View File
@@ -19,16 +19,24 @@ BEGIN_ENUM(GameEntityType)
}
END_ENUM(GameEntityType)
BEGIN_ENUM(ViewState)
{
DECL_ENUM_ELEMENT(ViewStateMainMenu),
DECL_ENUM_ELEMENT(ViewStateGameRunning),
DECL_ENUM_ELEMENT(ViewStatePaused),
DECL_ENUM_ELEMENT(ViewStatePlayerDied),
DECL_ENUM_ELEMENT(ViewStateLevelComplete),
DECL_ENUM_ELEMENT(ViewStateShowHighscore),
DECL_ENUM_ELEMENT(ViewStateEnterPlayername),
DECL_ENUM_ELEMENT(ViewStateOptions),
DECL_ENUM_ELEMENT(ViewStateGameOver)
}
END_ENUM(ViewState)
BEGIN_ENUM(GameState)
{
DECL_ENUM_ELEMENT(GameStateMainMenu),
DECL_ENUM_ELEMENT(GameStateRunning),
DECL_ENUM_ELEMENT(GameStatePaused),
DECL_ENUM_ELEMENT(GameStatePlayerDied),
DECL_ENUM_ELEMENT(GameStateLevelComplete),
DECL_ENUM_ELEMENT(GameStateShowHighscore),
DECL_ENUM_ELEMENT(GameStateEnterPlayername),
DECL_ENUM_ELEMENT(GameStateGameOver)
}
END_ENUM(GameState)
+31 -34
View File
@@ -27,8 +27,8 @@ int Model::OnInit (int argc, char* argv[]) {
ModelInstance = this;
mGameState = GameStateMainMenu;
mLastGameState = GameStateMainMenu;
mGameState = GameStatePaused;
mLastGameState = GameStatePaused;
/// \TODO use <optional> or similar for initialization of mCurrentLevelIndex
mCurrentLevelIndex = 99999;
@@ -62,6 +62,7 @@ int Model::OnInit (int argc, char* argv[]) {
// initialize event handlers and register them
Engine::RegisterListener (this, EventLevelComplete);
Engine::RegisterListener (this, EventGameOver);
Engine::RegisterListener (this, EventShipExplode);
mPlayerName = "Player";
@@ -84,6 +85,9 @@ bool Model::OnReceiveEvent (const Engine::EventBasePtr &event) {
case EventLevelComplete:
return OnLevelComplete();
break;
case EventShipExplode:
OnShipExplode();
break;
case EventGameOver:
return OnGameOver();
break;
@@ -110,36 +114,6 @@ void Model::Process () {
// some action.
Engine::LogDebug ("Switching from %s->%s", GetStringGameState(mLastGameState), GetStringGameState(mGameState));
if ( (mLastGameState == GameStateMainMenu && mGameState == GameStateRunning)
|| (mLastGameState == GameStateEnterPlayername && mGameState == GameStateRunning)){
OnNewGame();
}
else if (mLastGameState == GameStateRunning && mGameState == GameStatePlayerDied) {
mPlayerLives --;
ClearEntities();
if (mPlayerLives == 0) {
Engine::EventBasePtr gameover_event (new Engine::EventBase());
gameover_event->mEventType = EventGameOver;
QueueEvent (gameover_event);
}
}
else if (mLastGameState == GameStateLevelComplete && mGameState == GameStateRunning) {
mCurrentLevelIndex++;
if (mCurrentLevelIndex == mLevelList.size()) {
Engine::EventBasePtr gameover_event (new Engine::EventBase());
gameover_event->mEventType = EventGameOver;
QueueEvent (gameover_event);
} else {
DoLoadLevel(mLevelList[mCurrentLevelIndex].c_str());
}
}
else if (mLastGameState == GameStatePlayerDied && mGameState == GameStateRunning)
DoLoadLevel(mLevelList[mCurrentLevelIndex].c_str());
else if (mLastGameState == GameStateRunning && mGameState == GameStateGameOver)
ClearEntities();
// ... and we have to set the last game state to the current gamestate
// otherwise we end up in an infinite loop of performing the switching
// action.
@@ -391,24 +365,31 @@ 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 {
SetGameState (GameStateLevelComplete);
DoLoadLevel(mLevelList[mCurrentLevelIndex].c_str());
}
SetGameState(GameStatePaused);
return true;
}
bool Model::OnGameOver() {
ClearEntities();
Engine::LogMessage ("Points = %d lowest = %d", mPoints, mHighscoreList.back().points );
if (mPoints > mHighscoreList.back().points) {
Engine::LogMessage ("New Highscore!");
AddHighscoreEntry (mPlayerName, mPoints);
}
SetGameState(GameStateGameOver);
SetGameState(GameStatePaused);
return false;
};
@@ -421,6 +402,22 @@ void Model::OnNewGame() {
DoLoadLevel (mLevelList[mCurrentLevelIndex].c_str());
}
void Model::OnShipExplode () {
mPlayerLives --;
ClearEntities();
if (mPlayerLives == 0) {
Engine::EventBasePtr gameover_event (new Engine::EventBase());
gameover_event->mEventType = EventGameOver;
QueueEvent (gameover_event);
} else {
DoLoadLevel(mLevelList[mCurrentLevelIndex].c_str());
}
SetGameState(GameStatePaused);
}
void Model::OnCreateEntity (const int type, const unsigned int id) {
GameEntityType entity_type = (GameEntityType) type;
+1
View File
@@ -16,6 +16,7 @@ class Model : public Engine::ModelBase {
bool OnGameOver();
/** \brief Resets values from a previous game */
void OnNewGame ();
void OnShipExplode ();
virtual void SetGameState (const unsigned int &state);
+1 -1
View File
@@ -26,7 +26,7 @@ void ShipEntity::Update (float delta_sec) {
if (mFadeTimer <= 0.) {
Model *model = (Model*) Engine::EngineGetModel();
model->SetGameState (GameStatePlayerDied);
model->SetGameState (GameStatePaused);
}
return;
}
+76 -24
View File
@@ -72,6 +72,8 @@ int View::OnInit (int argc, char* argv[]) {
Engine::RegisterListener (this, EventAccelerateStop);
Engine::RegisterListener (this, EventShipExplode);
PushViewState (ViewStateMainMenu);
return 0;
}
@@ -355,34 +357,42 @@ void View::DrawUi () {
SetFontJustification (Engine::FontJustificationLeft);
GetController()->EnableTextinput(true);
switch (game_state) {
case GameStateMainMenu:
ViewState current_view_state = GetViewState();
SelectFont ("console.ttf size=12");
Engine::GUI::Label (99999, GetStringViewState(current_view_state), 8, 16);
switch (current_view_state) {
case ViewStateMainMenu:
DrawUiMainMenu();
break;
case GameStateRunning:
case ViewStateGameRunning:
DrawUiGameRunning();
break;
case GameStatePaused:
case ViewStatePaused:
DrawUiGamePaused();
break;
case GameStatePlayerDied:
case ViewStatePlayerDied:
DrawUiPlayerDied();
break;
case GameStateLevelComplete:
case ViewStateLevelComplete:
DrawUiLevelComplete();
break;
case GameStateShowHighscore:
case ViewStateShowHighscore:
DrawUiHighscore();
break;
case GameStateEnterPlayername:
case ViewStateOptions:
DrawUiOptions();
break;
case ViewStateEnterPlayername:
DrawUiEnterPlayername();
break;
case GameStateGameOver:
case ViewStateGameOver:
DrawUiGameOver();
break;
default:
Engine::LogWarning ("Trying to draw unknown GameState: %s (%d)",
GetStringGameState (game_state), game_state);
Engine::LogWarning ("Trying to draw unknown ViewState: %s (%d)",
GetStringViewState (game_state), game_state);
break;
}
@@ -412,14 +422,18 @@ void View::DrawUiMainMenu() {
SelectFont("console.ttf size=23");
if (Engine::GUI::Button (1, "New Game", screen_right * 0.5 - 100, 200, button_width, button_height)) {
GetModel()->SetGameState(GameStateEnterPlayername);
PushViewState(ViewStateEnterPlayername);
}
if (Engine::GUI::Button (2, "Highscores", screen_right * 0.5 - 100, 250, button_width, button_height)) {
GetModel()->SetGameState(GameStateShowHighscore);
if (Engine::GUI::Button (2, "Options", screen_right * 0.5 - 100, 250, button_width, button_height)) {
PushViewState (ViewStateOptions);
}
if (Engine::GUI::Button (3, "Quit", screen_right * 0.5 - 100, 330, button_width, button_height)) {
if (Engine::GUI::Button (3, "Highscores", screen_right * 0.5 - 100, 300, button_width, button_height)) {
PushViewState(ViewStateShowHighscore);
}
if (Engine::GUI::Button (4, "Quit", screen_right * 0.5 - 100, 380, button_width, button_height)) {
Engine::RunCommand("quit");
}
}
@@ -443,8 +457,9 @@ void View::DrawUiGameRunning() {
// revert the font justification
SetFontJustification (Engine::FontJustificationLeft);
if (Engine::GUI::CheckKeyPress(SDLK_ESCAPE))
GetModel()->SetGameState(GameStatePaused);
if (Engine::GUI::CheckKeyPress(SDLK_ESCAPE)) {
PushViewState(ViewStatePaused);
}
}
void View::DrawUiGameOver() {
@@ -472,7 +487,8 @@ void View::DrawUiGameOver() {
if (Engine::GUI::Button (2, "Continue...",
(screen_right - button_width) * 0.5,
screen_bottom * 0.5 + 80, button_width, button_height)) {
GetModel()->SetGameState(GameStateShowHighscore);
PopViewState();
PushViewState(ViewStateShowHighscore);
}
}
@@ -482,7 +498,7 @@ void View::DrawUiLevelComplete() {
SelectFont ("console.ttf size=23");
if(Engine::GUI::Button (1, "Next level ...", (screen_right - button_width) * 0.5, screen_bottom * 0.5 + 60, button_width, button_height)) {
GetModel()->SetGameState(GameStateRunning);
PopViewState();
}
}
@@ -491,11 +507,15 @@ void View::DrawUiGamePaused() {
SelectFont ("console.ttf size=23");
if (Engine::GUI::Button (1, "Resume Game", screen_right * 0.5 - 100, 200, button_width, button_height)) {
GetModel()->SetGameState(GameStateRunning);
PopViewState();
}
if (Engine::GUI::Button (2, "Abort Game", screen_right * 0.5 - 100, 250, button_width, button_height)) {
GetModel()->SetGameState(GameStateMainMenu);
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)) {
@@ -508,7 +528,7 @@ void View::DrawUiPlayerDied() {
SelectFont ("console.ttf size=23");
if (Engine::GUI::Button (1, "Continue", screen_right * 0.5 - 100, 200, button_width, button_height)) {
GetModel()->SetGameState(GameStateRunning);
PopViewState();
}
}
@@ -588,7 +608,36 @@ void View::DrawUiHighscore() {
}
if (Engine::GUI::Button (1, "Back to Menu", screen_right * 0.5 - 250 * 0.5, y + 16, button_width, button_height)) {
GetModel()->SetGameState(GameStateMainMenu);
PopViewState();
}
}
void View::DrawUiOptions() {
DrawPageTitle ("Options");
SelectFont ("console.ttf size=23");
// Enter your name
std::string player_name = GetModel()->GetPlayerName();
Engine::GUI::Label (1, "Effects Volume: ", screen_right * 0.5 - 150, 240);
float effects_volume = Engine::GetEffectsVolume();
if (Engine::GUI::VerticalSlider (2, screen_right * 0.5 - 100, 250, 250, 16, 0., 1., effects_volume)) {
Engine::LogDebug ("Setting effects volume to: %f", effects_volume);
Engine::SetEffectsVolume(effects_volume);
}
Engine::GUI::Label (3, "Music Volume: ", screen_right * 0.5 - 150, 300);
float music_volume = Engine::GetMusicVolume();
if (Engine::GUI::VerticalSlider (4, screen_right * 0.5 - 100, 310, 250, 16, 0., 1., music_volume)) {
Engine::LogDebug ("Setting music volume to: %f", music_volume);
Engine::SetMusicVolume(music_volume);
}
if (Engine::GUI::Button (5, "Back", screen_right * 0.5 - 100, 380, button_width, button_height)) {
PopViewState();
}
}
@@ -607,11 +656,14 @@ void View::DrawUiEnterPlayername() {
}
if (Engine::GUI::Button (3, "Start Game", screen_right - 180 - 20, 500, 180, 40)) {
PopViewState();
PushViewState(ViewStateGameRunning);
GetModel()->OnNewGame();
GetModel()->SetGameState(GameStateRunning);
}
if (Engine::GUI::Button (5, "Back", 20, 500, 180, 40)) {
GetModel()->SetGameState(GameStateMainMenu);
PopViewState();
}
}
+16
View File
@@ -7,6 +7,7 @@
#include "Sprite.h"
#include "EntityBase.h"
#include "SimpleConsoleOverlay.h"
#include "AsteroidsEnums.h"
namespace asteroids {
@@ -45,6 +46,7 @@ class View : public Engine::ViewBase {
void DrawUiPlayerDied();
void DrawHighscoreEntry(float x, float y, float entry_width, const std::string &name, unsigned int points);
void DrawUiHighscore();
void DrawUiOptions();
void DrawUiEnterPlayername();
virtual void Draw ();
@@ -64,6 +66,20 @@ class View : public Engine::ViewBase {
std::vector<unsigned int> mShipPartsEntityIds;
std::stack<ViewState> mViewStateStack;
void PushViewState (const ViewState state) {
Engine::LogDebug ("Pushing ViewState %s", GetStringViewState(state));
mViewStateStack.push(state);
}
ViewState GetViewState () {
return mViewStateStack.top();
}
void PopViewState () {
mViewStateStack.pop();
Engine::LogDebug ("Popped ViewState top is now:%s", GetStringViewState(mViewStateStack.top()));
}
// \todo [high] add Resource Manager!
Engine::Sprite mGUIShipSprite;
Engine::Sprite mAsteroidSprite;