diff --git a/lib/PacMan.cpp b/lib/PacMan.cpp index ba933c1..0164a2a 100644 --- a/lib/PacMan.cpp +++ b/lib/PacMan.cpp @@ -18,10 +18,11 @@ Position PacMan::positionInGrid() const { void PacMan::update(std::chrono::milliseconds time_delta, InputState state, const Board & board) { setDirection(state); - auto old = pos; + const auto old = pos; updateMazePosition(time_delta, board); - if(old != pos) - updateAnimationPosition(time_delta); + + const bool paused = pos == old; + updateAnimationPosition(time_delta, paused); } void PacMan::setDirection(const InputState & state) { @@ -35,8 +36,13 @@ void PacMan::setDirection(const InputState & state) { desired_direction = Direction::DOWN; } -void PacMan::updateAnimationPosition(std::chrono::milliseconds time_delta) { - pacManAnimation.updateAnimationPosition(time_delta); +void PacMan::updateAnimationPosition(std::chrono::milliseconds time_delta, bool paused) { + if(paused) { + pacManAnimation.pause(); + } + else { + pacManAnimation.updateAnimationPosition(time_delta); + } } void PacMan::updateMazePosition(std::chrono::milliseconds time_delta, const Board & board) { diff --git a/lib/PacMan.hpp b/lib/PacMan.hpp index 04b95bd..77bd2e1 100644 --- a/lib/PacMan.hpp +++ b/lib/PacMan.hpp @@ -29,7 +29,6 @@ private: void setDirection(const InputState & state); - void updateAnimationPosition(std::chrono::milliseconds time_delta); - + void updateAnimationPosition(std::chrono::milliseconds time_delta, bool paused); void updateMazePosition(std::chrono::milliseconds time_delta, const Board & board); }; diff --git a/lib/PacManAnimation.cpp b/lib/PacManAnimation.cpp index d483f4e..4d2d57a 100644 --- a/lib/PacManAnimation.cpp +++ b/lib/PacManAnimation.cpp @@ -21,3 +21,8 @@ void PacManAnimation::updateAnimationPosition(std::chrono::milliseconds time_del animation_position = int(animation_position + animation_position_delta) % 4; animation_position_delta = (animation_position_delta < 1) ? animation_position_delta : (animation_position_delta - 1); } + +void PacManAnimation::pause() { + animation_position = 0; + animation_position_delta = 0; +} diff --git a/lib/PacManAnimation.hpp b/lib/PacManAnimation.hpp index 88afb3b..9ce5327 100644 --- a/lib/PacManAnimation.hpp +++ b/lib/PacManAnimation.hpp @@ -12,6 +12,8 @@ public: [[nodiscard]] PositionInt animationFrame(Direction direction) const; void updateAnimationPosition(std::chrono::milliseconds time_delta); + void pause(); + private: uint8_t animation_position = 0;