-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimation.h
72 lines (57 loc) · 1.21 KB
/
Animation.h
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
#ifndef __ANIMATION_H__
#define __ANIMATION_H__
#include "SDL/include/SDL_rect.h"
#define MAX_FRAMES 25
class Animation {
public:
bool loop = true;
bool pingpong = false;
float speed = 1.0f;
SDL_Rect frames[MAX_FRAMES];
private:
float currentFrame = 0.0f;
int totalFrames = 0;
int loopCount = 0;
int pingpongDirection = 1;
public:
void PushBack(const SDL_Rect& rect) {
frames[totalFrames++] = rect;
}
void Reset() {
this->currentFrame = 0;
this->loopCount = 0;
}
void FullReset() {
this->currentFrame = 0;
this->totalFrames = 0;
this->loopCount = 0;
this->pingpongDirection = 1;
}
bool HasFinished() {
return !loop && !pingpong && loopCount > 0;
}
void Update() {
currentFrame += speed;
if (currentFrame >= totalFrames)
{
currentFrame = (loop || pingpong) ? 0.0001f : totalFrames - 1;
++loopCount;
if (pingpong)
pingpongDirection = -pingpongDirection;
}
}
int GetCurrentFrameNum() {
return (int)currentFrame;
}
void BeginAnimationIn(int index) {
this->currentFrame = index;
}
SDL_Rect& GetCurrentFrame()
{
int actualFrame = currentFrame;
if (pingpongDirection == -1)
actualFrame = totalFrames - currentFrame;
return frames[actualFrame];
}
};
#endif