From 4c2390a3ff24ab0937ff163ac782d31d7f85ecbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Mon, 18 Oct 2021 14:46:12 +0200 Subject: [PATCH] Ex 161: Implement Dave. --- lib/Canvas.cpp | 1 + lib/Dave.cpp | 37 +++++++++++++++++++++++++++++++++++++ lib/GameState.cpp | 5 +++++ lib/include/Atlas.hpp | 4 ++-- lib/include/Dave.hpp | 19 +++++++++++++++++++ lib/include/GameState.hpp | 1 + 6 files changed, 65 insertions(+), 2 deletions(-) diff --git a/lib/Canvas.cpp b/lib/Canvas.cpp index cac63ee..a4f01f6 100644 --- a/lib/Canvas.cpp +++ b/lib/Canvas.cpp @@ -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); diff --git a/lib/Dave.cpp b/lib/Dave.cpp index e1afcf8..1520855 100644 --- a/lib/Dave.cpp +++ b/lib/Dave.cpp @@ -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 diff --git a/lib/GameState.cpp b/lib/GameState.cpp index b47523c..d575937 100644 --- a/lib/GameState.cpp +++ b/lib/GameState.cpp @@ -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(); } } diff --git a/lib/include/Atlas.hpp b/lib/include/Atlas.hpp index 6a70cd8..747fd2a 100644 --- a/lib/include/Atlas.hpp +++ b/lib/include/Atlas.hpp @@ -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(ghost); size_t x = 0; switch (direction) { diff --git a/lib/include/Dave.hpp b/lib/include/Dave.hpp index 6f70f09..935fb96 100644 --- a/lib/include/Dave.hpp +++ b/lib/include/Dave.hpp @@ -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 diff --git a/lib/include/GameState.hpp b/lib/include/GameState.hpp index cf97ca3..976dc97 100644 --- a/lib/include/GameState.hpp +++ b/lib/include/GameState.hpp @@ -21,6 +21,7 @@ struct GameState { Blinky blinky; Pinky pinky; Inky inky; + Dave dave; PacMan pacMan; PacManAI pacManAI;