Extract PacManAnimation

This commit is contained in:
Patricia Aas 2020-11-28 13:04:14 +01:00
parent bbea9213ea
commit bf9aaa5cfc
4 changed files with 64 additions and 39 deletions

View file

@ -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) {

View file

@ -5,14 +5,13 @@
#include "Direction.h"
#include "InputState.h"
#include "Position.h"
#include "PacManAnimation.hpp"
#include <SDL2/SDL_rect.h>
#include <chrono>
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);

View file

@ -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);
}

View file

@ -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 <SDL2/SDL_rect.h>
#include <chrono>
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