nsnake
Classic snake game for the terminal
GameStateGame.cpp
1 #include <Flow/GameStateGame.hpp>
2 #include <Flow/StateManager.hpp>
3 #include <Misc/Utils.hpp>
4 #include <Interface/Dialog.hpp>
5 #include <Interface/Ncurses.hpp>
6 #include <Config/Globals.hpp>
7 #include <Game/BoardParser.hpp>
8 
9 GameStateGame::GameStateGame():
10  game(NULL),
11  willQuit(false)
12 { }
13 GameStateGame::~GameStateGame()
14 { }
15 void GameStateGame::load(int stack)
16 {
17  UNUSED(stack);
18 
19  try {
20  this->game = new Game();
21  this->game->start(Globals::Game::current_level);
22  this->game->scores->load();
23  }
24  catch (BoardParserException& e)
25  {
26  Dialog::show("Couldn't load the level! (Error: \"" + e.message + "\")", true);
27  this->willQuit = true;
28  }
29  catch (ScoreFileException& e)
30  {
31  // Show a non-intrusive dialog with why
32  // we couldn't open high score file
33  //e.message
34  }
35  catch (std::runtime_error& e)
36  {
37  // Some error happened during INI parsing...
38  // What should we do?
39  }
40 }
42 {
43  SAFE_DELETE(this->game);
44 
45  return 0;
46 }
48 {
49  if (this->willQuit)
50  return GameState::QUIT;
51 
52  this->game->handleInput();
53  this->game->update();
54 
55  if (this->game->isOver())
56  {
57  // I'm touching a lot of different stuff
58  // inside the update() function.
59  // I know I shouldn't render things here.
60  // Oh boy, this should be refactored away.
61  this->game->scores->save();
62  Ncurses::delay_ms(500);
63 
64  this->game->draw();
65 
66  if (Dialog::askBool("Retry?", "Game Over", true))
67  this->load(); // restart the game
68  else
69  return GameState::MAIN_MENU;
70  }
71 
72  if (this->game->willQuit())
73  this->willQuit = true;
74 
75  if (this->game->willReturnToMenu())
76  return GameState::MAIN_MENU;
77 
78  return GameState::CONTINUE;
79 }
81 {
82  if (! this->willQuit)
83  this->game->draw();
84 }
85 
void draw()
Shows everything onscreen;.
Custom exception class to specify an error that occurred during a level loading.
Definition: ScoreFile.hpp:13
GameState::StateCode update()
Updates all possible things on the game.
Definition: Game.hpp:16
Custom exception class to specify an error that occurred during a level loading.
Definition: BoardParser.hpp:12
virtual void load(int stack=0)=0
Where every state initializes it&#39;s resources.
void delay_ms(int delay)
Sleeps for #delay miliseconds.
Definition: Ncurses.cpp:32
bool askBool(std::string question, std::string title="", bool default_value=false)
Spawns a Dialog box asking for a yes-or-no #question.
Definition: Dialog.cpp:58
void show(std::string message, bool pressAnyKey=false)
Shows a message on the screen.
Definition: Dialog.cpp:12
StateCode
All possible transitions between states.
Definition: GameState.hpp:39
void load(int stack=0)
Constructs everything necessary for the game.
int unload()
Destroys anything builded during the game.