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);
32 SAFE_DELETE(this->scores);
33 SAFE_DELETE(this->currentScore);
34 SAFE_DELETE(this->pauseMenu);
35 SAFE_DELETE(this->player);
36 SAFE_DELETE(this->board);
37 SAFE_DELETE(this->fruits);
42 SAFE_DELETE(this->layout);
43 SAFE_DELETE(this->scores);
44 SAFE_DELETE(this->currentScore);
45 SAFE_DELETE(this->pauseMenu);
46 SAFE_DELETE(this->player);
47 SAFE_DELETE(this->board);
48 SAFE_DELETE(this->fruits);
50 this->userAskedToQuit =
false;
51 this->userAskedToGoToMenu =
false;
52 this->gameOver =
false;
53 this->isPaused =
false;
59 this->currentScore->level = levelName;
60 this->currentScore->speed = Globals::Game::starting_speed;
61 this->currentScore->fruits = Globals::Game::fruits_at_once;
62 this->currentScore->random_walls = Globals::Game::random_walls;
63 this->currentScore->teleport = Globals::Game::teleport;
64 this->currentScore->board_size = Globals::Game::board_size;
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)
106 this->board->randomlyFillExceptBy(this->player->getX(),
107 this->player->getY());
110 this->fruits =
new FruitManager(Globals::Game::fruits_at_once);
111 this->fruits->update(this->player, this->board);
120 this->pauseMenu =
new Menu(1,
122 this->layout->pause->getW() - 2,
123 this->layout->pause->getH() - 2);
127 item =
new MenuItem(
"Resume", RESUME);
128 this->pauseMenu->add(item);
130 item =
new MenuItem(
"Restart", RESTART);
131 this->pauseMenu->add(item);
133 this->pauseMenu->addBlank();
135 item =
new MenuItem(
"Quit to Main Menu", QUIT_MENU);
136 this->pauseMenu->add(item);
138 item =
new MenuItem(
"Quit Game", QUIT_GAME);
139 this->pauseMenu->add(item);
142 this->timerSnake.start();
143 this->timerBoard.start();
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))
168 if (! this->isPaused)
177 else if (InputManager::isPressed(
"help"))
182 this->showHelp =
false;
183 this->timer.unpause();
184 this->timerSnake.unpause();
185 this->timerBoard.unpause();
189 this->showHelp =
true;
191 this->timerSnake.pause();
192 this->timerBoard.pause();
197 if (this->isPaused || this->showHelp)
199 this->pauseMenu->handleInput();
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);
228 if (this->pauseMenu->willQuit())
230 int option = this->pauseMenu->currentID();
239 this->start(Globals::Game::current_level);
243 this->userAskedToGoToMenu =
true;
247 this->userAskedToQuit =
true;
250 this->pauseMenu->reset();
257 this->timerSnake.pause();
258 int delta = this->getDelay(this->currentScore->speed);
260 if (this->timerSnake.delta_ms() >= delta)
264 if (! this->player->isAlive())
266 this->gameOver =
true;
270 this->scores->handle(this->currentScore);
275 this->player->update(this->board);
277 while (this->fruits->eatenFruit(this->player))
279 this->player->increase();
284 this->currentScore->points += this->currentScore->speed * 2;
287 this->fruits->update(this->player, this->board);
289 this->timerSnake.start();
292 this->timerSnake.unpause();
296 this->timerBoard.pause();
297 delta = Globals::Game::board_scroll_delay;
299 if (this->timerBoard.delta_ms() >= delta)
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();
306 this->timerBoard.start();
309 this->timerBoard.unpause();
313 this->layout->draw(this->pauseMenu);
317 return (this->gameOver);
321 return this->userAskedToQuit;
325 return this->userAskedToGoToMenu;
330 if (speed < 1)
return 800;
347 void Game::pause(
bool option)
354 this->isPaused =
true;
355 this->showPauseMenu =
true;
357 this->timerSnake.pause();
361 if (! this->isPaused)
364 this->isPaused =
false;
365 this->showPauseMenu =
false;
366 this->timer.unpause();
367 this->timerSnake.unpause();
Stores points the player made on the game.
void start(std::string levelName="")
Starts game, optionally loading a level.
A level where the snake runs and eats fruits.
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.
int getDelay(int speed)
Returns how much time (millisseconds) we need to wait for a specific #speed.
FruitManager(int amount)
Creates a Fruit container that has at most #amount fruits at once on the screen.
bool willQuit()
If we'll quit the game right away.