Ex 162: Refactor ghost state out of game state.

This commit is contained in:
Dag-Erling Smørgrav 2021-10-18 15:22:18 +02:00
parent 31412de8ea
commit c4d95e904c
5 changed files with 78 additions and 30 deletions

View file

@ -36,10 +36,10 @@ void Canvas::render(const GameState & gameState) {
renderPellets(gameState.pellets); renderPellets(gameState.pellets);
renderSuperPellets(gameState.superPellets); renderSuperPellets(gameState.superPellets);
renderGhost(gameState.blinky); renderGhost(gameState.ghosts.blinky);
renderGhost(gameState.pinky); renderGhost(gameState.ghosts.pinky);
renderGhost(gameState.inky); renderGhost(gameState.ghosts.inky);
renderGhost(gameState.dave); renderGhost(gameState.ghosts.dave);
renderScore(gameState.score.points); renderScore(gameState.score.points);
renderLives(gameState.score.lives); renderLives(gameState.score.lives);

View file

@ -18,21 +18,11 @@ void GameState::step(std::chrono::milliseconds delta) {
if (!pacMan.hasDirection()) if (!pacMan.hasDirection())
return; return;
blinky.setTarget(pacMan.position()); ghosts.setTarget(pacMan.positionInGrid(), pacMan.currentDirection());
blinky.update(delta); ghosts.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);
fruit.update(delta, score.eatenPellets); fruit.update(delta, score.eatenPellets);
ghosts.checkCollision(*this);
checkCollision(blinky);
checkCollision(pinky);
checkCollision(inky);
checkCollision(dave);
eatPellets(); eatPellets();
eatFruit(); eatFruit();
@ -58,10 +48,7 @@ void GameState::handleDeathAnimation(std::chrono::milliseconds delta) {
timeSinceDeath += delta; timeSinceDeath += delta;
if (timeSinceDeath.count() > 1000) { if (timeSinceDeath.count() > 1000) {
blinky.reset(); ghosts.reset();
pinky.reset();
inky.reset();
dave.reset();
pacMan.reset(); pacMan.reset();
pacManAI.reset(); pacManAI.reset();
timeSinceDeath = std::chrono::milliseconds(0); timeSinceDeath = std::chrono::milliseconds(0);
@ -78,11 +65,7 @@ void GameState::eatPellets() {
if (superPellets.eatPelletAtPosition(pos)) { if (superPellets.eatPelletAtPosition(pos)) {
score.eatenPellets++; score.eatenPellets++;
score.points += POWER_PELLET_POINTS; score.points += POWER_PELLET_POINTS;
ghosts.frighten();
blinky.frighten();
pinky.frighten();
inky.frighten();
dave.frighten();
} }
} }

41
lib/GhostState.cpp Normal file
View file

@ -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

View file

@ -4,6 +4,7 @@
#include "Dave.hpp" #include "Dave.hpp"
#include "Fruits.hpp" #include "Fruits.hpp"
#include "Ghost.hpp" #include "Ghost.hpp"
#include "GhostState.hpp"
#include "Inky.hpp" #include "Inky.hpp"
#include "InputState.hpp" #include "InputState.hpp"
#include "PacMan.hpp" #include "PacMan.hpp"
@ -18,10 +19,7 @@ namespace pacman {
struct GameState { struct GameState {
void step(std::chrono::milliseconds delta); void step(std::chrono::milliseconds delta);
Blinky blinky; GhostState ghosts;
Pinky pinky;
Inky inky;
Dave dave;
PacMan pacMan; PacMan pacMan;
PacManAI pacManAI; PacManAI pacManAI;

View file

@ -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