From 82598dbaac1bc99a0a4f99833b68915ffbec0495 Mon Sep 17 00:00:00 2001 From: Ekaitz Zarraga Date: Thu, 17 Nov 2022 16:59:15 +0100 Subject: Simplify animations --- src/graphics/animation.h | 70 +++++++++++++----------------------------------- 1 file changed, 18 insertions(+), 52 deletions(-) (limited to 'src/graphics/animation.h') diff --git a/src/graphics/animation.h b/src/graphics/animation.h index 583da8a..04bf9ff 100644 --- a/src/graphics/animation.h +++ b/src/graphics/animation.h @@ -2,65 +2,31 @@ #define GRAPHICS_ANIMATION_H #include +#include namespace Graphics{ - class Animation{ + class Animation : public sf::Drawable, sf::Transformable{ protected: - int i_; - int count_; - int row_; - int lastTime_; - int switchTime_; - unsigned int w_, h_; - sf::IntRect rect_; - sf::Texture texture_; - bool ticked(int deltaTime); - public: - Animation(); - ~Animation(); - Animation(const sf::Texture& texture, int count, int switchTime, - int height, int width, int row); - virtual sf::IntRect& next(int deltaTime); - virtual void reset(); - virtual bool finished(); - static sf::Sprite& flip(sf::Sprite &sprite){ - sf::IntRect frame = sprite.getTextureRect(); - frame.left += frame.width; - frame.width *= -1; - sprite.setTextureRect(frame); - return sprite; - }; - }; + sf::Sprite sprite_; + sf::Vector2i frameSize_; + std::size_t numFrames_; + std::size_t currentFrame_; + std::size_t row_; + sf::Time duration_; + sf::Time elapsedTime_; + bool repeat_; - class OneShotAnimation : public Animation { public: - using Animation::Animation; - virtual sf::IntRect& next(int deltaTime) override; - bool finished() override; - }; - - class BouncingAnimation : public Animation{ - private: - bool up_; - public: - using Animation::Animation; - ~BouncingAnimation(); - virtual sf::IntRect& next(int deltaTime) override; + Animation(); + Animation(const sf::Texture& texture, std::size_t count, + sf::Time duration, bool repeat = false); + void update(sf::Time deltaTime); + void draw(sf::RenderTarget& target, sf::RenderStates states) + const override; + void reset(); + bool finished() const; }; - template - class FlippedAnimation : public T{ - static_assert(std::is_base_of::value, - "Base class of FlippedAnimation is not an Animation"); - public: - using T::T; - sf::IntRect& next(int deltaTime) override{ - T::next(deltaTime); - T::rect_.left = (T::i_ + 1) * T::w_; - T::rect_.width = -T::w_; - return T::rect_; - } - }; } #endif // GRAPHICS_ANIMATION_H -- cgit v1.2.3