class ParticleEmitter { static pool = []; constructor() { } static unPool() //get a particle from the static pool { if(ParticleEmitter.pool.length > 0) { return ParticleEmitter.pool.pop() } return new Particle(); } static returnToPool(particle) { if(!ParticleEmitter.pool.contains(particle)) { ParticleEmitter.pool.push(particle); } } } class Particle { position = null; rotation = 0; velocity = null; angularVelocity = 0; acceleration = null; angularAcceleration = 0; sprite = null; animTimer = 0; animationSpeed = 0; subImage = 0; startSize = null endSize = null; fadeInTime = 0; fadeInStart = 0; fadeOutTime = 0; lifeTime = 0; lifeTimeStart = 0; constructor() { this.position = new vector(0,0); this.velocity = new vector(0,0); this.acceleration = new vector(0,0) this.startSize = new vector(0,0) this.endSize = new vector(0,0) } emit(x, y, r, vx, vy, va, ax, ay, ar, spr, ssx, ssy, esx, esy, lmin, lmax, fi = 0, fo = 0, as = 0) { this.position.x = x; this.position.y = y; this.rotation = r; this.velocity.x = vx; this.velocity.y = vy; this.angularVelocity = va; this.acceleration.x = ax; this.acceleration.y = ay; this.angularAcceleration = ar; this.sprite = spr; this.startSize.x = ssx; this.startSize.y = ssy; this.endSize.x = esx; this.endSize.y = esy; this.lifeTime = Math.randomRange(lmin, lmax); this.lifeTimeStart = this.lifeTime; this.fadeInTime = fi; this.fadeInStart = fi; this.fadeOutTime = fo; this.animationSpeed = as; this.animTimer = as; } update(deltaTime) { this.position.x += this.velocity.x; this.position.y += this.velocity.y; this.velocity.x += this.acceleration.x; this.velocity.y += this.acceleration.y; this.rotation += this.angularVelocity; this.angularVelocity += this.angularAcceleration; if(this.fadeInTime > 0) this.fadeInTime -= deltaTime; if(this.animationSpeed > 0) { this.animTimer -= deltaTime; if(this.animTimer <= 0) { this.animTimer = this.animationSpeed; this.subImage = this.subImage % this.sprite.subImages; } } } draw(scene) { let foAlpha = Math.min(1, this.lifeTime / this.fadeOutTime) let fiAlpha = this.fadeInTime / this.fadeInStart; this.sprite.draw( scene, Math.round(this.position.x), Math.round(this.position.y), Math.lerp(this.startSize.x, this.endSize.x, this.lifeTime / this.lifeTimeStart), Math.lerp(this.startSize.y, this.endSize.y, this.lifeTime / this.lifeTimeStart), this.rotation, false, false, Math.min(fiAlpha, foAlpha), this.subImage ); } }