2020-11-28 12:04:14 +00:00
|
|
|
#include "PacManAnimation.hpp"
|
|
|
|
|
2021-07-05 12:10:01 +00:00
|
|
|
namespace pacman {
|
|
|
|
|
2021-07-19 10:06:27 +00:00
|
|
|
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 };
|
|
|
|
|
2021-07-05 09:46:49 +00:00
|
|
|
GridPosition PacManAnimation::animationFrame(Direction direction) const {
|
2020-11-28 12:04:14 +00:00
|
|
|
switch (direction) {
|
|
|
|
case Direction::LEFT:
|
|
|
|
return left_animation[animation_position];
|
|
|
|
case Direction::RIGHT:
|
|
|
|
return right_animation[animation_position];
|
|
|
|
case Direction::UP:
|
|
|
|
return up_animation[animation_position];
|
|
|
|
case Direction::DOWN:
|
|
|
|
return down_animation[animation_position];
|
2021-06-16 10:52:04 +00:00
|
|
|
case Direction::NONE:
|
|
|
|
default:
|
2021-06-24 11:32:52 +00:00
|
|
|
return Atlas::pacman_closed;
|
2020-11-28 12:04:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-02 13:31:32 +00:00
|
|
|
GridPosition PacManAnimation::deathAnimationFrame() const {
|
2021-07-05 09:46:49 +00:00
|
|
|
return GridPosition{ animation_position, 1 };
|
2021-06-28 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PacManAnimation::updateAnimationPosition(std::chrono::milliseconds time_delta, bool dead) {
|
|
|
|
if (dead && animation_position >= 11)
|
|
|
|
return;
|
|
|
|
|
2021-07-05 09:40:10 +00:00
|
|
|
animation_position_delta += (0.02) * double(time_delta.count());
|
2021-07-19 10:06:27 +00:00
|
|
|
|
2021-06-28 10:42:21 +00:00
|
|
|
animation_position = int(animation_position + animation_position_delta);
|
|
|
|
|
|
|
|
if (!dead)
|
2021-07-19 10:06:27 +00:00
|
|
|
animation_position = animation_position % 4;
|
|
|
|
|
|
|
|
if(animation_position_delta > 1)
|
|
|
|
animation_position_delta = animation_position_delta - 1;
|
2021-06-28 10:42:21 +00:00
|
|
|
|
2020-11-28 12:04:14 +00:00
|
|
|
}
|
2021-06-24 07:46:58 +00:00
|
|
|
|
|
|
|
void PacManAnimation::pause() {
|
2021-07-19 10:06:27 +00:00
|
|
|
// when hitting a wall, Pacman's mouth stays wide open
|
2021-06-24 11:32:52 +00:00
|
|
|
animation_position = 0;
|
|
|
|
animation_position_delta = 0;
|
2021-06-24 07:46:58 +00:00
|
|
|
}
|
2021-07-05 12:10:01 +00:00
|
|
|
|
|
|
|
} // namespace pacman
|