summaryrefslogtreecommitdiff
path: root/src/graphics/animation.h
blob: 583da8a3cd73daa3973f9429c73b410367642ba9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef GRAPHICS_ANIMATION_H
#define GRAPHICS_ANIMATION_H

#include <SFML/Graphics.hpp>

namespace Graphics{

    class Animation{
        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;
            };
    };

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

    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