forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimation.js
More file actions
811 lines (713 loc) · 20.2 KB
/
Copy pathAnimation.js
File metadata and controls
811 lines (713 loc) · 20.2 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
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Class = require('../../utils/Class');
/**
* @classdesc
* A Game Object Animation Controller.
*
* This controller lives as an instance within a Game Object, accessible as `sprite.anims`.
*
* @class Animation
* @memberOf Phaser.GameObjects.Components
* @constructor
* @since 3.0.0
*
* @param {Phaser.GameObjects.GameObject} parent - The Game Object to which this animation controller belongs.
*/
var Animation = new Class({
initialize:
function Animation (parent)
{
/**
* The Game Object to which this animation controller belongs.
*
* @name Phaser.GameObjects.Components.Animation#parent
* @type {Phaser.GameObjects.GameObject}
* @since 3.0.0
*/
this.parent = parent;
/**
* A reference to the global Animation Manager.
*
* @name Phaser.GameObjects.Components.Animation#animationManager
* @type {Phaser.Animations.AnimationManager}
* @since 3.0.0
*/
this.animationManager = parent.scene.sys.anims;
this.animationManager.once('remove', this.remove, this);
/**
* Is an animation currently playing or not?
*
* @name Phaser.GameObjects.Components.Animation#isPlaying
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.isPlaying = false;
// Reference to the Phaser.Animation object
/**
* The current Animation loaded into this Animation Controller.
*
* @name Phaser.GameObjects.Components.Animation#currentAnim
* @type {?Phaser.Animations.Animation}
* @default null
* @since 3.0.0
*/
this.currentAnim = null;
/**
* The current AnimationFrame being displayed by this Animation Controller.
*
* @name Phaser.GameObjects.Components.Animation#currentFrame
* @type {?Phaser.Animations.AnimationFrame}
* @default null
* @since 3.0.0
*/
this.currentFrame = null;
/**
* Time scale factor.
*
* @name Phaser.GameObjects.Components.Animation#_timeScale
* @type {number}
* @private
* @default 1
* @since 3.0.0
*/
this._timeScale = 1;
/**
* The frame rate of playback in frames per second.
* The default is 24 if the `duration` property is `null`.
*
* @name Phaser.GameObjects.Components.Animation#frameRate
* @type {number}
* @default 0
* @since 3.0.0
*/
this.frameRate = 0;
/**
* How long the animation should play for.
* If the `frameRate` property has been set then it overrides this value,
* otherwise frameRate is derived from `duration`.
*
* @name Phaser.GameObjects.Components.Animation#duration
* @type {number}
* @default 0
* @since 3.0.0
*/
this.duration = 0;
/**
* ms per frame, not including frame specific modifiers that may be present in the Animation data.
*
* @name Phaser.GameObjects.Components.Animation#msPerFrame
* @type {number}
* @default 0
* @since 3.0.0
*/
this.msPerFrame = 0;
/**
* Skip frames if the time lags, or always advanced anyway?
*
* @name Phaser.GameObjects.Components.Animation#skipMissedFrames
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.skipMissedFrames = true;
/**
* A delay before starting playback, in seconds.
*
* @name Phaser.GameObjects.Components.Animation#_delay
* @type {number}
* @private
* @default 0
* @since 3.0.0
*/
this._delay = 0;
/**
* Number of times to repeat the animation (-1 for infinity)
*
* @name Phaser.GameObjects.Components.Animation#_repeat
* @type {number}
* @private
* @default 0
* @since 3.0.0
*/
this._repeat = 0;
/**
* Delay before the repeat starts, in seconds.
*
* @name Phaser.GameObjects.Components.Animation#_repeatDelay
* @type {number}
* @private
* @default 0
* @since 3.0.0
*/
this._repeatDelay = 0;
/**
* Should the animation yoyo? (reverse back down to the start) before repeating?
*
* @name Phaser.GameObjects.Components.Animation#_yoyo
* @type {boolean}
* @private
* @default false
* @since 3.0.0
*/
this._yoyo = false;
/**
* Will the playhead move forwards (`true`) or in reverse (`false`)
*
* @name Phaser.GameObjects.Components.Animation#forward
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.forward = true;
/**
* Internal time overflow accumulator.
*
* @name Phaser.GameObjects.Components.Animation#accumulator
* @type {number}
* @default 0
* @since 3.0.0
*/
this.accumulator = 0;
/**
* The time point at which the next animation frame will change.
*
* @name Phaser.GameObjects.Components.Animation#nextTick
* @type {number}
* @default 0
* @since 3.0.0
*/
this.nextTick = 0;
/**
* An internal counter keeping track of how many repeats are left to play.
*
* @name Phaser.GameObjects.Components.Animation#repeatCounter
* @type {number}
* @default 0
* @since 3.0.0
*/
this.repeatCounter = 0;
/**
* An internal flag keeping track of pending repeats.
*
* @name Phaser.GameObjects.Components.Animation#pendingRepeat
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.pendingRepeat = false;
/**
* Is the Animation paused?
*
* @name Phaser.GameObjects.Components.Animation#_paused
* @type {boolean}
* @private
* @default false
* @since 3.0.0
*/
this._paused = false;
/**
* Was the animation previously playing before being paused?
*
* @name Phaser.GameObjects.Components.Animation#_wasPlaying
* @type {boolean}
* @private
* @default false
* @since 3.0.0
*/
this._wasPlaying = false;
/**
* Container for the callback arguments.
*
* @name Phaser.GameObjects.Components.Animation#_callbackArgs
* @type {array}
* @private
* @since 3.0.0
*/
this._callbackArgs = [ parent, null ];
/**
* Container for the update arguments.
*
* @name Phaser.GameObjects.Components.Animation#_updateParams
* @type {array}
* @private
* @since 3.0.0
*/
this._updateParams = [];
},
/**
* Sets the amount of time, in seconds that the animation will be delayed before starting playback.
*
* @method Phaser.GameObjects.Components.Animation#delay
* @since 3.0.0
*
* @param {number} value - The amount of time, in seconds, to wait before starting playback.
*
* @return {Phaser.GameObjects.GameObject} This Game Object.
*/
delay: function (value)
{
if (value === undefined)
{
return this._delay;
}
else
{
this._delay = value;
return this;
}
},
/**
* [description]
*
* @method Phaser.GameObjects.Components.Animation#delayedPlay
* @since 3.0.0
*
* @param {number} delay - [description]
* @param {string} key - [description]
* @param {integer} startFrame - [description]
*
* @return {Phaser.GameObjects.GameObject} This Game Object.
*/
delayedPlay: function (delay, key, startFrame)
{
this.play(key, true, startFrame);
this.nextTick += (delay * 1000);
return this;
},
/**
* [description]
*
* @method Phaser.GameObjects.Components.Animation#getCurrentKey
* @since 3.0.0
*
* @return {string} [description]
*/
getCurrentKey: function ()
{
if (this.currentAnim)
{
return this.currentAnim.key;
}
},
/**
* [description]
*
* @method Phaser.GameObjects.Components.Animation#load
* @since 3.0.0
*
* @param {string} key - [description]
* @param {integer} [startFrame=0] - [description]
*
* @return {Phaser.GameObjects.GameObject} This Game Object.
*/
load: function (key, startFrame)
{
if (startFrame === undefined) { startFrame = 0; }
if (this.isPlaying)
{
this.stop();
}
// Load the new animation in
this.animationManager.load(this, key, startFrame);
return this;
},
/**
* [description]
*
* @method Phaser.GameObjects.Components.Animation#pause
* @since 3.0.0
*
* @param {Phaser.Animations.Animation} [atFrame] - [description]
*
* @return {Phaser.GameObjects.GameObject} This Game Object.
*/
pause: function (atFrame)
{
if (!this._paused)
{
this._paused = true;
this._wasPlaying = this.isPlaying;
this.isPlaying = false;
}
if (atFrame !== undefined)
{
this.updateFrame(atFrame);
}
return this;
},
/**
* [description]
*
* @method Phaser.GameObjects.Components.Animation#paused
* @since 3.0.0
*
* @param {boolean} [value] - [description]
*
* @return {(boolean|Phaser.GameObjects.GameObject)} [description]
*/
paused: function (value)
{
if (value !== undefined)
{
// Setter
if (value)
{
return this.pause();
}
else
{
return this.resume();
}
}
else
{
return this._paused;
}
},
/**
* [description]
*
* @method Phaser.GameObjects.Components.Animation#play
* @since 3.0.0
*
* @param {string} key - [description]
* @param {boolean} [ignoreIfPlaying=false] - [description]
* @param {integer} [startFrame=0] - [description]
*
* @return {Phaser.GameObjects.GameObject} This Game Object.
*/
play: function (key, ignoreIfPlaying, startFrame)
{
if (ignoreIfPlaying === undefined) { ignoreIfPlaying = false; }
if (startFrame === undefined) { startFrame = 0; }
if (ignoreIfPlaying && this.isPlaying && this.currentAnim.key === key)
{
return this;
}
this.load(key, startFrame);
var anim = this.currentAnim;
var gameObject = this.parent;
// Should give us 9,007,199,254,740,991 safe repeats
this.repeatCounter = (this._repeat === -1) ? Number.MAX_VALUE : this._repeat;
anim.getFirstTick(this);
this.forward = true;
this.isPlaying = true;
this.pendingRepeat = false;
if (anim.showOnStart)
{
gameObject.visible = true;
}
if (anim.onStart)
{
anim.onStart.apply(anim.callbackScope, this._callbackArgs.concat(anim.onStartParams));
}
gameObject.setSizeToFrame();
gameObject.updateDisplayOrigin();
return this;
},
/**
* Value between 0 and 1. How far this animation is through, ignoring repeats and yoyos.
* If the animation has a non-zero repeat defined, progress and totalProgress will be different
* because progress doesn't include any repeats or repeatDelays whereas totalProgress does.
*
* @method Phaser.GameObjects.Components.Animation#progress
* @since 3.0.0
*
* @param {number} [value] - [description]
*
* @return {(number|Phaser.GameObjects.GameObject)} [description]
*/
progress: function (value)
{
if (value === undefined)
{
var p = this.currentFrame.progress;
if (!this.forward)
{
p = 1 - p;
}
return p;
}
else
{
// TODO: Set progress
return this;
}
},
/**
* [description]
*
* @method Phaser.GameObjects.Components.Animation#remove
* @since 3.0.0
*
* @param {Phaser.Animations.Animation} [event] - [description]
*/
remove: function (event)
{
if (event === undefined) { event = this.currentAnim; }
if (this.isPlaying && event.key === this.currentAnim.key)
{
this.stop();
var sprite = this.parent;
var frame = this.currentAnim.frames[0];
this.currentFrame = frame;
sprite.texture = frame.frame.texture;
sprite.frame = frame.frame;
}
},
/**
* Gets or sets the number of times that the animation should repeat
* after its first iteration. For example, if repeat is 1, the animation will
* play a total of twice (the initial play plus 1 repeat).
* To repeat indefinitely, use -1. repeat should always be an integer.
*
* @method Phaser.GameObjects.Components.Animation#repeat
* @since 3.0.0
*
* @param {number} value - [description]
*
* @return {(number|Phaser.GameObjects.GameObject)} [description]
*/
repeat: function (value)
{
if (value === undefined)
{
return this._repeat;
}
else
{
this._repeat = value;
this.repeatCounter = 0;
return this;
}
},
/**
* Gets or sets the amount of time in seconds between repeats.
* For example, if repeat is 2 and repeatDelay is 1, the animation will play initially,
* then wait for 1 second before it repeats, then play again, then wait 1 second again
* before doing its final repeat.
*
* @method Phaser.GameObjects.Components.Animation#repeatDelay
* @since 3.0.0
*
* @param {number} [value] - [description]
*
* @return {(number|Phaser.GameObjects.GameObject)} [description]
*/
repeatDelay: function (value)
{
if (value === undefined)
{
return this._repeatDelay;
}
else
{
this._repeatDelay = value;
return this;
}
},
/**
* [description]
*
* @method Phaser.GameObjects.Components.Animation#restart
* @since 3.0.0
*
* @param {boolean} [includeDelay=false] - [description]
*
* @return {Phaser.GameObjects.GameObject} This Game Object.
*/
restart: function (includeDelay)
{
if (includeDelay === undefined) { includeDelay = false; }
this.currentAnim.getFirstTick(this, includeDelay);
this.forward = true;
this.isPlaying = true;
this.pendingRepeat = false;
this._paused = false;
// Set frame
this.updateFrame(this.currentAnim.frames[0]);
return this;
},
/**
* [description]
*
* @method Phaser.GameObjects.Components.Animation#resume
* @since 3.0.0
*
* @param {Phaser.Animations.AnimationFrame} fromFrame - [description]
*
* @return {Phaser.GameObjects.GameObject} This Game Object.
*/
resume: function (fromFrame)
{
if (this._paused)
{
this._paused = false;
this.isPlaying = this._wasPlaying;
}
if (fromFrame !== undefined)
{
this.updateFrame(fromFrame);
}
return this;
},
/**
* [description]
*
* @method Phaser.GameObjects.Components.Animation#stop
* @since 3.0.0
*
* @param {boolean} [dispatchCallbacks=false] - [description]
*
* @return {Phaser.GameObjects.GameObject} This Game Object.
*/
stop: function (dispatchCallbacks)
{
if (dispatchCallbacks === undefined) { dispatchCallbacks = false; }
this.isPlaying = false;
var anim = this.currentAnim;
if (dispatchCallbacks && anim.onComplete)
{
anim.onComplete.apply(anim.callbackScope, this._callbackArgs.concat(anim.onCompleteParams));
}
return this;
},
/**
* Scale the time (make it go faster / slower)
* Factor that's used to scale time where 1 = normal speed (the default), 0.5 = half speed, 2 = double speed, etc.
*
* @method Phaser.GameObjects.Components.Animation#timeScale
* @since 3.0.0
*
* @param {number} [value] - [description]
*
* @return {(number|Phaser.GameObjects.GameObject)} [description]
*/
timeScale: function (value)
{
if (value === undefined)
{
return this._timeScale;
}
else
{
this._timeScale = value;
return this;
}
},
/**
* [description]
*
* @method Phaser.GameObjects.Components.Animation#totalFrames
* @since 3.0.0
*
* @return {number} [description]
*/
totalFrames: function ()
{
return this.currentAnim.frames.length;
},
// Value between 0 and 1. How far this animation is through, including things like delays
// repeats, custom frame durations, etc. If the animation is set to repeat -1 it can never
// have a duration, therefore this will return -1.
/**
* [description]
*
* @method Phaser.GameObjects.Components.Animation#totalProgres
* @since 3.0.0
*/
totalProgres: function ()
{
// TODO
},
/**
* [description]
*
* @method Phaser.GameObjects.Components.Animation#update
* @since 3.0.0
*
* @param {number} timestamp - [description]
* @param {number} delta - The delta time, in ms, elapsed since the last frame.
*/
update: function (timestamp, delta)
{
if (!this.isPlaying || this.currentAnim.paused)
{
return;
}
this.accumulator += delta * this._timeScale;
if (this.accumulator >= this.nextTick)
{
this.currentAnim.setFrame(this);
}
},
/**
* [description]
*
* @method Phaser.GameObjects.Components.Animation#updateFrame
* @since 3.0.0
*
* @param {Phaser.Animations.AnimationFrame} animationFrame - [description]
*/
updateFrame: function (animationFrame)
{
var sprite = this.parent;
this.currentFrame = animationFrame;
sprite.texture = animationFrame.frame.texture;
sprite.frame = animationFrame.frame;
if (this.isPlaying)
{
if (animationFrame.setAlpha)
{
sprite.alpha = animationFrame.alpha;
}
var anim = this.currentAnim;
if (anim.onUpdate)
{
anim.onUpdate.apply(anim.callbackScope, this._updateParams);
}
if (animationFrame.onUpdate)
{
animationFrame.onUpdate(sprite, animationFrame);
}
}
},
/**
* [description]
*
* @method Phaser.GameObjects.Components.Animation#yoyo
* @since 3.0.0
*
* @param {boolean} [value] - [description]
*
* @return {(boolean|Phaser.GameObjects.GameObject)} [description]
*/
yoyo: function (value)
{
if (value === undefined)
{
return this._yoyo;
}
else
{
this._yoyo = value;
return this;
}
},
/**
* [description]
*
* @method Phaser.GameObjects.Components.Animation#destroy
* @since 3.0.0
*/
destroy: function ()
{
// TODO
}
});
module.exports = Animation;