game_actor_particle_particle.js
import NumberGradient from "../../../util/gradient/number_gradient.js";
import Vec2 from "../../vector/vec2.js";
import Vec2Gradient from "../../../util/gradient/vec2_gradient.js";
import Actor from "../actor.js";
/**
* @module Particle
* @fileoverview Contains Particle class.
*/
/**
* @class
* @extends Actor
* Individual particle to be emitted from a ParticleEmitter.
*/
class Particle extends Actor {
/**
* Create a new particle.
* @param {Vec2} pos Initial position
* @param {number} lifespan Total lifespan
* @param {string|Sprite} sprite Sprite or identifier of sprite to use
* @param {"random"|number} rotation Initial rotation
* @param {Number} spin Spin over time
* @param {Vec2Gradient} scale Scale over time
* @param {NumberGradient} opacity Opacity over time
* @param {Vec2Gradient} velocity Velocity of time
* @constructor
*/
constructor({
pos = new Vec2(),
lifespan = 1,
sprite = null,
rotation = "random",
spin = 0,
scale = new Vec2Gradient({0: 0}),
opacity = new NumberGradient({0: 1}),
velocity = new Vec2Gradient({0: new Vec2()})
} = {}) {
if (rotation === "random")
rotation = Math.random() * Math.PI * 2;
super({
pos: pos,
sprite: sprite,
scale: scale.get(0),
rotation: rotation
});
this._chaningScale = scale;
this._age = 0;
this._lifespan = lifespan;
if (this._lifespan <= 0)
this._lifespan = 0.0001;
this._spin = spin;
this._opacity = opacity;
this._velocity = velocity;
this._drawable = this.components.get("drawable");
}
update(elapsed) {
this._age += elapsed;
if (this._age >= this._lifespan) {
this.delete();
return;
}
const progress = this._age / this._lifespan;
super.update(elapsed);
this.rotation += this._spin * elapsed;
this.pos = this.pos.add(this._velocity.get(progress).mulv(elapsed));
this.scale = this._chaningScale.get(progress);
this._drawable.opacity = this._opacity.get(progress);
}
}
export default Particle;