-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimation.cpp
More file actions
70 lines (57 loc) · 1.19 KB
/
Copy pathAnimation.cpp
File metadata and controls
70 lines (57 loc) · 1.19 KB
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
#include "Animation.h"
using namespace std;
Animation::Animation(float aniSpeed, bool looping)
{
mAnimationSpeed = aniSpeed;
mCurrentSprite = 0;
mLooping = looping;
timeLastUpdated = -1.0f;
timeUntilNextSprite = mAnimationSpeed;
}
Animation::~Animation()
{
if (mSprites[0])
clearSprites();
}
void Animation::update(float currentTime)
{
if ((mSprites.size() > 0) && (mAnimationSpeed > 0.0f) && (mLooping || mCurrentSprite != mSprites.size() - 1))
{
if (timeLastUpdated != -1.0f)
{
timeUntilNextSprite -= currentTime - timeLastUpdated;
while (timeUntilNextSprite <= 0)
{
++mCurrentSprite;
if (mCurrentSprite >= mSprites.size())
mCurrentSprite = 0;
timeUntilNextSprite += mAnimationSpeed;
}
}
}
timeLastUpdated = currentTime;
}
void Animation::addSprite(Sprite* newSprite)
{
mSprites.push_back(newSprite);
}
void Animation::clearSprites()
{
for (unsigned int i = 0; i < mSprites.size(); ++i)
{
delete mSprites[i];
}
mSprites.clear();
}
void Animation::setAnimationSpeed(float speed)
{
mAnimationSpeed = speed;
}
void Animation::setCurrentSprite(unsigned int index)
{
mCurrentSprite = index;
}
void Animation::setLooping(bool loop)
{
mLooping = loop;
}