Cleanup PacManAnimation a bit

This commit is contained in:
Corentin Jabot 2021-07-19 12:06:27 +02:00
parent d6a046ff47
commit fe04e7d03a
2 changed files with 11 additions and 6 deletions

View file

@ -2,6 +2,11 @@
namespace pacman {
constexpr std::array<GridPosition,4> down_animation { Atlas::pacman_down_wide, Atlas::pacman_down_narrow, Atlas::pacman_closed, Atlas::pacman_down_narrow };
constexpr std::array<GridPosition,4> left_animation{ Atlas::pacman_left_wide, Atlas::pacman_left_narrow, Atlas::pacman_closed, Atlas::pacman_left_narrow };
constexpr std::array<GridPosition,4> right_animation{ Atlas::pacman_right_wide, Atlas::pacman_right_narrow, Atlas::pacman_closed, Atlas::pacman_right_narrow };
constexpr std::array<GridPosition,4> up_animation{ Atlas::pacman_up_wide, Atlas::pacman_up_narrow, Atlas::pacman_closed, Atlas::pacman_up_narrow };
GridPosition PacManAnimation::animationFrame(Direction direction) const {
switch (direction) {
case Direction::LEFT:
@ -27,15 +32,19 @@ void PacManAnimation::updateAnimationPosition(std::chrono::milliseconds time_del
return;
animation_position_delta += (0.02) * double(time_delta.count());
animation_position = int(animation_position + animation_position_delta);
if (!dead)
animation_position = animation_position % 4;
animation_position = animation_position % 4;
if(animation_position_delta > 1)
animation_position_delta = animation_position_delta - 1;
animation_position_delta = (animation_position_delta < 1) ? animation_position_delta : (animation_position_delta - 1);
}
void PacManAnimation::pause() {
// when hitting a wall, Pacman's mouth stays wide open
animation_position = 0;
animation_position_delta = 0;
}

View file

@ -20,10 +20,6 @@ public:
private:
uint8_t animation_position = 0;
double animation_position_delta = 0.0;
const GridPosition down_animation[4]{ Atlas::pacman_down_wide, Atlas::pacman_down_narrow, Atlas::pacman_closed, Atlas::pacman_down_narrow };
const GridPosition left_animation[4]{ Atlas::pacman_left_wide, Atlas::pacman_left_narrow, Atlas::pacman_closed, Atlas::pacman_left_narrow };
const GridPosition right_animation[4]{ Atlas::pacman_right_wide, Atlas::pacman_right_narrow, Atlas::pacman_closed, Atlas::pacman_right_narrow };
const GridPosition up_animation[4]{ Atlas::pacman_up_wide, Atlas::pacman_up_narrow, Atlas::pacman_closed, Atlas::pacman_up_narrow };
};
} // namespace pacman