From 1c2a216055a107c1ce8d9d6845be80df0e0764f4 Mon Sep 17 00:00:00 2001 From: Ekaitz Zarraga Date: Sun, 13 Nov 2022 20:39:55 +0100 Subject: Add flipped animations ftw --- src/graphics/animation.h | 25 +++++++++++++++++++++++-- src/main.cpp | 4 ++-- 2 files changed, 25 insertions(+), 4 deletions(-) (limited to 'src') 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 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 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 animation_frame_count { 6, 8, 6, 1, 14 }; gph::Animation walking_animation; gph::Animation standing_animation; - gph::OneShotAnimation shooting_animation; + gph::FlippedAnimation 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(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); -- cgit v1.2.3