-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparticlesgl-1.2.1.js
1894 lines (1709 loc) · 50.4 KB
/
particlesgl-1.2.1.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
/**
* @module Kiwi
* @submodule GameObjects
* @main StatelessParticles
*/
/**
* Creates a particle game object
* @class StatelessParticles
* @extends Kiwi.Entity
* @constructor
* @param state {Kiwi.State} State to which the game object belongs
* @param atlas {Kiwi.Textures.TextureAtlas} Texture for the particle object
* @param x {number} X position of the game object
* @param y {number} Y position of the game object
* @param config {object} Particle configuration object;
* see properties for more information.
* @param [clock] {Kiwi.Time.Clock} Clock to govern animation.
* If omitted, will use state.game.time.clock.
* @public
* @return {Kiwi.GameObjects.StatelessParticles}
*/
Kiwi.GameObjects.StatelessParticles =
function( state, atlas, x, y, config, clock ) {
Kiwi.Entity.call( this, state, x, y );
if ( !clock || clock.objType !== "Clock" ) {
clock = state.game.time.clock;
}
return this.constructor( state, atlas, x, y, config, clock );
};
Kiwi.extend( Kiwi.GameObjects.StatelessParticles, Kiwi.Entity );
(function() {
var protoProps = {
constructor : function( state, atlas, x, y, config, clock ) {
var i;
/**
* Config object. This allows you to alter any or all of the default
* properties of a particle object. You do not need to specify
* any config values; they all have default values.
* A complete listing is as follows:
* <ul>
* <li>additive: Whether to draw in additive mode. Boolean.
* Default false.</li>
* <li>alpha: Overall object transparency. Number, range 0-1.
* More sophisticated alpha control is available via
* alphaGradient and alphaStops.
* Default 1.</li>
* <li>alphaGradient: Alpha value at four points throughout the
* lifetime of each particle. Array of numbers, range 0-1.
* Default [ 1, 1, 1, 0 ].</li>
* <li>alphaStops: Coordinates for alpha gradient stops.
* The beginning and end are always 0 and 1 respectively;
* this specifies the middle two stops.
* Array of numbers, range 0-1. Default [ 0.3, 0.7 ].</li>
* <li>angStartMin: 0,</li>
* <li>angStartMax: 0,</li>
* <li>angVelocityConform: false,</li>
* <li>colEnv0: Initial color value in the particle's lifespan.
* Array of three numbers, range 0-1. Default [ 1, 0, 0 ].</li>
* <li>colEnv1: Second color value in the particle's lifespan.
* Array of three numbers, range 0-1. Default [ 1, 1, 0 ].</li>
* <li>colEnv2: Third color value in the particle's lifespan.
* Array of three numbers, range 0-1. Default [ 1, 1, 1 ].</li>
* <li>colEnv3: Final color value in the particle's lifespan.
* Array of three numbers, range 0-1. Default [ 0, 0, 0 ].</li>
* <li>colEnvKeyframes: Coordinates for color gradient stops.
* The beginning and end are always 0 and 1 respectively;
* this specifies the middle two stops.
* Array of numbers, range 0-1. Default [ 0.5, 0.6 ].</li>
* <li>endSize: Size of particle at end of lifespan in pixels.
* Number, default 150.</li>
* <li>gravityX: Acceleration along X axis. Number, default 0.</li>
* <li>gravityY: Acceleration along Y axis. Number, default -50.
* <li>loop: Whether the particles repeat, or play once.
* Boolean, default true.</li>
* <li>maxLifespan: Maximum random lifespan. Number, default 5.</li>
* <li>maxStartTime: Maximum random start time.
* Number, default 6.</li>
* <li>maxVel: Maximum random start velocity.
* Number, default 100.</li>
* <li>minLifespan: Minimum random lifespan. Number, default 3.</li>
* <li>minStartTime: Minimum random start time.
* Number, default 1.</li>
* <li>minVel: Minimum random start velocity.
* Number, default 70.</li>
* <li>numParts: Number of particles to emit, default 20.</li>
* <li>posAngle: Angle of "line" emitter. Number, default 0.
* <li>posConstrainRadial: Whether to emit from the edge of a
* "radial" emitter. Boolean, default true.</li>
* <li>posConstrainRect: Whether to emit from the edge of a
* "rectangle" emitter. Boolean, default true.</li>
* <li>posHeight: Height of "rectangle" emitter.
* Number, default 100.</li>
* <li>posLength: Length of "line" emitter.
* Number, default 100.</li>
* <li>posOffsetX: Position offset of emitter.
* Number, default 0.</li>
* <li>posOffsetY: Position offset of emitter.
* Number, default 0.</li>
* <li>posRadialStart: Beginning angle of arc of "radial"
* emitter. Number, default 4.363323129985823.</li>
* <li>posRadialEnd: End angle of arc of "radial" emitter.
* Number, default 5.061454830783556.</li>
* <li>posRadius: Radius of "radial" emitter.
* Number, default 50.</li>
* <li>posShape: Shape of emitter. String, default "radial".
* Choose one of:
* <ul>
* <li>"radial"</li>
* <li>"rectangle"</li>
* <li>"line"</li>
* </ul></li>
* <li>posWidth: Width of "rectangle" emitter.
* Number, default 100.</li>
* <li>startSize: Size of particle at start of lifespan in
* pixels. Number, default 4.</li>
* <li>velAngle: Angle of "line" emission velocity type.
* Number, default 0.</li>
* <li>velAngMin: Minimum angular velocity of emitted particles.
* Number, default -2.</li>
* <li>velAngMax: Maximum angular velocity of emitted particles.
* Number, default 2.</li>
* <li>velConstrainRadial: Whether to constrain velocities
* of particles emitted using "radial" velocity to the edge
* of a circle, ensuring they all have the same magnitude.
* Number, default false.</li>
* <li>velConstrainRect: Whether to constrain velocities
* of particles emitted using "rectangle" velocity to the edge
* of the rectangle. Number, default false.</li>
* <li>velHeight: Height of velocity vectors created by
* "rectangle" type emitter. Number, default 100.</li>
* <li>velLength: Range of lengths of velocity vectors
* created by "line" type emitter. Number, default 30.</li>
* <li>velOffsetX: Offset applied to all velocity vectors.
* Number, default 0.</li>
* <li>velOffsetY: Offset applied to all velocity vectors.
* Number, default 0.</li>
* <li>velRadialStart: Low angle of arc describing
* velocity vectors created with "radial" type velocity.
* Number, default 0.</li>
* <li>velRadialEnd: High angle of arc describing
* velocity vectors created with "radial" type velocity.
* Number, default 6.283185307179586.</li>
* <li>velRadius: Radius of velocity vectors created
* using "radial" type velocity. Number, default 100.</li>
* <li>velShape: Shape of velocity vector creator.
* String, default "line".
* <ul>
* <li>"center"</li>
* <li>"radial"</li>
* <li>"rectangle"</li>
* <li>"line"</li>
* </ul></li>
* <li>velWidth: Width of velocity vectors created by
* "rectangle" type emitter. Number, default 100.</li>
* </ul>
* @property config
* @type Object
* @public
*/
this.config = this.buildDefaultConfig();
/**
* Used to determine how to scale particles when the stage changes.
* @property _stageScale
* @type Kiwi.Geom.Point
* @default ( 1, 1 )
* @private
* @since 1.1.1
*/
this._stageScale = new Kiwi.Geom.Point( 1, 1 );
/**
* Indicates whether the object has been altered
* and is in need of update.
* @property dirty
* @type Boolean
* @public
* @since 1.1.2
*/
this.dirty = false;
/**
* Color at stop 0 (start)
* @property color0
* @type Kiwi.Utils.Color
* @private
* @since 1.2.0
*/
this._color0 = new Kiwi.Utils.Color();
/**
* Color at stop 1
* @property color1
* @type Kiwi.Utils.Color
* @private
* @since 1.2.0
*/
this._color1 = new Kiwi.Utils.Color();
/**
* Color at stop 2
* @property color2
* @type Kiwi.Utils.Color
* @private
* @since 1.2.0
*/
this._color2 = new Kiwi.Utils.Color();
/**
* Color at stop 3 (end)
* @property color3
* @type Kiwi.Utils.Color
* @private
* @since 1.2.0
*/
this._color3 = new Kiwi.Utils.Color();
/**
* Timer used to schedule particle cessation
* @property _timer
* @type Kiwi.Time.Timer
* @private
*/
this._timer = null;
/**
* Matrix used to compute angles during rendering.
* This is a scratch value and has no other meaning.
*
* @property _deriveAngleTransformMatrix
* @type Kiwi.Geom.Matrix
* @private
*/
this._deriveAngleTransformMatrix = new Kiwi.Geom.Matrix();
/**
* Matrix used to compute angles during rendering.
* This is a scratch value and has no other meaning.
*
* @property _deriveAngleOffsetMatrix
* @type Kiwi.Geom.Matrix
* @private
*/
this._deriveAngleOffsetMatrix = new Kiwi.Geom.Matrix();
this.clock = clock;
this.randoms = function() {
var arr = [];
for ( i =0; i < 5000; i++ ) {
arr.push( Math.random() );
}
return arr;
}();
if ( typeof x === "undefined" ) {
x = 0;
}
if ( typeof y === "undefined" ) {
y = 0; }
if( typeof config !== "undefined" ) {
this.mergeConfig( this.config, config );
}
if ( this.game.renderOption === Kiwi.RENDERER_WEBGL ) {
this.glRenderer = this.game.renderer.requestRendererInstance(
"StatelessParticleRenderer",
{
config: this.config,
gameObject: this
} );
}
if ( typeof atlas === "undefined" ) {
console.error( "A Texture Atlas was not passed when " +
"instantiating a new StatelessParticles." );
this.visible = false;
this.active = false;
return;
}
//Set coordinates and texture
this.atlas = atlas;
this.cellIndex = this.atlas.cellIndex;
this.width = atlas.cells[ 0 ].w;
this.height = atlas.cells[ 0 ].h;
this.transform.rotPointX = 0;
this.transform.rotPointY = 0;
this.box = this.components.add(
new Kiwi.Components.Box( this, x, y, this.width, this.height) );
// Hide object until it is fully initialised by startEmitting
this.visible = false;
},
/**
* Sets a color stop using Kiwi.Utils.Color terminology.
* Specify any valid Color arguments after the index.
* You may set alpha in this way.
* @method setColor
* @param index {number} Index of the color stop: 0-3.
* @public
* @since 1.2.0
*/
setColor: function( index ) {
// var args = arguments;//.slice( 1 );
// Kiwi.Log.log( "Arguments", "#debug", arguments);
var i,
args = [];
for ( i in arguments ) {
args[ i ] = arguments[ i ];
}
args = args.slice( 1 );
switch( index ) {
case 0:
this._color0.set.apply( this._color0, args );
this.setConfigProp( "colEnv0",
[ this._color0.r, this._color0.g, this._color0.b ],
true );
this.setConfigProp( "alphaGradient",
[ this._color0.a,
this.config.alphaGradient[1],
this.config.alphaGradient[2],
this.config.alphaGradient[3] ],
true );
break;
case 1:
this._color1.set.apply( this._color1, args );
this.setConfigProp( "colEnv1",
[ this._color1.r, this._color1.g, this._color1.b ],
true );
this.setConfigProp( "alphaGradient",
[ this.configalphaGradient[0],
this._color1.a,
this.config.alphaGradient[2],
this.config.alphaGradient[3] ],
true );
break;
case 2:
this._color2.set.apply( this._color2, args );
this.setConfigProp( "colEnv2",
[ this._color2.r, this._color2.g, this._color2.b ],
true );
this.setConfigProp( "alphaGradient",
[ this.config.alphaGradient[0],
this.config.alphaGradient[1],
this._color2.a,
this.config.alphaGradient[3] ],
true );
break;
case 3:
this._color3.set.apply( this._color3, args );
this.setConfigProp( "colEnv3",
[ this._color3.r, this._color3.g, this._color3.b ],
true );
this.setConfigProp( "alphaGradient",
[ this.config.alphaGradient[0],
this.config.alphaGradient[1],
this.config.alphaGradient[2],
this._color3.a ],
true );
break;
}
},
/**
* Returns a color from this object. Note that this color will not
* update the particles if you change it; you must use "setColor".
* @method getColor
* @param index {number} Position of color stop, 0-3
* @return Kiwi.Utils.Color
* @public
* @since 1.2.0
*/
getColor: function( index ) {
switch( index ) {
case 1:
this._color1.set(
this.config.colEnv1[0],
this.config.colEnv1[1],
this.config.colEnv1[2],
this.config.alphaGradient[1] );
return this._color1;
case 2:
this._color2.set(
this.config.colEnv2[0],
this.config.colEnv2[1],
this.config.colEnv2[2],
this.config.alphaGradient[2] );
return this._color2;
case 3:
this._color3.set(
this.config.colEnv3[0],
this.config.colEnv3[1],
this.config.colEnv3[2],
this.config.alphaGradient[3] );
return this._color3;
default:
this._color0.set(
this.config.colEnv0[0],
this.config.colEnv0[1],
this.config.colEnv0[2],
this.config.alphaGradient[0] );
return this._color0;
}
},
/**
* Populates a new object with default config parameters
* @method buildDefaultConfig
* @return {object}
* @public
*/
buildDefaultConfig: function()
{
return {
"additive": false,
"alpha": 1,
"alphaGradient": [
1,
1,
1,
0
],
"alphaStops": [
0.3,
0.7
],
"angStartMin": 0,
"angStartMax": 0,
"angVelocityConform": false,
"numParts": 20,
"colEnv0": [
1,
0,
0
],
"colEnv1": [
1,
1,
0
],
"colEnv2": [
1,
1,
1
],
"colEnv3": [
0,
0,
0
],
"colEnvKeyframes": [
0.5,
0.6
],
"endSize": 150,
"gravityX": 0,
"gravityY": -50,
"loop": true,
"maxLifespan": 5,
"maxStartTime": 6,
"maxVel": 100,
"minLifespan": 3,
"minStartTime": 1,
"minVel": 70,
"posAngle": 0,
"posConstrainRadial": true,
"posConstrainRect": true,
"posHeight": 100,
"posLength": 100,
"posOffsetX": 0,
"posOffsetY": 0,
"posRadialStart": 4.363323129985823,
"posRadialEnd": 5.061454830783556,
"posRadius": 50,
"posShape": "radial",
"posWidth": 100,
"startSize": 4,
"velAngle": 0,
"velAngMin": -2,
"velAngMax": +2,
"velConstrainRadial": false,
"velConstrainRect": false,
"velHeight": 100,
"velLength": 30,
"velOffsetX": 0,
"velOffsetY": 0,
"velRadialStart": 0,
"velRadialEnd": 6.283185307179586,
"velRadius": 100,
"velShape": "line",
"velWidth": 100
};
},
/**
* Merges config objects, overwriting the first config with all
* definitions in the second while preserving non-revised terms.
* @method mergeConfig
* @param config1 {object} Config object to modify
* @param config2 {object} Config object to copy in
* @public
*/
mergeConfig: function( config1, config2 ) {
var i;
for ( i in config2 ) {
config1[ i ] = this.forceNumeric( config2[ i ] );
}
},
/**
* Recursively forces anything that can be a number
* to be a number, including array members.
* @method forceNumeric
* @param value {any} A value to force
* @return {any} The value
* @public
* @since 1.2.0
*/
forceNumeric: function( value ) {
var i, num;
if ( typeof value === "string" ) {
num = +value;
if ( !isNaN( num ) ) {
return num;
}
}
if ( Kiwi.Utils.Common.isArray( value ) ) {
for ( i in value ) {
value[ i ] = this.forceNumeric( value[ i ] );
}
}
return value;
},
/**
* Returns the state of the particle effect.
* Either "stopped","started" or "stopping"
* @property state
* @type boolean
* @readonly
* @public
*/
effectState : "stopped",
/**
* The type of object that this is.
* @method objType
* @return string
* @public
*/
objType : function() {
return "StatelessParticles";
},
/**
* An array of vectors that conatains generated velocities
* if useDrawingVectors is true. Used by the particle editor.
* @property drawingVectors
* @type array
* @private
*/
drawingVectors : [],
/**
* If true, velocity vectors will be stored on particle generation.
* Used by the particle editor.
* @property useDrawingVectors
* @type boolean
* @private
*/
useDrawingVectors : false,
/**
* A function delegated to return a random number.
* Used by the particle editor.
* @property rnd
* @type function
* @private
*/
rnd : null,
/**
* If useRandoms is true, This array will contain pregenerate random
* numbers which will be used every new generation refresh.
* Used by the particle editor.
* @property randoms
* @type array
* @private
*/
randoms : [],
/**
* If true, pregenerate random numbers. Used by the particle editor.
* @property useRandoms
* @type boolean
* @private
*/
useRandoms : false,
/**
* The number of random numbers to generate if useRandoms is true.
* @property numRandoms
* @type number
* @private
*/
numRandoms: 5000,
/**
* The index of the next random number in useRandoms.
* Used by the particle editor.
* @property nextRandomIndex
* @type number
* @private
*/
nextRandomIndex : -1,
/**
* The maximum loop length of the system. Used for calculating
* the timeout when stopping emission. This is calculated when
* the particles are generated. It can be overridden once
* the emission has started.
* @property timeoutLength
* @type number
* @public
*/
timeoutLength:0,
/**
* Get the next random number from the randoms list.
* Used by the particle editor.
* @method nextRandom
* @return number
* @private
*/
nextRandom : function() {
this.nextRandomIndex++;
if (this.nextRandomIndex >= this.numRandoms) {
this.nextRandomIndex = -1;
}
return this.randoms[ this.nextRandomIndex ];
},
/**
* Starts the system emitting particles.
* Particles will be regenerated each time.
* @method startEmitting
* @param loop {boolean} Set to true for continuous looping.
* Overrides and updates the config loop setting.
* @param removeOnComplete {boolean} If not looping, then
* the gameobject will destroy itself after one full emission cycle.
* @param numParts {number} Number of particles to generate,
* set on the config object - if not provided
* the current config value will be used
* @public
*/
startEmitting : function( loop, removeOnComplete, numParts ) {
if ( typeof loop === "undefined" ) {
loop = true;
}
if ( typeof removeOnComplete === "undefined" ) {
removeOnComplete = false;
}
if ( typeof numParts === "undefined" ) {
numParts = this.config.numParts;
}
this.config.numParts = numParts;
this.config.loop = loop;
this.glRenderer.resetTime();
this.glRenderer.resetPauseTime();
this.setConfig( this.config, true, true );
if ( !loop && removeOnComplete ) {
this.scheduleStop( this.timeoutLength * 1000, true );
}
this.effectState = "started";
this.visible = true;
this.clock.removeTimer( this._timer );
},
/**
* Stops the system from emitting particles.
* @method stopEmitting
* @param immediate {boolean} Stops the emitter
* and removes any existing particles.
* @param remove {boolean} If true the gameobject will mark itself
* for removal either immediately, or after a completed cycle.
* @public
*/
stopEmitting : function( immediate, remove ) {
if ( typeof immediate === "undefined") {
immediate = false;
}
if ( typeof remove === "undefined") {
remove = false;
}
if ( this.effectState === "started" ) {
if ( immediate && remove ) {
this.remove();
} else if ( immediate && !remove ) {
this.effectState = "stopped";
this.visible = false;
} else if ( !immediate && !remove ) {
this.glRenderer.pause();
this.effectState = "stopping";
this.scheduleStop( this.timeoutLength * 1000, false );
} else if ( !immediate && remove ) {
this.config.loop = false;
this.scheduleStop( this.timeoutLength * 1000, true );
}
}
},
/**
* Schedules the particle effect to stop (discontinue rendering),
* and optionally marks the gameobject for removal.
* @method scheduleStop
* @param milliseconds {number} Delay time in milliseconds
* before being marked for removal.
* @param remove {boolean} Mark the gameobject for removal.
* @public
*/
scheduleStop: function( milliseconds, remove ) {
var that = this;
this.clock.removeTimer( this._timer );
this._timer = this.clock.setTimeout( function() {
that.effectState = "stopped";
that.visible = false;
if ( remove ) {
that.remove.call( that );
}
}, milliseconds );
},
/**
* Immediately marks the gameobject for removal.
* @method remove
* @public
*/
remove : function() {
this.glRenderer.destroy();
this.exists = false;
},
/**
* Sets the configuration object and optionally regenerates particles
* and sets runtime properties.
* @method setConfig
* @param config {object} New configuration object
* @param doGenerate {boolean} Immediately regenerate particles
* @param doUniforms {boolean} Apply runtime properties.
* Deprecated: would apply runtime properties before rendering,
* and this parameter only served to create errors.
* @public
*/
setConfig : function( config, doGenerate, doUniforms ) {
this.config = config;
this.glRenderer.setConfig( config );
if ( doGenerate ) {
this._generateParticles();
}
this.dirty = true;
},
/**
* Sets a property on the configuration object and optionally
* regenerates particles and sets runtime properties.
* @method setConfigProp
* @param prop {string} Name of the property to set
* @param val {any} Value of the property to set
* @param doGenerate {boolean} Immediately regenerate particles
* @param doUniforms {boolean} Apply runtime properties.
* Deprecated: would apply runtime properties before rendering,
* and this parameter only served to create errors.
* @public
*/
setConfigProp : function( prop, val, doGenerate, doUniforms ) {
this.config[ prop ] = val;
this.setConfig( this.config, doGenerate, doUniforms );
},
/**
* Gets the configuration object.
* To change it, use setConfig or setConfigProp.
* @method getConfig
* @return {object}
* @public
*/
getConfig : function() {
return this.config;
},
/**
* Generates particles based on configuration object.
* @method _generateParticles
* @private
*/
_generateParticles : function() {
var angDiff, cell, cellIndex, diff, direction, i, lifespan,
magnitude, numCells, pos, posSeed, startAng, startTime,
vel, velAng, velSeed,
cfg = this.config,
vertexItems = [];
this.nextRandomIndex = -1;
this.drawingVectors = [];
if ( this.useRandoms ) {
this.rnd = this.nextRandom;
} else {
this.rnd = Math.random;
}
for ( i = 0; i < cfg.numParts; i++ ) {
// Calculate pos
pos = { x: 0, y: 0 };
posSeed = { x: 0, y: 0 };
vel = { x: cfg.velOffsetX, y: cfg.velOffsetY };
velSeed = { x: 0, y: 0 };
switch ( cfg.posShape ) {
case "radial":
if ( cfg.posRandomRadial ) {
posSeed = ( cfg.posConstrainRadial ) ?
this.randomPointCirclePerimeter(
cfg.posRadialStart, cfg.posRadialEnd ) :
this.randomPointCircle(
cfg.posRadialStart, cfg.posRadialEnd );
} else {
posSeed = ( cfg.posConstrainRadial ) ?
this.regularPointCirclePerimeter(
cfg.posRadialStart, cfg.posRadialEnd,
i, cfg.numParts - 1 ) :
this.randomPointCircle(
cfg.posRadialStart, cfg.posRadialEnd );
}
pos.x = posSeed.x * cfg.posRadius;
pos.y = posSeed.y * cfg.posRadius;
break;
case "rectangle":
posSeed = cfg.posConstrainRect ?
this.randomPointRectPerimeter() :
this.randomPointRect();
pos.x += posSeed.x * cfg.posWidth;
pos.y += posSeed.y * cfg.posHeight;
break;
case "line":
if ( cfg.posRandomLine ) {
posSeed = this.randomPointLine( cfg.posAngle );
} else {
posSeed = this.regularPointLine(
cfg.posAngle, i, cfg.numParts - 1 );
}
pos.x += posSeed.x * cfg.posLength;
pos.y += posSeed.y * cfg.posLength;
break;
case "point" :
break;
}
switch (cfg.velShape) {
case "center":
direction = posSeed;
magnitude = cfg.minVel + this.rnd() *
(cfg.maxVel - cfg.minVel);
vel.x = direction.x * magnitude ;
vel.y = direction.y * magnitude ;
break;
case "radial":
if ( cfg.velRandomRadial ) {
velSeed = cfg.velConstrainRadial ?
this.randomPointCirclePerimeter(
cfg.velRadialStart, cfg.velRadialEnd ) :
this.randomPointCircle(
cfg.velRadialStart, cfg.velRadialEnd );
} else {
velSeed = cfg.velConstrainRadial ?
this.regularPointCirclePerimeter(
cfg.velRadialStart, cfg.velRadialEnd,
i, cfg.numParts - 1 ) :
this.randomPointCircle(
cfg.velRadialStart, cfg.velRadialEnd );
}
vel.x += velSeed.x * cfg.velRadius;
vel.y += velSeed.y * cfg.velRadius;
break;
case "rectangle":
velSeed = cfg.velConstrainRect ?
this.randomPointRectPerimeter() :
this.randomPointRect();
vel.x += velSeed.x * cfg.velWidth;
vel.y += velSeed.y * cfg.velHeight;
break;
case "line":
if ( cfg.velRandomLine ) {
velSeed = this.randomPointLine( cfg.velAngle );
} else {
velSeed = this.regularPointLine(
cfg.velAngle, i, cfg.numParts - 1 );
}
vel.x += velSeed.x * cfg.velLength;
vel.y += velSeed.y * cfg.velLength;
break;
case "point":
break;
}
// Angular velocity
diff = Math.max( cfg.velAngMax, cfg.velAngMin ) -
Math.min( cfg.velAngMax, cfg.velAngMin );
velAng = cfg.velAngMin + this.rnd() * diff;
// Angular spawn parameters
angDiff = Math.abs( cfg.angStartMax - cfg.angStartMin );
startAng = cfg.angStartMin + this.rnd() * angDiff;
if ( cfg.angVelocityConform ) {
// Base angle is based on velocity vector
startAng += Math.atan2( vel.y, vel.x );
}
pos.x += cfg.posOffsetX;
pos.y += cfg.posOffsetY;
vertexItems.push( pos.x, pos.y, vel.x, vel.y );
this.drawingVectors.push(
{ x: pos.x, y: pos.y, vx: vel.x, vy: vel.y } );
startTime = cfg.minStartTime + this.rnd() *
( cfg.maxStartTime - cfg.minStartTime );
lifespan = cfg.minLifespan + this.rnd() *
( cfg.maxLifespan - cfg.minLifespan );
this.timeoutLength =
Math.max( this.timeoutLength, startTime + lifespan );
cellIndex = 0;
if ( cfg.cells ) {
numCells = cfg.cells.length;
if ( numCells > 1 ) {
cellIndex =
cfg.cells[ Math.floor( this.rnd() * numCells ) ];
} else {
cellIndex = cfg.cells[ 0 ];
}
}
vertexItems.push(
startTime,
lifespan,
velAng,
startAng );
cell = this.atlas.cells[ cellIndex ];
vertexItems.push( cell.x, cell.y, cell.w, cell.h );
}
this.glRenderer.initBatch( vertexItems );
},
/**
* Instructs the renderer to draw the particles.
* @method renderGL
* @private