This commit is contained in:
Patricia Aas 2021-07-13 14:26:57 +02:00
parent 122f47e964
commit 522362152d
6 changed files with 38 additions and 30 deletions

View file

@ -38,13 +38,18 @@ void Game::run() {
}
}
void Game::step(std::chrono::milliseconds delta, InputState inputState) {
void Game::killPacMan() {
gameState.pacMan.die();
score.lives--;
timeSinceDeath = std::chrono::milliseconds(1);
}
gameState.pacMan.update(delta, inputState.direction());
bool Game::pacManDying() const {
return timeSinceDeath.count() != 0;
}
if (timeSinceDeath.count() != 0) {
timeSinceDeath += delta;
}
void Game::handleDeathAnimation(std::chrono::milliseconds delta) {
timeSinceDeath += delta;
if (timeSinceDeath.count() > 1000) {
std::apply([&](auto &... ghost) {
@ -54,9 +59,16 @@ void Game::step(std::chrono::milliseconds delta, InputState inputState) {
gameState.pacMan.reset();
timeSinceDeath = std::chrono::milliseconds(0);
}
}
if (timeSinceDeath.count())
void Game::step(std::chrono::milliseconds delta, InputState inputState) {
gameState.pacMan.update(delta, inputState.direction());
if (pacManDying()) {
handleDeathAnimation(delta);
return;
}
if (!gameState.pacMan.hasDirection())
return;
@ -71,19 +83,17 @@ void Game::step(std::chrono::milliseconds delta, InputState inputState) {
}
void Game::checkCollision(Ghost & ghost) {
if (timeSinceDeath.count() || ghost.isEyes())
if (pacManDying() || ghost.isEyes())
return;
if (ghost.positionInGrid() != gameState.pacMan.positionInGrid())
return;
if (ghost.isFrightened()) {
ghost.eat();
ghost.die();
score.points += GHOST_POINTS;
} else {
gameState.pacMan.eat();
score.lives--;
timeSinceDeath = std::chrono::milliseconds(1);
killPacMan();
}
}

View file

@ -27,7 +27,7 @@ bool Ghost::isEyes() const {
return state == State::Eyes;
}
void Ghost::eat() {
void Ghost::die() {
if (state == State::Eyes)
return;
direction = oppositeDirection(direction);
@ -160,7 +160,7 @@ void Ghost::updateDirection() {
if (opposite_direction)
continue;
const GridPosition grid_position = {size_t(move.position.x), size_t(move.position.y)};
const GridPosition grid_position = { size_t(move.position.x), size_t(move.position.y) };
const bool can_walk = pacman::isWalkableForGhost(grid_position, current_grid_position, isEyes());
if (!can_walk)
continue;

View file

@ -3,11 +3,8 @@
namespace pacman {
PacMan::PacMan()
: pos(initialPacManPosition()) {}
GridPosition PacMan::currentSprite() const {
return eaten ? pacManAnimation.deathAnimationFrame() : pacManAnimation.animationFrame(direction);
return dead ? pacManAnimation.deathAnimationFrame() : pacManAnimation.animationFrame(direction);
}
Position PacMan::position() const {
@ -18,21 +15,21 @@ GridPosition PacMan::positionInGrid() const {
return positionToGridPosition(pos);
}
void PacMan::eat() {
if (eaten)
void PacMan::die() {
if (dead)
return;
eaten = true;
direction = Direction::NONE;
dead = true;
}
void PacMan::reset() {
eaten = false;
dead = false;
direction = Direction::NONE;
desired_direction = Direction::NONE;
pos = pacman::initialPacManPosition();
}
void PacMan::update(std::chrono::milliseconds time_delta, Direction input_direction) {
if (eaten) {
if (dead) {
updateAnimationPosition(time_delta, false);
return;
}
@ -48,7 +45,7 @@ void PacMan::updateAnimationPosition(std::chrono::milliseconds time_delta, bool
if (paused) {
pacManAnimation.pause();
} else {
pacManAnimation.updateAnimationPosition(time_delta, eaten);
pacManAnimation.updateAnimationPosition(time_delta, dead);
}
}

View file

@ -21,6 +21,9 @@ private:
void eatPellets();
void processEvents(InputState & inputState);
void checkCollision(Ghost & ghost);
void killPacMan();
bool pacManDying() const;
void handleDeathAnimation(std::chrono::milliseconds delta);
};
} // namespace pacman

View file

@ -27,7 +27,7 @@ public:
void update(std::chrono::milliseconds time_delta);
void frighten();
void eat();
void die();
[[nodiscard]] bool isFrightened() const;
[[nodiscard]] bool isEyes() const;
void reset();

View file

@ -13,8 +13,6 @@ class InputState;
class PacMan {
public:
explicit PacMan();
[[nodiscard]] GridPosition currentSprite() const;
[[nodiscard]] Position position() const;
@ -23,7 +21,7 @@ public:
void update(std::chrono::milliseconds time_delta, Direction input_direction);
void eat();
void die();
void reset();
[[nodiscard]] bool hasDirection() const {
return direction != Direction::NONE;
@ -32,9 +30,9 @@ public:
private:
Direction direction = Direction::NONE;
Direction desired_direction = Direction::NONE;
Position pos;
Position pos = initialPacManPosition();
PacManAnimation pacManAnimation;
bool eaten = false;
bool dead = false;
void updateAnimationPosition(std::chrono::milliseconds time_delta, bool paused);
void updateMazePosition(std::chrono::milliseconds time_delta);