1 #include <Game/Game.hpp> 2 #include <Config/Globals.hpp> 3 #include <Misc/Utils.hpp> 4 #include <Interface/LayoutGame.hpp> 5 #include <Flow/InputManager.hpp> 6 #include <Game/BoardParser.hpp> 11 enum NamesToEasilyIdentifyTheMenuItemsInsteadOfRawNumbers
13 RESUME, RESTART, QUIT_MENU, QUIT_GAME
31 SAFE_DELETE(this->layout);
35 SAFE_DELETE(this->player);
36 SAFE_DELETE(this->board);
37 SAFE_DELETE(this->fruits);
42 SAFE_DELETE(this->layout);
46 SAFE_DELETE(this->player);
47 SAFE_DELETE(this->board);
48 SAFE_DELETE(this->fruits);
50 this->userAskedToQuit =
false;
51 this->userAskedToGoToMenu =
false;
65 this->
currentScore->board_scroll_delay = Globals::Game::board_scroll_delay;
66 this->
currentScore->board_scroll_left = Globals::Game::board_scroll_left;
67 this->
currentScore->board_scroll_right = Globals::Game::board_scroll_right;
68 this->
currentScore->board_scroll_up = Globals::Game::board_scroll_up;
69 this->
currentScore->board_scroll_down = Globals::Game::board_scroll_down;
72 int boardw = Board::large_width;
73 int boardh = Board::large_height;
75 if (Globals::Game::board_size == Globals::Game::SMALL)
77 boardw = Board::small_width;
78 boardh = Board::small_height;
80 else if (Globals::Game::board_size == Globals::Game::MEDIUM)
82 boardw = Board::medium_width;
83 boardh = Board::medium_height;
86 if (! levelName.empty())
94 this->board =
new Board(boardw,
96 ((Globals::Game::teleport) ?
102 this->player =
new Player(this->board->getStartX(),
103 this->board->getStartY());
105 if (Globals::Game::random_walls)
107 this->player->
getY());
110 this->fruits =
new FruitManager(Globals::Game::fruits_at_once);
111 this->fruits->
update(this->player, this->board);
122 this->layout->
pause->getW() - 2,
123 this->layout->
pause->getH() - 2);
127 item =
new MenuItem(
"Resume", RESUME);
130 item =
new MenuItem(
"Restart", RESTART);
135 item =
new MenuItem(
"Quit to Main Menu", QUIT_MENU);
138 item =
new MenuItem(
"Quit Game", QUIT_GAME);
146 void Game::handleInput()
148 if (InputManager::noKeyPressed())
153 if (InputManager::isPressed(
"quit"))
155 this->userAskedToQuit =
true;
157 else if (InputManager::isPressed(
"pause"))
165 else if (InputManager::isPressed(
'\n') ||
166 InputManager::isPressed(KEY_ENTER))
177 else if (InputManager::isPressed(
"help"))
203 if (InputManager::isPressed(
"left"))
205 this->player->move(Player::LEFT);
207 else if (InputManager::isPressed(
"right"))
209 this->player->move(Player::RIGHT);
211 else if (InputManager::isPressed(
"up"))
213 this->player->move(Player::UP);
215 else if (InputManager::isPressed(
"down"))
217 this->player->move(Player::DOWN);
239 this->
start(Globals::Game::current_level);
243 this->userAskedToGoToMenu =
true;
247 this->userAskedToQuit =
true;
264 if (! this->player->isAlive())
275 this->player->update(this->board);
277 while (this->fruits->
eatenFruit(this->player))
279 this->player->increase();
287 this->fruits->
update(this->player, this->board);
297 delta = Globals::Game::board_scroll_delay;
301 if (Globals::Game::board_scroll_up) this->board->scrollUp();
302 if (Globals::Game::board_scroll_down) this->board->scrollDown();
303 if (Globals::Game::board_scroll_left) this->board->scrollLeft();
304 if (Globals::Game::board_scroll_right) this->board->scrollRight();
321 return this->userAskedToQuit;
325 return this->userAskedToGoToMenu;
330 if (speed < 1)
return 800;
347 void Game::pause(
bool option)
bool showHelp
If it's showing the help screen.
bool showPauseMenu
If it's showing the pause menu.
Stores points the player made on the game.
bool gameOver
If the game is over (board is full of blocks).
bool eatenFruit(Player *player)
Tells if the #player has eaten a fruit this frame.
Menu * pauseMenu
Menu that's shown only when the user presses Pause.
void randomlyFillExceptBy(int x, int y)
Places random walls all over the Board except by #x and #y, allowing the Player to move a little bit ...
int fruits
How many fruits at once were allowed on this level.
std::string level
On which level the user made this score.
void start(std::string levelName="")
Starts game, optionally loading a level.
Controls how many Fruits are there and how they're spawned.
bool random_walls
If random walls were spawned on this level.
Timer timerBoard
Timer that tells when to scroll the board, if this game setting is activated.
Globals::Game::BoardSize board_size
How large was the game board on this score.
void update(Player *player, Board *board)
Updates internal fruits, adding them to the #board and making sure it doesn't touch #player.
A level where the snake runs and eats fruits.
bool teleport
If teleport was enabled on this level.
bool willReturnToMenu()
If we'll return to the main menu.
static Board * load(std::string filename)
Loads and parses level with name.
A single entry on the high-score file.
Timer timerSnake
Timer that tells when to move the player, based on the current speed).
void start()
Sets a starting point for the timer.
Window * pause
Contains the pause menu.
bool handle(ScoreEntry *score)
Checks if #score is the highest score and make it so.
void pause()
Temporarily stops the timer.
suseconds_t delta_ms()
Returns the milisseconds part of the timer's difference.
int getDelay(int speed)
Returns how much time (millisseconds) we need to wait for a specific #speed.
unsigned int points
How many points the user got.
int getX()
Returns the head's x position.
ScoreEntry * currentScore
Current score for this level.
void unpause()
Restarts the timer if it was paused.
unsigned int speed
Under which game speed the score was made.
int getY()
Returns the head's y position.
bool willQuit()
If we'll quit the game right away.
bool isPaused
If the game is paused.
ScoreFile * scores
All the current level's score.