2014-04-09 13:29:23 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2014 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @class Phaser.Particle
|
|
|
|
*
|
2014-04-10 00:48:55 +00:00
|
|
|
* @classdesc Create a new `Particle` object. Particles are extended Sprites that are emitted by a particle emitter such as Phaser.Particles.Arcade.Emitter.
|
2014-04-09 13:29:23 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @extends Phaser.Sprite
|
|
|
|
* @param {Phaser.Game} game - A reference to the currently running game.
|
2014-04-10 00:48:55 +00:00
|
|
|
* @param {number} x - The x coordinate (in world space) to position the Particle at.
|
|
|
|
* @param {number} y - The y coordinate (in world space) to position the Particle at.
|
|
|
|
* @param {string|Phaser.RenderTexture|Phaser.BitmapData|PIXI.Texture} key - This is the image or texture used by the Particle 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 Particle 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.
|
2014-04-09 13:29:23 +00:00
|
|
|
*/
|
|
|
|
Phaser.Particle = function (game, x, y, key, frame) {
|
|
|
|
|
|
|
|
Phaser.Sprite.call(this, game, x, y, key, frame);
|
|
|
|
|
2014-04-10 00:48:55 +00:00
|
|
|
/**
|
|
|
|
* @property {boolean} autoScale - If this Particle automatically scales this is set to true by Particle.setScaleData.
|
|
|
|
* @protected
|
|
|
|
*/
|
2014-04-10 00:36:05 +00:00
|
|
|
this.autoScale = false;
|
2014-04-10 00:48:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {array} scaleData - A reference to the scaleData array owned by the Emitter that emitted this Particle.
|
|
|
|
* @protected
|
|
|
|
*/
|
2014-04-10 00:36:05 +00:00
|
|
|
this.scaleData = null;
|
2014-04-10 00:48:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _s - Internal cache var for tracking auto scale.
|
|
|
|
* @private
|
|
|
|
*/
|
2014-04-10 00:36:05 +00:00
|
|
|
this._s = 0;
|
|
|
|
|
2014-04-10 00:48:55 +00:00
|
|
|
/**
|
|
|
|
* @property {boolean} autoAlpha - If this Particle automatically changes alpha this is set to true by Particle.setAlphaData.
|
|
|
|
* @protected
|
|
|
|
*/
|
2014-04-10 00:36:05 +00:00
|
|
|
this.autoAlpha = false;
|
2014-04-10 00:48:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {array} alphaData - A reference to the alphaData array owned by the Emitter that emitted this Particle.
|
|
|
|
* @protected
|
|
|
|
*/
|
2014-04-10 00:36:05 +00:00
|
|
|
this.alphaData = null;
|
2014-04-10 00:48:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _a - Internal cache var for tracking auto alpha.
|
|
|
|
* @private
|
|
|
|
*/
|
2014-04-10 00:36:05 +00:00
|
|
|
this._a = 0;
|
|
|
|
|
|
|
|
};
|
2014-04-09 13:29:23 +00:00
|
|
|
|
|
|
|
Phaser.Particle.prototype = Object.create(Phaser.Sprite.prototype);
|
|
|
|
Phaser.Particle.prototype.constructor = Phaser.Particle;
|
|
|
|
|
|
|
|
/**
|
2014-04-10 00:48:55 +00:00
|
|
|
* Updates the Particle scale or alpha if autoScale and autoAlpha are set.
|
2014-04-09 13:29:23 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Particle#update
|
|
|
|
* @memberof Phaser.Particle
|
|
|
|
*/
|
|
|
|
Phaser.Particle.prototype.update = function() {
|
|
|
|
|
2014-04-10 00:36:05 +00:00
|
|
|
if (this.autoScale)
|
|
|
|
{
|
|
|
|
this._s--;
|
|
|
|
|
|
|
|
if (this._s)
|
|
|
|
{
|
|
|
|
this.scale.set(this.scaleData[this._s].v);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.autoScale = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.autoAlpha)
|
|
|
|
{
|
|
|
|
this._a--;
|
|
|
|
|
|
|
|
if (this._a)
|
|
|
|
{
|
|
|
|
this.alpha = this.alphaData[this._a].v;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.autoAlpha = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-04-10 00:48:55 +00:00
|
|
|
/**
|
|
|
|
* Called by the Emitter when this particle is emitted. Left empty for you to over-ride as required.
|
|
|
|
*
|
|
|
|
* @method Phaser.Particle#onEmit
|
|
|
|
* @memberof Phaser.Particle
|
|
|
|
*/
|
2014-04-10 00:36:05 +00:00
|
|
|
Phaser.Particle.prototype.onEmit = function() {
|
|
|
|
};
|
|
|
|
|
2014-04-10 00:48:55 +00:00
|
|
|
/**
|
|
|
|
* Called by the Emitter if autoAlpha has been enabled. Passes over the alpha ease data and resets the alpha counter.
|
|
|
|
*
|
|
|
|
* @method Phaser.Particle#setAlphaData
|
|
|
|
* @memberof Phaser.Particle
|
|
|
|
*/
|
2014-04-10 00:36:05 +00:00
|
|
|
Phaser.Particle.prototype.setAlphaData = function(data) {
|
|
|
|
|
|
|
|
this.alphaData = data;
|
|
|
|
this._a = data.length - 1;
|
|
|
|
this.alpha = this.alphaData[this._a].v;
|
|
|
|
this.autoAlpha = true;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-04-10 00:48:55 +00:00
|
|
|
/**
|
|
|
|
* Called by the Emitter if autoScale has been enabled. Passes over the scale ease data and resets the scale counter.
|
|
|
|
*
|
|
|
|
* @method Phaser.Particle#setScaleData
|
|
|
|
* @memberof Phaser.Particle
|
|
|
|
*/
|
2014-04-10 00:36:05 +00:00
|
|
|
Phaser.Particle.prototype.setScaleData = function(data) {
|
|
|
|
|
|
|
|
this.scaleData = data;
|
|
|
|
this._s = data.length - 1;
|
|
|
|
this.scale.set(this.scaleData[this._s].v);
|
|
|
|
this.autoScale = true;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets the Particle. This places the Particle at the given x/y world coordinates and then
|
|
|
|
* sets alive, exists, visible and renderable all to true. Also resets the outOfBounds state and health values.
|
|
|
|
* If the Particle has a physics body that too is reset.
|
|
|
|
*
|
|
|
|
* @method Phaser.Particle#reset
|
|
|
|
* @memberof Phaser.Particle
|
|
|
|
* @param {number} x - The x coordinate (in world space) to position the Particle at.
|
|
|
|
* @param {number} y - The y coordinate (in world space) to position the Particle at.
|
|
|
|
* @param {number} [health=1] - The health to give the Particle.
|
|
|
|
* @return (Phaser.Particle) This instance.
|
|
|
|
*/
|
|
|
|
Phaser.Particle.prototype.reset = function(x, y, health) {
|
|
|
|
|
|
|
|
if (typeof health === 'undefined') { health = 1; }
|
|
|
|
|
|
|
|
this.world.setTo(x, y);
|
|
|
|
this.position.x = x;
|
|
|
|
this.position.y = y;
|
|
|
|
this.alive = true;
|
|
|
|
this.exists = true;
|
|
|
|
this.visible = true;
|
|
|
|
this.renderable = true;
|
|
|
|
this._outOfBoundsFired = false;
|
|
|
|
|
|
|
|
this.health = health;
|
|
|
|
|
|
|
|
if (this.body)
|
|
|
|
{
|
|
|
|
this.body.reset(x, y, false, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._cache[4] = 1;
|
|
|
|
|
2014-04-10 03:19:43 +00:00
|
|
|
this.alpha = 1;
|
|
|
|
this.scale.set(1);
|
|
|
|
|
2014-04-10 00:36:05 +00:00
|
|
|
this.autoScale = false;
|
|
|
|
this.autoAlpha = false;
|
2014-04-09 13:29:23 +00:00
|
|
|
|
2014-04-10 00:36:05 +00:00
|
|
|
return this;
|
2014-04-09 13:29:23 +00:00
|
|
|
|
|
|
|
};
|