phaser/src/particles/arcade/Emitter.js

687 lines
17 KiB
JavaScript
Raw Normal View History

2013-10-01 12:54:29 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
* @module Phaser.Emitter
*/
/**
* Phaser - ArcadeEmitter
*
2013-10-01 12:54:29 +00:00
* @class Phaser.Particles.Arcade.Emitter
* @classdesc Emitter is a lightweight particle emitter. It can be used for one-time explosions or for
* continuous effects like rain and fire. All it really does is launch Particle objects out
* at set intervals, and fixes their positions and velocities accorindgly.
2013-10-01 12:54:29 +00:00
* @constructor
* @param {Phaser.Game} game - Current game instance.
* @param {number} x - Description.
* @param {number} y - Description.
* @param {number} maxParticles - Description.
*/
Phaser.Particles.Arcade.Emitter = function (game, x, y, maxParticles) {
2013-10-01 12:54:29 +00:00
/**
* @property {number} maxParticles - Description.
* @default
*/
maxParticles = maxParticles || 50;
Phaser.Group.call(this, game);
2013-10-01 12:54:29 +00:00
/**
* @property {string} name - Description.
*/
this.name = 'emitter' + this.game.particles.ID++;
2013-10-01 12:54:29 +00:00
/**
* @property {Description} type - Description.
*/
this.type = Phaser.EMITTER;
/**
2013-10-01 12:54:29 +00:00
* @property {number} x - The X position of the top left corner of the emitter in world space.
* @default
*/
this.x = 0;
/**
2013-10-01 12:54:29 +00:00
* @property {number} y - The Y position of the top left corner of emitter in world space.
* @default
*/
this.y = 0;
/**
2013-10-01 12:54:29 +00:00
* @property {number} width - The width of the emitter. Particles can be randomly generated from anywhere within this box.
* @default
*/
this.width = 1;
/**
2013-10-01 12:54:29 +00:00
* @property {number} height - The height of the emitter. Particles can be randomly generated from anywhere within this box.
* @default
*/
this.height = 1;
/**
2013-10-01 12:54:29 +00:00
* The minimum possible velocity of a particle.
* The default value is (-100,-100).
* @property {Phaser.Point} minParticleSpeed
*/
this.minParticleSpeed = new Phaser.Point(-100, -100);
/**
2013-10-01 12:54:29 +00:00
* The maximum possible velocity of a particle.
* The default value is (100,100).
* @property {Phaser.Point} maxParticleSpeed
*/
this.maxParticleSpeed = new Phaser.Point(100, 100);
/**
2013-10-01 12:54:29 +00:00
* The minimum possible scale of a particle.
* The default value is 1.
* @property {number} minParticleScale
* @default
*/
this.minParticleScale = 1;
/**
* The maximum possible scale of a particle.
* The default value is 1.
2013-10-01 12:54:29 +00:00
* @property {number} maxParticleScale
* @default
*/
this.maxParticleScale = 1;
/**
* The minimum possible angular velocity of a particle. The default value is -360.
2013-10-01 12:54:29 +00:00
* @property {number} minRotation
* @default
*/
this.minRotation = -360;
/**
* The maximum possible angular velocity of a particle. The default value is 360.
2013-10-01 12:54:29 +00:00
* @property {number} maxRotation
* @default
*/
this.maxRotation = 360;
/**
* Sets the <code>gravity.y</code> of each particle to this value on launch.
2013-10-01 12:54:29 +00:00
* @property {number} gravity
* @default
*/
this.gravity = 2;
/**
* Set your own particle class type here.
2013-10-01 12:54:29 +00:00
* @property {Description} particleClass
* @default
*/
this.particleClass = null;
/**
* The X and Y drag component of particles launched from the emitter.
2013-10-01 12:54:29 +00:00
* @property {Phaser.Point} particleDrag
*/
this.particleDrag = new Phaser.Point();
2013-09-16 00:08:06 +00:00
/**
* The angular drag component of particles launched from the emitter if they are rotating.
2013-10-01 12:54:29 +00:00
* @property {number} angularDrag
* @default
2013-09-16 00:08:06 +00:00
*/
this.angularDrag = 0;
/**
* How often a particle is emitted in ms (if emitter is started with Explode == false).
2013-10-01 15:39:39 +00:00
* @property {boolean} frequency
2013-10-01 12:54:29 +00:00
* @default
*/
this.frequency = 100;
/**
* The total number of particles in this emitter.
2013-10-01 12:54:29 +00:00
* @property {number} maxParticles
*/
this.maxParticles = maxParticles;
/**
* How long each particle lives once it is emitted in ms. Default is 2 seconds.
* Set lifespan to 'zero' for particles to live forever.
2013-10-01 12:54:29 +00:00
* @property {number} lifespan
* @default
*/
this.lifespan = 2000;
/**
2013-09-16 00:08:06 +00:00
* How much each particle should bounce on each axis. 1 = full bounce, 0 = no bounce.
2013-10-01 12:54:29 +00:00
* @property {Phaser.Point} bounce
*/
2013-09-16 00:08:06 +00:00
this.bounce = new Phaser.Point();
/**
* Internal helper for deciding how many particles to launch.
2013-10-01 12:54:29 +00:00
* @property {number} _quantity
* @private
* @default
*/
this._quantity = 0;
/**
* Internal helper for deciding when to launch particles or kill them.
2013-10-01 12:54:29 +00:00
* @property {number} _timer
* @private
* @default
*/
this._timer = 0;
/**
* Internal counter for figuring out how many particles to launch.
2013-10-01 12:54:29 +00:00
* @property {number} _counter
* @private
* @default
*/
this._counter = 0;
/**
* Internal helper for the style of particle emission (all at once, or one at a time).
2013-10-01 15:39:39 +00:00
* @property {boolean} _explode
2013-10-01 12:54:29 +00:00
* @private
* @default
*/
this._explode = true;
/**
* Determines whether the emitter is currently emitting particles.
* It is totally safe to directly toggle this.
2013-10-01 15:39:39 +00:00
* @property {boolean} on
2013-10-01 12:54:29 +00:00
* @default
*/
this.on = false;
/**
* Determines whether the emitter is being updated by the core game loop.
2013-10-01 15:39:39 +00:00
* @property {boolean} exists
2013-10-01 12:54:29 +00:00
* @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.
2013-10-01 15:39:39 +00:00
* @property {boolean} emitX
*/
this.emitX = x;
2013-10-01 12:54:29 +00:00
/**
* 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.
2013-10-01 15:39:39 +00:00
* @property {boolean} emitY
2013-10-01 12:54:29 +00:00
*/
this.emitY = y;
};
Phaser.Particles.Arcade.Emitter.prototype = Object.create(Phaser.Group.prototype);
Phaser.Particles.Arcade.Emitter.prototype.constructor = Phaser.Particles.Arcade.Emitter;
/**
* Called automatically by the game loop, decides when to launch particles and when to "die".
2013-10-01 12:54:29 +00:00
* @method Phaser.Particles.Arcade.Emitter.prototype.update
*/
Phaser.Particles.Arcade.Emitter.prototype.update = function () {
if (this.on)
{
if (this._explode)
{
this._counter = 0;
do
{
this.emitParticle();
this._counter++;
}
while (this._counter < this._quantity);
this.on = false;
}
else
{
if (this.game.time.now >= this._timer)
{
this.emitParticle();
this._counter++;
if (this._quantity > 0)
{
if (this._counter >= this._quantity)
{
this.on = false;
}
}
this._timer = this.game.time.now + this.frequency;
}
}
}
}
/**
* This function generates a new array of particle sprites to attach to the emitter.
*
2013-10-01 12:54:29 +00:00
* @method Phaser.Particles.Arcade.Emitter.prototype.makeParticles
* @param {Description} keys - Description.
* @param {number} frames - Description.
* @param {number} quantity - The number of particles to generate when using the "create from image" option.
* @param {number} collide - Description.
2013-10-01 15:39:39 +00:00
* @param {boolean} collideWorldBounds - Description.
2013-10-01 12:54:29 +00:00
* @return This Emitter instance (nice for chaining stuff together, if you're into that).
*/
2013-09-16 00:08:06 +00:00
Phaser.Particles.Arcade.Emitter.prototype.makeParticles = function (keys, frames, quantity, collide, collideWorldBounds) {
if (typeof frames == 'undefined')
{
frames = 0;
}
quantity = quantity || this.maxParticles;
2013-09-16 00:08:06 +00:00
collide = collide || 0;
if (typeof collideWorldBounds == 'undefined')
{
collideWorldBounds = false;
}
var particle;
var i = 0;
var rndKey = keys;
var rndFrame = 0;
while (i < quantity)
{
if (this.particleClass == null)
{
if (typeof keys == 'object')
{
rndKey = this.game.rnd.pick(keys);
}
if (typeof frames == 'object')
{
rndFrame = this.game.rnd.pick(frames);
}
particle = new Phaser.Sprite(this.game, 0, 0, rndKey, rndFrame);
}
else
{
// particle = new this.particleClass(this.game);
}
if (collide > 0)
{
particle.body.allowCollision.any = true;
particle.body.allowCollision.none = false;
}
else
{
particle.body.allowCollision.none = true;
}
2013-09-16 00:08:06 +00:00
particle.body.collideWorldBounds = collideWorldBounds;
particle.exists = false;
particle.visible = false;
// Center the origin for rotation assistance
particle.anchor.setTo(0.5, 0.5);
this.add(particle);
i++;
}
return this;
}
/**
* Call this function to turn off all the particles and the emitter.
2013-10-01 12:54:29 +00:00
* @method Phaser.Particles.Arcade.Emitter.prototype.kill
*/
Phaser.Particles.Arcade.Emitter.prototype.kill = function () {
this.on = false;
this.alive = false;
this.exists = false;
}
/**
* Handy for bringing game objects "back to life". Just sets alive and exists back to true.
* In practice, this is most often called by <code>Object.reset()</code>.
2013-10-01 12:54:29 +00:00
* @method Phaser.Particles.Arcade.Emitter.prototype.revive
*/
Phaser.Particles.Arcade.Emitter.prototype.revive = function () {
this.alive = true;
this.exists = true;
}
/**
* Call this function to start emitting particles.
2013-10-01 12:54:29 +00:00
* @method Phaser.Particles.Arcade.Emitter.prototype.start
* @param {boolean} explode - Whether the particles should all burst out at once.
* @param {number} lifespan - How long each particle lives once emitted. 0 = forever.
* @param {number} frequency - Ignored if Explode is set to true. Frequency is how often to emit a particle in ms.
* @param {number} quantity - How many particles to launch. 0 = "all of the particles".
*/
Phaser.Particles.Arcade.Emitter.prototype.start = function (explode, lifespan, frequency, quantity) {
if (typeof explode !== 'boolean')
{
explode = true;
}
lifespan = lifespan || 0;
// How many ms between emissions?
frequency = frequency || 250;
// Total number of particles to emit
quantity = quantity || 0;
this.revive();
this.visible = true;
this.on = true;
this._explode = explode;
this.lifespan = lifespan;
this.frequency = frequency;
2013-09-16 00:08:06 +00:00
if (explode)
{
this._quantity = quantity;
}
else
{
this._quantity += quantity;
}
this._counter = 0;
this._timer = this.game.time.now + frequency;
}
/**
* This function can be used both internally and externally to emit the next particle.
2013-10-01 12:54:29 +00:00
* @method Phaser.Particles.Arcade.Emitter.prototype.emitParticle
*/
Phaser.Particles.Arcade.Emitter.prototype.emitParticle = function () {
var particle = this.getFirstExists(false);
if (particle == null)
{
return;
}
if (this.width > 1 || this.height > 1)
{
particle.reset(this.game.rnd.integerInRange(this.left, this.right), this.game.rnd.integerInRange(this.top, this.bottom));
}
else
{
particle.reset(this.emitX, this.emitY);
}
particle.lifespan = this.lifespan;
2013-09-16 00:08:06 +00:00
particle.body.bounce.setTo(this.bounce.x, this.bounce.y);
if (this.minParticleSpeed.x != this.maxParticleSpeed.x)
{
particle.body.velocity.x = this.game.rnd.integerInRange(this.minParticleSpeed.x, this.maxParticleSpeed.x);
}
else
{
particle.body.velocity.x = this.minParticleSpeed.x;
}
if (this.minParticleSpeed.y != this.maxParticleSpeed.y)
{
particle.body.velocity.y = this.game.rnd.integerInRange(this.minParticleSpeed.y, this.maxParticleSpeed.y);
}
else
{
particle.body.velocity.y = this.minParticleSpeed.y;
}
particle.body.gravity.y = this.gravity;
if (this.minRotation != this.maxRotation)
{
particle.body.angularVelocity = this.game.rnd.integerInRange(this.minRotation, this.maxRotation);
}
else
{
particle.body.angularVelocity = this.minRotation;
}
if (this.minParticleScale !== 1 || this.maxParticleScale !== 1)
{
var scale = this.game.rnd.realInRange(this.minParticleScale, this.maxParticleScale);
particle.scale.setTo(scale, scale);
}
particle.body.drag.x = this.particleDrag.x;
particle.body.drag.y = this.particleDrag.y;
2013-09-16 00:08:06 +00:00
particle.body.angularDrag = this.angularDrag;
}
/**
* A more compact way of setting the width and height of the emitter.
2013-10-01 12:54:29 +00:00
* @method Phaser.Particles.Arcade.Emitter.prototype.setSize
* @param {number} width - The desired width of the emitter (particles are spawned randomly within these dimensions).
* @param {number} height - The desired height of the emitter.
*/
Phaser.Particles.Arcade.Emitter.prototype.setSize = function (width, height) {
this.width = width;
this.height = height;
}
/**
* A more compact way of setting the X velocity range of the emitter.
2013-10-01 12:54:29 +00:00
* @method Phaser.Particles.Arcade.Emitter.prototype.setXSpeed
* @param {number} min - The minimum value for this range.
* @param {number} max - The maximum value for this range.
*/
Phaser.Particles.Arcade.Emitter.prototype.setXSpeed = function (min, max) {
min = min || 0;
max = max || 0;
this.minParticleSpeed.x = min;
this.maxParticleSpeed.x = max;
}
/**
* A more compact way of setting the Y velocity range of the emitter.
2013-10-01 12:54:29 +00:00
* @method Phaser.Particles.Arcade.Emitter.prototype.setYSpeed
* @param {number} min - The minimum value for this range.
* @param {number} max - The maximum value for this range.
*/
Phaser.Particles.Arcade.Emitter.prototype.setYSpeed = function (min, max) {
min = min || 0;
max = max || 0;
this.minParticleSpeed.y = min;
this.maxParticleSpeed.y = max;
}
/**
* A more compact way of setting the angular velocity constraints of the emitter.
2013-10-01 12:54:29 +00:00
* @method Phaser.Particles.Arcade.Emitter.prototype.setRotation
* @param {number} min - The minimum value for this range.
* @param {number} max - The maximum value for this range.
*/
Phaser.Particles.Arcade.Emitter.prototype.setRotation = function (min, max) {
min = min || 0;
max = max || 0;
this.minRotation = min;
this.maxRotation = max;
}
/**
* Change the emitter's midpoint to match the midpoint of a <code>Object</code>.
2013-10-01 12:54:29 +00:00
* @method Phaser.Particles.Arcade.Emitter.prototype.at
* @param {object} object - The <code>Object</code> that you want to sync up with.
*/
Phaser.Particles.Arcade.Emitter.prototype.at = function (object) {
this.emitX = object.center.x;
this.emitY = object.center.y;
}
2013-10-01 12:54:29 +00:00
/**
* Get the emitter alpha.
* @return {Description}
*//**
* Set the emiter alpha value.
* @param {Description} value - Description
*/
Object.defineProperty(Phaser.Particles.Arcade.Emitter.prototype, "alpha", {
get: function () {
return this._container.alpha;
},
set: function (value) {
this._container.alpha = value;
}
});
2013-10-01 12:54:29 +00:00
/**
* Get the emitter visible state.
2013-10-01 15:39:39 +00:00
* @return {boolean}
2013-10-01 12:54:29 +00:00
*//**
* Set the emitter visible state.
2013-10-01 15:39:39 +00:00
* @param {boolean} value - Description
2013-10-01 12:54:29 +00:00
*/
Object.defineProperty(Phaser.Particles.Arcade.Emitter.prototype, "visible", {
get: function () {
return this._container.visible;
},
set: function (value) {
this._container.visible = value;
}
});
2013-10-01 12:54:29 +00:00
/**
* Get
2013-10-01 15:39:39 +00:00
* @return {boolean}
2013-10-01 12:54:29 +00:00
*//**
* Set
2013-10-01 15:39:39 +00:00
* @param {boolean} value - Description
2013-10-01 12:54:29 +00:00
*/
Object.defineProperty(Phaser.Particles.Arcade.Emitter.prototype, "x", {
get: function () {
return this.emitX;
},
set: function (value) {
this.emitX = value;
}
});
2013-10-01 12:54:29 +00:00
/**
* Get
2013-10-01 15:39:39 +00:00
* @return {boolean}
2013-10-01 12:54:29 +00:00
*//**
* Set
2013-10-01 15:39:39 +00:00
* @param {boolean} value - Description
2013-10-01 12:54:29 +00:00
*/
Object.defineProperty(Phaser.Particles.Arcade.Emitter.prototype, "y", {
get: function () {
return this.emitY;
},
set: function (value) {
this.emitY = value;
}
});
2013-10-01 12:54:29 +00:00
/**
* Get
* @return {number}
*/
Object.defineProperty(Phaser.Particles.Arcade.Emitter.prototype, "left", {
get: function () {
return Math.floor(this.x - (this.width / 2));
}
});
2013-10-01 12:54:29 +00:00
/**
* Get
* @return {number}
*/
Object.defineProperty(Phaser.Particles.Arcade.Emitter.prototype, "right", {
get: function () {
return Math.floor(this.x + (this.width / 2));
}
});
2013-10-01 12:54:29 +00:00
/**
* Get
* @return {number}
*/
Object.defineProperty(Phaser.Particles.Arcade.Emitter.prototype, "top", {
get: function () {
return Math.floor(this.y - (this.height / 2));
}
});
2013-10-01 12:54:29 +00:00
/**
* Get
* @return {number}
*/
Object.defineProperty(Phaser.Particles.Arcade.Emitter.prototype, "bottom", {
get: function () {
return Math.floor(this.y + (this.height / 2));
}
});