Skip to content

Commit

Permalink
Made particles more extendable.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Start committed Sep 12, 2014
1 parent c5610c5 commit 214eea3
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .bowerrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"directory" : "examples/libs"
}
5 changes: 3 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "pixi-particles",
"version": "1.0.0",
"version": "1.1.0",
"main": "dist/pixi-particles.min.js",
"dependencies": {
"pixi.js": "*"
},
"ignore": [
".gitignore",
".bowerrc",
"package.json",
"tasks",
"Gruntfile.js",
Expand All @@ -15,4 +16,4 @@
"docs",
"src"
]
}
}
2 changes: 1 addition & 1 deletion build.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name" : "PixiParticles",
"version" : "0.0.1",
"version" : "1.1.0",
"description" : "Particle system for PixiJS",
"url" : "http://github.com/CloudKidStudio/PixiParticles",
"output" : "pixi-particles",
Expand Down
62 changes: 54 additions & 8 deletions dist/pixi-particles.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! PixiParticles 0.0.1 */
/*! PixiParticles 1.1.0 */
/**
* @module cloudkid
*/
Expand Down Expand Up @@ -212,7 +212,8 @@
*/
var Particle = function(emitter)
{
PIXI.Sprite.call(this, emitter.particleImages[0]);
var art = emitter.particleImages[0] instanceof PIXI.Texture ? [emitter.particleImages[0]] : emitter.particleImages[0];
PIXI.MovieClip.call(this, art);

/**
* The emitter that controls this particle.
Expand Down Expand Up @@ -243,6 +244,11 @@
*/
this.ease = null;
/**
* Extra data that the emitter passes along for custom particles.
* @property {Object} extraData
*/
this.extraData = null;
/**
* The alpha of the particle at the start of its life.
* @property {Number} startAlpha
*/
Expand Down Expand Up @@ -357,7 +363,7 @@
};

// Reference to the prototype
var p = Particle.prototype = Object.create(PIXI.Sprite.prototype);
var p = Particle.prototype = Object.create(PIXI.MovieClip.prototype);

/**
* Initializes the particle for use, based on the properties that have to
Expand Down Expand Up @@ -404,6 +410,17 @@
this.tint = ParticleUtils.combineRGBComponents(this._sR, this._sG, this._sB);
};

/**
* Sets the texture for the particle. This can be overridden to allow
* for an animated particle.
* @method applyArt
* @param {PIXI.Texture} art The texture to set.
*/
p.applyArt = function(art)
{
this.setTexture(art);
};

/**
* Updates the particle.
* @method update
Expand All @@ -416,7 +433,7 @@
//recycle particle if it is too old
if(this.age >= this.maxLife)
{
this.emitter.recycle(this);
this.kill();
return;
}

Expand Down Expand Up @@ -463,6 +480,16 @@
}
};

/**
* Kills the particle, removing it from the display list
* and telling the emitter to recycle it.
* @method kill
*/
p.kill = function()
{
this.emitter.recycle(this);
};

