aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/graphics/animations/je_animation.cpp
blob: 028ef5667b0b04b34c41e2b28ddec80a113c2aed (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "../../math/je_vector2.hpp"
#include "je_animation.h"

using namespace JinEngine::Math;

namespace JinEngine
{
    namespace Graphics
    {
        namespace Animations
        {

            Animation::Animation()
                : mIndex(0)
                , mActive(true)
                , mLoop(true)
                , mTick(0)
            {
            }

            void Animation::addFrame(const Sprite* frame)
            {
                if(frame != nullptr)
                    mFrames.push_back(frame);
            }

            void Animation::addFrames(const std::vector<Sprite*>& frames)
            {
                mFrames.insert(mFrames.end(), frames.begin(), frames.end());
            }

            void Animation::update(float dt)
            {
                if (!mActive)
                    return;
                mTick += dt;
                if (mTick >= mSpeed)
                {
                    mTick -= mSpeed;
                    next();
                }
            }

            void Animation::next()
            {
                int count = mFrames.size(); 
                ++mIndex;
                if (mLoop)
                    mIndex %= count;
                mIndex = clamp<uint>(mIndex, 0, count - 1);
            }

            void Animation::pause()
            {
                mActive = false;
            }

            void Animation::resume()
            {
                mActive = true;
            }

            void Animation::rewind()
            {
                mIndex = 0;
                mTick = 0;
            }

            void Animation::setSpeed(float speed)
            {
                mSpeed = speed;
            }

            void Animation::setLoop(bool isLoop)
            {
                mLoop = isLoop;
            }

            uint Animation::getCurrentFrameIndex()
            {
                return mIndex;
            }

            const Sprite* Animation::getCurrentFrame()
            {
                if (mIndex >= mFrames.size())
                    return nullptr;
                return mFrames[mIndex];
            }

            void Animation::setCurrentFrame(uint frame)
            {
                mIndex = frame;
            }

            void Animation::render(float x, float y, float sx, float sy, float r)
            {
                if (mFrames.size() == 0)
                    return;
                if (without<uint>(mIndex, 0, mFrames.size() - 1))
                    return;
                const Sprite* spr = getCurrentFrame();
                if(spr)
                    spr->render(x, y, sx, sy, r);
            }

        }
    }
}