2017-10-17 03:16:08 +00:00
|
|
|
var Class = require('../../utils/Class');
|
2017-10-17 20:32:45 +00:00
|
|
|
var DegToRad = require('../../math/DegToRad');
|
2017-10-17 03:16:08 +00:00
|
|
|
|
|
|
|
var Particle = new Class({
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2017-10-21 04:05:51 +00:00
|
|
|
function Particle (emitter)
|
2017-10-17 03:16:08 +00:00
|
|
|
{
|
2017-10-21 04:05:51 +00:00
|
|
|
this.emitter = emitter;
|
|
|
|
|
2017-10-17 03:16:08 +00:00
|
|
|
// Phaser.Texture.Frame
|
2017-10-18 14:18:42 +00:00
|
|
|
this.frame = null;
|
2017-10-17 03:16:08 +00:00
|
|
|
|
|
|
|
this.index = 0;
|
|
|
|
|
2017-10-18 14:18:42 +00:00
|
|
|
this.x = 0;
|
|
|
|
this.y = 0;
|
2017-10-17 03:16:08 +00:00
|
|
|
|
|
|
|
this.velocityX = 0;
|
|
|
|
this.velocityY = 0;
|
|
|
|
|
|
|
|
this.scaleX = 1;
|
|
|
|
this.scaleY = 1;
|
|
|
|
|
2017-10-18 01:26:15 +00:00
|
|
|
this.alpha = 1;
|
|
|
|
|
2017-10-20 17:49:45 +00:00
|
|
|
// degs
|
|
|
|
this.angle = 0;
|
|
|
|
|
2017-10-18 01:26:15 +00:00
|
|
|
// rads
|
2017-10-17 03:16:08 +00:00
|
|
|
this.rotation = 0;
|
|
|
|
|
|
|
|
this.scrollFactorX = 1;
|
|
|
|
this.scrollFactorY = 1;
|
|
|
|
|
2017-10-18 01:26:15 +00:00
|
|
|
this.color = 0xffffffff;
|
|
|
|
|
|
|
|
// in ms
|
|
|
|
this.life = 1000;
|
|
|
|
this.lifeCurrent = 1000;
|
2017-10-17 03:16:08 +00:00
|
|
|
|
2017-10-18 01:26:15 +00:00
|
|
|
// ease data
|
|
|
|
this.data = {
|
|
|
|
tint: { min: 0xffffff, max: 0xffffff, current: 0xffffff },
|
2017-10-23 16:11:13 +00:00
|
|
|
alpha: { min: 1, max: 1 },
|
|
|
|
angle: { min: 0, max: 0 },
|
|
|
|
scaleX: { min: 1, max: 1 },
|
|
|
|
scaleY: { min: 1, max: 1 }
|
2017-10-17 03:16:08 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
isAlive: function ()
|
|
|
|
{
|
2017-10-18 01:26:15 +00:00
|
|
|
return (this.lifeCurrent > 0);
|
2017-10-17 03:16:08 +00:00
|
|
|
},
|
|
|
|
|
2017-10-21 04:05:51 +00:00
|
|
|
emit: function ()
|
2017-10-17 03:16:08 +00:00
|
|
|
{
|
2017-10-21 04:05:51 +00:00
|
|
|
var emitter = this.emitter;
|
|
|
|
|
2017-10-18 14:18:42 +00:00
|
|
|
this.frame = emitter.getFrame();
|
2017-10-18 01:26:15 +00:00
|
|
|
|
2017-10-18 14:18:42 +00:00
|
|
|
if (emitter.zone)
|
|
|
|
{
|
|
|
|
emitter.zone.getRandomPoint(this);
|
|
|
|
}
|
2017-10-18 01:26:15 +00:00
|
|
|
|
2017-10-23 16:11:13 +00:00
|
|
|
this.x += emitter.x.onEmit(this, 'x');
|
|
|
|
this.y += emitter.y.onEmit(this, 'y');
|
2017-10-18 01:26:15 +00:00
|
|
|
|
2017-10-23 16:11:13 +00:00
|
|
|
var sx = emitter.speedX.onEmit(this, 'speedX');
|
|
|
|
var sy = (emitter.speedY) ? emitter.speedY.onEmit(this, 'speedY') : sx;
|
2017-10-17 20:32:45 +00:00
|
|
|
|
2017-10-18 14:18:42 +00:00
|
|
|
if (emitter.radial)
|
2017-10-18 01:26:15 +00:00
|
|
|
{
|
2017-10-23 16:11:13 +00:00
|
|
|
var rad = DegToRad(emitter.emitterAngle.onEmit(this, 'angle'));
|
2017-10-18 14:18:42 +00:00
|
|
|
|
|
|
|
this.velocityX = Math.cos(rad) * Math.abs(sx);
|
|
|
|
this.velocityY = Math.sin(rad) * Math.abs(sy);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.velocityX = sx;
|
|
|
|
this.velocityY = sy;
|
2017-10-18 01:26:15 +00:00
|
|
|
}
|
2017-10-17 20:32:45 +00:00
|
|
|
|
2017-10-23 16:11:13 +00:00
|
|
|
this.life = emitter.lifespan.onEmit(this, 'lifespan');
|
2017-10-18 01:26:15 +00:00
|
|
|
this.lifeCurrent = this.life;
|
2017-10-17 20:32:45 +00:00
|
|
|
|
2017-10-23 16:11:13 +00:00
|
|
|
this.scaleX = emitter.scaleX.onEmit(this, 'scaleX');
|
|
|
|
this.scaleY = (emitter.scaleY) ? emitter.scaleY.onEmit(this, 'scaleY') : this.scaleX;
|
2017-10-20 17:49:45 +00:00
|
|
|
|
2017-10-23 16:11:13 +00:00
|
|
|
this.angle = emitter.particleAngle.onEmit(this, 'angle');
|
|
|
|
this.rotation = DegToRad(this.angle);
|
2017-10-20 00:49:03 +00:00
|
|
|
|
2017-10-23 16:11:13 +00:00
|
|
|
this.alpha = emitter.alpha.onEmit(this, 'alpha');
|
2017-10-20 17:49:45 +00:00
|
|
|
|
2017-10-20 00:49:03 +00:00
|
|
|
this.color = (this.color & 0x00FFFFFF) | (((this.alpha * 0xFF) | 0) << 24);
|
2017-10-17 20:32:45 +00:00
|
|
|
|
|
|
|
this.index = emitter.alive.length;
|
2017-10-17 03:16:08 +00:00
|
|
|
},
|
|
|
|
|
2017-10-18 01:26:15 +00:00
|
|
|
// delta = ms, step = delta / 1000
|
2017-10-21 04:05:51 +00:00
|
|
|
update: function (delta, step)
|
2017-10-17 03:16:08 +00:00
|
|
|
{
|
2017-10-21 04:05:51 +00:00
|
|
|
var emitter = this.emitter;
|
|
|
|
|
2017-10-18 01:26:15 +00:00
|
|
|
// How far along in life is this particle? (t = 0 to 1)
|
2017-10-20 00:49:03 +00:00
|
|
|
var t = 1 - (this.lifeCurrent / this.life);
|
2017-10-17 20:32:45 +00:00
|
|
|
|
2017-10-23 16:11:13 +00:00
|
|
|
this.velocityX += (emitter.gravityX * step);
|
|
|
|
this.velocityY += (emitter.gravityY * step);
|
2017-10-17 20:32:45 +00:00
|
|
|
|
|
|
|
this.x += this.velocityX * step;
|
|
|
|
this.y += this.velocityY * step;
|
|
|
|
|
2017-10-23 16:11:13 +00:00
|
|
|
this.scaleX = emitter.scaleX.onUpdate(this, 'scaleX', t, this.scaleX);
|
2017-10-20 17:49:45 +00:00
|
|
|
|
2017-10-23 16:11:13 +00:00
|
|
|
if (emitter.scaleY)
|
|
|
|
{
|
|
|
|
this.scaleY = emitter.scaleY.onUpdate(this, 'scaleY', t, this.scaleY);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.scaleY = this.scaleX;
|
|
|
|
}
|
2017-10-20 00:49:03 +00:00
|
|
|
|
2017-10-23 16:11:13 +00:00
|
|
|
this.angle = emitter.particleAngle.onUpdate(this, 'angle', t, this.angle);
|
2017-10-20 17:49:45 +00:00
|
|
|
this.rotation = DegToRad(this.angle);
|
2017-10-18 01:26:15 +00:00
|
|
|
|
2017-10-23 16:11:13 +00:00
|
|
|
this.alpha = emitter.alpha.onUpdate(this, 'alpha', t, this.alpha);
|
2017-10-18 01:26:15 +00:00
|
|
|
|
2017-10-20 00:49:03 +00:00
|
|
|
this.color = (this.color & 0x00FFFFFF) | (((this.alpha * 0xFF) | 0) << 24);
|
2017-10-18 01:26:15 +00:00
|
|
|
|
|
|
|
this.lifeCurrent -= delta;
|
2017-10-17 20:32:45 +00:00
|
|
|
|
2017-10-18 01:26:15 +00:00
|
|
|
return (this.lifeCurrent <= 0);
|
2017-10-17 03:16:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = Particle;
|