/
ParticleEmitter.js
2073 lines (1816 loc) · 63.5 KB
/
ParticleEmitter.js
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
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var BlendModes = require('../../renderer/BlendModes');
var Class = require('../../utils/Class');
var Components = require('../components');
var DeathZone = require('./zones/DeathZone');
var EdgeZone = require('./zones/EdgeZone');
var EmitterOp = require('./EmitterOp');
var GetFastValue = require('../../utils/object/GetFastValue');
var GetRandom = require('../../utils/array/GetRandom');
var HasAny = require('../../utils/object/HasAny');
var HasValue = require('../../utils/object/HasValue');
var Particle = require('./Particle');
var RandomZone = require('./zones/RandomZone');
var Rectangle = require('../../geom/rectangle/Rectangle');
var StableSort = require('../../utils/array/StableSort');
var Vector2 = require('../../math/Vector2');
var Wrap = require('../../math/Wrap');
/**
* @classdesc
* A particle emitter represents a single particle stream.
* It controls a pool of {@link Phaser.GameObjects.Particles.Particle Particles} and is controlled by a {@link Phaser.GameObjects.Particles.ParticleEmitterManager Particle Emitter Manager}.
*
* @class ParticleEmitter
* @memberof Phaser.GameObjects.Particles
* @constructor
* @since 3.0.0
*
* @extends Phaser.GameObjects.Components.BlendMode
* @extends Phaser.GameObjects.Components.Mask
* @extends Phaser.GameObjects.Components.ScrollFactor
* @extends Phaser.GameObjects.Components.Visible
*
* @param {Phaser.GameObjects.Particles.ParticleEmitterManager} manager - The Emitter Manager this Emitter belongs to.
* @param {Phaser.Types.GameObjects.Particles.ParticleEmitterConfig} config - Settings for this emitter.
*/
var ParticleEmitter = new Class({
Mixins: [
Components.BlendMode,
Components.Mask,
Components.ScrollFactor,
Components.Visible
],
initialize:
function ParticleEmitter (manager, config)
{
/**
* The Emitter Manager this Emitter belongs to.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#manager
* @type {Phaser.GameObjects.Particles.ParticleEmitterManager}
* @since 3.0.0
*/
this.manager = manager;
/**
* The texture assigned to particles.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#texture
* @type {Phaser.Textures.Texture}
* @since 3.0.0
*/
this.texture = manager.texture;
/**
* The texture frames assigned to particles.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#frames
* @type {Phaser.Textures.Frame[]}
* @since 3.0.0
*/
this.frames = [ manager.defaultFrame ];
/**
* The default texture frame assigned to particles.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#defaultFrame
* @type {Phaser.Textures.Frame}
* @since 3.0.0
*/
this.defaultFrame = manager.defaultFrame;
/**
* Names of simple configuration properties.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#configFastMap
* @type {object}
* @since 3.0.0
*/
this.configFastMap = [
'active',
'blendMode',
'collideBottom',
'collideLeft',
'collideRight',
'collideTop',
'deathCallback',
'deathCallbackScope',
'emitCallback',
'emitCallbackScope',
'follow',
'frequency',
'gravityX',
'gravityY',
'maxParticles',
'name',
'on',
'particleBringToTop',
'particleClass',
'radial',
'timeScale',
'trackVisible',
'visible'
];
/**
* Names of complex configuration properties.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#configOpMap
* @type {object}
* @since 3.0.0
*/
this.configOpMap = [
'accelerationX',
'accelerationY',
'angle',
'alpha',
'bounce',
'delay',
'lifespan',
'maxVelocityX',
'maxVelocityY',
'moveToX',
'moveToY',
'quantity',
'rotate',
'scaleX',
'scaleY',
'speedX',
'speedY',
'tint',
'x',
'y'
];
/**
* The name of this Particle Emitter.
*
* Empty by default and never populated by Phaser, this is left for developers to use.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#name
* @type {string}
* @default ''
* @since 3.0.0
*/
this.name = '';
/**
* The Particle Class which will be emitted by this Emitter.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#particleClass
* @type {Phaser.GameObjects.Particles.Particle}
* @default Phaser.GameObjects.Particles.Particle
* @since 3.0.0
*/
this.particleClass = Particle;
/**
* The x-coordinate of the particle origin (where particles will be emitted).
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#x
* @type {Phaser.GameObjects.Particles.EmitterOp}
* @default 0
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setPosition
*/
this.x = new EmitterOp(config, 'x', 0, true);
/**
* The y-coordinate of the particle origin (where particles will be emitted).
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#y
* @type {Phaser.GameObjects.Particles.EmitterOp}
* @default 0
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setPosition
*/
this.y = new EmitterOp(config, 'y', 0, true);
/**
* A radial emitter will emit particles in all directions between angle min and max,
* using {@link Phaser.GameObjects.Particles.ParticleEmitter#speed} as the value. If set to false then this acts as a point Emitter.
* A point emitter will emit particles only in the direction derived from the speedX and speedY values.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#radial
* @type {boolean}
* @default true
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setRadial
*/
this.radial = true;
/**
* Horizontal acceleration applied to emitted particles, in pixels per second squared.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#gravityX
* @type {number}
* @default 0
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setGravity
*/
this.gravityX = 0;
/**
* Vertical acceleration applied to emitted particles, in pixels per second squared.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#gravityY
* @type {number}
* @default 0
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setGravity
*/
this.gravityY = 0;
/**
* Whether accelerationX and accelerationY are non-zero. Set automatically during configuration.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#acceleration
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.acceleration = false;
/**
* Horizontal acceleration applied to emitted particles, in pixels per second squared.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#accelerationX
* @type {Phaser.GameObjects.Particles.EmitterOp}
* @default 0
* @since 3.0.0
*/
this.accelerationX = new EmitterOp(config, 'accelerationX', 0, true);
/**
* Vertical acceleration applied to emitted particles, in pixels per second squared.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#accelerationY
* @type {Phaser.GameObjects.Particles.EmitterOp}
* @default 0
* @since 3.0.0
*/
this.accelerationY = new EmitterOp(config, 'accelerationY', 0, true);
/**
* The maximum horizontal velocity of emitted particles, in pixels per second squared.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#maxVelocityX
* @type {Phaser.GameObjects.Particles.EmitterOp}
* @default 10000
* @since 3.0.0
*/
this.maxVelocityX = new EmitterOp(config, 'maxVelocityX', 10000, true);
/**
* The maximum vertical velocity of emitted particles, in pixels per second squared.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#maxVelocityY
* @type {Phaser.GameObjects.Particles.EmitterOp}
* @default 10000
* @since 3.0.0
*/
this.maxVelocityY = new EmitterOp(config, 'maxVelocityY', 10000, true);
/**
* The initial horizontal speed of emitted particles, in pixels per second.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#speedX
* @type {Phaser.GameObjects.Particles.EmitterOp}
* @default 0
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setSpeedX
*/
this.speedX = new EmitterOp(config, 'speedX', 0, true);
/**
* The initial vertical speed of emitted particles, in pixels per second.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#speedY
* @type {Phaser.GameObjects.Particles.EmitterOp}
* @default 0
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setSpeedY
*/
this.speedY = new EmitterOp(config, 'speedY', 0, true);
/**
* Whether moveToX and moveToY are nonzero. Set automatically during configuration.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#moveTo
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.moveTo = false;
/**
* The x-coordinate emitted particles move toward, when {@link Phaser.GameObjects.Particles.ParticleEmitter#moveTo} is true.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#moveToX
* @type {Phaser.GameObjects.Particles.EmitterOp}
* @default 0
* @since 3.0.0
*/
this.moveToX = new EmitterOp(config, 'moveToX', 0, true);
/**
* The y-coordinate emitted particles move toward, when {@link Phaser.GameObjects.Particles.ParticleEmitter#moveTo} is true.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#moveToY
* @type {Phaser.GameObjects.Particles.EmitterOp}
* @default 0
* @since 3.0.0
*/
this.moveToY = new EmitterOp(config, 'moveToY', 0, true);
/**
* Whether particles will rebound when they meet the emitter bounds.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#bounce
* @type {Phaser.GameObjects.Particles.EmitterOp}
* @default 0
* @since 3.0.0
*/
this.bounce = new EmitterOp(config, 'bounce', 0, true);
/**
* The horizontal scale of emitted particles.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#scaleX
* @type {Phaser.GameObjects.Particles.EmitterOp}
* @default 1
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setScale
* @see Phaser.GameObjects.Particles.ParticleEmitter#setScaleX
*/
this.scaleX = new EmitterOp(config, 'scaleX', 1);
/**
* The vertical scale of emitted particles.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#scaleY
* @type {Phaser.GameObjects.Particles.EmitterOp}
* @default 1
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setScale
* @see Phaser.GameObjects.Particles.ParticleEmitter#setScaleY
*/
this.scaleY = new EmitterOp(config, 'scaleY', 1);
/**
* Color tint applied to emitted particles. Value must not include the alpha channel.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#tint
* @type {Phaser.GameObjects.Particles.EmitterOp}
* @default 0xffffff
* @since 3.0.0
*/
this.tint = new EmitterOp(config, 'tint', 0xffffff);
/**
* The alpha (transparency) of emitted particles.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#alpha
* @type {Phaser.GameObjects.Particles.EmitterOp}
* @default 1
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setAlpha
*/
this.alpha = new EmitterOp(config, 'alpha', 1);
/**
* The lifespan of emitted particles, in ms.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#lifespan
* @type {Phaser.GameObjects.Particles.EmitterOp}
* @default 1000
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setLifespan
*/
this.lifespan = new EmitterOp(config, 'lifespan', 1000, true);
/**
* The angle of the initial velocity of emitted particles, in degrees.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#angle
* @type {Phaser.GameObjects.Particles.EmitterOp}
* @default { min: 0, max: 360 }
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setAngle
*/
this.angle = new EmitterOp(config, 'angle', { min: 0, max: 360 }, true);
/**
* The rotation of emitted particles, in degrees.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#rotate
* @type {Phaser.GameObjects.Particles.EmitterOp}
* @default 0
* @since 3.0.0
*/
this.rotate = new EmitterOp(config, 'rotate', 0);
/**
* A function to call when a particle is emitted.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#emitCallback
* @type {?Phaser.Types.GameObjects.Particles.ParticleEmitterCallback}
* @default null
* @since 3.0.0
*/
this.emitCallback = null;
/**
* The calling context for {@link Phaser.GameObjects.Particles.ParticleEmitter#emitCallback}.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#emitCallbackScope
* @type {?*}
* @default null
* @since 3.0.0
*/
this.emitCallbackScope = null;
/**
* A function to call when a particle dies.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#deathCallback
* @type {?Phaser.Types.GameObjects.Particles.ParticleDeathCallback}
* @default null
* @since 3.0.0
*/
this.deathCallback = null;
/**
* The calling context for {@link Phaser.GameObjects.Particles.ParticleEmitter#deathCallback}.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#deathCallbackScope
* @type {?*}
* @default null
* @since 3.0.0
*/
this.deathCallbackScope = null;
/**
* Set to hard limit the amount of particle objects this emitter is allowed to create.
* 0 means unlimited.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#maxParticles
* @type {number}
* @default 0
* @since 3.0.0
*/
this.maxParticles = 0;
/**
* How many particles are emitted each time particles are emitted (one explosion or one flow cycle).
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#quantity
* @type {Phaser.GameObjects.Particles.EmitterOp}
* @default 1
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setFrequency
* @see Phaser.GameObjects.Particles.ParticleEmitter#setQuantity
*/
this.quantity = new EmitterOp(config, 'quantity', 1, true);
/**
* How many ms to wait after emission before the particles start updating.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#delay
* @type {Phaser.GameObjects.Particles.EmitterOp}
* @default 0
* @since 3.0.0
*/
this.delay = new EmitterOp(config, 'delay', 0, true);
/**
* For a flow emitter, the time interval (>= 0) between particle flow cycles in ms.
* A value of 0 means there is one particle flow cycle for each logic update (the maximum flow frequency). This is the default setting.
* For an exploding emitter, this value will be -1.
* Calling {@link Phaser.GameObjects.Particles.ParticleEmitter#flow} also puts the emitter in flow mode (frequency >= 0).
* Calling {@link Phaser.GameObjects.Particles.ParticleEmitter#explode} also puts the emitter in explode mode (frequency = -1).
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#frequency
* @type {number}
* @default 0
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setFrequency
*/
this.frequency = 0;
/**
* Controls if the emitter is currently emitting a particle flow (when frequency >= 0).
* Already alive particles will continue to update until they expire.
* Controlled by {@link Phaser.GameObjects.Particles.ParticleEmitter#start} and {@link Phaser.GameObjects.Particles.ParticleEmitter#stop}.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#on
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.on = true;
/**
* Newly emitted particles are added to the top of the particle list, i.e. rendered above those already alive.
* Set to false to send them to the back.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#particleBringToTop
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.particleBringToTop = true;
/**
* The time rate applied to active particles, affecting lifespan, movement, and tweens. Values larger than 1 are faster than normal.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#timeScale
* @type {number}
* @default 1
* @since 3.0.0
*/
this.timeScale = 1;
/**
* An object describing a shape to emit particles from.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#emitZone
* @type {?Phaser.GameObjects.Particles.Zones.EdgeZone|Phaser.GameObjects.Particles.Zones.RandomZone}
* @default null
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setEmitZone
*/
this.emitZone = null;
/**
* An object describing a shape that deactivates particles when they interact with it.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#deathZone
* @type {?Phaser.GameObjects.Particles.Zones.DeathZone}
* @default null
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setDeathZone
*/
this.deathZone = null;
/**
* A rectangular boundary constraining particle movement.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#bounds
* @type {?Phaser.Geom.Rectangle}
* @default null
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setBounds
*/
this.bounds = null;
/**
* Whether particles interact with the left edge of the emitter {@link Phaser.GameObjects.Particles.ParticleEmitter#bounds}.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#collideLeft
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.collideLeft = true;
/**
* Whether particles interact with the right edge of the emitter {@link Phaser.GameObjects.Particles.ParticleEmitter#bounds}.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#collideRight
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.collideRight = true;
/**
* Whether particles interact with the top edge of the emitter {@link Phaser.GameObjects.Particles.ParticleEmitter#bounds}.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#collideTop
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.collideTop = true;
/**
* Whether particles interact with the bottom edge of the emitter {@link Phaser.GameObjects.Particles.ParticleEmitter#bounds}.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#collideBottom
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.collideBottom = true;
/**
* Whether this emitter updates itself and its particles.
*
* Controlled by {@link Phaser.GameObjects.Particles.ParticleEmitter#pause}
* and {@link Phaser.GameObjects.Particles.ParticleEmitter#resume}.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#active
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.active = true;
/**
* Set this to false to hide any active particles.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#visible
* @type {boolean}
* @default true
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setVisible
*/
this.visible = true;
/**
* The blend mode of this emitter's particles.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#blendMode
* @type {number}
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setBlendMode
*/
this.blendMode = BlendModes.NORMAL;
/**
* A Game Object whose position is used as the particle origin.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#follow
* @type {?Phaser.GameObjects.GameObject}
* @default null
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#startFollow
* @see Phaser.GameObjects.Particles.ParticleEmitter#stopFollow
*/
this.follow = null;
/**
* The offset of the particle origin from the {@link Phaser.GameObjects.Particles.ParticleEmitter#follow} target.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#followOffset
* @type {Phaser.Math.Vector2}
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#startFollow
*/
this.followOffset = new Vector2();
/**
* Whether the emitter's {@link Phaser.GameObjects.Particles.ParticleEmitter#visible} state will track
* the {@link Phaser.GameObjects.Particles.ParticleEmitter#follow} target's visibility state.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#trackVisible
* @type {boolean}
* @default false
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#startFollow
*/
this.trackVisible = false;
/**
* The current texture frame, as an index of {@link Phaser.GameObjects.Particles.ParticleEmitter#frames}.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#currentFrame
* @type {number}
* @default 0
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setFrame
*/
this.currentFrame = 0;
/**
* Whether texture {@link Phaser.GameObjects.Particles.ParticleEmitter#frames} are selected at random.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#randomFrame
* @type {boolean}
* @default true
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setFrame
*/
this.randomFrame = true;
/**
* The number of consecutive particles that receive a single texture frame (per frame cycle).
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#frameQuantity
* @type {number}
* @default 1
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setFrame
*/
this.frameQuantity = 1;
/**
* Inactive particles.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#dead
* @type {Phaser.GameObjects.Particles.Particle[]}
* @private
* @since 3.0.0
*/
this.dead = [];
/**
* Active particles
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#alive
* @type {Phaser.GameObjects.Particles.Particle[]}
* @private
* @since 3.0.0
*/
this.alive = [];
/**
* The time until the next flow cycle.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#_counter
* @type {number}
* @private
* @default 0
* @since 3.0.0
*/
this._counter = 0;
/**
* Counts up to {@link Phaser.GameObjects.Particles.ParticleEmitter#frameQuantity}.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#_frameCounter
* @type {number}
* @private
* @default 0
* @since 3.0.0
*/
this._frameCounter = 0;
if (config)
{
this.fromJSON(config);
}
},
/**
* Merges configuration settings into the emitter's current settings.
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#fromJSON
* @since 3.0.0
*
* @param {Phaser.Types.GameObjects.Particles.ParticleEmitterConfig} config - Settings for this emitter.
*
* @return {this} This Particle Emitter.
*/
fromJSON: function (config)
{
if (!config)
{
return this;
}
// Only update properties from their current state if they exist in the given config
var i = 0;
var key = '';
for (i = 0; i < this.configFastMap.length; i++)
{
key = this.configFastMap[i];
if (HasValue(config, key))
{
this[key] = GetFastValue(config, key);
}
}
for (i = 0; i < this.configOpMap.length; i++)
{
key = this.configOpMap[i];
if (HasValue(config, key))
{
this[key].loadConfig(config);
}
}
this.acceleration = (this.accelerationX.propertyValue !== 0 || this.accelerationY.propertyValue !== 0);
this.moveTo = (this.moveToX.propertyValue !== 0 || this.moveToY.propertyValue !== 0);
// Special 'speed' override
if (HasValue(config, 'speed'))
{
this.speedX.loadConfig(config, 'speed');
this.speedY = null;
}
// If you specify speedX, speedY or moveTo then it changes the emitter from radial to a point emitter
if (HasAny(config, [ 'speedX', 'speedY' ]) || this.moveTo)
{
this.radial = false;
}
// Special 'scale' override
if (HasValue(config, 'scale'))
{
this.scaleX.loadConfig(config, 'scale');
this.scaleY = null;
}
if (HasValue(config, 'callbackScope'))
{
var callbackScope = GetFastValue(config, 'callbackScope', null);
this.emitCallbackScope = callbackScope;
this.deathCallbackScope = callbackScope;
}
if (HasValue(config, 'emitZone'))
{
this.setEmitZone(config.emitZone);
}
if (HasValue(config, 'deathZone'))
{
this.setDeathZone(config.deathZone);
}
if (HasValue(config, 'bounds'))
{
this.setBounds(config.bounds);
}
if (HasValue(config, 'followOffset'))
{
this.followOffset.setFromObject(GetFastValue(config, 'followOffset', 0));
}
if (HasValue(config, 'frame'))
{
this.setFrame(config.frame);
}
if (HasValue(config, 'reserve'))
{
this.reserve(config.reserve);
}
return this;
},
/**
* Creates a description of this emitter suitable for JSON serialization.
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#toJSON
* @since 3.0.0
*
* @param {object} [output] - An object to copy output into.
*
* @return {object} - The output object.
*/
toJSON: function (output)
{
if (output === undefined) { output = {}; }
var i = 0;
var key = '';
for (i = 0; i < this.configFastMap.length; i++)
{
key = this.configFastMap[i];
output[key] = this[key];
}
for (i = 0; i < this.configOpMap.length; i++)
{
key = this.configOpMap[i];
if (this[key])
{
output[key] = this[key].toJSON();
}
}
// special handlers
if (!this.speedY)
{
delete output.speedX;
output.speed = this.speedX.toJSON();
}
if (!this.scaleY)
{
delete output.scaleX;
output.scale = this.scaleX.toJSON();
}
return output;
},
/**
* Continuously moves the particle origin to follow a Game Object's position.
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#startFollow
* @since 3.0.0
*
* @param {Phaser.GameObjects.GameObject} target - The Game Object to follow.
* @param {number} [offsetX=0] - Horizontal offset of the particle origin from the Game Object.
* @param {number} [offsetY=0] - Vertical offset of the particle origin from the Game Object.
* @param {boolean} [trackVisible=false] - Whether the emitter's visible state will track the target's visible state.
*
* @return {this} This Particle Emitter.
*/
startFollow: function (target, offsetX, offsetY, trackVisible)
{
if (offsetX === undefined) { offsetX = 0; }
if (offsetY === undefined) { offsetY = 0; }
if (trackVisible === undefined) { trackVisible = false; }
this.follow = target;
this.followOffset.set(offsetX, offsetY);
this.trackVisible = trackVisible;
return this;
},
/**
* Stops following a Game Object.
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#stopFollow
* @since 3.0.0
*
* @return {this} This Particle Emitter.
*/
stopFollow: function ()
{
this.follow = null;
this.followOffset.set(0, 0);
this.trackVisible = false;
return this;
},
/**
* Chooses a texture frame from {@link Phaser.GameObjects.Particles.ParticleEmitter#frames}.
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#getFrame
* @since 3.0.0
*
* @return {Phaser.Textures.Frame} The texture frame.
*/
getFrame: function ()
{
if (this.frames.length === 1)
{
return this.defaultFrame;
}
else if (this.randomFrame)
{
return GetRandom(this.frames);
}
else
{
var frame = this.frames[this.currentFrame];
this._frameCounter++;
if (this._frameCounter === this.frameQuantity)
{
this._frameCounter = 0;
this.currentFrame = Wrap(this.currentFrame + 1, 0, this._frameLength);
}
return frame;
}
},