From bf9aaa5cfc06a2ebf7e47302b8960cce0f67c811 Mon Sep 17 00:00:00 2001 From: Patricia Aas Date: Sat, 28 Nov 2020 13:04:14 +0100 Subject: [PATCH] Extract PacManAnimation --- pacman/lib/PacMan.cpp | 25 +++-------------------- pacman/lib/PacMan.h | 19 ++--------------- pacman/lib/PacManAnimation.cpp | 22 ++++++++++++++++++++ pacman/lib/PacManAnimation.hpp | 37 ++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 39 deletions(-) create mode 100644 pacman/lib/PacManAnimation.cpp create mode 100644 pacman/lib/PacManAnimation.hpp diff --git a/pacman/lib/PacMan.cpp b/pacman/lib/PacMan.cpp index 36a8d6d..4832aa1 100644 --- a/pacman/lib/PacMan.cpp +++ b/pacman/lib/PacMan.cpp @@ -1,25 +1,8 @@ #include "PacMan.h" - -PacMan::PacMan() : - right_animation{right_wide, right_narrow, closed, right_narrow}, - left_animation{left_wide, left_narrow, closed, left_narrow}, - up_animation{up_wide, up_narrow, closed, up_narrow}, - down_animation{down_wide, down_narrow, closed, down_narrow} { -} +#include "PacManAnimation.hpp" SDL_Rect PacMan::currentSprite() const { - switch (direction) { - case Direction::NONE: - return closed; - 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]; - } + return pacManAnimation.animationFrame(direction); } Position PacMan::currentPosition() const { @@ -44,9 +27,7 @@ void PacMan::setDirection(const InputState & state) { } void PacMan::updateAnimationPosition(std::chrono::milliseconds time_delta) { - animation_position_delta += (time_delta.count() / 100.0); - animation_position = int(animation_position + animation_position_delta) % 4; - animation_position_delta = (animation_position_delta < 1) ? animation_position_delta : (animation_position_delta - 1); + pacManAnimation.updateAnimationPosition(time_delta); } void PacMan::updateMazePosition(std::chrono::milliseconds time_delta, const Board & board) { diff --git a/pacman/lib/PacMan.h b/pacman/lib/PacMan.h index ca1c534..781eb6e 100644 --- a/pacman/lib/PacMan.h +++ b/pacman/lib/PacMan.h @@ -5,14 +5,13 @@ #include "Direction.h" #include "InputState.h" #include "Position.h" +#include "PacManAnimation.hpp" #include #include class PacMan { public: - PacMan(); - [[nodiscard]] SDL_Rect currentSprite() const; [[nodiscard]] Position currentPosition() const; @@ -24,21 +23,7 @@ private: Direction direction = Direction::NONE; Direction desired_direction = Direction::NONE; Position pos = {14, 23}; - const SDL_Rect right_wide = {0 * 32, 0, 32, 32}; - const SDL_Rect right_narrow = {1 * 32, 0, 32, 32}; - const SDL_Rect closed = {2 * 32, 0, 32, 32}; - const SDL_Rect left_narrow = {3 * 32, 0, 32, 32}; - const SDL_Rect left_wide = {4 * 32, 0, 32, 32}; - const SDL_Rect up_wide = {5 * 32, 0, 32, 32}; - const SDL_Rect up_narrow = {6 * 32, 0, 32, 32}; - const SDL_Rect down_wide = {7 * 32, 0, 32, 32}; - const SDL_Rect down_narrow = {8 * 32, 0, 32, 32}; - const SDL_Rect right_animation[4]; - const SDL_Rect left_animation[4]; - const SDL_Rect up_animation[4]; - const SDL_Rect down_animation[4]; - uint8_t animation_position = 0; - float_t animation_position_delta = 0.0; + PacManAnimation pacManAnimation; void setDirection(const InputState & state); diff --git a/pacman/lib/PacManAnimation.cpp b/pacman/lib/PacManAnimation.cpp new file mode 100644 index 0000000..8365bbc --- /dev/null +++ b/pacman/lib/PacManAnimation.cpp @@ -0,0 +1,22 @@ +#include "PacManAnimation.hpp" + +SDL_Rect PacManAnimation::animationFrame(Direction direction) const { + switch (direction) { + case Direction::NONE: + return closed; + 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]; + } +} + +void PacManAnimation::updateAnimationPosition(std::chrono::milliseconds time_delta) { + animation_position_delta += (time_delta.count() / 100.0); + animation_position = int(animation_position + animation_position_delta) % 4; + animation_position_delta = (animation_position_delta < 1) ? animation_position_delta : (animation_position_delta - 1); +} diff --git a/pacman/lib/PacManAnimation.hpp b/pacman/lib/PacManAnimation.hpp new file mode 100644 index 0000000..7d754f9 --- /dev/null +++ b/pacman/lib/PacManAnimation.hpp @@ -0,0 +1,37 @@ +#ifndef PACMAN_PACMAN_ANIMATION_HPP +#define PACMAN_PACMAN_ANIMATION_HPP + +#include "Board.h" +#include "Direction.h" +#include "InputState.h" +#include "Position.h" + +#include +#include + +class PacManAnimation { +public: + + SDL_Rect animationFrame(Direction direction) const; + + void updateAnimationPosition(std::chrono::milliseconds time_delta); + +private: + uint8_t animation_position = 0; + float_t animation_position_delta = 0.0; + const SDL_Rect right_wide = {0 * 32, 0, 32, 32}; + const SDL_Rect right_narrow = {1 * 32, 0, 32, 32}; + const SDL_Rect closed = {2 * 32, 0, 32, 32}; + const SDL_Rect left_narrow = {3 * 32, 0, 32, 32}; + const SDL_Rect left_wide = {4 * 32, 0, 32, 32}; + const SDL_Rect up_wide = {5 * 32, 0, 32, 32}; + const SDL_Rect up_narrow = {6 * 32, 0, 32, 32}; + const SDL_Rect down_wide = {7 * 32, 0, 32, 32}; + const SDL_Rect down_narrow = {8 * 32, 0, 32, 32}; + const SDL_Rect down_animation[4]{down_wide, down_narrow, closed, down_narrow}; + const SDL_Rect left_animation[4]{left_wide, left_narrow, closed, left_narrow}; + const SDL_Rect right_animation[4]{right_wide, right_narrow, closed, right_narrow}; + const SDL_Rect up_animation[4]{up_wide, up_narrow, closed, up_narrow}; +}; + +#endif //PACMAN_PACMAN_ANIMATION_HPP