nsnake
Classic snake game for the terminal
Loading...
Searching...
No Matches
GameStateGame.cpp
1#include <States/GameStateGame.hpp>
2#include <Engine/Flow/StateManager.hpp>
3#include <Engine/Helpers/Utils.hpp>
4#include <Engine/Graphics/Widgets/Dialog.hpp>
5#include <Engine/Graphics/Ncurses.hpp>
6#include <Config/Globals.hpp>
7#include <Entities/BoardParser.hpp>
8
9#include <Engine/Flow/StateManager.hpp>
10#include <States/GameStateMainMenu.hpp>
11
12GameStateGame::GameStateGame():
13 game(NULL),
14 willQuit(false)
15{ }
16GameStateGame::~GameStateGame()
17{ }
18void GameStateGame::load()
19{
20 try {
21 this->game = new Game();
22 this->game->start(Globals::Game::current_level);
23 this->game->scores->load();
24 }
25 catch (BoardParserException& e)
26 {
27 Dialog::show("Couldn't load the level! (Error: \"" + e.message + "\")", true);
28 this->willQuit = true;
29 }
30 catch (ScoreFileException& e)
31 {
32 // Show a non-intrusive dialog with why
33 // we couldn't open high score file
34 //e.message
35 }
36 catch (std::runtime_error& e)
37 {
38 // Some error happened during INI parsing...
39 // What should we do?
40 }
41}
42void GameStateGame::unload()
43{
44 SAFE_DELETE(this->game);
45}
46void GameStateGame::update()
47{
48 if (this->willQuit)
49 StateManager::quit();
50
51 this->game->handleInput();
52 this->game->update();
53
54 if (this->game->isOver())
55 {
56 // I'm touching a lot of different stuff
57 // inside the update() function.
58 // I know I shouldn't render things here.
59 // Oh boy, this should be refactored away.
60 this->game->scores->save();
61 Utils::Time::delay_ms(500);
62
63 this->game->draw();
64
65 if (Dialog::askBool("Retry?", "Game Over", true))
66 this->load(); // restart the game
67 else
68 StateManager::change(new GameStateMainMenu());
69 }
70
71 if (this->game->willQuit())
72 this->willQuit = true;
73
74 if (this->game->willReturnToMenu())
75 StateManager::change(new GameStateMainMenu());
76}
77void GameStateGame::draw()
78{
79 if (! this->willQuit)
80 this->game->draw();
81}
82
Custom exception class to specify an error that occurred during a level loading.
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:98
void save()
Saves all the current scores on the file.