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

246 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:
function Particle (x, y, frame)
{
// Phaser.Texture.Frame
this.frame = frame;
this.index = 0;
this.x = x;
this.y = y;
// 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
},
reset: function (x, y, frame)
{
this.index = 0;
this.frame = frame;
this.x = x;
this.y = y;
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 01:26:15 +00:00
// var rad = DegToRad(emitter.angle.getRandom());
// this.velocityX = Math.cos(rad) * emitter.velocity.getRandomX();
// this.velocityY = Math.sin(rad) * emitter.velocity.getRandomY();
this.velocityX = emitter.velocity.getRandomX();
this.velocityY = emitter.velocity.getRandomY();
var rad = DegToRad(emitter.angle.getRandom());
2017-10-18 01:26:15 +00:00
if (rad !== 0)
{
this.velocityX *= Math.cos(rad);
this.velocityY *= Math.sin(rad);
}
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;