Skip to content

Commit

Permalink
Emitter now has minParticleAlpha and maxParticleAlpha values for sett…
Browse files Browse the repository at this point in the history
…ing a random alpha on emitted particles.

Emitter.particleAnchor allows you to control the anchor of emitted Particles. Defaults to 0.5 (same as before) but now under your control.
Emitter now emits Phaser.Particle objects instead of Phaser.Sprites, which can be extended as required.
Emitter has had various local properties removed that were already declared in Phaser.Group which it extends.
  • Loading branch information
photonstorm committed Apr 9, 2014
1 parent 73d0414 commit 50981fd
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 49 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ Updated
* Keyboard.stop nulls the function references after removing the event listeners (thanks @bmceldowney, #691)
* Tilemap.hasTile allows for multi-layer type parameter (thanks @Raeven0, #680)
* Grunt update to dev dependencies (thanks @xtian, #695)
* Emitter now emits Phaser.Particle objects instead of Phaser.Sprites, which can be extended as required.
* Emitter has had various local properties removed that were already declared in Phaser.Group which it extends.


New Features
Expand All @@ -101,6 +103,8 @@ New Features
* Tilemap.removeTile(x, y, layer) lets you remove the tile at the given coordinates and updates the collision data.
* Tilemap.removeTileWorldXY lets you remove the tile at the given pixel value coordinates and updates the collision data.
* Key.enabled boolean allows you to toggle if a Key processes its update method or dispatches any events without deleting and re-creating it.
* Emitter now has minParticleAlpha and maxParticleAlpha values for setting a random alpha on emitted particles.
* Emitter.particleAnchor allows you to control the anchor of emitted Particles. Defaults to 0.5 (same as before) but now under your control.


Bug Fixes
Expand Down
1 change: 1 addition & 0 deletions build/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
<script src="$path/src/gameobjects/RenderTexture.js"></script>
<script src="$path/src/gameobjects/SpriteBatch.js"></script>
<script src="$path/src/gameobjects/RetroFont.js"></script>
<script src="$path/src/gameobjects/Particle.js"></script>
<script src="$path/src/system/Canvas.js"></script>
<script src="$path/src/system/Device.js"></script>
Expand Down
39 changes: 39 additions & 0 deletions src/gameobjects/Particle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @author Richard Davey <[email protected]>
* @copyright 2014 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/

/**
* @class Phaser.Particle
*
* @classdesc Create a new `Particle` object. Particles are extended Sprites that are emitted by a particle emitter.
*
* @constructor
* @extends Phaser.Sprite
* @param {Phaser.Game} game - A reference to the currently running game.
* @param {number} x - The x coordinate (in world space) to position the Sprite at.
* @param {number} y - The y coordinate (in world space) to position the Sprite at.
* @param {string|Phaser.RenderTexture|Phaser.BitmapData|PIXI.Texture} key - This is the image or texture used by the Sprite during rendering. It can be a string which is a reference to the Cache entry, or an instance of a RenderTexture or PIXI.Texture.
* @param {string|number} frame - If this Sprite is using part of a sprite sheet or texture atlas you can specify the exact frame to use by giving a string or numeric index.
*/
Phaser.Particle = function (game, x, y, key, frame) {

Phaser.Sprite.call(this, game, x, y, key, frame);

}

Phaser.Particle.prototype = Object.create(Phaser.Sprite.prototype);
Phaser.Particle.prototype.constructor = Phaser.Particle;

/**
* Override and use this function in your own custom objects to handle any update requirements you may have.
*
* @method Phaser.Particle#update
* @memberof Phaser.Particle
*/
Phaser.Particle.prototype.update = function() {



};
8 changes: 4 additions & 4 deletions src/gameobjects/Sprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,10 @@ Phaser.Sprite.prototype.preUpdate = function() {
}

// Update any Children
// for (var i = 0, len = this.children.length; i < len; i++)
// {
// this.children[i].preUpdate();
// }
for (var i = 0, len = this.children.length; i < len; i++)
{
this.children[i].preUpdate();
}

return true;

Expand Down
146 changes: 101 additions & 45 deletions src/particles/arcade/Emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ Phaser.Particles.Arcade.Emitter = function (game, x, y, maxParticles) {
* @property {number} x - The X position of the top left corner of the emitter in world space.
* @default
*/
this.x = 0;
// this.x = 0;

/**
* @property {number} y - The Y position of the top left corner of emitter in world space.
* @default
*/
this.y = 0;
// this.y = 0;

/**
* @property {number} width - The width of the emitter. Particles can be randomly generated from anywhere within this box.
Expand Down Expand Up @@ -100,17 +100,29 @@ Phaser.Particles.Arcade.Emitter = function (game, x, y, maxParticles) {
*/
this.maxRotation = 360;

/**
* @property {number} minParticleAlpha - The minimum possible alpha value of a particle.
* @default
*/
this.minParticleAlpha = 1;

/**
* @property {number} maxParticleAlpha - The maximum possible alpha value of a particle.
* @default
*/
this.maxParticleAlpha = 1;

/**
* @property {number} gravity - Sets the `body.gravity.y` of each particle sprite to this value on launch.
* @default
*/
this.gravity = 100;

/**
* @property {any} particleClass - For emitting your own particle class types. They must extend Phaser.Sprite.
* @property {any} particleClass - For emitting your own particle class types. They must extend Phaser.Particle.
* @default
*/
this.particleClass = Phaser.Sprite;
this.particleClass = Phaser.Particle;

/**
* @property {Phaser.Point} particleDrag - The X and Y drag component of particles launched from the emitter.
Expand All @@ -136,10 +148,44 @@ Phaser.Particles.Arcade.Emitter = function (game, x, y, maxParticles) {
this.lifespan = 2000;

/**
* @property {Phaser.Point} bounce - How much each particle should bounce on each axis. 1 = full bounce, 0 = no bounce.
* @property {Phaser.Point} bounce - How much each particle should bounce on each axis. 1 = full bounce, 0 = no bounce.
*/
this.bounce = new Phaser.Point();

/**
* @property {boolean} on - Determines whether the emitter is currently emitting particles. It is totally safe to directly toggle this.
* @default
*/
this.on = false;

/**
* @property {Phaser.Point} particleAnchor - When a particle is created its anchor will be set to match this Point object (defaults to x/y: 0.5 to aid in rotation)
* @default
*/
this.particleAnchor = new Phaser.Point(0.5, 0.5);

/**
* @property {boolean} exists - Determines whether the emitter is being updated by the core game loop.
* @default
*/
// this.exists = true;

/**
* The point the particles are emitted from.
* Emitter.x and Emitter.y control the containers location, which updates all current particles
* Emitter.emitX and Emitter.emitY control the emission location relative to the x/y position.
* @property {number} emitX
*/
this.emitX = x;

/**
* The point the particles are emitted from.
* Emitter.x and Emitter.y control the containers location, which updates all current particles
* Emitter.emitX and Emitter.emitY control the emission location relative to the x/y position.
* @property {number} emitY
*/
this.emitY = y;

/**
* @property {number} _quantity - Internal helper for deciding how many particles to launch.
* @private
Expand Down Expand Up @@ -170,34 +216,6 @@ Phaser.Particles.Arcade.Emitter = function (game, x, y, maxParticles) {
*/
this._frames = null;

/**
* @property {boolean} on - Determines whether the emitter is currently emitting particles. It is totally safe to directly toggle this.
* @default
*/
this.on = false;

/**
* @property {boolean} exists - Determines whether the emitter is being updated by the core game loop.
* @default
*/
this.exists = true;

/**
* The point the particles are emitted from.
* Emitter.x and Emitter.y control the containers location, which updates all current particles
* Emitter.emitX and Emitter.emitY control the emission location relative to the x/y position.
* @property {boolean} emitX
*/
this.emitX = x;

/**
* The point the particles are emitted from.
* Emitter.x and Emitter.y control the containers location, which updates all current particles
* Emitter.emitX and Emitter.emitY control the emission location relative to the x/y position.
* @property {boolean} emitY
*/
this.emitY = y;

};

Phaser.Particles.Arcade.Emitter.prototype = Object.create(Phaser.Group.prototype);
Expand All @@ -211,6 +229,13 @@ Phaser.Particles.Arcade.Emitter.prototype.update = function () {

if (this.on)
{
var i = this.children.length;

while (i--)
{
this.children[i].update();
}

if (this._explode)
{
this._counter = 0;
Expand Down Expand Up @@ -248,13 +273,14 @@ Phaser.Particles.Arcade.Emitter.prototype.update = function () {
};

/**
* This function generates a new array of particle sprites to attach to the emitter.
* This function generates a new set of particles for use by this emitter.
* The particles are stored internally waiting to be emitted via Emitter.start.
*
* @method Phaser.Particles.Arcade.Emitter#makeParticles
* @param {array|string} keys - A string or an array of strings that the particle sprites will use as their texture. If an array one is picked at random.
* @param {array|number} frames - A frame number, or array of frames that the sprite will use. If an array one is picked at random.
* @param {number} quantity - The number of particles to generate.
* @param {boolean} [collide=false] - Sets the checkCollision.none flag on the particle sprites body.
* @param {array|number} [frames=0] - A frame number, or array of frames that the sprite will use. If an array one is picked at random.
* @param {number} [quantity] - The number of particles to generate. If not given it will use the value of Emitter.maxParticles.
* @param {boolean} [collide=false] - If you want the particles to be able to collide with other Arcade Physics bodies then set this to true.
* @param {boolean} [collideWorldBounds=false] - A particle can be set to collide against the World bounds automatically and rebound back into the World if this is set to true. Otherwise it will leave the World.
* @return {Phaser.Particles.Arcade.Emitter} This Emitter instance.
*/
Expand Down Expand Up @@ -301,9 +327,7 @@ Phaser.Particles.Arcade.Emitter.prototype.makeParticles = function (keys, frames

particle.exists = false;
particle.visible = false;

// Center the origin for rotation assistance
particle.anchor.set(0.5);
particle.anchor.copyFrom(this.particleAnchor);

this.add(particle);

Expand All @@ -316,6 +340,7 @@ Phaser.Particles.Arcade.Emitter.prototype.makeParticles = function (keys, frames

/**
* Call this function to turn off all the particles and the emitter.
*
* @method Phaser.Particles.Arcade.Emitter#kill
*/
Phaser.Particles.Arcade.Emitter.prototype.kill = function () {
Expand All @@ -328,6 +353,7 @@ Phaser.Particles.Arcade.Emitter.prototype.kill = function () {

/**
* Handy for bringing game objects "back to life". Just sets alive and exists back to true.
*
* @method Phaser.Particles.Arcade.Emitter#revive
*/
Phaser.Particles.Arcade.Emitter.prototype.revive = function () {
Expand All @@ -340,16 +366,16 @@ Phaser.Particles.Arcade.Emitter.prototype.revive = function () {
/**
* Call this function to start emitting particles.
* @method Phaser.Particles.Arcade.Emitter#start
* @param {boolean} [explode=true] - Whether the particles should all burst out at once.
* @param {number} [lifespan=0] - How long each particle lives once emitted. 0 = forever.
* @param {boolean} [explode=true] - Whether the particles should all burst out at once (true) or at the frequency given (false).
* @param {number} [lifespan=0] - How long each particle lives once emitted in ms. 0 = forever.
* @param {number} [frequency=250] - Ignored if Explode is set to true. Frequency is how often to emit a particle in ms.
* @param {number} [quantity=0] - How many particles to launch. 0 = "all of the particles".
*/
Phaser.Particles.Arcade.Emitter.prototype.start = function (explode, lifespan, frequency, quantity) {

if (typeof explode === 'undefined') { explode = true; }
if (typeof lifespan === 'undefined') { lifespan = 0; }
if (typeof frequency === 'undefined') { frequency = 250; }
if (typeof frequency === 'undefined' || frequency === null) { frequency = 250; }
if (typeof quantity === 'undefined') { quantity = 0; }

this.revive();
Expand All @@ -376,7 +402,8 @@ Phaser.Particles.Arcade.Emitter.prototype.start = function (explode, lifespan, f
};

/**
* This function can be used both internally and externally to emit the next particle.
* This function can be used both internally and externally to emit the next particle in the queue.
*
* @method Phaser.Particles.Arcade.Emitter#emitParticle
*/
Phaser.Particles.Arcade.Emitter.prototype.emitParticle = function () {
Expand Down Expand Up @@ -447,6 +474,11 @@ Phaser.Particles.Arcade.Emitter.prototype.emitParticle = function () {
particle.frame = this._frames;
}

if (this.minParticleAlpha !== 1 || this.maxParticleAlpha !== 1)
{
particle.alpha = this.game.rnd.realInRange(this.minParticleAlpha, this.maxParticleAlpha);
}

particle.body.gravity.y = this.gravity;
particle.body.drag.x = this.particleDrag.x;
particle.body.drag.y = this.particleDrag.y;
Expand Down Expand Up @@ -515,10 +547,29 @@ Phaser.Particles.Arcade.Emitter.prototype.setRotation = function (min, max) {

};

/**
* A more compact way of setting the alpha constraints of the emitter.
*
* @method Phaser.Particles.Arcade.Emitter#setAlpha
* @param {number} [min=1] - The minimum value for this range.
* @param {number} [max=1] - The maximum value for this range.
*/
Phaser.Particles.Arcade.Emitter.prototype.setAlpha = function (min, max) {

if (typeof min === 'undefined') { min = 1; }
if (typeof max === 'undefined') { max = 1; }

this.minParticleAlpha = min;
this.maxParticleAlpha = max;

};

/**
* Change the emitters center to match the center of any object with a `center` property, such as a Sprite.
* If the object doesn't have a center property it will be set to object.x + object.width / 2
*
* @method Phaser.Particles.Arcade.Emitter#at
* @param {object|Phaser.Sprite} object - The object that you wish to match the center with.
* @param {object|Phaser.Sprite|Phaser.Image|Phaser.TileSprite|Phaser.Text|PIXI.DisplayObject} object - The object that you wish to match the center with.
*/
Phaser.Particles.Arcade.Emitter.prototype.at = function (object) {

Expand All @@ -527,6 +578,11 @@ Phaser.Particles.Arcade.Emitter.prototype.at = function (object) {
this.emitX = object.center.x;
this.emitY = object.center.y;
}
else
{
this.emitX = object.world.x + (object.anchor.x * object.width);
this.emitY = object.world.y + (object.anchor.y * object.height);
}

};

Expand Down
1 change: 1 addition & 0 deletions tasks/manifests/phaser.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"src/gameobjects/RenderTexture.js",
"src/gameobjects/SpriteBatch.js",
"src/gameobjects/RetroFont.js",
"src/gameobjects/Particle.js",

"src/system/Canvas.js",
"src/system/Device.js",
Expand Down

0 comments on commit 50981fd

Please sign in to comment.