forked from dgcor/DGEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseAnimation.cpp
More file actions
executable file
·159 lines (149 loc) · 3.31 KB
/
Copy pathBaseAnimation.cpp
File metadata and controls
executable file
·159 lines (149 loc) · 3.31 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "BaseAnimation.h"
#include "SFML/CompositeSprite.h"
#include "TextureInfo.h"
void BaseAnimation::setAnimation(int32_t groupIdx, int32_t directionIdx,
bool resetAnimation, bool updateAnimationType)
{
AnimationType at = {};
uint32_t currentRelativeIndex = 0;
if (resetAnimation == false)
{
currentRelativeIndex = currentTextureIdx - textureIndexRange.first;
}
if (texturePackVar.holdsNullTexturePack() == true)
{
textureIndexRange = std::make_pair(0, 0);
at = AnimationType::PlayOnce;
}
else if (texturePackVar.holdsTexturePack() == true)
{
textureIndexRange = texturePackVar.getTexturePack()->getRange(
groupIdx, directionIdx, at
);
}
else
{
textureIndexRange = texturePackVar.getCompositeTexture()->getRange(
groupIdx, directionIdx, at
);
}
currentTextureIdx = textureIndexRange.first + currentRelativeIndex;
if (updateAnimationType == true)
{
animType = at;
}
if (resetAnimation == true ||
(resetAnimation == false && animType != AnimationType::BackAndForth))
{
backDirection = false;
}
}
void BaseAnimation::updateFrameIndex() noexcept
{
if (animType != AnimationType::BackAndForth)
{
if (currentTextureIdx < textureIndexRange.second)
{
currentTextureIdx++;
}
else
{
currentTextureIdx = textureIndexRange.first;
}
}
else
{
if (backDirection == false)
{
if (currentTextureIdx < textureIndexRange.second)
{
currentTextureIdx++;
}
if (currentTextureIdx == textureIndexRange.second)
{
backDirection = true;
}
}
else
{
if (currentTextureIdx > textureIndexRange.first)
{
currentTextureIdx--;
}
if (currentTextureIdx == textureIndexRange.first)
{
backDirection = false;
}
}
}
}
bool BaseAnimation::isAnimationAtBeginning() const noexcept
{
return currentTextureIdx <= textureIndexRange.first;
}
bool BaseAnimation::isAnimationAtEnd() const noexcept
{
return currentTextureIdx >= textureIndexRange.second;
}
bool BaseAnimation::hasPlayOnceAnimationFinished() const noexcept
{
return animType == AnimationType::PlayOnce &&
currentTextureIdx >= textureIndexRange.second;
}
bool BaseAnimation::update(sf::Time elapsedTime_) noexcept
{
if (pause == true ||
textureIndexRange.second <= textureIndexRange.first)
{
return false;
}
if (hasPlayOnceAnimationFinished() == true)
{
return false;
}
elapsedTime.update(elapsedTime_, [&]() -> bool
{
updateFrameIndex();
if (hasPlayOnceAnimationFinished() == true)
{
return false;
}
return true;
});
return true;
}
bool BaseAnimation::updateTexture(CompositeSprite& sprite, bool& absoluteOffset) const
{
if (texturePackVar.holdsNullTexturePack() == true)
{
return false;
}
else if (texturePackVar.holdsTexturePack() == true)
{
TextureInfo ti;
auto texturePack = texturePackVar.getTexturePack().get();
if (texturePack->get(currentTextureIdx, ti) == true)
{
sprite.setTexture(ti);
absoluteOffset = ti.absoluteOffset;
return true;
}
}
else
{
std::vector<TextureInfo> ti;
auto compTexture = texturePackVar.getCompositeTexture().get();
if (compTexture->get(currentTextureIdx, ti) == true)
{
sprite.setTexture(ti);
absoluteOffset = ti.front().absoluteOffset;
return true;
}
}
return false;
}
bool BaseAnimation::updateTexture(CompositeSprite& sprite) const
{
bool absoluteoffet;
return updateTexture(sprite, absoluteoffet);
}