-
Notifications
You must be signed in to change notification settings - Fork 13
/
AnimationController.as
314 lines (260 loc) · 12.3 KB
/
AnimationController.as
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*******************************************************************************
* PushButton Engine
* Copyright (C) 2009 PushButton Labs, LLC
* For more information see http://www.pushbuttonengine.com
*
* This file is licensed under the terms of the MIT license, which is included
* in the License.html file at the root directory of this SDK.
******************************************************************************/
package com.pblabs.rendering2D
{
import com.pblabs.engine.PBE;
import com.pblabs.engine.components.AnimatedComponent;
import com.pblabs.engine.core.ProcessManager;
import com.pblabs.engine.debug.Logger;
import com.pblabs.engine.debug.Profiler;
import com.pblabs.engine.entity.PropertyReference;
import com.pblabs.rendering2D.spritesheet.SpriteContainerComponent;
import flash.events.Event;
import flash.utils.Dictionary;
/**
* Manage sprite sheet and frame selection based on named animation definitions.
*/
public class AnimationController extends AnimatedComponent
{
//--------------------------------------------------------------------------
//
// Constructor
//
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
//
// Variables
//
//--------------------------------------------------------------------------
/**
* Animations, indexed by name.
*
* @see AnimationControllerInfo
*/
[TypeHint(type="com.pblabs.rendering2D.AnimationControllerInfo")]
public var animations:Array = new Array();
/**
* Property indicating the duration of the current animation in ticks.
*/
public var currentAnimationDurationReference:PropertyReference;
/**
* Property that indicates the name of the current animation.
*
* We read this each frame to determine what we should be playing.
*/
public var currentAnimationReference:PropertyReference;
/**
* Property indicating the time at which the current animation started
* (in virtual time ms). This can be used to make sure that animations
* don't "lag" behind their actions if framerate is low.
*/
public var currentAnimationStartTimeReference:PropertyReference;
/**
* Property to set with current frame of animation.
*/
public var currentFrameReference:PropertyReference;
/**
* Name of animation to play when we have nothing better to do.
* Defaults to "Idle"
*/
public var defaultAnimation:String = "Idle";
/**
* Property to set to the current sprite sheet.
*/
public var spriteSheetReference:PropertyReference;
/**
* If true, do not advance the animation state.
*/
public var paused:Boolean = false;
/**
* If set, the name of the event to listen to for animation changes on the
* owning entity. This is not required, but offers greater performance than
* checking for the name of the animation on every frame.
*
* No data is read from this event, it is simply a signal to check the
* currentAnimationReference.
*/
public var changeAnimationEvent:String;
/**
* If true the animation controller always finishes an animation before going
* to the next animation.
*/
public var waitForAnimationsToFinish:Boolean = true;
/**
* Contains the currently playing animation if any.
*/
private var _currentAnimation:AnimationControllerInfo;
private var _currentAnimationDuration:Number = 0;
private var _currentAnimationStartTime:Number = 0;
private var _badAnimations:Dictionary;
private var _changeAnimationEvent:String;
private var _lastSpriteSheet:SpriteContainerComponent;
private var _lastFrameIndex:int;
public function set currentAnimationName(value:String):void
{
var potentialAnim:AnimationControllerInfo = animations[value];
if(!potentialAnim)
{
Logger.warn(this, "set currentAnimationName", "Couldn't find animation '" + value + "' to set.");
return;
}
setAnimation(potentialAnim);
}
override public function onFrame(elapsed:Number):void
{
if(paused)
return;
// Find an animation onFrame only if we're not bound to an event,
// or we don't have an animation set.
// The hash lookup and findProperty each frame gets expensive with a lot of
// animation components running.
var nextAnim:AnimationControllerInfo = null;
if (!changeAnimationEvent || !_currentAnimation)
nextAnim = getNextAnimation();
else
nextAnim = _currentAnimation;
if (!nextAnim)
throw new Error("Unable to find default animation '" + defaultAnimation + "'!");
// Expire current animation if it has finished playing and it's what we
// want to keep playing.
if (_currentAnimation !== nextAnim && (!waitForAnimationsToFinish || PBE.processManager.virtualTime > (_currentAnimationStartTime + _currentAnimationDuration)))
_currentAnimation = null;
// If we do not have a current animation, start playing the next.
if (!_currentAnimation || nextAnim.priority > _currentAnimation.priority)
setAnimation(nextAnim);
// If no current animation, then abort!
if (!_currentAnimation)
{
Logger.warn(this, "OnFrame", "No current animation. Aborting!");
return;
}
// Ok, we have a current, valid animation at this point. So let's set
// the sprite sheet and frame properties.
// Fast path for single frame "animations".
if (_currentAnimation.spriteSheet.frameCount == 1)
{
curFrame = 0;
}
else
{
// If the sprite sheet was not initialized before we tick, the duration can be 0, causing us never to get past frame 0.
if (_currentAnimationDuration == 0)
updateAnimationDuration();
// Figure out what frame we are on.
var frameTime:Number = _currentAnimationDuration / _currentAnimation.spriteSheet.frameCount;
if (frameTime > _currentAnimation.maxFrameDelay)
frameTime = _currentAnimation.maxFrameDelay;
var animationAge:Number = PBE.processManager.virtualTime - _currentAnimationStartTime;
var curFrame:int = Math.floor(animationAge / frameTime);
// Deal with clamping/looping.
if (_currentAnimation.loop)
{
var wasFrame:int = curFrame;
curFrame = curFrame % _currentAnimation.spriteSheet.frameCount;
}
else
{
if (curFrame >= _currentAnimation.spriteSheet.frameCount)
curFrame = _currentAnimation.spriteSheet.frameCount - 1;
}
}
// Assign properties.
// For performance, we only set the properties if they have changed since the last frame.
if (curFrame != _lastFrameIndex)
{
_lastFrameIndex = curFrame;
owner.setProperty(currentFrameReference, curFrame);
}
if (_currentAnimation.spriteSheet !== _lastSpriteSheet)
{
_lastSpriteSheet = _currentAnimation.spriteSheet;
owner.setProperty(spriteSheetReference, _currentAnimation.spriteSheet);
}
}
/**
* Set the current animation to specified info.
*/
public function setAnimation(ai:AnimationControllerInfo):void
{
Profiler.enter("AnimationController.SetAnimation");
// Fire stop event.
if (_currentAnimation && _currentAnimation.completeEvent)
owner.eventDispatcher.dispatchEvent(new Event(_currentAnimation.completeEvent));
_currentAnimation = ai;
// Fire start event.
if (_currentAnimation.startEvent)
owner.eventDispatcher.dispatchEvent(new Event(_currentAnimation.startEvent));
if (!ai.spriteSheet)
throw new Error("Animation had no sprite sheet!");
// Note when we started.
if (currentAnimationStartTimeReference)
_currentAnimationStartTime = owner.getProperty(currentAnimationStartTimeReference);
else
_currentAnimationStartTime = PBE.processManager.virtualTime;
updateAnimationDuration();
//trace("Age at start was " + (PBE.processManager.virtualTime - _currentAnimationStartTime));
//Logger.(this, "Changed animation to: " + _currentAnimation.spriteSheet.name + ". duration is " + _currentAnimationDuration);
Profiler.exit("AnimationController.SetAnimation");
}
protected function updateAnimationDuration():void
{
// Update our duration information.
if (currentAnimationDurationReference)
_currentAnimationDuration = owner.getProperty(currentAnimationDurationReference) * ProcessManager.TICK_RATE_MS;
else
_currentAnimationDuration = _currentAnimation.spriteSheet.frameCount * (1000/_currentAnimation.frameRate);
}
protected function getNextAnimation():AnimationControllerInfo
{
var nextAnim:AnimationControllerInfo = null;
if (currentAnimationReference)
{
var nextAnimName:String = owner.getProperty(currentAnimationReference);
nextAnim = animations[nextAnimName];
}
else
{
nextAnim = animations[defaultAnimation];
}
// Go to default animation if we've nothing better to do.
if (nextAnim == null)
{
// Supressing duplicate log messages for animations that we know are already missing.
// A log message per frame is pretty expensive.
if (!_badAnimations)
_badAnimations = new Dictionary();
if (!_badAnimations[nextAnimName])
{
Logger.warn(this, "OnFrame", "Animation '" + nextAnimName + "' not found, going with default animation '" + defaultAnimation + "'.");
_badAnimations[nextAnimName] = true;
}
nextAnim = animations[defaultAnimation];
}
return nextAnim;
}
override protected function onAdd(): void
{
super.onAdd();
if (owner.eventDispatcher && changeAnimationEvent)
owner.eventDispatcher.addEventListener(changeAnimationEvent, animationChangedHandler);
}
override protected function onRemove() : void
{
super.onRemove();
if (owner.eventDispatcher && changeAnimationEvent)
owner.eventDispatcher.removeEventListener(changeAnimationEvent, animationChangedHandler);
}
private function animationChangedHandler(event:Event):void
{
// This is all we need to do for the onFrame method to pick up that the
// animation is missing and load the current one based on the property references.
_currentAnimation = null;
}
}
}