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
15 int indexY = 0;
16 size_t total_frames = 0;
17 float speed = 1.0f;
18 animType type = animType::ANIMTYPE_NONE;
19 int reps = 0;
20
21 int frame_times_played = 0;
22 int cur_frame_index = 0;
23 float cur_frame_index_f = 0;
24 int times_played = 0;
25
26 int flow_direction = 1;
27
28 bool finished = false;
29
30 Animation()
31 {
32
33 }
34
35 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
36 {
37 indexX = ix;
38 indexY = iy;
39 total_frames = f;
40 speed = s;
41
42 type = _type == "play_n_times" ? ANIMTYPE_PLAY_N_TIMES :
43 _type == "back_forth" ? ANIMTYPE_BACK_FORTH :
44 _type == "looped" ? ANIMTYPE_LOOPED :
45 ANIMTYPE_NONE;
46 reps = _reps;
47 }
48
49 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
50 {
51 indexX = ix;
52 indexY = iy;
53 total_frames = f;
54 speed = s;
55
56 type = _type;
57 reps = _reps;
58 }
59
60 void advanceFrame(float deltaTime) {
61 unsigned short prev_frame_index = cur_frame_index;
62
63 switch (type) {
64 case Animation::animType::ANIMTYPE_LOOPED:
65 case Animation::animType::ANIMTYPE_PLAY_N_TIMES:
66 cur_frame_index_f += speed * deltaTime;
67 cur_frame_index = static_cast<unsigned short>(cur_frame_index_f);
68
69 // Check if the frame index has changed
70 if (prev_frame_index != cur_frame_index) {
71 frame_times_played = 1;
72 }
73 else {
74 frame_times_played++;
75 }
76
77 if (cur_frame_index > total_frames - 1) //essentially when we see that now we reach a frame out of total frames we reset it
78 {
79 resetFrameIndex();
80 times_played++;
81 if (reps && times_played >= reps) {
82 finished = true;
83 }
84 }
85 break;
86
87 case Animation::animType::ANIMTYPE_BACK_FORTH:
88 if (flow_direction == 1) {
89 cur_frame_index_f += speed * deltaTime;
90
91 if (cur_frame_index_f > total_frames) {
92 cur_frame_index_f -= speed;
93 flow_direction = -1;
94 }
95 cur_frame_index = static_cast<unsigned short>(cur_frame_index_f);
96 }
97 else if (flow_direction == -1) {
98 if (cur_frame_index > 0) {
99 cur_frame_index_f -= speed * deltaTime;
100 cur_frame_index = static_cast<unsigned short>(cur_frame_index_f);
101 }
102 else {
103 times_played++;
104 flow_direction = 1;
105 resetFrameIndex();
106 if (reps && times_played >= reps) {
107 finished = true;
108 }
109 }
110 }
111 // Check if the frame index has changed
112 if (prev_frame_index != cur_frame_index) {
113 frame_times_played = 1;
114 }
115 else {
116 frame_times_played++;
117 }
118 break;
119
120 case Animation::animType::ANIMTYPE_NONE:
121 break;
122 }
123 }
124
125 void resetFrameIndex() {
126 cur_frame_index = 0;
127 cur_frame_index_f = 0;
128 frame_times_played = 0;
129 }
130
131 bool hasFinished() {
132 return finished;
133 }
134};
Definition Animation.h:6