forked from 15-466/15-466-f21-base6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnimationStateMachine.hpp
61 lines (51 loc) · 1.14 KB
/
AnimationStateMachine.hpp
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
#pragma once
// generated from animation_state_machine.py
#include <map>
#include <vector>
enum AnimationState {
IDLE,
RUN,
HIT_1,
BLOCK
};
struct AnimationStateMachine {
AnimationState current_state = IDLE;
std::map<AnimationState, std::pair<unsigned, unsigned>> times;
std::map<AnimationState, bool> is_oneshot;
unsigned current_frame;
bool state_has_changed = false;
AnimationStateMachine() {
times[IDLE] = {0, 29};
times[RUN] = {40, 59};
times[HIT_1] = {70, 99};
times[BLOCK] = {100, 129};
is_oneshot[IDLE] = false;
is_oneshot[RUN] = false;
is_oneshot[HIT_1] = true;
is_oneshot[BLOCK] = true;
current_frame = times[IDLE].first;
}
void update() {
const auto& time = times[current_state];
if (state_has_changed) {
current_frame = time.first;
state_has_changed = false;
}
else {
current_frame++;
if (current_frame > time.second) {
if (is_oneshot[current_state]) {
current_state = IDLE;
current_frame = times[IDLE].first;
}
else {
current_frame = time.first;
}
}
}
}
void set_state(AnimationState state) {
current_state = state;
state_has_changed = true;
}
};