diff options
-rw-r--r-- | src/graphics/animation.h | 25 | ||||
-rw-r--r-- | src/main.cpp | 4 |
2 files changed, 25 insertions, 4 deletions
diff --git a/src/graphics/animation.h b/src/graphics/animation.h index 010a64a..583da8a 100644 --- a/src/graphics/animation.h +++ b/src/graphics/animation.h @@ -24,12 +24,19 @@ namespace Graphics{ 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; + }; }; class OneShotAnimation : public Animation { public: using Animation::Animation; - sf::IntRect& next(int deltaTime) override; + virtual sf::IntRect& next(int deltaTime) override; bool finished() override; }; @@ -39,7 +46,21 @@ namespace Graphics{ public: using Animation::Animation; ~BouncingAnimation(); - sf::IntRect& next(int deltaTime) override; + virtual sf::IntRect& next(int deltaTime) override; + }; + + template <class T> + class FlippedAnimation : public T{ + static_assert(std::is_base_of<Animation, T>::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 diff --git a/src/main.cpp b/src/main.cpp index 1987af4..94bf125 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -23,7 +23,7 @@ class Unit: Entity{ std::vector<int> animation_frame_count { 6, 8, 6, 1, 14 }; gph::Animation walking_animation; gph::Animation standing_animation; - gph::OneShotAnimation shooting_animation; + gph::FlippedAnimation<gph::OneShotAnimation> shooting_animation; gph::OneShotAnimation complaining_animation; gph::OneShotAnimation dying_animation; gph::Animation* current_animation = &standing_animation; @@ -38,7 +38,7 @@ class Unit: Entity{ spritesheet_.loadFromFile("assets/img/Player/Player Angle 1 Sheet.png"); standing_animation = gph::Animation(spritesheet_, 6, 60, 44, 48, 0); walking_animation = gph::Animation(spritesheet_, 8, 60, 44, 48, 1); - shooting_animation = gph::OneShotAnimation(spritesheet_, 6, 60, 44, 48, 2); + shooting_animation = gph::FlippedAnimation<gph::OneShotAnimation>(spritesheet_, 6, 60, 44, 48, 2); complaining_animation = gph::OneShotAnimation(spritesheet_, 1, 60, 44, 48, 3); dying_animation = gph::OneShotAnimation(spritesheet_, 14, 60, 44, 48, 4); |