TazGraph Project v0.1.0
Loading...
Searching...
No Matches
Animation.h
1#pragma once
2
3#include <string>
4
5struct Animation //todo for now just add a bool hasFinished (useful for scripts) and much later may need onStop(when game paused)
6{ // todo also we might need to have setReps/getReps and replace "play_once" with "play_n_times" where we pass 1 more value to call
7 typedef enum {
8 ANIMTYPE_NONE = 0, // just one animation
9 ANIMTYPE_PLAY_N_TIMES = 1, // just iterates over the images one time. it holds the final image when finished.
10 ANIMTYPE_LOOPED = 2, // going over the images again and again.
11 ANIMTYPE_BACK_FORTH = 3 // iterate from index=0 to maxframe and back again. keeps holding the first image afterwards.
12 } animType;
13
14 int indexX = 0; // initial position in source texture
15 int indexY = 0;
16
17
18
19 size_t total_frames = 0;
20 float speed = 1.0f;
21 animType type = animType::ANIMTYPE_NONE;
22 int reps = 0;
23
24 int frame_times_played = 0;
25 int cur_frame_index = 0;
26 float cur_frame_index_f = 0;
27 int times_played = 0;
28
29 int flow_direction = 1;
30
31 bool finished = false;
32
33 Animation()
34 {
35
36 }
37
38 Animation(int ix, int iy , size_t f, float s, const std::string _type, int _reps = 0) // Animation frames look the next number of frames from the index
39 {
40 indexX = ix;
41 indexY = iy;
42 total_frames = f;
43 speed = s;
44
45 type = _type == "play_n_times" ? ANIMTYPE_PLAY_N_TIMES :
46 _type == "back_forth" ? ANIMTYPE_BACK_FORTH :
47 _type == "looped" ? ANIMTYPE_LOOPED :
48 ANIMTYPE_NONE;
49 reps = _reps;
50 }
51
52 Animation(int ix, int iy, size_t f, float s, const animType _type, int _reps = 0) // Animation frames look the next number of frames from the index
53 {
54 indexX = ix;
55 indexY = iy;
56 total_frames = f;
57 speed = s;
58
59 type = _type;
60 reps = _reps;
61 }
62
63 void advanceFrame(float deltaTime) {
64 unsigned short prev_frame_index = cur_frame_index;
65
66 switch (type) {
67 case Animation::animType::ANIMTYPE_LOOPED:
68 case Animation::animType::ANIMTYPE_PLAY_N_TIMES:
69 cur_frame_index_f += speed * deltaTime;
70 cur_frame_index = static_cast<unsigned short>(cur_frame_index_f);
71
72 // Check if the frame index has changed - indicates how long we have
73 // stayed on the same frame
74 if (prev_frame_index != cur_frame_index) {
75 frame_times_played = 1;
76 }
77 else {
78 frame_times_played++;
79 }
80
81 if (cur_frame_index > total_frames - 1) //essentially when we see that now we reach a frame out of total frames we reset it
82 {
83 times_played++;
84 if (reps && times_played >= reps) {
85 finished = true;
86 }
87 }
88 break;
89
90 case Animation::animType::ANIMTYPE_BACK_FORTH:
91 if (flow_direction == 1) {
92 cur_frame_index_f += speed * deltaTime;
93
94 if (cur_frame_index_f > total_frames) {
95 cur_frame_index_f -= speed;
96 flow_direction = -1;
97 }
98 cur_frame_index = static_cast<unsigned short>(cur_frame_index_f);
99 }
100 else if (flow_direction == -1) {
101 if (cur_frame_index > 0) {
102 cur_frame_index_f -= speed * deltaTime;
103 cur_frame_index = static_cast<unsigned short>(cur_frame_index_f);
104 }
105 else {
106 times_played++;
107 flow_direction = 1;
108 resetFrameIndex();
109 if (reps && times_played >= reps) {
110 finished = true;
111 }
112 }
113 }
114 // Check if the frame index has changed
115 if (prev_frame_index != cur_frame_index) {
116 frame_times_played = 1;
117 }
118 else {
119 frame_times_played++;
120 }
121 break;
122
123 case Animation::animType::ANIMTYPE_NONE:
124 break;
125 }
126 }
127
128 void resetFrameIndex() {
129 cur_frame_index = 0;
130 cur_frame_index_f = 0;
131 frame_times_played = 0;
132 }
133
134 bool hasFinished() {
135 return finished;
136 }
137};
Definition Animation.h:6