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

254 lines
6.5 KiB
JavaScript
Raw Normal View History

2017-10-18 01:26:15 +00:00
var Clamp = require('../../math/Clamp');
2017-10-17 03:16:08 +00:00
var Class = require('../../utils/Class');
var DegToRad = require('../../math/DegToRad');
2017-10-17 03:16:08 +00:00
var Particle = new Class({
initialize:
2017-10-18 14:18:42 +00:00
function Particle ()
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
// Add Acceleration (and Bounce?)
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.angle = 0;
this.alpha = 1;
// 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 },
angle: { min: 0, max: 0 },
scaleX: { min: 1, max: 1 },
scaleY: { min: 1, max: 1 }
};
2017-10-17 03:16:08 +00:00
2017-10-18 01:26:15 +00:00
/*
2017-10-17 03:16:08 +00:00
this.start = {
tint: 0xFFFFFF,
alpha: 1,
scale: { x: 1, y: 1 },
2017-10-17 03:16:08 +00:00
angle: 0
};
this.end = {
tint: 0xFFFFFF,
alpha: 1,
scale: { x: 1, y: 1 },
2017-10-17 03:16:08 +00:00
angle: 0
};
2017-10-18 01:26:15 +00:00
*/
2017-10-17 03:16:08 +00:00
},
2017-10-18 14:18:42 +00:00
reset: function ()
2017-10-17 03:16:08 +00:00
{
this.index = 0;
2017-10-18 14:18:42 +00:00
// this.frame = frame;
2017-10-17 03:16:08 +00:00
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;
2017-10-18 01:26:15 +00:00
this.life = 1000;
this.lifeCurrent = 1000;
2017-10-17 03:16:08 +00:00
2017-10-18 01:26:15 +00:00
// this.scaleX = 1;
// this.scaleY = 1;
// this.rotation = 0;
2017-10-17 03:16:08 +00:00
2017-10-18 01:26:15 +00:00
// this.color = 0xFFFFFFFF;
2017-10-17 03:16:08 +00:00
2017-10-18 01:26:15 +00:00
// this.life = 1;
// this.lifeStep = 1;
// this.normLifeStep = 1;
2017-10-17 03:16:08 +00:00
2017-10-18 01:26:15 +00:00
/*
2017-10-17 03:16:08 +00:00
var start = this.start;
start.tint = 0xFFFFFF;
start.alpha = 1;
start.scale.x = 1;
start.scale.y = 1;
2017-10-17 03:16:08 +00:00
start.angle = 0;
var end = this.end;
end.tint = 0xFFFFFF;
end.alpha = 1;
end.scale.x = 1;
end.scale.y = 1;
2017-10-17 03:16:08 +00:00
end.angle = 0;
2017-10-18 01:26:15 +00:00
*/
2017-10-17 03:16:08 +00:00
return this;
},
isAlive: function ()
{
2017-10-18 01:26:15 +00:00
return (this.lifeCurrent > 0);
2017-10-17 03:16:08 +00:00
},
// var rad = DegToRad(Between(this.minEmitAngle, this.maxEmitAngle));
// var speed = Between(this.minSpeed, this.maxSpeed);
// var vx = Math.cos(rad) * speed;
// var vy = Math.sin(rad) * speed;
// particle.velocityX = vx;
// particle.velocityY = vy;
// particle.life = Math.max(this.life, Number.MIN_VALUE);
// particle.lifeStep = particle.life;
// particle.start.scale = this.startScale;
// particle.end.scale = this.endScale;
// particle.scaleX = this.startScale;
// particle.scaleY = this.startScale;
// particle.start.alpha = this.startAlpha;
// particle.end.alpha = this.endAlpha;
// particle.start.rotation = DegToRad(this.startAngle);
// particle.end.rotation = DegToRad(this.endAngle);
// particle.color = (particle.color & 0x00FFFFFF) | (((this.startAlpha * 0xFF)|0) << 24);
// particle.index = this.alive.length;
2017-10-18 01:26:15 +00:00
// this.data = {
// tint: { min: 0xffffff, max: 0xffffff, current: 0xffffff },
// alpha: { min: 1, max: 1, current: 1 },
// angle: { min: 0, max: 0, current: 0 },
// scaleX: { min: 1, max: 1, current: 1 },
// scaleY: { min: 1, max: 1, current: 1 }
// };
emit: function (emitter)
2017-10-17 03:16:08 +00:00
{
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-18 14:18:42 +00:00
this.x += emitter.x;
this.y += emitter.y;
2017-10-18 01:26:15 +00:00
2017-10-18 14:18:42 +00:00
var sx = emitter.speed.getRandomX();
var sy = emitter.speed.getRandomY();
2017-10-18 14:18:42 +00:00
if (emitter.radial)
2017-10-18 01:26:15 +00:00
{
2017-10-18 14:18:42 +00:00
var rad = DegToRad(emitter.angle.getRandom());
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-18 01:26:15 +00:00
this.life = emitter.lifespan.getRandom();
this.lifeCurrent = this.life;
2017-10-18 01:26:15 +00:00
// eased values
2017-10-18 01:26:15 +00:00
// emitter.scale.copyXToMinMax(this.data.scaleX);
// emitter.scale.copyYToMinMax(this.data.scaleY);
2017-10-18 01:26:15 +00:00
// emitter.particleAngle.copyToMinMax(this.data.angle);
2017-10-18 01:26:15 +00:00
// emitter.alpha.copyToMinMax(this.data.alpha);
// this.scaleX = emitter.scale.xMin;
// this.scaleY = emitter.scale.yMin;
// this.rotation = DegToRad(emitter.particleAngle.min);
// Set the starting ease data
// emitter.scale.copyX(this.start.scale);
// emitter.scale.copyY(this.end.scale);
// this.start.alpha = emitter.alpha.min;
// this.end.alpha = emitter.alpha.max;
// this.start.rotation = emitter.particleAngle.min;
// this.end.rotation = emitter.particleAngle.max;
// this.color = (this.color & 0x00ffffff) | (((this.start.alpha * 0xff) | 0) << 24);
this.index = emitter.alive.length;
2017-10-17 03:16:08 +00:00
},
// particle.velocityX += gravityX;
// particle.velocityY += gravityY;
// particle.x += particle.velocityX * emitterStep;
// particle.y += particle.velocityY * emitterStep;
// particle.normLifeStep = particle.lifeStep / particle.life;
// var norm = 1 - particle.normLifeStep;
// var alphaEase = this.easingFunctionAlpha(norm);
// var scaleEase = this.easingFunctionScale(norm);
// var rotationEase = this.easingFunctionRotation(norm);
// var alphaf = (particle.end.alpha - particle.start.alpha) * alphaEase + particle.start.alpha;
// var scale = (particle.end.scale - particle.start.scale) * scaleEase + particle.start.scale;
// var rotation = (particle.end.rotation - particle.start.rotation) * rotationEase + particle.start.rotation;
// particle.scaleX = particle.scaleY = scale;
// particle.color = (particle.color & 0x00FFFFFF) | (((alphaf * 0xFF)|0) << 24);
// particle.rotation = rotation;
2017-10-18 01:26:15 +00:00
// delta = ms, step = delta / 1000
update: function (emitter, delta, step)
2017-10-17 03:16:08 +00:00
{
2017-10-18 01:26:15 +00:00
// How far along in life is this particle? (t = 0 to 1)
// var t = this.lifeCurrent / this.life, 0, 1;
this.velocityX += (emitter.gravity.x * step);
this.velocityY += (emitter.gravity.y * step);
this.x += this.velocityX * step;
this.y += this.velocityY * step;
2017-10-18 01:26:15 +00:00
// scale
// rotation
// alpha
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;