Created
September 26, 2013 21:16
-
-
Save ashblue/6720678 to your computer and use it in GitHub Desktop.
ImpactJS particle generator, requires a particle class passed to it in order to work.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ig.module( | |
'game.entities.particle-generator' | |
) | |
.requires( | |
'impact.entity' | |
) | |
.defines(function(){ | |
'use strict'; | |
// @TODO Must use pooling for individual particles | |
window.EntityParticleGenerator = ig.Entity.extend({ | |
// Width and height are the distribution radius | |
size: { x: 50, y: 50 }, | |
// Type of particle to create | |
particleClass: null, | |
// @TODO Should be a range, not a set value | |
// Total number of particles to distribute | |
particles: 6, | |
// How many seconds to keep the particle generator alive before destroying | |
lifetime: 2, | |
// Make this particle generator run infinitely | |
repeat: false, | |
// Create all particles at one time and destroy immediately, performance intensive | |
instant: false, | |
init: function( x, y, settings ) { | |
this.parent( x, y, settings ); | |
// Force generate all particles | |
if (this.instant) { | |
for (var i = 0; i < this.particles; i++) { | |
this.createParticle(); | |
} | |
this.kill(); | |
return; | |
} | |
// Create a timer that generates a particle every time it expires | |
this.timerGenerate = new ig.Timer(); | |
this.timerGenerate.set(this.lifetime / this.particles); | |
// Create an expiration timer | |
this.timerExpire = new ig.Timer(); | |
this.timerExpire.set(this.lifetime); | |
}, | |
update: function () { | |
if (this.timerExpire.delta() > 0) { | |
if (this.repeat === true) { | |
this.timerExpire.reset(); | |
} else { | |
this.kill(); | |
} | |
} else if (this.timerGenerate.delta() > 0) { | |
this.createParticle(); | |
this.timerGenerate.reset(); | |
} | |
}, | |
// Generates a particle within the particle generator's bounds | |
createParticle: function () { | |
ig.game.spawnEntity(this.particleClass, this.pos.x + ig.game.calc.random(0, this.size.x), this.pos.y + ig.game.calc.random(0, this.size.y)); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment