nsnake
Classic snake game for the terminal
Toggle main menu visibility
Loading...
Searching...
No Matches
src
Flow
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
}
41
int
GameStateGame::unload
()
42
{
43
SAFE_DELETE(this->game);
44
45
return
0;
46
}
47
GameState::StateCode
GameStateGame::update
()
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
}
80
void
GameStateGame::draw
()
81
{
82
if
(! this->willQuit)
83
this->game->draw();
84
}
85
BoardParserException
Custom exception class to specify an error that occurred during a level loading.
Definition
BoardParser.hpp:13
GameStateGame::unload
int unload()
Destroys anything builded during the game.
Definition
GameStateGame.cpp:41
GameStateGame::update
GameState::StateCode update()
Updates all possible things on the game.
Definition
GameStateGame.cpp:47
GameStateGame::draw
void draw()
Shows everything onscreen;.
Definition
GameStateGame.cpp:80
GameStateGame::load
void load(int stack=0)
Constructs everything necessary for the game.
Definition
GameStateGame.cpp:15
GameState::StateCode
StateCode
All possible transitions between states.
Definition
GameState.hpp:40
Game
Definition
Game.hpp:17
ScoreFileException
Custom exception class to specify an error that occurred during a level loading.
Definition
ScoreFile.hpp:14
Dialog::askBool
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
Dialog::show
void show(std::string message, bool pressAnyKey=false)
Shows a message on the screen.
Definition
Dialog.cpp:12
Ncurses::delay_ms
void delay_ms(int delay)
Sleeps for delay miliseconds.
Definition
Ncurses.cpp:32
Generated on
for nsnake by
1.17.0