diff --git a/lib/Game.cpp b/lib/Game.cpp index 02a9d74..0499c58 100644 --- a/lib/Game.cpp +++ b/lib/Game.cpp @@ -11,8 +11,8 @@ void Game::run() { auto current_time = std::chrono::system_clock::now(); while (true) { - auto newTime = std::chrono::system_clock::now(); - auto frameTime = std::chrono::duration_cast(newTime - current_time); + const auto newTime = std::chrono::system_clock::now(); + const auto frameTime = std::chrono::duration_cast(newTime - current_time); current_time = newTime; accumulator += frameTime; @@ -31,7 +31,7 @@ void Game::run() { } void Game::processEvents(InputState & inputState) { - auto event = canvas.pollEvent(); + const auto event = canvas.pollEvent(); if (event && event.value().type == sf::Event::Closed) { inputState.close = true; return; diff --git a/lib/Ghost.cpp b/lib/Ghost.cpp index ea47fbe..4dfaf59 100644 --- a/lib/Ghost.cpp +++ b/lib/Ghost.cpp @@ -75,7 +75,7 @@ void Ghost::update(std::chrono::milliseconds time_delta, const GameState & gameS if (state == State::Scatter || state == State::Chase) { timeChase += time_delta; - auto newState = defaultStateAtDuration(std::chrono::duration_cast(timeChase)); + const auto newState = defaultStateAtDuration(std::chrono::duration_cast(timeChase)); if (newState != state) { direction = oppositeDirection(direction); state = newState; @@ -204,13 +204,17 @@ void Ghost::updateAnimation(std::chrono::milliseconds time_delta) { 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 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 // all the odd positions will be chase return count % 2 == 0 ? State::Scatter : State::Chase; diff --git a/lib/PacMan.cpp b/lib/PacMan.cpp index f3739f4..25d1b89 100644 --- a/lib/PacMan.cpp +++ b/lib/PacMan.cpp @@ -81,31 +81,34 @@ void PacMan::updateMazePosition(std::chrono::milliseconds time_delta) { return isWalkableForPacMan(moveToPosition(pos, move_direction)); }; - if (canGo(desired_direction)) { + if (desired_direction != direction && canGo(desired_direction)) { direction = desired_direction; } - if (canGo(direction)) { - switch (direction) { - case Direction::NONE: - break; - case Direction::LEFT: - pos.x -= position_delta; - pos.y = std::floor(pos.y); - break; - case Direction::RIGHT: - pos.x += position_delta; - pos.y = std::floor(pos.y); - break; - case Direction::UP: - pos.x = std::floor(pos.x); - pos.y -= position_delta; - break; - case Direction::DOWN: - pos.x = std::floor(pos.x); - pos.y += position_delta; - break; - } + if (!canGo(direction)) { + return; + } + + switch (direction) { + case Direction::LEFT: + pos.x -= position_delta; + pos.y = std::floor(pos.y); + break; + case Direction::RIGHT: + pos.x += position_delta; + pos.y = std::floor(pos.y); + break; + case Direction::UP: + pos.x = std::floor(pos.x); + pos.y -= position_delta; + break; + case Direction::DOWN: + pos.x = std::floor(pos.x); + pos.y += position_delta; + break; + case Direction::NONE: + default: + break; } } diff --git a/lib/include/Ghost.hpp b/lib/include/Ghost.hpp index 982c62e..4d473e6 100644 --- a/lib/include/Ghost.hpp +++ b/lib/include/Ghost.hpp @@ -23,9 +23,7 @@ public: virtual ~Ghost() = default; GridPosition currentSprite() const; - Position position() const; - GridPosition positionInGrid() const; void update(std::chrono::milliseconds time_delta, const GameState & gameState); @@ -41,12 +39,6 @@ private: void updateDirection(const GameState & gameState); protected: - State defaultStateAtDuration(std::chrono::seconds seconds); - - virtual double speed(const GameState & gameState) const = 0; - virtual Position target(const GameState & gameState) const = 0; - virtual Position initialPosition() const = 0; - Atlas::Ghost spriteSet; Direction direction = Direction::NONE; double timeForAnimation = 0; @@ -56,6 +48,13 @@ protected: std::chrono::milliseconds timeChase = {}; Position pos; GridPosition last_grid_position = { 0, 0 }; + + State defaultStateAtDuration(std::chrono::seconds seconds); + + virtual double speed(const GameState & gameState) const = 0; + virtual Position target(const GameState & gameState) const = 0; + virtual Position initialPosition() const = 0; + bool isInPen() const; };