2020-11-28 13:10:25 +00:00
|
|
|
#include "Game.hpp"
|
2020-11-27 13:10:09 +00:00
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
|
2021-06-15 21:54:35 +00:00
|
|
|
Game::Game()
|
|
|
|
: pacMan(board),
|
2020-11-28 13:10:25 +00:00
|
|
|
pellets(board),
|
|
|
|
superPellets(board) {}
|
2020-11-27 13:10:09 +00:00
|
|
|
|
|
|
|
auto Game::now() {
|
|
|
|
return std::chrono::system_clock::now();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Game::run() {
|
|
|
|
InputState inputState;
|
|
|
|
auto current_time = now();
|
|
|
|
while (!inputState.close) {
|
|
|
|
processEvents(inputState);
|
|
|
|
auto time_delta = now() - current_time;
|
|
|
|
auto milli_delta = std::chrono::duration_cast<std::chrono::milliseconds>(time_delta);
|
|
|
|
pacMan.update(milli_delta, inputState, board);
|
2021-06-16 11:59:16 +00:00
|
|
|
eatPellets();
|
2020-11-27 13:10:09 +00:00
|
|
|
current_time += time_delta;
|
2021-06-16 10:52:04 +00:00
|
|
|
canvas.update(pacMan, pellets, superPellets);
|
2020-11-27 13:10:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-16 11:59:16 +00:00
|
|
|
void Game::eatPellets() {
|
|
|
|
const auto pos = pacMan.positionInGrid();
|
|
|
|
pellets.eatPelletAtPosition(pos);
|
|
|
|
superPellets.eatPelletAtPosition(pos);
|
|
|
|
}
|
|
|
|
|
2020-11-27 13:10:09 +00:00
|
|
|
void Game::processEvents(InputState & inputState) {
|
|
|
|
|
2021-06-20 16:52:34 +00:00
|
|
|
auto event = canvas.pollEvent();
|
|
|
|
if (event->type == sf::Event::Closed) {
|
|
|
|
inputState.close = true;
|
|
|
|
return;
|
2020-11-27 13:10:09 +00:00
|
|
|
}
|
2021-06-20 16:52:34 +00:00
|
|
|
|
|
|
|
inputState.down = inputState.up = inputState.left = inputState.right = false;
|
|
|
|
|
|
|
|
inputState.down = sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down);
|
|
|
|
inputState.up = sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up);
|
|
|
|
inputState.left = sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left);
|
|
|
|
inputState.right = sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right);
|
2020-11-27 13:10:09 +00:00
|
|
|
}
|