/**
*
* Class Name : ParticleEmitter
* Created : 28/08/2012
* Require : Three.js
*
**/
GAME_ENGINE.ParticleEmitter = function(scene, picture, psize, max) {
this.scene = scene;
this.psize = (psize) ? psize : 20;
this.maxParticleCount = (max) ? max : 20;
this.emitting = false;
this.timer = 0;
this.delay = 0; //60 fps -> 1 particle per second
this.particleAnimation = null;
this.particleInitializer = null;
this.particles = [];
this.Material = { color: 0xFFFFFF, size: psize, map: THREE.ImageUtils.loadTexture(picture), blending: THREE.AdditiveBlending, transparent: true };
this.position = new THREE.Vector3(0, 0, 0);
}
GAME_ENGINE.ParticleEmitter.prototype.setParticleAnimation = function(animation) {
this.particleAnimation = animation;
};
GAME_ENGINE.ParticleEmitter.prototype.setParticleInitializer = function(initializer) {
this.particleInitializer = initializer;
};
GAME_ENGINE.ParticleEmitter.prototype.start = function() {
this.emitting = true;
};
GAME_ENGINE.ParticleEmitter.prototype.stop = function() {
this.emitting = false;
};
GAME_ENGINE.ParticleEmitter.prototype.destroyParticle = function (index) {
this.scene.remove( this.particles[index] );
this.particles.splice(index, 1);
}
GAME_ENGINE.ParticleEmitter.prototype.update = function( delta ) {
if (this.emitting) {
if (this.timer >= this.delay) {
if(this.particles.length < this.maxParticleCount) {
var particle = new THREE.Sprite( this.Material );
particle.useScreenCoordinates = false;
particle.mergeWith3D = true;
particle.affectedByDistance = true;
particle.scaleByViewport = true;
particle.position = new THREE.Vector3(this.position.x, this.position.y, this.position.z);
particle.scale.x = particle.scale.y = this.psize;
if (this.particleInitializer != null)
this.particleInitializer(particle);
this.particles.push( particle );
this.scene.add( particle );
}
this.timer = 0;
}
this.timer += 1;
}
if (this.particleAnimation != null) {
for (var i=0; i<this.particles.length; i++) {
this.particleAnimation(this.particles[i], i);
}
}
};
// Usage :
// scene : THREE.Scene
var particlesEmitter = new GAME_ENGINE.ParticleEmitter(scene, "./textures/particles/01.png", 600, 35);
particlesEmitter.setParticleInitializer(function(particle){
// Setup each particle at creation
particle.initPos = this.position;
});
particlesEmitter.setParticleAnimation( function(particle, index) {
// update each particle on loop
particle.scale.x = particle.scale.y = 0.98*particle.scale.x;
particle.opacity = 0.90*particle.opacity;
particle.position.y += 1;
if (particle.position > 100)
this.destroyParticle(index);
});
particlesEmitter.start();