/**
* Destroys the particle, removing references and preventing future use.
* @method destroy
Expand Down Expand Up @@ -498,6 +525,12 @@
*/
var Emitter = function(particleParent, particleImages, config)
{
/**
* The constructor used to create new particles. The default is
* the built in particle class.
* @property {Function} particleConstructor
*/
this.particleConstructor = Particle;
//properties for individual particles
/**
* An array of PIXI Texture objects.
Expand Down Expand Up @@ -587,9 +620,15 @@
/**
* An easing function for nonlinear interpolation of values. Accepts a single parameter of time
* as a value from 0-1, inclusive. Expected outputs are values from 0-1, inclusive.
* @property {Function} customeEase
* @property {Function} customEase
*/
this.customEase = null;
/**
* Extra data for use in custom particles. The emitter doesn't look inside, but
* passes it on to the particle to use in init().
* @property {Object} extraData
*/
this.extraData = null;
//properties for spawning particles
/**
* Time between particle spawns in seconds.
Expand Down Expand Up @@ -836,6 +875,7 @@
{
this.customEase = typeof config.ease == "function" ? config.ease : ParticleUtils.generateEase(config.ease);
}
this.extraData = config.extraData || null;
//////////////////////////
// Emitter Properties //
//////////////////////////
Expand Down Expand Up @@ -1066,12 +1106,12 @@
for(var len = Math.min(this.particlesPerWave, this.maxParticles - this._activeParticles.length); i < len; ++i)
{
//create particle
var p = this._pool.length ? this._pool.pop() : new Particle(this);
var p = this._pool.length ? this._pool.pop() : new this.particleConstructor(this);
//set a random texture if we have more than one
if(this.particleImages.length > 1)
p.setTexture(this.particleImages.random());
p.applyArt(this.particleImages.random());
else
p.setTexture(this.particleImages[0]);//if they are actually the same texture, this call will quit early
p.applyArt(this.particleImages[0]);//if they are actually the same texture, this call will quit early
//set up the start and end values
p.startAlpha = this.startAlpha;
p.endAlpha = this.endAlpha;
Expand All @@ -1081,11 +1121,17 @@
p.endScale = this.endScale;
p.startColor = this.startColor;
p.endColor = this.endColor;
//randomize the rotation speed
if(this.minRotationSpeed == this.maxRotationSpeed)
p.rotationSpeed = this.minRotationSpeed;
else
p.rotationSpeed = Math.random() * (this.maxRotationSpeed - this.minRotationSpeed) + this.minRotationSpeed;
//set up the lifetime
p.maxLife = lifetime;
//set the custom ease, if any
p.ease = this.customEase;
//set the extra data, if any
p.extraData = this.extraData;
//call the proper function to handle rotation and position of particle
this._spawnFunc(p, emitPosX, emitPosY, i);
//initialize particle
Expand Down
4 changes: 2 additions & 2 deletions dist/pixi-particles.min.js

Large diffs are not rendered by default.

27 changes: 23 additions & 4 deletions src/Emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
*/
var Emitter = function(particleParent, particleImages, config)
{
/**
* The constructor used to create new particles. The default is
* the built in particle class.
* @property {Function} particleConstructor
*/
this.particleConstructor = Particle;
//properties for individual particles
/**
* An array of PIXI Texture objects.
Expand Down Expand Up @@ -107,9 +113,15 @@
/**
* An easing function for nonlinear interpolation of values. Accepts a single parameter of time
* as a value from 0-1, inclusive. Expected outputs are values from 0-1, inclusive.
* @property {Function} customeEase
* @property {Function} customEase
*/
this.customEase = null;
/**
* Extra data for use in custom particles. The emitter doesn't look inside, but
* passes it on to the particle to use in init().
* @property {Object} extraData
*/
this.extraData = null;
//properties for spawning particles
/**
* Time between particle spawns in seconds.
Expand Down Expand Up @@ -356,6 +368,7 @@
{
this.customEase = typeof config.ease == "function" ? config.ease : ParticleUtils.generateEase(config.ease);
}
this.extraData = config.extraData || null;
//////////////////////////
// Emitter Properties //
//////////////////////////
Expand Down Expand Up @@ -586,12 +599,12 @@
for(var len = Math.min(this.particlesPerWave, this.maxParticles - this._activeParticles.length); i < len; ++i)
{
//create particle
var p = this._pool.length ? this._pool.pop() : new Particle(this);
var p = this._pool.length ? this._pool.pop() : new this.particleConstructor(this);
//set a random texture if we have more than one
if(this.particleImages.length > 1)
p.setTexture(this.particleImages.random());
p.applyArt(this.particleImages.random());
else
p.setTexture(this.particleImages[0]);//if they are actually the same texture, this call will quit early
p.applyArt(this.particleImages[0]);//if they are actually the same texture, this call will quit early
//set up the start and end values
p.startAlpha = this.startAlpha;
p.endAlpha = this.endAlpha;
Expand All @@ -601,11 +614,17 @@
p.endScale = this.endScale;
p.startColor = this.startColor;
p.endColor = this.endColor;
//randomize the rotation speed
if(this.minRotationSpeed == this.maxRotationSpeed)
p.rotationSpeed = this.minRotationSpeed;
else
p.rotationSpeed = Math.random() * (this.maxRotationSpeed - this.minRotationSpeed) + this.minRotationSpeed;
//set up the lifetime
p.maxLife = lifetime;
//set the custom ease, if any
p.ease = this.customEase;
//set the extra data, if any
p.extraData = this.extraData;
//call the proper function to handle rotation and position of particle
this._spawnFunc(p, emitPosX, emitPosY, i);
//initialize particle
Expand Down
33 changes: 30 additions & 3 deletions src/Particle.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
*/
var Particle = function(emitter)
{
PIXI.Sprite.call(this, emitter.particleImages[0]);
var art = emitter.particleImages[0] instanceof PIXI.Texture ? [emitter.particleImages[0]] : emitter.particleImages[0];
PIXI.MovieClip.call(this, art);

/**
* The emitter that controls this particle.
Expand Down Expand Up @@ -46,6 +47,11 @@
*/
this.ease = null;
/**
* Extra data that the emitter passes along for custom particles.
* @property {Object} extraData
*/
this.extraData = null;
/**
* The alpha of the particle at the start of its life.
* @property {Number} startAlpha
*/
Expand Down Expand Up @@ -160,7 +166,7 @@
};

// Reference to the prototype
var p = Particle.prototype = Object.create(PIXI.Sprite.prototype);
var p = Particle.prototype = Object.create(PIXI.MovieClip.prototype);

/**
* Initializes the particle for use, based on the properties that have to
Expand Down Expand Up @@ -207,6 +213,17 @@
this.tint = ParticleUtils.combineRGBComponents(this._sR, this._sG, this._sB);
};

/**
* Sets the texture for the particle. This can be overridden to allow
* for an animated particle.
* @method applyArt
* @param {PIXI.Texture} art The texture to set.
*/
p.applyArt = function(art)
{
this.setTexture(art);
};

/**
* Updates the particle.
* @method update
Expand All @@ -219,7 +236,7 @@
//recycle particle if it is too old
if(this.age >= this.maxLife)
{
this.emitter.recycle(this);
this.kill();
return;
}

Expand Down Expand Up @@ -266,6 +283,16 @@
}
};

/**
* Kills the particle, removing it from the display list
* and telling the emitter to recycle it.
* @method kill
*/
p.kill = function()
{
this.emitter.recycle(this);
};

/**
* Destroys the particle, removing references and preventing future use.
* @method destroy
Expand Down

0 comments on commit 214eea3

Please sign in to comment.