2021-09-07 14:00:19 +00:00
|
|
|
#include "GameState.hpp"
|
|
|
|
|
|
|
|
namespace pacman {
|
|
|
|
|
|
|
|
constexpr int GHOST_POINTS = 200;
|
|
|
|
constexpr int NORMAL_PELLET_POINTS = 10;
|
|
|
|
constexpr int POWER_PELLET_POINTS = 50;
|
|
|
|
|
|
|
|
void GameState::step(std::chrono::milliseconds delta) {
|
2021-09-22 12:06:17 +00:00
|
|
|
pacManAI.update(pacMan, pellets);
|
2021-10-05 12:30:34 +00:00
|
|
|
pacMan.update(delta, inputState.enableAI ? pacManAI.suggestedDirection() : inputState.direction());
|
2021-09-07 14:00:19 +00:00
|
|
|
|
|
|
|
if (isPacManDying()) {
|
|
|
|
handleDeathAnimation(delta);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pacMan.hasDirection())
|
|
|
|
return;
|
|
|
|
|
2021-09-23 12:57:53 +00:00
|
|
|
blinky.setTarget(pacMan.position());
|
|
|
|
blinky.update(delta);
|
|
|
|
pinky.setTarget(pacMan.positionInGrid(), pacMan.currentDirection());
|
|
|
|
pinky.update(delta);
|
|
|
|
inky.setTarget(pacMan.positionInGrid(), pacMan.currentDirection(), blinky.positionInGrid());
|
|
|
|
inky.update(delta);
|
|
|
|
|
|
|
|
fruit.update(delta, score.eatenPellets);
|
2021-09-07 14:00:19 +00:00
|
|
|
|
|
|
|
checkCollision(blinky);
|
|
|
|
checkCollision(pinky);
|
|
|
|
checkCollision(inky);
|
|
|
|
|
|
|
|
eatPellets();
|
2021-09-13 12:47:09 +00:00
|
|
|
eatFruit();
|
2021-09-07 14:00:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GameState::checkCollision(Ghost & ghost) {
|
|
|
|
if (isPacManDying() || ghost.isEyes())
|
|
|
|
return;
|
|
|
|
|
2021-09-13 12:47:09 +00:00
|
|
|
// TODO: hitboxes based collision
|
2021-09-07 14:00:19 +00:00
|
|
|
if (ghost.positionInGrid() != pacMan.positionInGrid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (ghost.isFrightened()) {
|
|
|
|
ghost.die();
|
|
|
|
score.points += GHOST_POINTS;
|
|
|
|
} else {
|
|
|
|
killPacMan();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameState::handleDeathAnimation(std::chrono::milliseconds delta) {
|
|
|
|
timeSinceDeath += delta;
|
|
|
|
|
|
|
|
if (timeSinceDeath.count() > 1000) {
|
|
|
|
blinky.reset();
|
|
|
|
pinky.reset();
|
|
|
|
inky.reset();
|
|
|
|
pacMan.reset();
|
2021-10-05 09:58:06 +00:00
|
|
|
pacManAI.reset();
|
2021-09-07 14:00:19 +00:00
|
|
|
timeSinceDeath = std::chrono::milliseconds(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameState::eatPellets() {
|
|
|
|
const auto pos = pacMan.positionInGrid();
|
|
|
|
if (pellets.eatPelletAtPosition(pos)) {
|
|
|
|
score.eatenPellets++;
|
|
|
|
score.points += NORMAL_PELLET_POINTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (superPellets.eatPelletAtPosition(pos)) {
|
|
|
|
score.eatenPellets++;
|
|
|
|
score.points += POWER_PELLET_POINTS;
|
|
|
|
|
|
|
|
blinky.frighten();
|
|
|
|
pinky.frighten();
|
|
|
|
inky.frighten();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-13 12:47:09 +00:00
|
|
|
void GameState::eatFruit() {
|
2021-09-17 14:44:16 +00:00
|
|
|
const auto pos = pacMan.positionInGrid();
|
|
|
|
const auto fruitpos = positionToGridPosition(fruit.position());
|
|
|
|
|
|
|
|
// TODO: hitboxes based collision
|
|
|
|
if (fruit.isVisible() && pos == fruitpos) {
|
|
|
|
score.points += fruit.eat();
|
|
|
|
score.eatenFruits++;
|
|
|
|
}
|
2021-09-13 12:47:09 +00:00
|
|
|
}
|
|
|
|
|
2021-09-07 14:00:19 +00:00
|
|
|
void GameState::killPacMan() {
|
|
|
|
pacMan.die();
|
|
|
|
score.lives--;
|
|
|
|
timeSinceDeath = std::chrono::milliseconds(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GameState::isPacManDying() const {
|
|
|
|
return timeSinceDeath.count() != 0;
|
|
|
|
}
|
|
|
|
|
2021-09-17 14:44:16 +00:00
|
|
|
} // namespace pacman
|