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 Color flashColor = Color(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(int ix, int iy, size_t f, float s, const std::string _type,
30 const std::vector<float>& flashTimes, Color flashC, int _reps = 0) : Animation(ix, iy, 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(int ix, int iy, size_t f, float s, const animType _type,
41 const std::vector<float>& flashTimes, Color flashC, int _reps = 0) : Animation(ix, iy, f, s, _type) // Animation frames look the next number of frames from the index
42 {
43 speeds[FlashState::FLASH_OUT] = flashTimes[0];
44 speeds[FlashState::EASE_IN] = flashTimes[1];
45 speeds[FlashState::FLASH_IN] = flashTimes[2];
46 speeds[FlashState::EASE_OUT] = flashTimes[3];
47 flashColor = flashC;
48 reps = _reps;
49 }
50
51 void advanceFrame(float deltaTime) {
52 unsigned short prev_frame_index = cur_frame_index;
53
54 speed = speeds[currentSpeedIndex];
55 switch (currentSpeedIndex) {
56 case FlashState::FLASH_OUT:
57 interpolation_a = 0;
58 break;
59 case FlashState::EASE_IN:
60 // Using simple subtraction to find the fractional part
61 interpolation_a = cur_frame_index_f - (int)cur_frame_index_f;
62 break;
63 case FlashState::FLASH_IN:
64 interpolation_a = 1;
65 break;
66 case FlashState::EASE_OUT:
67 // Using simple subtraction and inversion for the fractional part
68 interpolation_a = 1 - (cur_frame_index_f - (int)cur_frame_index_f);
69 break;
70 }
71 switch (type) {
72 case Animation::animType::ANIMTYPE_BACK_FORTH:
73
74 cur_frame_index_f += speed * deltaTime;
75 cur_frame_index = static_cast<unsigned short>(cur_frame_index_f);
76
77 if (cur_frame_index >= NUM_BACK_FORTH_STATES) {
78 times_played++;
79 resetFrameIndex();
80 if (reps && times_played >= reps) {
81 finished = true;
82 }
83 }
84 // Check if the frame index has changed
85 if (prev_frame_index != cur_frame_index) {
86 frame_times_played = 1;
87 if ((static_cast<int>(currentSpeedIndex) + 1) % NUM_BACK_FORTH_STATES < NUM_BACK_FORTH_STATES)
88 currentSpeedIndex = static_cast<FlashState>((static_cast<int>(currentSpeedIndex) + 1) % speeds.size());
89 }
90 else {
91 frame_times_played++;
92 }
93 break;
94 case Animation::animType::ANIMTYPE_LOOPED:
95 case Animation::animType::ANIMTYPE_PLAY_N_TIMES:
96 cur_frame_index_f += speed * deltaTime;
97 cur_frame_index = static_cast<unsigned short>(cur_frame_index_f);
98
99 // Check if the frame index has changed
100 if (prev_frame_index != cur_frame_index) {
101 frame_times_played = 1;
102 if( static_cast<int>(currentSpeedIndex) % NUM_LOOP_STATES < NUM_LOOP_STATES)
103 currentSpeedIndex = static_cast<FlashState>(static_cast<int>(currentSpeedIndex) % NUM_LOOP_STATES);
104 }
105 else {
106 frame_times_played++;
107 }
108
109 if (cur_frame_index >= NUM_LOOP_STATES) //essentially when we see that now we reach a frame out of total frames we reset it
110 {
111 resetFrameIndex();
112 times_played++;
113 if (reps && times_played >= reps) {
114 finished = true;
115 }
116 }
117 break;
118
119 case Animation::animType::ANIMTYPE_NONE:
120 break;
121 }
122 }
123
124 std::vector<float> getSpeedsAsVector() const {
125 std::vector<float> speedsVector(4); // Create a vector of size 4
126
127 // Ensure the vector fills according to the FlashState order
128 speedsVector[0] = speeds.at(FlashState::FLASH_OUT); // Use at() for direct access with bounds checking
129 speedsVector[1] = speeds.at(FlashState::EASE_IN);
130 speedsVector[2] = speeds.at(FlashState::FLASH_IN);
131 speedsVector[3] = speeds.at(FlashState::EASE_OUT);
132
133 return speedsVector;
134 }
135
136};
Definition Animation.h:6
Definition Vertex.h:47
Definition FlashAnimation.h:10