TazGraph Project v0.1.0
Loading...
Searching...
No Matches
FlashAnimation.h
1#pragma once
2
3#include "Animation.h"
4
5#define NUM_LOOP_STATES 3
6#define NUM_BACK_FORTH_STATES 4
7
8
9struct FlashAnimation : public Animation //todo moving animation can be moving sprite with of without transform
10{
11 enum class FlashState {
12 FLASH_OUT,
13 EASE_IN,
14 FLASH_IN,
15 EASE_OUT
16 };
17
18 float interpolation_a = 0.0f;
19 std::map<FlashState, float> speeds;
20
21 FlashState currentSpeedIndex = FlashState::FLASH_OUT;
22 TazColor flashColor = TazColor(255, 255, 255, 255);
23
25 {
26
27 }
28 // ix,iy is initial position (destX, destY), f is total frames to move, s is the speed to move frames, type as in animation, dx,dy distance to move
29 FlashAnimation(size_t f, float s, const std::string _type,
30 const std::vector<float>& flashTimes, TazColor flashC, int _reps = 0) : Animation(0, 0, f, s, _type) // Animation frames look the next number of frames from the index
31 {
32 speeds[FlashState::FLASH_OUT] = flashTimes[0];
33 speeds[FlashState::EASE_IN] = flashTimes[1];
34 speeds[FlashState::FLASH_IN] = flashTimes[2];
35 speeds[FlashState::EASE_OUT] = flashTimes[3];
36 flashColor = flashC;
37 reps = _reps;
38 }
39
40 FlashAnimation(size_t f, float s, const animType _type,
41 const std::vector<float>& flashTimes, TazColor flashC, int _reps = 0)
42 : Animation(0, 0, f, s, _type) // Animation frames look the next number of frames from the index
43 {
44 speeds[FlashState::FLASH_OUT] = flashTimes[0];
45 speeds[FlashState::EASE_IN] = flashTimes[1];
46 speeds[FlashState::FLASH_IN] = flashTimes[2];
47 speeds[FlashState::EASE_OUT] = flashTimes[3];
48 flashColor = flashC;
49 reps = _reps;
50 }
51
52 void advanceFrame(float deltaTime) {
53 unsigned short prev_frame_index = cur_frame_index;
54
55 speed = speeds[currentSpeedIndex];
56 switch (currentSpeedIndex) {
57 case FlashState::FLASH_OUT:
58 interpolation_a = 0;
59 break;
60 case FlashState::EASE_IN:
61 // Using simple subtraction to find the fractional part
62 interpolation_a = cur_frame_index_f - cur_frame_index;
63 break;
64 case FlashState::FLASH_IN:
65 interpolation_a = 1;
66 break;
67 case FlashState::EASE_OUT:
68 // Using simple subtraction and inversion for the fractional part
69 interpolation_a = 1 - (cur_frame_index_f - cur_frame_index);
70 break;
71 }
72 switch (type) {
73 case Animation::animType::ANIMTYPE_BACK_FORTH:
74
75 cur_frame_index_f += speed * deltaTime;
76 cur_frame_index = static_cast<unsigned short>(cur_frame_index_f);
77
78 if (cur_frame_index >= NUM_BACK_FORTH_STATES) {
79 times_played++;
80 resetFrameIndex();
81 if (reps && times_played >= reps) {
82 finished = true;
83 }
84 }
85 // Check if the frame index has changed
86 if (prev_frame_index != cur_frame_index) {
87 frame_times_played = 1;
88 if ((static_cast<int>(currentSpeedIndex) ) < NUM_BACK_FORTH_STATES)
89 currentSpeedIndex = static_cast<FlashState>(static_cast<int>(cur_frame_index));
90 }
91 else {
92 frame_times_played++;
93 }
94 break;
95 case Animation::animType::ANIMTYPE_LOOPED:
96 case Animation::animType::ANIMTYPE_PLAY_N_TIMES:
97 cur_frame_index_f += speed * deltaTime;
98 cur_frame_index = static_cast<unsigned short>(cur_frame_index_f);
99
100 // Check if the frame index has changed
101 if (prev_frame_index != cur_frame_index) {
102 frame_times_played = 1;
103 if (static_cast<int>(currentSpeedIndex) < NUM_LOOP_STATES)
104 currentSpeedIndex = static_cast<FlashState>(static_cast<int>(cur_frame_index));
105 }
106 else {
107 frame_times_played++;
108 }
109
110 if (cur_frame_index >= NUM_LOOP_STATES) //essentially when we see that now we reach a frame out of total frames we reset it
111 {
112 resetFrameIndex();
113 times_played++;
114 if (reps && times_played >= reps) {
115 finished = true;
116 }
117 }
118 break;
119
120 case Animation::animType::ANIMTYPE_NONE:
121 break;
122 }
123 }
124
125 std::vector<float> getSpeedsAsVector() const {
126 std::vector<float> speedsVector(4); // Create a vector of size 4
127
128 // Ensure the vector fills according to the FlashState order
129 speedsVector[0] = speeds.at(FlashState::FLASH_OUT); // Use at() for direct access with bounds checking
130 speedsVector[1] = speeds.at(FlashState::EASE_IN);
131 speedsVector[2] = speeds.at(FlashState::FLASH_IN);
132 speedsVector[3] = speeds.at(FlashState::EASE_OUT);
133
134 return speedsVector;
135 }
136
137};
Definition Animation.h:6
Definition FlashAnimation.h:10
Definition Vertex.h:44