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.pinky);
renderGhost(gameState.inky);
renderGhost(gameState.dave);
renderScore(gameState.score.points);
renderLives(gameState.score.lives);

View file

@ -1 +1,38 @@
#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);
inky.setTarget(pacMan.positionInGrid(), pacMan.currentDirection(), blinky.positionInGrid());
inky.update(delta);
dave.setTarget(pacMan.position());
dave.update(delta);
fruit.update(delta, score.eatenPellets);
checkCollision(blinky);
checkCollision(pinky);
checkCollision(inky);
checkCollision(dave);
eatPellets();
eatFruit();
@ -58,6 +61,7 @@ void GameState::handleDeathAnimation(std::chrono::milliseconds delta) {
blinky.reset();
pinky.reset();
inky.reset();
dave.reset();
pacMan.reset();
pacManAI.reset();
timeSinceDeath = std::chrono::milliseconds(0);
@ -78,6 +82,7 @@ void GameState::eatPellets() {
blinky.frighten();
pinky.frighten();
inky.frighten();
dave.frighten();
}
}

View file

@ -12,7 +12,7 @@ enum class Ghost : unsigned int {
blinky = 2,
pinky = 3,
inky = 4,
clyde = 5,
dave = 5,
};
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) {
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);
size_t x = 0;
switch (direction) {

View file

@ -1 +1,20 @@
#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;
Pinky pinky;
Inky inky;
Dave dave;
PacMan pacMan;
PacManAI pacManAI;