-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimation.cpp
More file actions
39 lines (35 loc) · 1.06 KB
/
Animation.cpp
File metadata and controls
39 lines (35 loc) · 1.06 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
#include "Animation.hpp"
void Animation::addFrame(Frame&& frame)
{
frames.push_back(std::move(frame));
animationLength += frame.duration;
// Set first animation checkpoint
if (frames.size() == 1) {
animationCheckpoint = frame.duration;
}
}
void Animation::updateAnimation(float elapsed)
{
if (!playing) return;
animationProgress += elapsed;
// Check if new frame is due
if (animationProgress > animationCheckpoint) {
// Check if it was last frame
if (animationProgress > animationLength) {
currentFrame = 0;
animationProgress -= animationLength;
sprite->setTextureRect(frames[currentFrame].rect);
animationCheckpoint = frames[currentFrame].duration;
}
else {
currentFrame++;
sprite->setTextureRect(frames[currentFrame].rect);
animationCheckpoint += frames[currentFrame].duration;
}
}
}
void Animation::reset() {
animationProgress = 0;
currentFrame = 0;
animationCheckpoint = frames[0].duration;
}