forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticle.js
More file actions
563 lines (480 loc) · 15.1 KB
/
Copy pathParticle.js
File metadata and controls
563 lines (480 loc) · 15.1 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
/**
* @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');
var DegToRad = require('../../math/DegToRad');
var DistanceBetween = require('../../math/distance/DistanceBetween');
/**
* @classdesc
* A Particle is a simple Game Object controlled by a Particle Emitter and Manager, and rendered by the Manager.
* It uses its own lightweight physics system, and can interact only with its Emitter's bounds and zones.
*
* @class Particle
* @memberOf Phaser.GameObjects.Particles
* @constructor
* @since 3.0.0
*
* @param {Phaser.GameObjects.Particles.ParticleEmitter} emitter - The Emitter to which this Particle belongs.
*/
var Particle = new Class({
initialize:
function Particle (emitter)
{
/**
* The Emitter to which this Particle belongs.
*
* A Particle can only belong to a single Emitter and is created, updated and destroyed via it.
*
* @name Phaser.GameObjects.Particles.Particle#emitter
* @type {Phaser.GameObjects.Particles.ParticleEmitter}
* @since 3.0.0
*/
this.emitter = emitter;
/**
* The texture frame used to render this Particle.
*
* @name Phaser.GameObjects.Particles.Particle#frame
* @type {Phaser.Textures.Frame}
* @default null
* @since 3.0.0
*/
this.frame = null;
/**
* The position of this Particle within its Emitter's particle pool.
*
* @name Phaser.GameObjects.Particles.Particle#index
* @type {number}
* @default 0
* @since 3.0.0
*/
this.index = 0;
/**
* The x coordinate of this Particle.
*
* @name Phaser.GameObjects.Particles.Particle#x
* @type {number}
* @default 0
* @since 3.0.0
*/
this.x = 0;
/**
* The y coordinate of this Particle.
*
* @name Phaser.GameObjects.Particles.Particle#y
* @type {number}
* @default 0
* @since 3.0.0
*/
this.y = 0;
/**
* The x velocity of this Particle.
*
* @name Phaser.GameObjects.Particles.Particle#velocityX
* @type {number}
* @default 0
* @since 3.0.0
*/
this.velocityX = 0;
/**
* The y velocity of this Particle.
*
* @name Phaser.GameObjects.Particles.Particle#velocityY
* @type {number}
* @default 0
* @since 3.0.0
*/
this.velocityY = 0;
/**
* The x acceleration of this Particle.
*
* @name Phaser.GameObjects.Particles.Particle#accelerationX
* @type {number}
* @default 0
* @since 3.0.0
*/
this.accelerationX = 0;
/**
* The y acceleration of this Particle.
*
* @name Phaser.GameObjects.Particles.Particle#accelerationY
* @type {number}
* @default 0
* @since 3.0.0
*/
this.accelerationY = 0;
/**
* The maximum horizontal velocity this Particle can travel at.
*
* @name Phaser.GameObjects.Particles.Particle#maxVelocityX
* @type {number}
* @default 10000
* @since 3.0.0
*/
this.maxVelocityX = 10000;
/**
* The maximum vertical velocity this Particle can travel at.
*
* @name Phaser.GameObjects.Particles.Particle#maxVelocityY
* @type {number}
* @default 10000
* @since 3.0.0
*/
this.maxVelocityY = 10000;
/**
* The bounciness, or restitution, of this Particle.
*
* @name Phaser.GameObjects.Particles.Particle#bounce
* @type {number}
* @default 0
* @since 3.0.0
*/
this.bounce = 0;
/**
* The horizontal scale of this Particle.
*
* @name Phaser.GameObjects.Particles.Particle#scaleX
* @type {number}
* @default 1
* @since 3.0.0
*/
this.scaleX = 1;
/**
* The vertical scale of this Particle.
*
* @name Phaser.GameObjects.Particles.Particle#scaleY
* @type {number}
* @default 1
* @since 3.0.0
*/
this.scaleY = 1;
/**
* The alpha value of this Particle.
*
* @name Phaser.GameObjects.Particles.Particle#alpha
* @type {number}
* @default 1
* @since 3.0.0
*/
this.alpha = 1;
/**
* The angle of this Particle in degrees.
*
* @name Phaser.GameObjects.Particles.Particle#angle
* @type {number}
* @default 0
* @since 3.0.0
*/
this.angle = 0;
/**
* The angle of this Particle in radians.
*
* @name Phaser.GameObjects.Particles.Particle#rotation
* @type {number}
* @default 0
* @since 3.0.0
*/
this.rotation = 0;
/**
* The tint applied to this Particle.
*
* @name Phaser.GameObjects.Particles.Particle#tint
* @type {integer}
* @webglOnly
* @since 3.0.0
*/
this.tint = 0xffffff;
/**
* The lifespan of this Particle in ms.
*
* @name Phaser.GameObjects.Particles.Particle#life
* @type {number}
* @default 1000
* @since 3.0.0
*/
this.life = 1000;
/**
* The current life of this Particle in ms.
*
* @name Phaser.GameObjects.Particles.Particle#lifeCurrent
* @type {number}
* @default 1000
* @since 3.0.0
*/
this.lifeCurrent = 1000;
/**
* The delay applied to this Particle upon emission, in ms.
*
* @name Phaser.GameObjects.Particles.Particle#delayCurrent
* @type {number}
* @default 0
* @since 3.0.0
*/
this.delayCurrent = 0;
/**
* The normalized lifespan T value, where 0 is the start and 1 is the end.
*
* @name Phaser.GameObjects.Particles.Particle#lifeT
* @type {number}
* @default 0
* @since 3.0.0
*/
this.lifeT = 0;
/**
* The data used by the ease equation.
*
* @name Phaser.GameObjects.Particles.Particle#data
* @type {object}
* @since 3.0.0
*/
this.data = {
tint: { min: 0xffffff, max: 0xffffff, current: 0xffffff },
alpha: { min: 1, max: 1 },
rotate: { min: 0, max: 0 },
scaleX: { min: 1, max: 1 },
scaleY: { min: 1, max: 1 }
};
},
/**
* Checks to see if this Particle is alive and updating.
*
* @method Phaser.GameObjects.Particles.Particle#isAlive
* @since 3.0.0
*
* @return {boolean} `true` if this Particle is alive and updating, otherwise `false`.
*/
isAlive: function ()
{
return (this.lifeCurrent > 0);
},
/**
* Starts this Particle from the given coordinates.
*
* @method Phaser.GameObjects.Particles.Particle#fire
* @since 3.0.0
*
* @param {number} x - The x coordinate to launch this Particle from.
* @param {number} y - The y coordinate to launch this Particle from.
*/
fire: function (x, y)
{
var emitter = this.emitter;
this.frame = emitter.getFrame();
if (emitter.emitZone)
{
// Updates particle.x and particle.y during this call
emitter.emitZone.getPoint(this);
}
if (x === undefined)
{
if (emitter.follow)
{
this.x += emitter.follow.x + emitter.followOffset.x;
}
this.x += emitter.x.onEmit(this, 'x');
}
else
{
this.x += x;
}
if (y === undefined)
{
if (emitter.follow)
{
this.y += emitter.follow.y + emitter.followOffset.y;
}
this.y += emitter.y.onEmit(this, 'y');
}
else
{
this.y += y;
}
this.life = emitter.lifespan.onEmit(this, 'lifespan');
this.lifeCurrent = this.life;
this.lifeT = 0;
var sx = emitter.speedX.onEmit(this, 'speedX');
var sy = (emitter.speedY) ? emitter.speedY.onEmit(this, 'speedY') : sx;
if (emitter.radial)
{
var rad = DegToRad(emitter.angle.onEmit(this, 'angle'));
this.velocityX = Math.cos(rad) * Math.abs(sx);
this.velocityY = Math.sin(rad) * Math.abs(sy);
}
else if (emitter.moveTo)
{
var mx = emitter.moveToX.onEmit(this, 'moveToX');
var my = (emitter.moveToY) ? emitter.moveToY.onEmit(this, 'moveToY') : mx;
var angle = Math.atan2(my - this.y, mx - this.x);
var speed = DistanceBetween(this.x, this.y, mx, my) / (this.life / 1000);
// We know how many pixels we need to move, but how fast?
// var speed = this.distanceToXY(displayObject, x, y) / (maxTime / 1000);
this.velocityX = Math.cos(angle) * speed;
this.velocityY = Math.sin(angle) * speed;
}
else
{
this.velocityX = sx;
this.velocityY = sy;
}
if (emitter.acceleration)
{
this.accelerationX = emitter.accelerationX.onEmit(this, 'accelerationX');
this.accelerationY = emitter.accelerationY.onEmit(this, 'accelerationY');
}
this.maxVelocityX = emitter.maxVelocityX.onEmit(this, 'maxVelocityX');
this.maxVelocityY = emitter.maxVelocityY.onEmit(this, 'maxVelocityY');
this.delayCurrent = emitter.delay.onEmit(this, 'delay');
this.scaleX = emitter.scaleX.onEmit(this, 'scaleX');
this.scaleY = (emitter.scaleY) ? emitter.scaleY.onEmit(this, 'scaleY') : this.scaleX;
this.angle = emitter.rotate.onEmit(this, 'rotate');
this.rotation = DegToRad(this.angle);
this.bounce = emitter.bounce.onEmit(this, 'bounce');
this.alpha = emitter.alpha.onEmit(this, 'alpha');
this.tint = emitter.tint.onEmit(this, 'tint');
this.index = emitter.alive.length;
},
/**
* An internal method that calculates the velocity of the Particle.
*
* @method Phaser.GameObjects.Particles.Particle#computeVelocity
* @since 3.0.0
*
* @param {Phaser.GameObjects.Particles.ParticleEmitter} emitter - The Emitter that is updating this Particle.
* @param {number} delta - The delta time in ms.
* @param {number} step - The delta value divided by 1000.
* @param {array} processors - Particle processors (gravity wells).
*/
computeVelocity: function (emitter, delta, step, processors)
{
var vx = this.velocityX;
var vy = this.velocityY;
var ax = this.accelerationX;
var ay = this.accelerationY;
var mx = this.maxVelocityX;
var my = this.maxVelocityY;
vx += (emitter.gravityX * step);
vy += (emitter.gravityY * step);
if (ax)
{
vx += (ax * step);
}
if (ay)
{
vy += (ay * step);
}
if (vx > mx)
{
vx = mx;
}
else if (vx < -mx)
{
vx = -mx;
}
if (vy > my)
{
vy = my;
}
else if (vy < -my)
{
vy = -my;
}
this.velocityX = vx;
this.velocityY = vy;
// Apply any additional processors
for (var i = 0; i < processors.length; i++)
{
processors[i].update(this, delta, step);
}
},
/**
* Checks if this Particle is still within the bounds defined by the given Emitter.
*
* If not, and depending on the Emitter collision flags, the Particle may either stop or rebound.
*
* @method Phaser.GameObjects.Particles.Particle#checkBounds
* @since 3.0.0
*
* @param {Phaser.GameObjects.Particles.ParticleEmitter} emitter - The Emitter to check the bounds against.
*/
checkBounds: function (emitter)
{
var bounds = emitter.bounds;
var bounce = -this.bounce;
if (this.x < bounds.x && emitter.collideLeft)
{
this.x = bounds.x;
this.velocityX *= bounce;
}
else if (this.x > bounds.right && emitter.collideRight)
{
this.x = bounds.right;
this.velocityX *= bounce;
}
if (this.y < bounds.y && emitter.collideTop)
{
this.y = bounds.y;
this.velocityY *= bounce;
}
else if (this.y > bounds.bottom && emitter.collideBottom)
{
this.y = bounds.bottom;
this.velocityY *= bounce;
}
},
/**
* The main update method for this Particle.
*
* Updates its life values, computes the velocity and repositions the Particle.
*
* @method Phaser.GameObjects.Particles.Particle#update
* @since 3.0.0
*
* @param {number} delta - The delta time in ms.
* @param {number} step - The delta value divided by 1000.
* @param {array} processors - An optional array of update processors.
*
* @return {boolean} Returns `true` if this Particle has now expired and should be removed, otherwise `false` if still active.
*/
update: function (delta, step, processors)
{
if (this.delayCurrent > 0)
{
this.delayCurrent -= delta;
return false;
}
var emitter = this.emitter;
// How far along in life is this particle? (t = 0 to 1)
var t = 1 - (this.lifeCurrent / this.life);
this.lifeT = t;
this.computeVelocity(emitter, delta, step, processors);
this.x += this.velocityX * step;
this.y += this.velocityY * step;
if (emitter.bounds)
{
this.checkBounds(emitter);
}
if (emitter.deathZone && emitter.deathZone.willKill(this))
{
this.lifeCurrent = 0;
// No need to go any further, particle has been killed
return true;
}
this.scaleX = emitter.scaleX.onUpdate(this, 'scaleX', t, this.scaleX);
if (emitter.scaleY)
{
this.scaleY = emitter.scaleY.onUpdate(this, 'scaleY', t, this.scaleY);
}
else
{
this.scaleY = this.scaleX;
}
this.angle = emitter.rotate.onUpdate(this, 'rotate', t, this.angle);
this.rotation = DegToRad(this.angle);
this.alpha = emitter.alpha.onUpdate(this, 'alpha', t, this.alpha);
this.tint = emitter.tint.onUpdate(this, 'tint', t, this.tint);
this.lifeCurrent -= delta;
return (this.lifeCurrent <= 0);
}
});
module.exports = Particle;