-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimationController.h
More file actions
125 lines (115 loc) · 3.1 KB
/
Copy pathAnimationController.h
File metadata and controls
125 lines (115 loc) · 3.1 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#pragma once
#include "Animation.h"
#include <map>
enum class TransitionCondition {
None,
SpeedAboveWalk,
SpeedAboveRun,
SpeedBelowRun,
SpeedBelowWalk,
EngageEnemy,
AlertSignal,
HealthDepleted,
AnimationComplete
};
struct AnimationState {
std::string animationName;
float animationSpeed;
};
struct AnimationTransition {
std::string fromState;
std::string toState;
float blendTime;
TransitionCondition condition;
};
class AnimationController {
public:
AnimationInstance instance;
AnimationState* currentState = nullptr;
std::map<std::string, AnimationState> states;
std::map<std::string, float> parameters;
std::map<std::string, bool> triggers;
std::vector<AnimationTransition> transitions;
void init(Animation* animaiton, int fromZYX) {
instance.init(animaiton, fromZYX);
}
void addState(const std::string& name, float speed) {
AnimationState s;
s.animationName = name;
s.animationSpeed = speed;
states[name] = s;
}
void setState(const std::string& name) {
if (currentState && currentState->animationName == name) {
return;
}
currentState = &states[name];
}
void forceState(const std::string& name) {
if (states.find(name) == states.end()) return;
currentState = &states[name];
instance.t = 0;
}
void update(float dt) {
if (!currentState) return;
if (checkCondition(TransitionCondition::HealthDepleted) && currentState->animationName != "death") {
forceState("death");
}
for (auto& t : transitions) {
if (t.fromState == currentState->animationName && checkCondition(t.condition)) {
setState(t.toState);
break;
}
}
instance.update(currentState->animationName, dt*currentState->animationSpeed);
}
Matrix* getMatricies() {
return instance.matrices;
}
void setParameter(const std::string& param, float value) {
parameters[param] = value;
}
float getParameter(std::string name) {
return parameters[name];
}
void setTrigger(const std::string& name) {
triggers[name] = true;
}
bool getTrigger(const std::string& name) {
if (triggers.find(name) != triggers.end() && triggers[name]) {
triggers[name] = false;
return true;
}
return false;
}
bool checkCondition(TransitionCondition condition) {
switch (condition) {
case TransitionCondition::SpeedAboveWalk:
return getParameter("speed") > 3.0f;
case TransitionCondition::SpeedBelowWalk:
return getParameter("speed") < 3.0f;
case TransitionCondition::SpeedAboveRun:
return getParameter("speed") > 7.0f;
case TransitionCondition::SpeedBelowRun:
return getParameter("speed") < 7.0f;
case TransitionCondition::HealthDepleted:
return getParameter("health") <= 0.0f;
case TransitionCondition::EngageEnemy:
return getTrigger("attack");
case TransitionCondition::AlertSignal:
return getTrigger("roar");
case TransitionCondition::AnimationComplete:
return instance.animationFinished();
}
return false;
}
void addTransition(const std::string& from, const std::string& to, float blendTime, TransitionCondition condition)
{
AnimationTransition t;
t.fromState = from;
t.toState = to;
t.blendTime = blendTime;
t.condition = condition;
transitions.push_back(t);
}
};