-
Notifications
You must be signed in to change notification settings - Fork 0
/
Animation.h
190 lines (168 loc) · 3.58 KB
/
Animation.h
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
#pragma once
#include "Model.h"
class AnimationSteap : public ModelDef, public ISerializable
{
public:
AnimationSteap( )
{
}
AnimationSteap( XMLloader& loader, istream& file )
{
deserialize( loader, file );
}
AnimationSteap( const string& str )
{
deserialize(str);
}
enum State
{
under,
inRange,
above
};
State update( Model& model, float step, float speed = 1 );
State update( vector<Model*>& model, float step, float speed = 1 )
{
return update( *model[idModel], step, speed);
}
void serialize( const string& str)
{
ISerializable::serialize(str);
}
void serialize( ostream& file ) override;
void deserialize( const string& str)
{
ISerializable::deserialize(str);
}
void deserialize( istream& file ) override;
void deserialize( XMLloader& loader, istream& file);
bool isInRange(float steap)
{
return steap >= stepMin && steap <= stepMax;
}
private:
float stepMin, stepMax;
int idModel;
};
class Animation : public ISerializable
{
public:
Animation(){}
Animation(const string& path)
{
deserialize(path);
}
// update functions
void update( vector<Model*>& model )
{
for( auto it = part.begin(); it != part.end(); ++it )
if (it->isInRange(step) )
it->update(model, step, speed);
step += speed;
}
void update( vector<Model*>& model, float _speed )
{
swap( _speed, speed);
update(model);
swap( _speed, speed);
}
// returns true when in range
bool updateInRange( vector<Model*>& model, float speed )
{
if( inRange(stepMin, stepMax, speed ) )
{
update(model, speed);
return true;
}
/*if( steap >= steapMax)
update(model, steapMax-speed > 0 ? steapMax - speed : 0 );
else if( steap <= steapMin)
update(model, steapMin-speed > 0 ? steapMin - speed : 0 );*/
return false;
}
bool updateInRange(vector<Model*>& model )
{
if( inRange(stepMin, stepMax, speed ) )
{
update(model);
return true;
}
return false;
}
AnimationSteap::State updateReturn(vector<Model*>& model )
{
update( model );
if( !inRange( stepMin, stepMax) )
{
speed *= -1;
return step >= stepMax ? AnimationSteap::above : AnimationSteap::under;
}
return AnimationSteap::inRange;
}
// initialize
void serialize( const string& str)
{
ISerializable::serialize(str);
}
void serialize( ostream& file ) override;
void deserialize( const string& str)
{
ISerializable::deserialize(str);
}
void deserialize( istream& file ) override;
void deserialize( const string& str, vector<Model*>& model );
void deserialize( istream& file, vector<Model*>& model );
// utility
float getStep() const
{
return step;
}
float getStepMin() const
{
return stepMin;
}
float getStepMax() const
{
return stepMax;
}
bool inRange() const
{
return inRange(stepMin, stepMax);
}
bool inRange(float speed) const
{
return inRange(stepMin, stepMax, speed);
}
bool inRange( float steapMin, float steapMax) const
{
return step >= steapMin && step <= steapMax;
}
bool inRange( float steapMin, float steapMax, float speed) const
{
return step + speed >= steapMin && step + speed <= steapMax;
}
void reset()
{
step = 0;
speed = abs(speed);
}
bool side() const
{
return speed > 0;
}
float getSpeed() const
{
return speed;
}
float getLenght()
{
return stepMax - stepMin;
}
float getPercentStep()
{
return (step - stepMin) / getLenght();
}
//private:
vector<AnimationSteap> part;
float step, speed, stepMin, stepMax;
};