Repository navigation
Expand file tree
/
Copy pathAnimationComponent.cpp
More file actions
403 lines (346 loc) · 9.19 KB
/
Copy pathAnimationComponent.cpp
File metadata and controls
403 lines (346 loc) · 9.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include "AnimationComponent.h"
#include <deki/assets/AssetManager.h>
#include "FrameAnimationMsgPack.h"
#include "Sprite.h"
#include <deki/LogSystem.h>
#include <deki/Time.h>
#include <deki/Object.h>
namespace Deki2D
{
AnimationComponent::AnimationComponent(SpriteComponent* spriteComp)
: spriteComponent(spriteComp),
animationData(nullptr),
ownsAnimationData(false),
currentSequence(0),
currentFrame(0),
frameStartTime(0),
isPlaying(false),
hasFinished(false),
playOnceOverride(false),
completionCallback(nullptr)
{
}
AnimationComponent::~AnimationComponent()
{
if (animationData && ownsAnimationData)
{
delete animationData;
}
animationData = nullptr;
}
void AnimationComponent::Awake()
{
Setup();
}
void AnimationComponent::Setup()
{
if (animation.ptr && !animationData)
{
animationData = animation.Get();
ownsAnimationData = false;
}
// Find the sprite component on the same object when none is set
if (!spriteComponent)
{
Deki::Object* owner = GetOwner();
if (owner)
{
spriteComponent = owner->GetComponent<SpriteComponent>();
}
}
// Start the first sequence
if (animationData && spriteComponent && !animationData->animations.empty())
{
isPlaying = true;
currentSequence = 0;
currentFrame = 0;
frameStartTime = 0;
ApplyCurrentFrame();
}
}
void AnimationComponent::Update()
{
clockMs += Deki::Time::GetDeltaTimeF();
// +1 so the clock is never 0, which frameStartTime uses for "not started".
UpdateAnimation(static_cast<uint32_t>(clockMs) + 1);
}
void AnimationComponent::Play(bool restartIfPlaying)
{
const FrameAnimSequence* seq = GetCurrentSequence();
if (!seq || seq->frames.empty())
{
return;
}
if (isPlaying && !restartIfPlaying)
{
return;
}
currentFrame = 0;
frameStartTime = 0;
isPlaying = true;
hasFinished = false;
playOnceOverride = false;
ApplyCurrentFrame();
}
bool AnimationComponent::PlayAnimation(const char* name, bool restartIfPlaying)
{
int index = FindAnimationIndex(name);
if (index < 0)
{
return false;
}
if (currentSequence == index && isPlaying && !restartIfPlaying)
{
return true;
}
currentSequence = index;
currentFrame = 0;
frameStartTime = 0;
isPlaying = true;
hasFinished = false;
playOnceOverride = false;
ApplyCurrentFrame();
return true;
}
void AnimationComponent::PlayOnce()
{
const FrameAnimSequence* seq = GetCurrentSequence();
if (!seq || seq->frames.empty())
{
return;
}
currentFrame = 0;
frameStartTime = 0;
isPlaying = true;
hasFinished = false;
playOnceOverride = true;
ApplyCurrentFrame();
}
bool AnimationComponent::PlayAnimationOnce(const char* name)
{
int index = FindAnimationIndex(name);
if (index < 0)
{
return false;
}
currentSequence = index;
currentFrame = 0;
frameStartTime = 0;
isPlaying = true;
hasFinished = false;
playOnceOverride = true;
ApplyCurrentFrame();
return true;
}
bool AnimationComponent::SetAnimation(const char* name)
{
int index = FindAnimationIndex(name);
if (index < 0)
{
return false;
}
currentSequence = index;
currentFrame = 0;
frameStartTime = 0;
hasFinished = false;
ApplyCurrentFrame();
return true;
}
const char* AnimationComponent::GetCurrentAnimationName() const
{
const FrameAnimSequence* seq = GetCurrentSequence();
return seq ? seq->name.c_str() : "";
}
int AnimationComponent::GetAnimationCount() const
{
return animationData ? static_cast<int>(animationData->animations.size()) : 0;
}
const char* AnimationComponent::GetAnimationName(int index) const
{
if (!animationData || index < 0 || index >= static_cast<int>(animationData->animations.size()))
{
return "";
}
return animationData->animations[index].name.c_str();
}
void AnimationComponent::Stop()
{
isPlaying = false;
currentFrame = 0;
hasFinished = false;
ApplyCurrentFrame();
}
void AnimationComponent::Pause()
{
isPlaying = false;
}
void AnimationComponent::Resume()
{
const FrameAnimSequence* seq = GetCurrentSequence();
if (seq && !seq->frames.empty())
{
isPlaying = true;
}
}
void AnimationComponent::UpdateAnimation(uint32_t currentTime)
{
const FrameAnimSequence* seq = GetCurrentSequence();
if (!isPlaying || !seq || seq->frames.empty())
{
return;
}
// A single frame never changes
if (seq->frames.size() == 1)
{
return;
}
// 0 means the sequence has just started: its first frame starts now
if (frameStartTime == 0)
{
frameStartTime = currentTime;
}
uint32_t elapsed = currentTime - frameStartTime;
const size_t frameCount = seq->frames.size();
bool advanced = false;
while (elapsed >= static_cast<uint32_t>(seq->frames[currentFrame].duration))
{
elapsed -= static_cast<uint32_t>(seq->frames[currentFrame].duration);
currentFrame++;
advanced = true;
if (currentFrame >= static_cast<int32_t>(frameCount))
{
if (seq->loop && !playOnceOverride)
{
currentFrame = 0;
}
else
{
currentFrame = static_cast<int32_t>(frameCount - 1);
isPlaying = false;
hasFinished = true;
playOnceOverride = false;
if (completionCallback)
{
completionCallback();
completionCallback = nullptr;
}
elapsed = 0;
break;
}
}
}
if (advanced)
{
frameStartTime = currentTime - elapsed;
ApplyCurrentFrame();
}
}
const FrameAnimSequence* AnimationComponent::GetCurrentSequence() const
{
if (!animationData || currentSequence < 0 ||
currentSequence >= static_cast<int32_t>(animationData->animations.size()))
{
return nullptr;
}
return &animationData->animations[currentSequence];
}
int AnimationComponent::FindAnimationIndex(const char* name) const
{
if (!animationData || !name)
{
return -1;
}
for (size_t i = 0; i < animationData->animations.size(); ++i)
{
if (animationData->animations[i].name == name)
{
return static_cast<int>(i);
}
}
return -1;
}
void AnimationComponent::ApplyCurrentFrame()
{
if (!spriteComponent || !spriteComponent->sprite)
{
return;
}
const FrameAnimSequence* seq = GetCurrentSequence();
if (!seq)
{
return;
}
if (currentFrame < 0 || currentFrame >= static_cast<int32_t>(seq->frames.size()))
{
return;
}
const auto& frameData = seq->frames[currentFrame];
Sprite* sprite = spriteComponent->sprite.Get();
if (!sprite)
{
DEKI_LOG_ERROR("AnimationComponent::ApplyCurrentFrame - sprite is not a Sprite");
return;
}
const uint64_t epoch = Deki::AssetManager::Get() ? Deki::AssetManager::Get()->GetEpoch() : 0;
if (resolvedSprite != sprite || resolvedData != animationData || resolvedEpoch != epoch)
{
ResolveFrames(sprite);
}
const SpriteFrame* spriteFrame = nullptr;
if (currentSequence >= 0 && currentSequence < static_cast<int32_t>(resolvedFrames.size()) &&
currentFrame < static_cast<int32_t>(resolvedFrames[currentSequence].size()))
{
spriteFrame = resolvedFrames[currentSequence][currentFrame];
}
if (!spriteFrame)
{
// ResolveFrames already reported the missing frame once; this runs
// at animation rate, so it stays quiet.
(void)frameData;
return;
}
spriteComponent->SetFrame(*spriteFrame);
}
void AnimationComponent::ResolveFrames(const Sprite* sprite)
{
resolvedFrames.clear();
resolvedSprite = sprite;
resolvedData = animationData;
resolvedEpoch = Deki::AssetManager::Get() ? Deki::AssetManager::Get()->GetEpoch() : 0;
if (!sprite || !animationData)
{
return;
}
resolvedFrames.resize(animationData->animations.size());
for (size_t s = 0; s < animationData->animations.size(); ++s)
{
const FrameAnimSequence& seq = animationData->animations[s];
auto& out = resolvedFrames[s];
out.resize(seq.frames.size(), nullptr);
for (size_t f = 0; f < seq.frames.size(); ++f)
{
out[f] = sprite->FindFrame(seq.frames[f].frameGuid);
if (!out[f])
{
DEKI_LOG_ERROR(
"AnimationComponent: animation '%s' frame %zu references frame %s, which the sprite does not have",
seq.name.c_str(), f, seq.frames[f].frameGuid.c_str());
}
}
}
}
void AnimationComponent::InitializeToFirstFrame()
{
if (!spriteComponent || !spriteComponent->sprite || !animationData)
{
return;
}
if (animationData->animations.empty())
{
return;
}
currentSequence = 0;
currentFrame = 0;
ApplyCurrentFrame();
}
} // namespace Deki2D