forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimationManager.js
More file actions
643 lines (554 loc) · 18.4 KB
/
Copy pathAnimationManager.js
File metadata and controls
643 lines (554 loc) · 18.4 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
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2019 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Animation = require('./Animation');
var Class = require('../utils/Class');
var CustomMap = require('../structs/Map');
var EventEmitter = require('eventemitter3');
var Events = require('./events');
var GameEvents = require('../core/events');
var GetValue = require('../utils/object/GetValue');
var Pad = require('../utils/string/Pad');
/**
* @classdesc
* The Animation Manager.
*
* Animations are managed by the global Animation Manager. This is a singleton class that is
* responsible for creating and delivering animations and their corresponding data to all Game Objects.
* Unlike plugins it is owned by the Game instance, not the Scene.
*
* Sprites and other Game Objects get the data they need from the AnimationManager.
*
* @class AnimationManager
* @extends Phaser.Events.EventEmitter
* @memberof Phaser.Animations
* @constructor
* @since 3.0.0
*
* @param {Phaser.Game} game - A reference to the Phaser.Game instance.
*/
var AnimationManager = new Class({
Extends: EventEmitter,
initialize:
function AnimationManager (game)
{
EventEmitter.call(this);
/**
* A reference to the Phaser.Game instance.
*
* @name Phaser.Animations.AnimationManager#game
* @type {Phaser.Game}
* @protected
* @since 3.0.0
*/
this.game = game;
/**
* A reference to the Texture Manager.
*
* @name Phaser.Animations.AnimationManager#textureManager
* @type {Phaser.Textures.TextureManager}
* @protected
* @since 3.0.0
*/
this.textureManager = null;
/**
* The global time scale of the Animation Manager.
*
* This scales the time delta between two frames, thus influencing the speed of time for the Animation Manager.
*
* @name Phaser.Animations.AnimationManager#globalTimeScale
* @type {number}
* @default 1
* @since 3.0.0
*/
this.globalTimeScale = 1;
/**
* The Animations registered in the Animation Manager.
*
* This map should be modified with the {@link #add} and {@link #create} methods of the Animation Manager.
*
* @name Phaser.Animations.AnimationManager#anims
* @type {Phaser.Structs.Map.<string, Phaser.Animations.Animation>}
* @protected
* @since 3.0.0
*/
this.anims = new CustomMap();
/**
* Whether the Animation Manager is paused along with all of its Animations.
*
* @name Phaser.Animations.AnimationManager#paused
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.paused = false;
/**
* The name of this Animation Manager.
*
* @name Phaser.Animations.AnimationManager#name
* @type {string}
* @since 3.0.0
*/
this.name = 'AnimationManager';
game.events.once(GameEvents.BOOT, this.boot, this);
},
/**
* Registers event listeners after the Game boots.
*
* @method Phaser.Animations.AnimationManager#boot
* @listens Phaser.Core.Events#DESTROY
* @since 3.0.0
*/
boot: function ()
{
this.textureManager = this.game.textures;
this.game.events.once(GameEvents.DESTROY, this.destroy, this);
},
/**
* Adds an existing Animation to the Animation Manager.
*
* @method Phaser.Animations.AnimationManager#add
* @fires Phaser.Animations.Events#ADD_ANIMATION
* @since 3.0.0
*
* @param {string} key - The key under which the Animation should be added. The Animation will be updated with it. Must be unique.
* @param {Phaser.Animations.Animation} animation - The Animation which should be added to the Animation Manager.
*
* @return {Phaser.Animations.AnimationManager} This Animation Manager.
*/
add: function (key, animation)
{
if (this.anims.has(key))
{
console.warn('Animation key exists: ' + key);
return;
}
animation.key = key;
this.anims.set(key, animation);
this.emit(Events.ADD_ANIMATION, key, animation);
return this;
},
/**
* Checks to see if the given key is already in use within the Animation Manager or not.
*
* Animations are global. Keys created in one scene can be used from any other Scene in your game. They are not Scene specific.
*
* @method Phaser.Animations.AnimationManager#exists
* @since 3.16.0
*
* @param {string} key - The key of the Animation to check.
*
* @return {boolean} `true` if the Animation already exists in the Animation Manager, or `false` if the key is available.
*/
exists: function (key)
{
return this.anims.has(key);
},
/**
* Creates a new Animation and adds it to the Animation Manager.
*
* Animations are global. Once created, you can use them in any Scene in your game. They are not Scene specific.
*
* If an invalid key is given this method will return `false`.
*
* If you pass the key of an animation that already exists in the Animation Manager, that animation will be returned.
*
* A brand new animation is only created if the key is valid and not already in use.
*
* If you wish to re-use an existing key, call `AnimationManager.remove` first, then this method.
*
* @method Phaser.Animations.AnimationManager#create
* @fires Phaser.Animations.Events#ADD_ANIMATION
* @since 3.0.0
*
* @param {Phaser.Types.Animations.Animation} config - The configuration settings for the Animation.
*
* @return {(Phaser.Animations.Animation|false)} The Animation that was created, or `false` is the key is already in use.
*/
create: function (config)
{
var key = config.key;
var anim = false;
if (key)
{
anim = this.get(key);
if (!anim)
{
anim = new Animation(this, key, config);
this.anims.set(key, anim);
this.emit(Events.ADD_ANIMATION, key, anim);
}
}
return anim;
},
/**
* Loads this Animation Manager's Animations and settings from a JSON object.
*
* @method Phaser.Animations.AnimationManager#fromJSON
* @since 3.0.0
*
* @param {(string|Phaser.Types.Animations.JSONAnimations|Phaser.Types.Animations.JSONAnimation)} data - The JSON object to parse.
* @param {boolean} [clearCurrentAnimations=false] - If set to `true`, the current animations will be removed (`anims.clear()`). If set to `false` (default), the animations in `data` will be added.
*
* @return {Phaser.Animations.Animation[]} An array containing all of the Animation objects that were created as a result of this call.
*/
fromJSON: function (data, clearCurrentAnimations)
{
if (clearCurrentAnimations === undefined) { clearCurrentAnimations = false; }
if (clearCurrentAnimations)
{
this.anims.clear();
}
// Do we have a String (i.e. from JSON, or an Object?)
if (typeof data === 'string')
{
data = JSON.parse(data);
}
var output = [];
// Array of animations, or a single animation?
if (data.hasOwnProperty('anims') && Array.isArray(data.anims))
{
for (var i = 0; i < data.anims.length; i++)
{
output.push(this.create(data.anims[i]));
}
if (data.hasOwnProperty('globalTimeScale'))
{
this.globalTimeScale = data.globalTimeScale;
}
}
else if (data.hasOwnProperty('key') && data.type === 'frame')
{
output.push(this.create(data));
}
return output;
},
/**
* [description]
*
* @method Phaser.Animations.AnimationManager#generateFrameNames
* @since 3.0.0
*
* @param {string} key - The key for the texture containing the animation frames.
* @param {Phaser.Types.Animations.GenerateFrameNames} [config] - The configuration object for the animation frame names.
*
* @return {Phaser.Types.Animations.AnimationFrame[]} The array of {@link Phaser.Types.Animations.AnimationFrame} objects.
*/
generateFrameNames: function (key, config)
{
var prefix = GetValue(config, 'prefix', '');
var start = GetValue(config, 'start', 0);
var end = GetValue(config, 'end', 0);
var suffix = GetValue(config, 'suffix', '');
var zeroPad = GetValue(config, 'zeroPad', 0);
var out = GetValue(config, 'outputArray', []);
var frames = GetValue(config, 'frames', false);
var texture = this.textureManager.get(key);
if (!texture)
{
return out;
}
var diff = (start < end) ? 1 : -1;
// Adjust because we use i !== end in the for loop
end += diff;
var i;
var frame;
if (!config)
{
// Use every frame in the atlas?
frames = texture.getFrameNames();
for (i = 0; i < frames.length; i++)
{
out.push({ key: key, frame: frames[i] });
}
}
else if (Array.isArray(frames))
{
// Have they provided their own custom frame sequence array?
for (i = 0; i < frames.length; i++)
{
frame = prefix + Pad(frames[i], zeroPad, '0', 1) + suffix;
if (texture.has(frame))
{
out.push({ key: key, frame: frame });
}
}
}
else
{
for (i = start; i !== end; i += diff)
{
frame = prefix + Pad(i, zeroPad, '0', 1) + suffix;
if (texture.has(frame))
{
out.push({ key: key, frame: frame });
}
}
}
return out;
},
/**
* Generate an array of {@link Phaser.Types.Animations.AnimationFrame} objects from a texture key and configuration object.
*
* Generates objects with numbered frame names, as configured by the given {@link Phaser.Types.Animations.GenerateFrameNumbers}.
*
* @method Phaser.Animations.AnimationManager#generateFrameNumbers
* @since 3.0.0
*
* @param {string} key - The key for the texture containing the animation frames.
* @param {Phaser.Types.Animations.GenerateFrameNumbers} config - The configuration object for the animation frames.
*
* @return {Phaser.Types.Animations.AnimationFrame[]} The array of {@link Phaser.Types.Animations.AnimationFrame} objects.
*/
generateFrameNumbers: function (key, config)
{
var startFrame = GetValue(config, 'start', 0);
var endFrame = GetValue(config, 'end', -1);
var firstFrame = GetValue(config, 'first', false);
var out = GetValue(config, 'outputArray', []);
var frames = GetValue(config, 'frames', false);
var texture = this.textureManager.get(key);
if (!texture)
{
return out;
}
if (firstFrame && texture.has(firstFrame))
{
out.push({ key: key, frame: firstFrame });
}
var i;
// Have they provided their own custom frame sequence array?
if (Array.isArray(frames))
{
for (i = 0; i < frames.length; i++)
{
if (texture.has(frames[i]))
{
out.push({ key: key, frame: frames[i] });
}
}
}
else
{
// No endFrame then see if we can get it
if (endFrame === -1)
{
endFrame = texture.frameTotal;
}
var diff = (startFrame < endFrame) ? 1 : -1;
// Adjust because we use i !== end in the for loop
endFrame += diff;
for (i = startFrame; i !== endFrame; i += diff)
{
if (texture.has(i))
{
out.push({ key: key, frame: i });
}
}
}
return out;
},
/**
* Get an Animation.
*
* @method Phaser.Animations.AnimationManager#get
* @since 3.0.0
*
* @param {string} key - The key of the Animation to retrieve.
*
* @return {Phaser.Animations.Animation} The Animation.
*/
get: function (key)
{
return this.anims.get(key);
},
/**
* Load an Animation into a Game Object's Animation Component.
*
* @method Phaser.Animations.AnimationManager#load
* @since 3.0.0
*
* @param {Phaser.GameObjects.GameObject} child - The Game Object to load the animation into.
* @param {string} key - The key of the animation to load.
* @param {(string|integer)} [startFrame] - The name of a start frame to set on the loaded animation.
*
* @return {Phaser.GameObjects.GameObject} The Game Object with the animation loaded into it.
*/
load: function (child, key, startFrame)
{
var anim = this.get(key);
if (anim)
{
anim.load(child, startFrame);
}
else
{
console.warn('Missing animation: ' + key);
}
return child;
},
/**
* Pause all animations.
*
* @method Phaser.Animations.AnimationManager#pauseAll
* @fires Phaser.Animations.Events#PAUSE_ALL
* @since 3.0.0
*
* @return {Phaser.Animations.AnimationManager} This Animation Manager.
*/
pauseAll: function ()
{
if (!this.paused)
{
this.paused = true;
this.emit(Events.PAUSE_ALL);
}
return this;
},
/**
* Play an animation on the given Game Objects that have an Animation Component.
*
* @method Phaser.Animations.AnimationManager#play
* @since 3.0.0
*
* @param {string} key - The key of the animation to play on the Game Object.
* @param {Phaser.GameObjects.GameObject|Phaser.GameObjects.GameObject[]} child - The Game Objects to play the animation on.
*
* @return {Phaser.Animations.AnimationManager} This Animation Manager.
*/
play: function (key, child)
{
if (!Array.isArray(child))
{
child = [ child ];
}
var anim = this.get(key);
if (!anim)
{
return;
}
for (var i = 0; i < child.length; i++)
{
child[i].anims.play(key);
}
return this;
},
/**
* Remove an animation.
*
* @method Phaser.Animations.AnimationManager#remove
* @fires Phaser.Animations.Events#REMOVE_ANIMATION
* @since 3.0.0
*
* @param {string} key - The key of the animation to remove.
*
* @return {Phaser.Animations.Animation} [description]
*/
remove: function (key)
{
var anim = this.get(key);
if (anim)
{
this.emit(Events.REMOVE_ANIMATION, key, anim);
this.anims.delete(key);
}
return anim;
},
/**
* Resume all paused animations.
*
* @method Phaser.Animations.AnimationManager#resumeAll
* @fires Phaser.Animations.Events#RESUME_ALL
* @since 3.0.0
*
* @return {Phaser.Animations.AnimationManager} This Animation Manager.
*/
resumeAll: function ()
{
if (this.paused)
{
this.paused = false;
this.emit(Events.RESUME_ALL);
}
return this;
},
/**
* Takes an array of Game Objects that have an Animation Component and then
* starts the given animation playing on them, each one offset by the
* `stagger` amount given to this method.
*
* @method Phaser.Animations.AnimationManager#staggerPlay
* @since 3.0.0
*
* @generic {Phaser.GameObjects.GameObject[]} G - [items,$return]
*
* @param {string} key - The key of the animation to play on the Game Objects.
* @param {Phaser.GameObjects.GameObject|Phaser.GameObjects.GameObject[]} children - An array of Game Objects to play the animation on. They must have an Animation Component.
* @param {number} [stagger=0] - The amount of time, in milliseconds, to offset each play time by.
*
* @return {Phaser.Animations.AnimationManager} This Animation Manager.
*/
staggerPlay: function (key, children, stagger)
{
if (stagger === undefined) { stagger = 0; }
if (!Array.isArray(children))
{
children = [ children ];
}
var anim = this.get(key);
if (!anim)
{
return;
}
for (var i = 0; i < children.length; i++)
{
children[i].anims.delayedPlay(stagger * i, key);
}
return this;
},
/**
* Get the animation data as javascript object by giving key, or get the data of all animations as array of objects, if key wasn't provided.
*
* @method Phaser.Animations.AnimationManager#toJSON
* @since 3.0.0
*
* @param {string} key - [description]
*
* @return {Phaser.Types.Animations.JSONAnimations} [description]
*/
toJSON: function (key)
{
if (key !== undefined && key !== '')
{
return this.anims.get(key).toJSON();
}
else
{
var output = {
anims: [],
globalTimeScale: this.globalTimeScale
};
this.anims.each(function (animationKey, animation)
{
output.anims.push(animation.toJSON());
});
return output;
}
},
/**
* Destroy this Animation Manager and clean up animation definitions and references to other objects.
* This method should not be called directly. It will be called automatically as a response to a `destroy` event from the Phaser.Game instance.
*
* @method Phaser.Animations.AnimationManager#destroy
* @since 3.0.0
*/
destroy: function ()
{
this.anims.clear();
this.textureManager = null;
this.game = null;
}
});
module.exports = AnimationManager;