2020-11-28 13:10:25 +00:00
|
|
|
#include "Game.hpp"
|
2020-11-27 13:10:09 +00:00
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
|
2021-06-24 08:32:54 +00:00
|
|
|
constexpr int DEFAULT_LIVES = 3;
|
|
|
|
constexpr int NORMAL_PELLET_POINTS = 10;
|
2021-06-24 11:32:52 +00:00
|
|
|
constexpr int POWER_PELLET_POINTS = 50;
|
2021-06-24 08:32:54 +00:00
|
|
|
//constexpr int GHOST_POINTS[] = {200, 400, 800, 1600};
|
|
|
|
|
2021-06-15 21:54:35 +00:00
|
|
|
Game::Game()
|
|
|
|
: pacMan(board),
|
2020-11-28 13:10:25 +00:00
|
|
|
pellets(board),
|
2021-06-24 11:32:52 +00:00
|
|
|
superPellets(board),
|
2021-06-28 08:37:52 +00:00
|
|
|
ghosts(Blinky(board), Speedy(board), Inky(board), Clyde(board)) {
|
2021-06-24 11:32:52 +00:00
|
|
|
score.lives = DEFAULT_LIVES;
|
2021-06-24 08:32:54 +00:00
|
|
|
}
|
2020-11-27 13:10:09 +00:00
|
|
|
|
2021-06-28 08:37:52 +00:00
|
|
|
|
2020-11-27 13:10:09 +00:00
|
|
|
auto Game::now() {
|
|
|
|
return std::chrono::system_clock::now();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Game::run() {
|
2021-06-22 13:37:36 +00:00
|
|
|
|
2021-06-24 11:32:52 +00:00
|
|
|
const std::chrono::milliseconds delta_time(1000 / 60);
|
|
|
|
|
|
|
|
std::chrono::milliseconds t(0);
|
|
|
|
std::chrono::milliseconds accumulator(0);
|
|
|
|
auto current_time = now();
|
|
|
|
|
|
|
|
InputState inputState;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
auto newTime = now();
|
|
|
|
auto frameTime = std::chrono::duration_cast<std::chrono::milliseconds>(newTime - current_time);
|
|
|
|
current_time = newTime;
|
|
|
|
accumulator += frameTime;
|
|
|
|
processEvents(inputState);
|
|
|
|
if (inputState.close)
|
|
|
|
return;
|
|
|
|
while (accumulator >= delta_time) {
|
|
|
|
step(delta_time, inputState);
|
|
|
|
accumulator -= delta_time;
|
|
|
|
t += delta_time;
|
2021-06-22 13:37:36 +00:00
|
|
|
}
|
2021-06-24 11:32:52 +00:00
|
|
|
canvas.update(*this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Game::step(std::chrono::milliseconds delta, InputState inputState) {
|
|
|
|
pacMan.update(delta, inputState, board);
|
|
|
|
|
2021-06-28 08:37:52 +00:00
|
|
|
std::apply([&](auto &... ghost) {
|
|
|
|
(ghost.update(delta, board),...);
|
|
|
|
}, ghosts);
|
2021-06-24 11:32:52 +00:00
|
|
|
|
|
|
|
eatPellets();
|
2020-11-27 13:10:09 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 11:59:16 +00:00
|
|
|
void Game::eatPellets() {
|
|
|
|
const auto pos = pacMan.positionInGrid();
|
2021-06-24 11:32:52 +00:00
|
|
|
if (pellets.eatPelletAtPosition(pos)) {
|
|
|
|
score.eatenPellets++;
|
|
|
|
score.points += NORMAL_PELLET_POINTS;
|
2021-06-24 08:32:54 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 11:32:52 +00:00
|
|
|
if (superPellets.eatPelletAtPosition(pos)) {
|
|
|
|
score.eatenPellets++;
|
|
|
|
score.points += POWER_PELLET_POINTS;
|
2021-06-24 08:32:54 +00:00
|
|
|
}
|
2021-06-16 11:59:16 +00:00
|
|
|
}
|
|
|
|
|
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();
|
2021-06-22 08:27:33 +00:00
|
|
|
if (event && event->type == sf::Event::Closed) {
|
2021-06-20 16:52:34 +00:00
|
|
|
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
|
|
|
}
|