| 1 | class ParticleEmitter |
| 2 | { |
| 3 | static pool = []; |
| 4 | enabled = false; |
| 5 | mine = []; |
| 6 | position = null; |
| 7 | |
| 8 | emissionRate = 0; |
| 9 | emissionTimer = 0; |
| 10 | emissionAngle = 0; |
| 11 | |
| 12 | startRotation = 0; |
| 13 | startVelocity = 0; |
| 14 | startAngularVelocity = 0; |
| 15 | acceleration = null; |
| 16 | angularAcceleration = 0; |
| 17 | sprite = null; |
| 18 | startSize = null; |
| 19 | endSize = null; |
| 20 | lifetimeMin = 0; |
| 21 | lifetimeMax = 0; |
| 22 | fadeIn = 0; |
| 23 | fadeOut = 0; |
| 24 | animationSpeed = 0; |
| 25 | startFrame = 0; |
| 26 | |
| 27 | friction = null; |
| 28 | angularFriction = 0; |
| 29 | |
| 30 | constructor(position, sprite, emissionRate, emissionAngle, |
| 31 | startRotation, startVelocity, startAngularVelocity, acceleration, |
| 32 | angularAcceleration, startSize, endSize, lifetimeMin, |
| 33 | lifetimeMax = -1, fadeIn = 0, fadeOut = 0, startFrame = 0, animationSpeed = 0) |
| 34 | { |
| 35 | if(lifetimeMax < 0) |
| 36 | lifetimeMax = lifetimeMin; |
| 37 | |
| 38 | this.position = position; |
| 39 | this.sprite = sprite; |
| 40 | |
| 41 | this.emissionRate = emissionRate; |
| 42 | this.emissionTimer = emissionRate; |
| 43 | this.emissionAngle = emissionAngle; |
| 44 | |
| 45 | this.startRotation = startRotation; |
| 46 | this.startVelocity = startVelocity * 10; |
| 47 | this.startAngularVelocity = startAngularVelocity * 10; |
| 48 | this.acceleration = acceleration.stretch(10); |
| 49 | this.angularAcceleration = angularAcceleration * 10; |
| 50 | this.startSize = startSize; |
| 51 | this.endSize = endSize; |
| 52 | this.lifetimeMin = lifetimeMin; |
| 53 | this.lifetimeMax = lifetimeMax; |
| 54 | this.fadeIn = fadeIn; |
| 55 | this.fadeOut = fadeOut; |
| 56 | this.startFrame = startFrame; |
| 57 | this.animationSpeed = animationSpeed; |
| 58 | |
| 59 | this.friction = new vector(0,0); |
| 60 | } |
| 61 | |
| 62 | start() |
| 63 | { |
| 64 | this.enabled = true; |
| 65 | } |
| 66 | |
| 67 | stop() |
| 68 | { |
| 69 | this.enabled = false; |
| 70 | } |
| 71 | |
| 72 | emit(amount = 1) |
| 73 | { |
| 74 | for (let i = 0; i < amount; i++) |
| 75 | { |
| 76 | let newParticle = ParticleEmitter.unPool(); |
| 77 | this.mine.push(newParticle); |
| 78 | |
| 79 | let velocity = vector.fromAngle(this.emissionAngle).stretch(this.startVelocity) |
| 80 | newParticle.emit(this, this.position.x, this.position.y, this.startRotation, |
| 81 | velocity.x, velocity.y, this.startAngularVelocity, this.acceleration.x, |
| 82 | this.acceleration.y, this.angularAcceleration, this.sprite, this.startSize.x, |
| 83 | this.startSize.y, this.endSize.x, this.endSize.y, this.lifetimeMin, |
| 84 | this.lifetimeMax, this.fadeIn, this.fadeOut, this.startFrame, this.animationSpeed, |
| 85 | this.friction, this.angularFriction) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | update(deltaTime) |
| 90 | { |
| 91 | if(this.enabled) |
| 92 | this.emissionTimer -= deltaTime; |
| 93 | |
| 94 | if(this.emissionTimer <= 0) |
| 95 | { |
| 96 | this.emissionTimer = this.emissionRate; |
| 97 | this.emit(); |
| 98 | } |
| 99 | |
| 100 | for(let i = 0; i < this.mine.length; i++) |
| 101 | { |
| 102 | this.mine[i].update(deltaTime); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | draw(scene) |
| 107 | { |
| 108 | for(let i = 0; i < this.mine.length; i++) |
| 109 | { |
| 110 | this.mine[i].draw(scene); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | dispose(particle) |
| 115 | { |
| 116 | this.mine.remove(particle); |
| 117 | ParticleEmitter.returnToPool(particle); |
| 118 | } |
| 119 | |
| 120 | static unPool() //get a particle from the static pool |
| 121 | { |
| 122 | if(ParticleEmitter.pool.length > 0) |
| 123 | { |
| 124 | return ParticleEmitter.pool.pop() |
| 125 | } |
| 126 | |
| 127 | return new Particle(); |
| 128 | } |
| 129 | |
| 130 | static returnToPool(particle) |
| 131 | { |
| 132 | if(!ParticleEmitter.pool.contains(particle)) |
| 133 | { |
| 134 | ParticleEmitter.pool.push(particle); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | class Particle |
| 140 | { |
| 141 | parent = null; |
| 142 | position = null; |
| 143 | rotation = 0; |
| 144 | velocity = null; |
| 145 | angularVelocity = 0; |
| 146 | acceleration = null; |
| 147 | angularAcceleration = 0; |
| 148 | sprite = null; |
| 149 | animTimer = 0; |
| 150 | animationSpeed = 0; |
| 151 | subImage = 0; |
| 152 | startSize = null |
| 153 | endSize = null; |
| 154 | fadeInTime = 0; |
| 155 | fadeInStart = 0; |
| 156 | fadeOutTime = 0; |
| 157 | lifeTime = 0; |
| 158 | lifeTimeStart = 0; |
| 159 | friction = null; |
| 160 | angularfriction = 0; |
| 161 | |
| 162 | constructor() |
| 163 | { |
| 164 | this.position = new vector(0,0); |
| 165 | this.velocity = new vector(0,0); |
| 166 | this.acceleration = new vector(0,0) |
| 167 | this.startSize = new vector(0,0) |
| 168 | this.endSize = new vector(0,0) |
| 169 | } |
| 170 | |
| 171 | emit(parent, x, y, r, vx, vy, va, ax, ay, ar, spr, ssx, ssy, esx, esy, lmin, lmax, fi = 0, fo = 0, sf = 0, as = 0, fric, anfric) |
| 172 | { |
| 173 | this.parent = parent; |
| 174 | this.position.x = x; |
| 175 | this.position.y = y; |
| 176 | this.rotation = r; |
| 177 | this.velocity.x = vx; |
| 178 | this.velocity.y = vy; |
| 179 | this.angularVelocity = va; |
| 180 | this.acceleration.x = ax; |
| 181 | this.acceleration.y = ay; |
| 182 | this.angularAcceleration = ar; |
| 183 | this.sprite = spr; |
| 184 | this.startSize.x = ssx; |
| 185 | this.startSize.y = ssy; |
| 186 | this.endSize.x = esx; |
| 187 | this.endSize.y = esy; |
| 188 | this.lifeTime = Math.randomRange(lmin, lmax); |
| 189 | this.lifeTimeStart = this.lifeTime; |
| 190 | this.fadeInTime = fi; |
| 191 | this.fadeInStart = fi; |
| 192 | this.fadeOutTime = fo; |
| 193 | this.animationSpeed = as; |
| 194 | this.animTimer = as; |
| 195 | this.subImage = sf; |
| 196 | this.friction = fric; |
| 197 | this.angularFriction = anfric; |
| 198 | } |
| 199 | |
| 200 | update(deltaTime) |
| 201 | { |
| 202 | this.position.x += this.velocity.x * deltaTime; |
| 203 | this.position.y += this.velocity.y * deltaTime; |
| 204 | this.velocity.x += this.acceleration.x * deltaTime; |
| 205 | this.velocity.y += this.acceleration.y * deltaTime; |
| 206 | |
| 207 | this.velocity.x += this.friction.x * deltaTime * -Math.sign(this.velocity.x * 10); |
| 208 | this.velocity.y += this.friction.y * deltaTime * -Math.sign(this.velocity.y * 10); |
| 209 | |
| 210 | this.rotation += this.angularVelocity * deltaTime; |
| 211 | this.angularVelocity += this.angularAcceleration * deltaTime; |
| 212 | |
| 213 | this.angularVelocity += this.angularFriction * deltaTime * -Math.sign(this.angularVelocity * 10); |
| 214 | |
| 215 | this.lifeTime -= deltaTime; |
| 216 | |
| 217 | if(this.fadeInTime > 0) |
| 218 | this.fadeInTime -= deltaTime; |
| 219 | |
| 220 | if(this.animationSpeed > 0) |
| 221 | { |
| 222 | this.animTimer -= deltaTime; |
| 223 | |
| 224 | if(this.animTimer <= 0) |
| 225 | { |
| 226 | this.animTimer = this.animationSpeed; |
| 227 | this.subImage = this.subImage + 1 % this.sprite.subImages; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | if(this.lifeTime <= 0) |
| 232 | this.parent.dispose(this); |
| 233 | } |
| 234 | |
| 235 | draw(scene) |
| 236 | { |
| 237 | let foAlpha = Math.min(1, this.lifeTime / this.fadeOutTime) |
| 238 | |
| 239 | let fiAlpha = 1 - (this.fadeInTime / this.fadeInStart); |
| 240 | |
| 241 | this.sprite.draw( |
| 242 | scene, |
| 243 | Math.round(this.position.x), |
| 244 | Math.round(this.position.y), |
| 245 | Math.lerp(this.endSize.x, this.startSize.x, this.lifeTime / this.lifeTimeStart), |
| 246 | Math.lerp(this.endSize.y, this.startSize.y, this.lifeTime / this.lifeTimeStart), |
| 247 | this.rotation, |
| 248 | false, |
| 249 | false, |
| 250 | Math.min(fiAlpha, foAlpha), |
| 251 | this.subImage |
| 252 | ); |
| 253 | } |
| 254 | } |