From c4d95e904c7a77fbcfa4f702068abbb6b03f1058 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Mon, 18 Oct 2021 15:22:18 +0200 Subject: [PATCH] Ex 162: Refactor ghost state out of game state. --- lib/Canvas.cpp | 8 ++++---- lib/GameState.cpp | 27 +++++-------------------- lib/GhostState.cpp | 41 ++++++++++++++++++++++++++++++++++++++ lib/include/GameState.hpp | 6 ++---- lib/include/GhostState.hpp | 26 ++++++++++++++++++++++++ 5 files changed, 78 insertions(+), 30 deletions(-) create mode 100644 lib/GhostState.cpp create mode 100644 lib/include/GhostState.hpp diff --git a/lib/Canvas.cpp b/lib/Canvas.cpp index a4f01f6..0c50c83 100644 --- a/lib/Canvas.cpp +++ b/lib/Canvas.cpp @@ -36,10 +36,10 @@ void Canvas::render(const GameState & gameState) { renderPellets(gameState.pellets); renderSuperPellets(gameState.superPellets); - renderGhost(gameState.blinky); - renderGhost(gameState.pinky); - renderGhost(gameState.inky); - renderGhost(gameState.dave); + renderGhost(gameState.ghosts.blinky); + renderGhost(gameState.ghosts.pinky); + renderGhost(gameState.ghosts.inky); + renderGhost(gameState.ghosts.dave); renderScore(gameState.score.points); renderLives(gameState.score.lives); diff --git a/lib/GameState.cpp b/lib/GameState.cpp index d575937..c25cc85 100644 --- a/lib/GameState.cpp +++ b/lib/GameState.cpp @@ -18,21 +18,11 @@ void GameState::step(std::chrono::milliseconds delta) { if (!pacMan.hasDirection()) return; - 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); - dave.setTarget(pacMan.position()); - dave.update(delta); + ghosts.setTarget(pacMan.positionInGrid(), pacMan.currentDirection()); + ghosts.update(delta); fruit.update(delta, score.eatenPellets); - - checkCollision(blinky); - checkCollision(pinky); - checkCollision(inky); - checkCollision(dave); + ghosts.checkCollision(*this); eatPellets(); eatFruit(); @@ -58,10 +48,7 @@ void GameState::handleDeathAnimation(std::chrono::milliseconds delta) { timeSinceDeath += delta; if (timeSinceDeath.count() > 1000) { - blinky.reset(); - pinky.reset(); - inky.reset(); - dave.reset(); + ghosts.reset(); pacMan.reset(); pacManAI.reset(); timeSinceDeath = std::chrono::milliseconds(0); @@ -78,11 +65,7 @@ void GameState::eatPellets() { if (superPellets.eatPelletAtPosition(pos)) { score.eatenPellets++; score.points += POWER_PELLET_POINTS; - - blinky.frighten(); - pinky.frighten(); - inky.frighten(); - dave.frighten(); + ghosts.frighten(); } } diff --git a/lib/GhostState.cpp b/lib/GhostState.cpp new file mode 100644 index 0000000..ffb61ae --- /dev/null +++ b/lib/GhostState.cpp @@ -0,0 +1,41 @@ +#include "GhostState.hpp" +#include "GameState.hpp" + +namespace pacman { + +void GhostState::setTarget(GridPosition pacManPosition, Direction pacManDirection) { + blinky.setTarget(gridPositionToPosition(pacManPosition)); + pinky.setTarget(pacManPosition, pacManDirection); + inky.setTarget(pacManPosition, pacManDirection, blinky.positionInGrid()); + dave.setTarget(gridPositionToPosition(pacManPosition)); +} + +void GhostState::update(std::chrono::milliseconds delta) { + blinky.update(delta); + pinky.update(delta); + inky.update(delta); + dave.update(delta); +} + +void GhostState::checkCollision(GameState & gameState) { + gameState.checkCollision(blinky); + gameState.checkCollision(pinky); + gameState.checkCollision(inky); + gameState.checkCollision(dave); +} + +void GhostState::reset(void) { + blinky.reset(); + pinky.reset(); + inky.reset(); + dave.reset(); +} + +void GhostState::frighten(void) { + blinky.frighten(); + pinky.frighten(); + inky.frighten(); + dave.frighten(); +} + +} // namespace pacman diff --git a/lib/include/GameState.hpp b/lib/include/GameState.hpp index 976dc97..2837e54 100644 --- a/lib/include/GameState.hpp +++ b/lib/include/GameState.hpp @@ -4,6 +4,7 @@ #include "Dave.hpp" #include "Fruits.hpp" #include "Ghost.hpp" +#include "GhostState.hpp" #include "Inky.hpp" #include "InputState.hpp" #include "PacMan.hpp" @@ -18,10 +19,7 @@ namespace pacman { struct GameState { void step(std::chrono::milliseconds delta); - Blinky blinky; - Pinky pinky; - Inky inky; - Dave dave; + GhostState ghosts; PacMan pacMan; PacManAI pacManAI; diff --git a/lib/include/GhostState.hpp b/lib/include/GhostState.hpp new file mode 100644 index 0000000..7b862b4 --- /dev/null +++ b/lib/include/GhostState.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include "Blinky.hpp" +#include "Dave.hpp" +#include "Inky.hpp" +#include "Pinky.hpp" + +namespace pacman { + +class GameState; + +class GhostState { +public: + void setTarget(GridPosition pacManPosition, Direction pacManDirection); + void update(std::chrono::milliseconds delta); + void checkCollision(GameState & gameState); + void reset(); + void frighten(); + + Blinky blinky; + Pinky pinky; + Inky inky; + Dave dave; +}; + +} // namespace pacman