diff --git a/lib/Blinky.cpp b/lib/Blinky.cpp index 34b363f..c864f06 100644 --- a/lib/Blinky.cpp +++ b/lib/Blinky.cpp @@ -23,7 +23,8 @@ Position Blinky::target(const GameState & gameState) const { if (isInPen()) return penDoorPosition(); - return state == State::Chase ? gridPositionToPosition(gameState.pacMan.positionInGrid()) : scatterTarget(); + const auto pacManPosition = gridPositionToPosition(gameState.pacMan.positionInGrid()); + return state == State::Chase ? pacManPosition : scatterTarget(); } Position Blinky::initialPosition() const { diff --git a/lib/Clyde.cpp b/lib/Clyde.cpp index b11e380..087a9ee 100644 --- a/lib/Clyde.cpp +++ b/lib/Clyde.cpp @@ -26,10 +26,10 @@ Position Clyde::target(const GameState & gameState) const { // Clyde always target its scatter target, unless pacman is further than 8 tiles away auto targetPosition = scatterTarget(); - const auto & pacmanPosition = gameState.pacMan.positionInGrid(); - auto distanceFomPacMan = std::hypot(pos.x - pacmanPosition.x, pos.y - pacmanPosition.y); + const auto pacManPosition = gameState.pacMan.positionInGrid(); + auto distanceFomPacMan = std::hypot(pos.x - pacManPosition.x, pos.y - pacManPosition.y); if (state == State::Chase && distanceFomPacMan > 8) - targetPosition = gridPositionToPosition(pacmanPosition); + targetPosition = gridPositionToPosition(pacManPosition); return targetPosition; } diff --git a/lib/Ghost.cpp b/lib/Ghost.cpp index 90576ac..31fb4a6 100644 --- a/lib/Ghost.cpp +++ b/lib/Ghost.cpp @@ -199,14 +199,14 @@ void Ghost::updateAnimation(std::chrono::milliseconds time_delta) { * Ghosts alternate between the scatter and chase states at * specific intervals */ -Ghost::State Ghost::defaultStateAtDuration(std::chrono::seconds s) { +Ghost::State Ghost::defaultStateAtDuration(std::chrono::seconds seconds) { // This array denotes the duration of each state, alternating between scatter and chase std::array changes = { /*scatter*/ 7, 20, 7, 20, 5, 20, 5 }; // To know the current state we first compute the cumulative time using std::partial_sum // This gives us {7, 27, 34, 54, 59, 79, 84} std::partial_sum(std::begin(changes), std::end(changes), std::begin(changes)); - // Then we look for the first value in the array greater than the time spend in chase/scatter states - auto it = std::upper_bound(std::begin(changes), std::end(changes), s.count()); + // Then we look for the first value in the array greater than the time spent in chase/scatter states + auto it = std::upper_bound(std::begin(changes), std::end(changes), seconds.count()); // We get the position of that iterator in the array auto count = std::distance(std::begin(changes), it); // Because the first positition is scatter, all the even positions will be scatter diff --git a/lib/include/Ghost.hpp b/lib/include/Ghost.hpp index d61b06d..421f8cf 100644 --- a/lib/include/Ghost.hpp +++ b/lib/include/Ghost.hpp @@ -41,7 +41,7 @@ private: void updateDirection(const GameState & gameState); protected: - State defaultStateAtDuration(std::chrono::seconds s); + State defaultStateAtDuration(std::chrono::seconds seconds); virtual double speed(const GameState & gameState) const = 0; virtual Position target(const GameState & gameState) const = 0;