-
Notifications
You must be signed in to change notification settings - Fork 2
/
Animator.ts
524 lines (430 loc) · 14.1 KB
/
Animator.ts
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
import { IAnimation, IEntity, IEventline, IMainlineKeyFrame } from "../file/IParsedFile";
import AnimationState from "./animation/AnimationState";
import Event from "./Event";
import IAnimatorState, { IEvent } from "./IAnimatorState";
import clamp from "../utils/clamp";
import extrapolate from "../utils/extrapolate";
import wrap from "../utils/wrap";
import getKeyFrames from "./getKeyFrames";
import { interpolateKeyFrames, IKeyFrameData } from "./animation/AnimationUtils";
/**
* Responsible for interfacing with and translating the Spriter animations/timelines in to something more usable.
*
* @export
* @class Animator
*/
export default class Animator {
/**
* Invoked when an animation completes.
*
* @memberof Animator
*/
public readonly onComplete = new Event<(target: Animator) => void>();
/**
* The animation playback speed.
*
* @type {number}
* @memberof Animator
*/
public speed: number;
private _entity: IEntity;
private _current: IAnimation;
private _next: IAnimation;
private _currentFrame: IMainlineKeyFrame;
private _currentState: IAnimatorState;
private _triggeredEvents: string[];
private _playing: boolean;
private _playTime: number;
private _transitionDuration: number;
private _transitionTime: number;
private _transitionScale: number;
/**
* Indicates whether the animator is currently playing an animation.
*
* @readonly
* @type {boolean}
* @memberof Animator
*/
public get playing(): boolean { return this._playing };
/**
* The current playback time of the Animator.
*
* @readonly
* @type {number}
* @memberof Animator
*/
public get time(): number { return this._playTime; }
public set time(value: number) {
value = clamp(value, 0, this._current.length);
if (this._playTime === value) {
return;
}
this._playTime = value;
this.update(0);
}
/**
* The current animation.
*
* @readonly
* @type {IAnimation}
* @memberof Animator
*/
public get animation(): IAnimation { return this._current; }
/**
* The current state of the Animator.
*
* @readonly
* @type {IAnimatorState}
* @memberof Animator
*/
public get state(): IAnimatorState { return this._currentState; }
/**
* The current Entity data.
*
* @readonly
* @type {IEntity}
* @memberof Animator
*/
public get entity(): IEntity { return this._entity; }
/**
* The percentage of the playback duration (from 0-1).
*
* @type {number}
* @memberof Animator
*/
public get progress(): number { return this._playTime / this._current.length; }
public set progress(value: number) { this._playTime = value * this._current.length; }
public constructor() {
this._triggeredEvents = [];
this.speed = 1;
}
/**
* Sets the Spriter Entity for this Animator to use.
*
* @param {IEntity} entity The Entity to use.
* @returns {this}
* @memberof Animator
*/
public setEntity(entity: IEntity): this {
this._entity = entity;
this._currentFrame = undefined;
if (this._current) {
this._current = entity.animation[this._current.id];
}
if (this._next) {
this._next = entity.animation[this._next.id];
}
return this;
}
/**
* Resumes playback if previously stopped.
*
* @memberof Animator
*/
public play(): void;
/**
* Plays the animation.
*
* @param {number} id The animation identifier.
* @memberof Animator
*/
public play(id: number): void;
/**
* Plays the animation.
*
* @param {string} name The animation name.
* @memberof Animator
*/
public play(name: string): void;
/**
* Plays the animation.
*
* @param {IAnimation} animation The animation.
* @memberof Animator
*/
public play(animation: IAnimation): void;
public play(arg?: number | string | IAnimation): void {
if (arguments.length === 0) {
if (this._current) {
this._playing = true;
}
return;
}
if (typeof arg === "object") {
return this.setAnimation(arg);
}
const anims = this._entity.animation;
if (typeof arg === "number") {
if (anims[arg]) {
this.setAnimation(anims[arg]);
}
return;
}
let i = anims.length;
while (i-- > 0) {
if (anims[i].name === arg) {
return this.setAnimation(anims[i]);
}
}
}
/**
* Halts playback (stops the Animtor from updating internally).
*
* @memberof Animator
*/
public stop(): void {
this._playing = false;
}
/**
* Transitions to the animation from the current state.
*
* @param {number} id The animation identifier.
* @memberof Animator
*/
public transition(id: number, time: number): void;
/**
* Transitions to the animation from the current state.
*
* @param {string} name The animation name.
* @memberof Animator
*/
public transition(name: string, time: number): void;
/**
* Transitions to the animation from the current state.
*
* @param {IAnimation} animation The animation.
* @memberof Animator
*/
public transition(animation: IAnimation, time: number): void;
public transition(arg: number | string | IAnimation, time: number): void {
if (this._next) {
console.warn(
`Can't transition to ${(typeof arg === "object") ? arg.name : arg}`,
`already transitioning to ${this._next.name} from ${this._current.name}.`
);
return;
}
if (typeof arg === "object") {
return this.startTransition(arg, time);
}
const anims = this._entity.animation;
if (typeof arg === "number") {
if (anims[arg]) {
this.startTransition(anims[arg], time);
}
return;
}
let i = anims.length;
while (i-- > 0) {
if (anims[i].name === arg) {
return this.startTransition(anims[i], time);
}
}
}
/**
* Progresses the animation by the supplied time.
*
* @param {number} delta The time since the last update (in milliseconds).
* @returns {IAnimatorState} The state of the animator.
* @memberof Animator
*/
public update(delta: number): IAnimatorState {
// Ensure we have an animaiton playing.
if (!this._current) {
this.setAnimation(this._entity.animation[0]);
}
if (!this._playing || !this.speed) {
return;
}
let animation = this._current;
delta *= this.speed;
// Update transition state.
if (this._next) {
delta += delta * this._transitionScale * (animation.length / this._next.length);
// Note: Abs this to cater for playing backwards.
this._transitionTime += Math.abs(delta);
if (this._transitionDuration <= this._transitionTime) {
const progress = this.progress;
this.setAnimation(this._next);
this.progress = progress;
animation = this._current;
} else {
this._transitionScale = this._transitionTime / this._transitionDuration;
}
}
// Calculate playback time.
const duration = animation.length;
const playTime = this._playTime;
if (animation.looping) {
this._playTime = wrap(playTime + delta, 0, duration);
} else {
this._playTime = clamp(playTime + delta, 0, duration);
this._playing = (0 < this._playTime && this._playTime < duration);
}
let frameData: IKeyFrameData;
// Get blended animation while transitioning.
if (this._next) {
frameData = this.getTransitionState(animation, this._next, this._playTime, this._transitionScale);
// Get the simple interpolated state.
} else {
frameData = this.getState(animation, this._playTime, true);
}
const state: IAnimatorState = frameData as any;
if (animation.eventline) {
state.events = this.getTriggeredEvents(animation.eventline, playTime, this._playTime);
}
this._currentState = state;
if (!this._playing || this._playTime < playTime) {
this.onComplete.dispatch(this);
}
return state;
}
/**
* Sets a new current animation.
*
* @private
* @param {IAnimation} animation The animation to use.
* @memberof Animator
*/
private setAnimation(animation: IAnimation): void {
if (animation && this._current === animation) {
this._playing = true;
return;
}
this._current = animation;
this._currentFrame = null;
this._next = null;
this._playTime = 0;
this._playing = true;
}
/**
* Starts transitioning to the supplied animation.
*
* @private
* @param {IAnimation} animation The next animation.
* @param {number} time The transition time.
* @memberof Animator
*/
private startTransition(animation: IAnimation, time: number): void {
if (animation === this._current || !time) {
this.setAnimation(animation);
return;
}
this._next = animation;
this._transitionDuration = time;
this._transitionTime = 0;
this._transitionScale = 0;
}
/**
* Indicates whether two key frames can be blended.
*
* @private
* @param {IKeyFrameData} first The interpolated state from the first animation.
* @param {IKeyFrameData} second The interpolated state from the second animation.
* @returns {boolean}
* @memberof Animator
*/
private canBlend(first: IKeyFrameData, second: IKeyFrameData): boolean {
if (first.bones.length !== second.bones.length) {
return false;
}
if (first.sprites.length !== second.sprites.length) {
return false;
}
return true;
}
/**
* Returns the interpolated state of the supplied animations.
*
* @private
* @param {IAnimation} first The first animation to blend.
* @param {IAnimation} second The second animation to blend.
* @param {number} time The current play time.
* @param {number} progress The progression (0-1) of the transition.
* @returns {IKeyFrameData}
* @memberof Animator
*/
private getTransitionState(first: IAnimation, second: IAnimation, time: number, progress: number): IKeyFrameData {
const timeforSecond = time / first.length * second.length;
const firstState = this.getState(first, time, true);
const secondState = this.getState(second, timeforSecond);
// If we can't blend just return the state of the first animation.
if (!this.canBlend(firstState, secondState)) {
return firstState;
}
return interpolateKeyFrames(firstState, secondState, progress);
}
/**
* Returns the inerpolated state of the animation.
*
* @private
* @param {IAnimation} animation The animation to interpolate.
* @param {number} time The current play time.
* @param {boolean} [updateCurrent]
* @returns {IAnimatorState}
* @memberof Animator
*/
private getState(animation: IAnimation, time: number, updateCurrent?: boolean): IKeyFrameData {
const isCurrentValid = (this._currentFrame?.animation === animation.id);
const [startFrame, endFrame] = getKeyFrames(animation.mainline.key, time, (isCurrentValid) ? this._currentFrame : null);
if (updateCurrent && startFrame !== this._currentFrame) {
this._currentFrame = startFrame;
}
return AnimationState.from(animation, startFrame, time);
}
/**
* Checks the supplied eventlines and returns the data for any events that have triggered between the supplied times.
*
* @private
* @param {IEventline[]} events The events to check.
* @param {number} prevTime The previous update time.
* @param {number} curTime The current update time.
* @returns {IEvent[]}
* @memberof Animator
*/
private getTriggeredEvents(events: IEventline[], prevTime: number, curTime: number): IEvent[] {
let output: IEvent[];
let i = events.length;
while (i-- > 0) {
const data = events[i];
const triggerIndex = this._triggeredEvents.indexOf(data.name);
let j = data.key.length;
let process: boolean;
while (j-- > 0) {
const frame = data.key[j];
if (prevTime <= frame.time && frame.time < curTime) {
process = true;
break;
}
if (prevTime < curTime) {
continue;
}
if (prevTime <= frame.time && frame.time < curTime) {
process = true;
break;
}
}
if (!process) {
// Clear the event if it was previously triggered.
if (triggerIndex > -1) {
this._triggeredEvents.splice(triggerIndex, 1);
}
continue;
}
// Don't include in the trigger list again until cleared.
if (triggerIndex > -1) {
continue;
}
this._triggeredEvents.push(data.name);
const event: IEvent = { name: data.name };
if (data.meta) {
event.metaData = data.meta;
}
if (data.obj != null) {
event.infoId = data.obj;
}
(output) ? output.push(event) : output = [event];
}
return output;
}
}