phaser/v3/src/gameobjects/particles/Particle.js

226 lines
6.3 KiB
JavaScript
Raw Normal View History

2017-10-17 03:16:08 +00:00
var Class = require('../../utils/Class');
var DegToRad = require('../../math/DegToRad');
var FloatBetween = require('../../math/FloatBetween');
2017-10-17 03:16:08 +00:00
var Particle = new Class({
initialize:
function Particle (emitter)
2017-10-17 03:16:08 +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;
// 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 },
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 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
},
emit: function ()
2017-10-17 03:16:08 +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
this.x += emitter.x.getNext();
this.y += emitter.y.getNext();
2017-10-18 01:26:15 +00:00
var sx = emitter.speedX.getNext();
var sy = emitter.speedY.getNext();
2017-10-18 14:18:42 +00:00
if (emitter.radial)
2017-10-18 01:26:15 +00:00
{
var rad = DegToRad(emitter.emitterAngle.getNext());
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
}
this.life = emitter.lifespan.getNext();
2017-10-18 01:26:15 +00:00
this.lifeCurrent = this.life;
2017-10-18 01:26:15 +00:00
// eased values
var dataScaleX = this.data.scaleX;
var dataScaleY = this.data.scaleY;
var dataAngle = this.data.angle;
var dataAlpha = this.data.alpha;
emitter.scaleX.copyToMinMax(dataScaleX);
emitter.scaleY.copyToMinMax(dataScaleY);
emitter.particleAngle.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;
}
}
// Pre-calc ease values
dataScaleX.calc = dataScaleX.max - dataScaleX.min;
dataScaleY.calc = dataScaleY.max - dataScaleY.min;
dataAngle.calc = dataAngle.max - dataAngle.min;
dataAlpha.calc = dataAlpha.max - dataAlpha.min;
// Set initial values
this.scaleX = dataScaleX.min;
this.scaleY = dataScaleY.min;
this.angle = dataAngle.min;
this.rotation = DegToRad(dataAngle.min);
this.alpha = dataAlpha.min;
this.color = (this.color & 0x00FFFFFF) | (((this.alpha * 0xFF) | 0) << 24);
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
update: function (delta, step)
2017-10-17 03:16:08 +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)
var t = 1 - (this.lifeCurrent / this.life);
this.velocityX += (emitter.gravity.x * step);
this.velocityY += (emitter.gravity.y * step);
this.x += this.velocityX * step;
this.y += this.velocityY * step;
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;
this.angle = data.angle.calc * emitter.easingFunctionRotation(t) + data.angle.min;
this.rotation = DegToRad(this.angle);
2017-10-18 01:26:15 +00:00
this.alpha = data.alpha.calc * emitter.easingFunctionAlpha(t) + data.alpha.min;
2017-10-18 01:26:15 +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-18 01:26:15 +00:00
return (this.lifeCurrent <= 0);
2017-10-17 03:16:08 +00:00
}
});
module.exports = Particle;