pacman/lib/Game.cpp

141 lines
3.3 KiB
C++
Raw Normal View History

#include "Game.hpp"
2021-07-08 14:57:18 +00:00
#include "GameState.hpp"
2020-11-27 13:10:09 +00:00
#include <chrono>
2021-07-05 12:10:01 +00:00
namespace pacman {
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-28 10:42:21 +00:00
constexpr int GHOST_POINTS = 200;
2021-06-24 08:32:54 +00:00
2021-07-08 14:57:18 +00:00
Game::Game() {
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
void Game::run() {
2021-06-24 11:32:52 +00:00
const std::chrono::milliseconds delta_time(1000 / 60);
std::chrono::milliseconds accumulator(0);
2021-07-08 16:12:03 +00:00
auto current_time = std::chrono::system_clock::now();
2021-06-24 11:32:52 +00:00
InputState inputState;
while (true) {
2021-07-08 16:12:03 +00:00
auto newTime = std::chrono::system_clock::now();
2021-06-24 11:32:52 +00:00
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;
}
2021-07-08 14:57:18 +00:00
canvas.update(gameState, score);
2021-06-24 11:32:52 +00:00
}
}
2021-07-13 12:26:57 +00:00
void Game::killPacMan() {
gameState.pacMan.die();
score.lives--;
timeSinceDeath = std::chrono::milliseconds(1);
}
2021-06-28 10:42:21 +00:00
2021-07-13 12:26:57 +00:00
bool Game::pacManDying() const {
return timeSinceDeath.count() != 0;
}
2021-06-24 11:32:52 +00:00
2021-07-13 12:26:57 +00:00
void Game::handleDeathAnimation(std::chrono::milliseconds delta) {
timeSinceDeath += delta;
2021-06-28 10:42:21 +00:00
if (timeSinceDeath.count() > 1000) {
2021-07-15 07:14:25 +00:00
gameState.blinky.reset();
2021-07-16 07:35:32 +00:00
gameState.pinky.reset();
2021-07-15 07:14:25 +00:00
gameState.inky.reset();
gameState.clyde.reset();
2021-07-08 14:57:18 +00:00
gameState.pacMan.reset();
2021-06-28 10:42:21 +00:00
timeSinceDeath = std::chrono::milliseconds(0);
}
2021-07-13 12:26:57 +00:00
}
2021-06-28 10:42:21 +00:00
2021-07-13 12:26:57 +00:00
void Game::step(std::chrono::milliseconds delta, InputState inputState) {
gameState.pacMan.update(delta, inputState.direction());
if (pacManDying()) {
handleDeathAnimation(delta);
2021-06-28 10:42:21 +00:00
return;
2021-07-13 12:26:57 +00:00
}
2021-06-28 10:42:21 +00:00
2021-07-08 14:57:18 +00:00
if (!gameState.pacMan.hasDirection())
2021-06-28 10:42:21 +00:00
return;
2021-07-28 13:28:36 +00:00
gameState.blinky.update(delta, gameState);
gameState.pinky.update(delta, gameState);
gameState.inky.update(delta, gameState);
gameState.clyde.update(delta, gameState);
2021-07-16 07:19:10 +00:00
checkCollision(gameState.blinky);
2021-07-16 07:35:32 +00:00
checkCollision(gameState.pinky);
2021-07-16 07:19:10 +00:00
checkCollision(gameState.inky);
checkCollision(gameState.clyde);
2021-06-24 11:32:52 +00:00
eatPellets();
2020-11-27 13:10:09 +00:00
}
2021-07-28 13:28:36 +00:00
void Game::checkCollision(Ghost & ghost) {
2021-07-13 12:26:57 +00:00
if (pacManDying() || ghost.isEyes())
2021-06-28 10:42:21 +00:00
return;
2021-07-08 14:57:18 +00:00
if (ghost.positionInGrid() != gameState.pacMan.positionInGrid())
2021-06-28 10:42:21 +00:00
return;
2021-07-08 14:57:18 +00:00
if (ghost.isFrightened()) {
2021-07-13 12:26:57 +00:00
ghost.die();
2021-06-28 10:42:21 +00:00
score.points += GHOST_POINTS;
} else {
2021-07-13 12:26:57 +00:00
killPacMan();
2021-06-28 10:42:21 +00:00
}
}
2021-06-16 11:59:16 +00:00
void Game::eatPellets() {
2021-07-08 14:57:18 +00:00
const auto pos = gameState.pacMan.positionInGrid();
if (gameState.pellets.eatPelletAtPosition(pos)) {
2021-06-24 11:32:52 +00:00
score.eatenPellets++;
score.points += NORMAL_PELLET_POINTS;
2021-06-24 08:32:54 +00:00
}
2021-07-08 14:57:18 +00:00
if (gameState.superPellets.eatPelletAtPosition(pos)) {
2021-06-24 11:32:52 +00:00
score.eatenPellets++;
score.points += POWER_PELLET_POINTS;
2021-07-15 07:14:25 +00:00
gameState.blinky.frighten();
2021-07-16 07:35:32 +00:00
gameState.pinky.frighten();
2021-07-15 07:14:25 +00:00
gameState.inky.frighten();
gameState.clyde.frighten();
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) {
auto event = canvas.pollEvent();
2021-07-06 15:09:42 +00:00
if (event && event.value().type == sf::Event::Closed) {
inputState.close = true;
return;
2020-11-27 13:10:09 +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
}
2021-07-05 12:10:01 +00:00
} // namespace pacman