-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathAnimationSystem.h
More file actions
216 lines (199 loc) · 5.7 KB
/
AnimationSystem.h
File metadata and controls
216 lines (199 loc) · 5.7 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#ifndef _ANIMATIONSYSTEM_H
#define _ANIMATIONSYSTEM_H
#include "physics.h"
#include <iostream>
#include "nlohmann/json.hpp"
using json = nlohmann::json;
// --- actionInterpolants
class ScalarInterpolant {
public:
bool evaluatee;
float value;
float minValue;
float maxValue;
ScalarInterpolant(float minValue, float maxValue) {
this->value = minValue;
this->minValue = minValue;
this->maxValue = maxValue;
}
virtual float get() {
return this->value;
}
virtual float getNormalized() {
return this->value / (this->maxValue - this->minValue);
}
virtual float getInverse() {
return this->maxValue - this->value;
}
virtual void update(float timeDiff, bool evaluatee) { }
};
class BiActionInterpolant: public ScalarInterpolant {
public:
using ScalarInterpolant::ScalarInterpolant;
void update(float timeDiff, bool evaluatee) {
this->value += (evaluatee ? 1 : -1) * timeDiff;
this->value = fmin(fmax(this->value, this->minValue), this->maxValue);
}
};
class UniActionInterpolant: public ScalarInterpolant {
public:
using ScalarInterpolant::ScalarInterpolant;
void update(float timeDiff, bool evaluatee) {
if (evaluatee) {
this->value += timeDiff;
this->value = fmin(fmax(this->value, this->minValue), this->maxValue);
} else {
this->value = this->minValue;
}
}
};
class InfiniteActionInterpolant: public ScalarInterpolant {
public:
InfiniteActionInterpolant(float minValue): ScalarInterpolant(minValue, std::numeric_limits<float>::infinity()) { }
void update(float timeDiff, bool evaluatee) {
if (evaluatee) {
this->value += timeDiff;
} else {
this->value = this->minValue;
}
}
};
// --- End: actionInterpolants
namespace AnimationSystem {
struct Interpolant;
struct Animation;
struct AnimationMapping;
class Avatar;
class AnimationNode;
class AnimationMixer;
struct Interpolant {
unsigned int numParameterPositions;
float *parameterPositions;
float resultBuffer[4];
unsigned int numSampleValues;
float *sampleValues;
unsigned int valueSize;
};
struct Animation {
float duration;
std::vector<Interpolant *> interpolants;
unsigned int currentInterpolantIndex = 0;
unsigned int index;
std::string name;
};
struct AnimationMapping { // spec
float dst[4];
bool isPosition;
unsigned int index;
std::string boneName;
bool isTop;
bool isArm;
};
class Avatar {
public:
std::unordered_map<std::string, json> actions;
std::unordered_map<std::string, ScalarInterpolant *> actionInterpolants;
AnimationMixer *mixer;
// values
float activateTime;
float landTime;
float fallLoopFactor;
float fallLoopTime;
float skydiveTime;
float gliderTime;
float flyTime;
float doubleJumpTime;
float jumpTime;
float narutoRunTime;
float danceFactor;
float emoteFactor;
float lastEmoteTime;
float idleWalkFactor;
float useTime;
float useAnimationEnvelopeLength;
float hurtTime;
float readyGrabTime;
float unuseTime;
float aimTime;
float aimMaxTime;
float walkRunFactor;
float crouchFactor;
float pickUpTime;
float forwardFactor;
float backwardFactor;
float leftFactor;
float rightFactor;
float mirrorLeftFactorReverse;
float mirrorLeftFactor;
float mirrorRightFactorReverse;
float mirrorRightFactor;
float landTimeS;
float timeSinceLastMoveS;
float swimTime;
float movementsTime;
float sprintFactor;
float movementsTransitionFactor;
// states
bool jumpState;
bool doubleJumpState;
bool landState;
bool flyState;
bool crouchState;
bool narutoRunState;
bool sitState;
bool holdState;
bool pickUpState;
bool swimState;
bool activateState;
bool useState;
bool aimState;
bool fallLoopState;
bool skydiveState;
bool gliderState;
bool danceState;
bool emoteState;
bool hurtState;
bool readyGrabState;
bool rightHandState;
bool leftHandState;
bool sprintState;
bool movementsState;
//
bool landWithMoving;
bool fallLoopFromJump;
int activateAnimationIndex;
int sitAnimationIndex;
int danceAnimationIndex;
int emoteAnimationIndex;
int useAnimationIndex;
int useAnimationComboIndex;
int hurtAnimationIndex;
int unuseAnimationIndex;
int aimAnimationIndex;
std::vector<int> useAnimationEnvelopeIndices;
//
void updateInterpolation(float timeDiff); // note: call before `update()`
void update(float *scratchStack);
void addAction(char *scratchStack, unsigned int stringByteLength);
void removeAction(char *scratchStack, unsigned int stringByteLength);
float getActionInterpolant(char *scratchStack, unsigned int stringByteLength, unsigned int type = 0); // 0: get(), 1: getNormalized(), 2: getInverse()
void getValues(float *scratchStack);
};
class AnimationMixer {
public:
static float nowS;
Avatar *avatar;
float *animationValues;
float *update(float now, float nowS);
};
// ------
// need run in this order
void createAnimationMapping(bool isPosition, unsigned int index, bool isTop, bool isArm, char *scratchStack, unsigned int nameByteLength);
Animation *createAnimation(char *scratchStack, unsigned int nameByteLength, float duration);
void createAnimationInterpolant(Animation *animation, unsigned int numParameterPositions, float *parameterPositions, unsigned int numSampleValues, float *sampleValues, unsigned int valueSize);
unsigned int initAnimationSystem(char *scratchStack);
AnimationMixer *createAnimationMixer();
Avatar *createAnimationAvatar(AnimationMixer *mixer);
// end: need run in this order
};
#endif // _ANIMATIONSYSTEM_H