Ex 161: Implement Dave.

This commit is contained in:
Dag-Erling Smørgrav 2021-10-18 14:46:12 +02:00
parent 850ef774d1
commit 4c2390a3ff
6 changed files with 65 additions and 2 deletions

View File

@ -39,6 +39,7 @@ void Canvas::render(const GameState & gameState) {
renderGhost(gameState.blinky); renderGhost(gameState.blinky);
renderGhost(gameState.pinky); renderGhost(gameState.pinky);
renderGhost(gameState.inky); renderGhost(gameState.inky);
renderGhost(gameState.dave);
renderScore(gameState.score.points); renderScore(gameState.score.points);
renderLives(gameState.score.lives); renderLives(gameState.score.lives);

View File

@ -1 +1,38 @@
#include "Dave.hpp" #include "Dave.hpp"
namespace pacman {
Dave::Dave()
: Ghost(Atlas::Ghost::dave) {
pos = initialPosition();
}
double Dave::speed() const {
if (state == State::Eyes)
return 2;
if (state == State::Frightened)
return 0.5;
return 0.75;
}
void Dave::setTarget(Position pacManPos) {
if (isInPen()) {
target = penDoorPosition();
return;
}
if (positionDistance(pos, pacManPos) > 8) {
target = pacManPos;
} else {
target = scatterTarget();
}
}
Position Dave::initialPosition() const {
return { 15.5, 14 };
}
Position Dave::scatterTarget() const {
return { 0, 30 };
}
} // namespace pacman

View File

@ -24,12 +24,15 @@ void GameState::step(std::chrono::milliseconds delta) {
pinky.update(delta); pinky.update(delta);
inky.setTarget(pacMan.positionInGrid(), pacMan.currentDirection(), blinky.positionInGrid()); inky.setTarget(pacMan.positionInGrid(), pacMan.currentDirection(), blinky.positionInGrid());
inky.update(delta); inky.update(delta);
dave.setTarget(pacMan.position());
dave.update(delta);
fruit.update(delta, score.eatenPellets); fruit.update(delta, score.eatenPellets);
checkCollision(blinky); checkCollision(blinky);
checkCollision(pinky); checkCollision(pinky);
checkCollision(inky); checkCollision(inky);
checkCollision(dave);
eatPellets(); eatPellets();
eatFruit(); eatFruit();
@ -58,6 +61,7 @@ void GameState::handleDeathAnimation(std::chrono::milliseconds delta) {
blinky.reset(); blinky.reset();
pinky.reset(); pinky.reset();
inky.reset(); inky.reset();
dave.reset();
pacMan.reset(); pacMan.reset();
pacManAI.reset(); pacManAI.reset();
timeSinceDeath = std::chrono::milliseconds(0); timeSinceDeath = std::chrono::milliseconds(0);
@ -78,6 +82,7 @@ void GameState::eatPellets() {
blinky.frighten(); blinky.frighten();
pinky.frighten(); pinky.frighten();
inky.frighten(); inky.frighten();
dave.frighten();
} }
} }

View File

@ -12,7 +12,7 @@ enum class Ghost : unsigned int {
blinky = 2, blinky = 2,
pinky = 3, pinky = 3,
inky = 4, inky = 4,
clyde = 5, dave = 5,
}; };
constexpr GridPosition pacman_right_wide = { 0, 0 }; constexpr GridPosition pacman_right_wide = { 0, 0 };
@ -46,7 +46,7 @@ constexpr GridPosition eyeSprite(Direction direction) {
} }
constexpr GridPosition ghostSprite(Ghost ghost, Direction direction, bool alternative) { constexpr GridPosition ghostSprite(Ghost ghost, Direction direction, bool alternative) {
assert(ghost >= Ghost::blinky && ghost <= Ghost::clyde && "Invalid Ghost"); assert(ghost >= Ghost::blinky && ghost <= Ghost::dave && "Invalid Ghost");
auto y = static_cast<size_t>(ghost); auto y = static_cast<size_t>(ghost);
size_t x = 0; size_t x = 0;
switch (direction) { switch (direction) {

View File

@ -1 +1,20 @@
#pragma once #pragma once
#include "Ghost.hpp"
namespace pacman {
class Dave final : public Ghost {
public:
Dave();
void setTarget(Position pacManPos);
protected:
double speed() const override;
Position initialPosition() const override;
private:
Position scatterTarget() const;
};
} // namespace pacman

View File

@ -21,6 +21,7 @@ struct GameState {
Blinky blinky; Blinky blinky;
Pinky pinky; Pinky pinky;
Inky inky; Inky inky;
Dave dave;
PacMan pacMan; PacMan pacMan;
PacManAI pacManAI; PacManAI pacManAI;