Ex 162: Refactor ghost state out of game state.
This commit is contained in:
parent
4c2390a3ff
commit
ea46f9909e
5 changed files with 78 additions and 30 deletions
|
@ -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);
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
41
lib/GhostState.cpp
Normal file
41
lib/GhostState.cpp
Normal 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
|
|
@ -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;
|
||||
|
|
26
lib/include/GhostState.hpp
Normal file
26
lib/include/GhostState.hpp
Normal 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
|
Loading…
Reference in a new issue