2017-10-17 04:16:08 +01:00
|
|
|
var Class = require('../../utils/Class');
|
2017-10-17 21:32:45 +01:00
|
|
|
var DegToRad = require('../../math/DegToRad');
|
2017-10-20 18:49:45 +01:00
|
|
|
var FloatBetween = require('../../math/FloatBetween');
|
2017-10-17 04:16:08 +01:00
|
|
|
|
|
|
|
var Particle = new Class({
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2017-10-18 15:18:42 +01:00
|
|
|
function Particle ()
|
2017-10-17 04:16:08 +01:00
|
|
|
{
|
|
|
|
// Phaser.Texture.Frame
|
2017-10-18 15:18:42 +01:00
|
|
|
this.frame = null;
|
2017-10-17 04:16:08 +01:00
|
|
|
|
|
|
|
this.index = 0;
|
|
|
|
|
2017-10-18 15:18:42 +01:00
|
|
|
this.x = 0;
|
|
|
|
this.y = 0;
|
2017-10-17 04:16:08 +01:00
|
|
|
|
|
|
|
this.velocityX = 0;
|
|
|
|
this.velocityY = 0;
|
|
|
|
|
|
|
|
this.scaleX = 1;
|
|
|
|
this.scaleY = 1;
|
|
|
|
|
2017-10-18 02:26:15 +01:00
|
|
|
this.alpha = 1;
|
|
|
|
|
2017-10-20 18:49:45 +01:00
|
|
|
// degs
|
|
|
|
this.angle = 0;
|
|
|
|
|
2017-10-18 02:26:15 +01:00
|
|
|
// rads
|
2017-10-17 04:16:08 +01:00
|
|
|
this.rotation = 0;
|
|
|
|
|
|
|
|
this.scrollFactorX = 1;
|
|
|
|
this.scrollFactorY = 1;
|
|
|
|
|
2017-10-18 02:26:15 +01:00
|
|
|
this.color = 0xffffffff;
|
|
|
|
|
|
|
|
// in ms
|
|
|
|
this.life = 1000;
|
|
|
|
this.lifeCurrent = 1000;
|
2017-10-17 04:16:08 +01:00
|
|
|
|
2017-10-18 02:26:15 +01:00
|
|
|
// ease data
|
|
|
|
this.data = {
|
|
|
|
tint: { min: 0xffffff, max: 0xffffff, current: 0xffffff },
|
2017-10-20 01:49:03 +01:00
|
|
|
alpha: { min: 1, max: 1, calc: 0 },
|
|
|
|
angle: { min: 0, max: 0, calc: 0 },
|
|
|
|
scaleX: { min: 1, max: 1, calc: 0 },
|
|
|
|
scaleY: { min: 1, max: 1, calc: 0 }
|
2017-10-17 04:16:08 +01:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
isAlive: function ()
|
|
|
|
{
|
2017-10-18 02:26:15 +01:00
|
|
|
return (this.lifeCurrent > 0);
|
2017-10-17 04:16:08 +01:00
|
|
|
},
|
|
|
|
|
2017-10-17 21:32:45 +01:00
|
|
|
emit: function (emitter)
|
2017-10-17 04:16:08 +01:00
|
|
|
{
|
2017-10-18 15:18:42 +01:00
|
|
|
this.frame = emitter.getFrame();
|
2017-10-18 02:26:15 +01:00
|
|
|
|
2017-10-18 15:18:42 +01:00
|
|
|
if (emitter.zone)
|
|
|
|
{
|
|
|
|
emitter.zone.getRandomPoint(this);
|
|
|
|
}
|
2017-10-18 02:26:15 +01:00
|
|
|
|
2017-10-18 15:18:42 +01:00
|
|
|
this.x += emitter.x;
|
|
|
|
this.y += emitter.y;
|
2017-10-18 02:26:15 +01:00
|
|
|
|
2017-10-18 15:18:42 +01:00
|
|
|
var sx = emitter.speed.getRandomX();
|
|
|
|
var sy = emitter.speed.getRandomY();
|
2017-10-17 21:32:45 +01:00
|
|
|
|
2017-10-18 15:18:42 +01:00
|
|
|
if (emitter.radial)
|
2017-10-18 02:26:15 +01:00
|
|
|
{
|
2017-10-20 18:49:45 +01:00
|
|
|
var rad = DegToRad(emitter.emitterAngle.getRandom());
|
2017-10-18 15:18:42 +01: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 02:26:15 +01:00
|
|
|
}
|
2017-10-17 21:32:45 +01:00
|
|
|
|
2017-10-18 02:26:15 +01:00
|
|
|
this.life = emitter.lifespan.getRandom();
|
|
|
|
this.lifeCurrent = this.life;
|
2017-10-17 21:32:45 +01:00
|
|
|
|
2017-10-18 02:26:15 +01:00
|
|
|
// eased values
|
2017-10-20 18:49:45 +01:00
|
|
|
|
|
|
|
var dataScaleX = this.data.scaleX;
|
|
|
|
var dataScaleY = this.data.scaleY;
|
|
|
|
var dataAngle = this.data.angle;
|
|
|
|
var dataAlpha = this.data.alpha;
|
|
|
|
|
|
|
|
emitter.scale.copyXToMinMax(dataScaleX);
|
|
|
|
emitter.scale.copyYToMinMax(dataScaleY);
|
|
|
|
emitter.angle.copyToMinMax(dataAngle);
|
|
|
|
emitter.alpha.copyToMinMax(dataAlpha);
|
|
|
|
|
|
|
|
// Random overrides
|
|
|
|
|
|
|
|
if (emitter.randomScaleX)
|
|
|
|
{
|
|
|
|
var randomScaleX = FloatBetween(emitter.randomScaleX[0], emitter.randomScaleX[1]);
|
|
|
|
|
|
|
|
// If there is no current ease value set we override them both
|
|
|
|
if (dataScaleX.min === dataScaleX.max)
|
|
|
|
{
|
|
|
|
dataScaleX.min = randomScaleX;
|
|
|
|
dataScaleX.max = randomScaleX;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Otherwise we just reset the start value, so it still eases to the end value
|
|
|
|
dataScaleX.min = randomScaleX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (emitter.randomScaleY)
|
|
|
|
{
|
|
|
|
var randomScaleY = FloatBetween(emitter.randomScaleY[0], emitter.randomScaleY[1]);
|
|
|
|
|
|
|
|
// If there is no current ease value set we override them both
|
|
|
|
if (dataScaleY.min === dataScaleY.max)
|
|
|
|
{
|
|
|
|
dataScaleY.min = randomScaleY;
|
|
|
|
dataScaleY.max = randomScaleY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Otherwise we just reset the start value, so it still eases to the end value
|
|
|
|
dataScaleY.min = randomScaleY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (emitter.randomAngle)
|
|
|
|
{
|
|
|
|
var randomAngle = FloatBetween(emitter.randomAngle[0], emitter.randomAngle[1]);
|
|
|
|
|
|
|
|
// If there is no current ease value set we override them both
|
|
|
|
if (dataAngle.min === dataAngle.max)
|
|
|
|
{
|
|
|
|
dataAngle.min = randomAngle;
|
|
|
|
dataAngle.max = randomAngle;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Otherwise we just reset the start value, so it still eases to the end value
|
|
|
|
dataAngle.min = randomAngle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (emitter.randomAlpha)
|
|
|
|
{
|
|
|
|
var randomAlpha = FloatBetween(emitter.randomAlpha[0], emitter.randomAlpha[1]);
|
|
|
|
|
|
|
|
// If there is no current ease value set we override them both
|
|
|
|
if (dataAlpha.min === dataAlpha.max)
|
|
|
|
{
|
|
|
|
dataAlpha.min = randomAlpha;
|
|
|
|
dataAlpha.max = randomAlpha;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Otherwise we just reset the start value, so it still eases to the end value
|
|
|
|
dataAlpha.min = randomAlpha;
|
|
|
|
}
|
|
|
|
}
|
2017-10-20 01:49:03 +01:00
|
|
|
|
|
|
|
// Pre-calc ease values
|
2017-10-20 18:49:45 +01:00
|
|
|
dataScaleX.calc = dataScaleX.max - dataScaleX.min;
|
|
|
|
dataScaleY.calc = dataScaleY.max - dataScaleY.min;
|
|
|
|
dataAngle.calc = dataAngle.max - dataAngle.min;
|
|
|
|
dataAlpha.calc = dataAlpha.max - dataAlpha.min;
|
2017-10-20 01:49:03 +01:00
|
|
|
|
|
|
|
// Set initial values
|
2017-10-20 18:49:45 +01:00
|
|
|
this.scaleX = dataScaleX.min;
|
|
|
|
this.scaleY = dataScaleY.min;
|
|
|
|
this.angle = dataAngle.min;
|
|
|
|
this.rotation = DegToRad(dataAngle.min);
|
|
|
|
|
|
|
|
this.alpha = dataAlpha.min;
|
2017-10-20 01:49:03 +01:00
|
|
|
this.color = (this.color & 0x00FFFFFF) | (((this.alpha * 0xFF) | 0) << 24);
|
2017-10-17 21:32:45 +01:00
|
|
|
|
|
|
|
this.index = emitter.alive.length;
|
2017-10-17 04:16:08 +01:00
|
|
|
},
|
|
|
|
|
2017-10-18 02:26:15 +01:00
|
|
|
// delta = ms, step = delta / 1000
|
|
|
|
update: function (emitter, delta, step)
|
2017-10-17 04:16:08 +01:00
|
|
|
{
|
2017-10-18 02:26:15 +01:00
|
|
|
// How far along in life is this particle? (t = 0 to 1)
|
2017-10-20 01:49:03 +01:00
|
|
|
var t = 1 - (this.lifeCurrent / this.life);
|
2017-10-17 21:32:45 +01:00
|
|
|
|
|
|
|
this.velocityX += (emitter.gravity.x * step);
|
|
|
|
this.velocityY += (emitter.gravity.y * step);
|
|
|
|
|
|
|
|
this.x += this.velocityX * step;
|
|
|
|
this.y += this.velocityY * step;
|
|
|
|
|
2017-10-20 18:49:45 +01:00
|
|
|
var data = this.data;
|
|
|
|
|
|
|
|
this.scaleX = data.scaleX.calc * emitter.easingFunctionScale(t) + data.scaleX.min;
|
|
|
|
this.scaleY = data.scaleY.calc * emitter.easingFunctionScale(t) + data.scaleY.min;
|
2017-10-20 01:49:03 +01:00
|
|
|
|
2017-10-20 18:49:45 +01:00
|
|
|
this.angle = data.angle.calc * emitter.easingFunctionRotation(t) + data.angle.min;
|
|
|
|
this.rotation = DegToRad(this.angle);
|
2017-10-18 02:26:15 +01:00
|
|
|
|
2017-10-20 18:49:45 +01:00
|
|
|
this.alpha = data.alpha.calc * emitter.easingFunctionAlpha(t) + data.alpha.min;
|
2017-10-18 02:26:15 +01:00
|
|
|
|
2017-10-20 01:49:03 +01:00
|
|
|
this.color = (this.color & 0x00FFFFFF) | (((this.alpha * 0xFF) | 0) << 24);
|
2017-10-18 02:26:15 +01:00
|
|
|
|
|
|
|
this.lifeCurrent -= delta;
|
2017-10-17 21:32:45 +01:00
|
|
|
|
2017-10-18 02:26:15 +01:00
|
|
|
return (this.lifeCurrent <= 0);
|
2017-10-17 04:16:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = Particle;
|