nsnake
Classic snake game for the terminal
Loading...
Searching...
No Matches
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
9GameStateGame::GameStateGame():
10 game(NULL),
11 willQuit(false)
12{ }
13GameStateGame::~GameStateGame()
14{ }
15void 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();
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
Custom exception class to specify an error that occurred during a level loading.
int unload()
Destroys anything builded during the game.
GameState::StateCode update()
Updates all possible things on the game.
void draw()
Shows everything onscreen;.
void load(int stack=0)
Constructs everything necessary for the game.
StateCode
All possible transitions between states.
Definition GameState.hpp:40
Definition Game.hpp:17
void start(std::string levelName="")
Starts game, optionally loading a level.
Definition Game.cpp:39
ScoreFile * scores
All the current level's score.
Definition Game.hpp:59
bool willQuit()
If we'll quit the game right away.
Definition Game.cpp:319
bool willReturnToMenu()
If we'll return to the main menu.
Definition Game.cpp:323
Custom exception class to specify an error that occurred during a level loading.
Definition ScoreFile.hpp:14
void load()
Loads all high score entries based on a level name.
Definition ScoreFile.cpp:96
void save()
Saves all the current scores on the file.
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
void delay_ms(int delay)
Sleeps for #delay miliseconds.
Definition Ncurses.cpp:32