Skip to content

Commit a5d9d85

Browse files
committed
Explode on particle emitter
1 parent 066ce23 commit a5d9d85

1 file changed

Lines changed: 64 additions & 32 deletions

File tree

v3/src/gameobjects/emitter/ParticleEmitter.js

Lines changed: 64 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ var ParticleEmitter = new Class({
4747
this.gravityX = 0;
4848
this.gravityY = 0;
4949
this.life = 1.0;
50+
this.delay = 0.0;
51+
this.delayCounter = 0.0;
5052
this.deathCallback = null;
53+
this.emitCount = 1;
54+
this.enabled = true;
55+
this.allowCreation = true;
5156
this.easingFunctionAlpha = Easing.Linear;
5257
this.easingFunctionScale = Easing.Linear;
5358
this.easingFunctionRotation = Easing.Linear;
@@ -115,6 +120,11 @@ var ParticleEmitter = new Class({
115120
this.gravityY = y;
116121
},
117122

123+
setEmitterDelay: function (delay)
124+
{
125+
this.delay = delay;
126+
},
127+
118128
reserve: function (particleCount)
119129
{
120130
var dead = this.dead;
@@ -178,41 +188,56 @@ var ParticleEmitter = new Class({
178188
}
179189
},
180190

181-
emitParticle: function()
191+
explode: function (count)
192+
{
193+
if (!count) count = 100;
194+
this.emitParticle(100);
195+
},
196+
197+
emitParticle: function(count)
182198
{
199+
count = count || 1;
200+
183201
var particle = null;
184-
var rad = DegToRad(Between(this.minEmitAngle, this.maxEmitAngle));
185-
var speed = Between(this.minSpeed, this.maxSpeed);
186-
var vx = Math.cos(rad) * speed;
187-
var vy = Math.sin(rad) * speed;
188-
189-
if (this.dead.length > 0)
190-
{
191-
particle = this.dead.pop();
192-
particle.reset(this.x, this.y);
193-
}
194-
else
195-
{
196-
particle = new Particle(this.x, this.y);
197-
}
198202

199-
particle.velocityX = vx;
200-
particle.velocityY = vy;
201-
particle.life = Math.max(this.life, Number.MIN_VALUE);
202-
particle.lifeStep = particle.life;
203-
particle.start.scale = this.startScale;
204-
particle.end.scale = this.endScale;
205-
particle.scaleX = this.startScale;
206-
particle.scaleY = this.startScale;
207-
particle.start.alpha = this.startAlpha;
208-
particle.end.alpha = this.endAlpha;
209-
particle.start.rotation = this.startAngle * Math.PI / 180;
210-
particle.end.rotation = this.endAngle * Math.PI / 180;
211-
particle.color = (particle.color & 0x00FFFFFF) | (((this.startAlpha * 0xFF)|0) << 24);
212-
particle.index = this.alive.length;
213-
214-
this.alive.push(particle);
203+
for (var index = 0; index < count; ++index)
204+
{
205+
var rad = DegToRad(Between(this.minEmitAngle, this.maxEmitAngle));
206+
var speed = Between(this.minSpeed, this.maxSpeed);
207+
var vx = Math.cos(rad) * speed;
208+
var vy = Math.sin(rad) * speed;
209+
210+
if (this.dead.length > 0)
211+
{
212+
particle = this.dead.pop();
213+
particle.reset(this.x, this.y);
214+
}
215+
else if (this.allowCreation)
216+
{
217+
particle = new Particle(this.x, this.y);
218+
}
219+
else
220+
{
221+
return null;
222+
}
215223

224+
particle.velocityX = vx;
225+
particle.velocityY = vy;
226+
particle.life = Math.max(this.life, Number.MIN_VALUE);
227+
particle.lifeStep = particle.life;
228+
particle.start.scale = this.startScale;
229+
particle.end.scale = this.endScale;
230+
particle.scaleX = this.startScale;
231+
particle.scaleY = this.startScale;
232+
particle.start.alpha = this.startAlpha;
233+
particle.end.alpha = this.endAlpha;
234+
particle.start.rotation = this.startAngle * Math.PI / 180;
235+
particle.end.rotation = this.endAngle * Math.PI / 180;
236+
particle.color = (particle.color & 0x00FFFFFF) | (((this.startAlpha * 0xFF)|0) << 24);
237+
particle.index = this.alive.length;
238+
239+
this.alive.push(particle);
240+
}
216241
return particle;
217242
},
218243

@@ -259,7 +284,6 @@ var ParticleEmitter = new Class({
259284
if (deathCallback) deathCallback(particle);
260285
}
261286
particle.lifeStep -= emitterStep;
262-
263287
}
264288

265289
/* Cleanup */
@@ -269,6 +293,14 @@ var ParticleEmitter = new Class({
269293
dead.push.apply(dead, particles.splice(particles.length - deadLength, deadLength));
270294
StableSort(particles, function (a, b) { return a.index - b.index; });
271295
}
296+
297+
this.delayCounter -= emitterStep;
298+
299+
if (this.delayCounter <= 0 && this.enabled)
300+
{
301+
this.emitParticle(this.emitCount);
302+
this.delayCounter = this.delay;
303+
}
272304
}
273305

274306
});

0 commit comments

Comments
 (0)