phaser/src/gameobjects/particles/ParticleEmitter.js

2162 lines
72 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var BlendModes = require('../../renderer/BlendModes');
var Class = require('../../utils/Class');
var Components = require('../components');
var DeathZone = require('./zones/DeathZone');
2017-10-26 16:02:34 +00:00
var EdgeZone = require('./zones/EdgeZone');
var EmitterOp = require('./EmitterOp');
var GetFastValue = require('../../utils/object/GetFastValue');
2018-04-10 03:13:38 +00:00
var GetRandom = require('../../utils/array/GetRandom');
var HasAny = require('../../utils/object/HasAny');
2018-02-06 22:25:23 +00:00
var HasValue = require('../../utils/object/HasValue');
var Particle = require('./Particle');
2017-10-26 16:02:34 +00:00
var RandomZone = require('./zones/RandomZone');
var Rectangle = require('../../geom/rectangle/Rectangle');
var StableSort = require('../../utils/array/StableSort');
var Vector2 = require('../../math/Vector2');
var Wrap = require('../../math/Wrap');
2018-03-19 21:27:16 +00:00
/**
* @callback ParticleEmitterCallback
*
2018-04-18 15:32:03 +00:00
* @param {Phaser.GameObjects.Particles.Particle} particle - The particle associated with the call.
* @param {Phaser.GameObjects.Particles.ParticleEmitter} emitter - This particle emitter associated with the call.
2018-03-19 21:27:16 +00:00
*/
/**
* @callback ParticleDeathCallback
*
2018-04-18 15:32:03 +00:00
* @param {Phaser.GameObjects.Particles.Particle} particle - The particle that died.
*/
/**
* @typedef {object} ParticleEmitterBounds
*
* @property {float} x - The left edge of the rectangle.
* @property {float} y - The top edge of the rectangle.
* @property {float} width - The width of the rectangle.
* @property {float} height - The height of the rectangle.
*
* @see Phaser.GameObjects.Particles.ParticleEmitter#setBounds
*/
/**
* @typedef {object} ParticleEmitterBoundsAlt
*
* @property {float} x - The left edge of the rectangle.
* @property {float} y - The top edge of the rectangle.
* @property {float} w - The width of the rectangle.
* @property {float} h - The height of the rectangle.
*
* @see Phaser.GameObjects.Particles.ParticleEmitter#setBounds
*/
/**
* @typedef {object} ParticleEmitterDeathZoneConfig
*
* @property {DeathZoneSource} source - A shape representing the zone. See {@link Phaser.GameObjects.Particles.Zones.DeathZone#source}.
* @property {string} [type='onEnter'] - 'onEnter' or 'onLeave'.
*/
/**
* @typedef {object} ParticleEmitterEdgeZoneConfig
*
* @property {EdgeZoneSource} source - A shape representing the zone. See {@link Phaser.GameObjects.Particles.Zones.EdgeZone#source}.
2018-04-18 15:32:03 +00:00
* @property {string} type - 'edge'.
* @property {integer} quantity - The number of particles to place on the source edge. Set to 0 to use `stepRate` instead.
* @property {float} [stepRate] - The distance between each particle. When set, `quantity` is implied and should be set to 0.
* @property {boolean} [yoyo=false] - Whether particles are placed from start to end and then end to start.
* @property {boolean} [seamless=true] - Whether one endpoint will be removed if it's identical to the other.
*/
/**
* @typedef {object} ParticleEmitterRandomZoneConfig
*
* @property {RandomZoneSource} source - A shape representing the zone. See {@link Phaser.GameObjects.Particles.Zones.RandomZone#source}.
2018-04-18 15:32:03 +00:00
* @property {string} [type] - 'random'.
*/
/**
* @typedef {object} ParticleEmitterConfig
*
* @property {boolean} [active] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#active}.
* @property {integer} [blendMode] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#blendMode}.
* @property {*} [callbackScope] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#deathCallbackScope} and {@link Phaser.GameObjects.Particles.ParticleEmitter#emitCallbackScope}.
* @property {boolean} [collideBottom] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#collideBottom}.
* @property {boolean} [collideLeft] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#collideLeft}.
* @property {boolean} [collideRight] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#collideRight}.
* @property {boolean} [collideTop] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#collideTop}.
* @property {boolean} [deathCallback] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#deathCallback}.
* @property {*} [deathCallbackScope] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#deathCallbackScope}.
* @property {function} [emitCallback] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#emitCallback}.
* @property {*} [emitCallbackScope] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#emitCallbackScope}.
* @property {Phaser.GameObjects.GameObject} [follow] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#follow}.
* @property {float} [frequency] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#frequency}.
* @property {float} [gravityX] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#gravityX}.
* @property {float} [gravityY] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#gravityY}.
* @property {integer} [maxParticles] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#maxParticles}.
* @property {string} [name] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#name}.
* @property {boolean} [on] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#on}.
* @property {boolean} [particleBringToTop] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#particleBringToTop}.
* @property {Phaser.GameObjects.Particles.Particle} [particleClass] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#particleClass}.
* @property {boolean} [radial] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#radial}.
* @property {float} [timeScale] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#timeScale}.
* @property {boolean} [trackVisible] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#trackVisible}.
* @property {boolean} [visible] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#visible}.
* @property {float|float[]|EmitterOpOnEmitCallback|object} [accelerationX] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#accelerationX} (emit only).
* @property {float|float[]|EmitterOpOnEmitCallback|object} [accelerationY] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#accelerationY} (emit only).
* @property {float|float[]|EmitterOpOnUpdateCallback|object} [alpha] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#alpha}.
* @property {float|float[]|EmitterOpOnEmitCallback|object} [angle] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#angle} (emit only)
* @property {float|float[]|EmitterOpOnEmitCallback|object} [bounce] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#bounce} (emit only).
* @property {float|float[]|EmitterOpOnEmitCallback|object} [delay] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#delay} (emit only).
* @property {float|float[]|EmitterOpOnEmitCallback|object} [lifespan] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#lifespan} (emit only).
* @property {float|float[]|EmitterOpOnEmitCallback|object} [maxVelocityX] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#maxVelocityX} (emit only).
* @property {float|float[]|EmitterOpOnEmitCallback|object} [maxVelocityY] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#maxVelocityY} (emit only).
* @property {float|float[]|EmitterOpOnEmitCallback|object} [moveToX] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#moveToX} (emit only).
* @property {float|float[]|EmitterOpOnEmitCallback|object} [moveToY] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#moveToY} (emit only).
* @property {float|float[]|EmitterOpOnEmitCallback|object} [quantity] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#quantity} (emit only).
* @property {float|float[]|EmitterOpOnUpdateCallback|object} [rotate] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#rotate}.
* @property {float|float[]|EmitterOpOnUpdateCallback|object} [scale] - As {@link Phaser.GameObjects.Particles.ParticleEmitter#setScale}.
* @property {float|float[]|EmitterOpOnUpdateCallback|object} [scaleX] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#scaleX}.
* @property {float|float[]|EmitterOpOnUpdateCallback|object} [scaleY] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#scaleY}.
* @property {float|float[]|EmitterOpOnEmitCallback|object} [speed] - As {@link Phaser.GameObjects.Particles.ParticleEmitter#setSpeed} (emit only).
* @property {float|float[]|EmitterOpOnEmitCallback|object} [speedX] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#speedX} (emit only).
* @property {float|float[]|EmitterOpOnEmitCallback|object} [speedY] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#speedY} (emit only).
* @property {float|float[]|EmitterOpOnEmitCallback|object} [tint] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#tint}.
* @property {float|float[]|EmitterOpOnEmitCallback|object} [x] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#x} (emit only).
* @property {float|float[]|EmitterOpOnEmitCallback|object} [y] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#y} (emit only).
2018-04-18 15:32:03 +00:00
* @property {object} [emitZone] - As {@link Phaser.GameObjects.Particles.ParticleEmitter#setEmitZone}.
* @property {ParticleEmitterBounds|ParticleEmitterBoundsAlt} [bounds] - As {@link Phaser.GameObjects.Particles.ParticleEmitter#setBounds}.
* @property {object} [followOffset] - Assigns to {@link Phaser.GameObjects.Particles.ParticleEmitter#followOffset}.
* @property {float} [followOffset.x] - x-coordinate of the offset.
* @property {float} [followOffset.y] - y-coordinate of the offset.
* @property {number|number[]|string|string[]|Phaser.Textures.Frame|Phaser.Textures.Frame[]|ParticleEmitterFrameConfig} [frames] - Sets {@link Phaser.GameObjects.Particles.ParticleEmitter#frames}.
*/
/**
* @typedef {object} ParticleEmitterFrameConfig
*
* @property {number|number[]|string|string[]|Phaser.Textures.Frame|Phaser.Textures.Frame[]} [frames] - One or more texture frames.
* @property {boolean} [cycle] - Whether texture frames will be assigned consecutively (true) or at random (false).
* @property {integer} [quantity] - The number of consecutive particles receiving each texture frame, when `cycle` is true.
2018-03-19 21:27:16 +00:00
*/
2018-02-06 22:25:23 +00:00
/**
2018-02-07 15:27:21 +00:00
* @classdesc
2018-04-18 15:32:03 +00:00
* A particle emitter represents a single particle stream.
* It controls a pool of {@link Phaser.GameObjects.Particles.Particle Particles} and is controlled by a {@link Phaser.GameObjects.Particles.ParticleEmitterManager Particle Emitter Manager}.
2018-02-06 22:25:23 +00:00
*
* @class ParticleEmitter
* @memberOf Phaser.GameObjects.Particles
* @constructor
* @since 3.0.0
*
* @extends Phaser.GameObjects.Components.BlendMode
* @extends Phaser.GameObjects.Components.Mask
2018-02-06 22:25:23 +00:00
* @extends Phaser.GameObjects.Components.ScrollFactor
* @extends Phaser.GameObjects.Components.Visible
*
* @param {Phaser.GameObjects.Particles.ParticleEmitterManager} manager - The Emitter Manager this Emitter belongs to.
2018-04-18 15:32:03 +00:00
* @param {ParticleEmitterConfig} config - Settings for this emitter.
2018-02-06 22:25:23 +00:00
*/
var ParticleEmitter = new Class({
Mixins: [
Components.BlendMode,
Components.Mask,
Components.ScrollFactor,
Components.Visible
],
initialize:
function ParticleEmitter (manager, config)
{
2018-02-06 22:25:23 +00:00
/**
* The Emitter Manager this Emitter belongs to.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#manager
* @type {Phaser.GameObjects.Particles.ParticleEmitterManager}
* @since 3.0.0
*/
this.manager = manager;
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* The texture assigned to particles.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#texture
* @type {Phaser.Textures.Texture}
* @since 3.0.0
*/
this.texture = manager.texture;
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* The texture frames assigned to particles.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#frames
* @type {Phaser.Textures.Frame[]}
* @since 3.0.0
*/
this.frames = [ manager.defaultFrame ];
2017-10-18 14:18:42 +00:00
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* The default texture frame assigned to particles.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#defaultFrame
* @type {Phaser.Textures.Frame}
* @since 3.0.0
*/
this.defaultFrame = manager.defaultFrame;
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Names of simple configuration properties.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#configFastMap
* @type {object}
* @since 3.0.0
*/
this.configFastMap = [
'active',
'blendMode',
'collideBottom',
'collideLeft',
'collideRight',
'collideTop',
'deathCallback',
'deathCallbackScope',
'emitCallback',
'emitCallbackScope',
'follow',
'frequency',
'gravityX',
'gravityY',
'maxParticles',
'name',
'on',
'particleBringToTop',
'particleClass',
'radial',
'timeScale',
'trackVisible',
'visible'
];
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Names of complex configuration properties.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#configOpMap
* @type {object}
* @since 3.0.0
*/
this.configOpMap = [
'accelerationX',
'accelerationY',
'angle',
'alpha',
'bounce',
'delay',
'lifespan',
'maxVelocityX',
'maxVelocityY',
'moveToX',
'moveToY',
'quantity',
'rotate',
'scaleX',
'scaleY',
'speedX',
'speedY',
'tint',
'x',
'y'
];
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* The name of this Particle Emitter.
2018-03-19 12:50:32 +00:00
*
2018-02-06 22:25:23 +00:00
* Empty by default and never populated by Phaser, this is left for developers to use.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#name
* @type {string}
* @default ''
* @since 3.0.0
*/
this.name = '';
2018-02-06 22:25:23 +00:00
/**
* The Particle Class which will be emitted by this Emitter.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#particleClass
* @type {Phaser.GameObjects.Particles.Particle}
2018-04-18 15:32:03 +00:00
* @default Phaser.GameObjects.Particles.Particle
2018-02-06 22:25:23 +00:00
* @since 3.0.0
*/
this.particleClass = Particle;
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* The x-coordinate of the particle origin (where particles will be emitted).
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#x
2018-04-18 15:32:03 +00:00
* @type {Phaser.GameObjects.Particles.EmitterOp}
* @default 0
2018-02-06 22:25:23 +00:00
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setPosition
2018-02-06 22:25:23 +00:00
*/
this.x = new EmitterOp(config, 'x', 0);
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* The y-coordinate of the particle origin (where particles will be emitted).
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#y
2018-04-18 15:32:03 +00:00
* @type {Phaser.GameObjects.Particles.EmitterOp}
* @default 0
2018-02-06 22:25:23 +00:00
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setPosition
2018-02-06 22:25:23 +00:00
*/
this.y = new EmitterOp(config, 'y', 0);
2018-02-06 22:25:23 +00:00
/**
* A radial emitter will emit particles in all directions between angle min and max,
2018-04-18 15:32:03 +00:00
* using {@link Phaser.GameObjects.Particles.ParticleEmitter#speed} as the value. If set to false then this acts as a point Emitter.
2018-02-06 22:25:23 +00:00
* A point emitter will emit particles only in the direction derived from the speedX and speedY values.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#radial
* @type {boolean}
* @default true
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setRadial
2018-02-06 22:25:23 +00:00
*/
this.radial = true;
2017-10-18 14:18:42 +00:00
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Horizontal acceleration applied to emitted particles, in pixels per second squared.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#gravityX
2018-04-18 15:32:03 +00:00
* @type {float}
2018-02-06 22:25:23 +00:00
* @default 0
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setGravity
2018-02-06 22:25:23 +00:00
*/
this.gravityX = 0;
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Vertical acceleration applied to emitted particles, in pixels per second squared.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#gravityY
2018-04-18 15:32:03 +00:00
* @type {float}
2018-02-06 22:25:23 +00:00
* @default 0
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setGravity
2018-02-06 22:25:23 +00:00
*/
this.gravityY = 0;
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Whether accelerationX and accelerationY are nonzero. Set automatically during configuration.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#acceleration
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.acceleration = false;
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Horizontal acceleration applied to emitted particles, in pixels per second squared.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#accelerationX
2018-04-18 15:32:03 +00:00
* @type {Phaser.GameObjects.Particles.EmitterOp}
2018-02-06 22:25:23 +00:00
* @default 0
* @since 3.0.0
*/
this.accelerationX = new EmitterOp(config, 'accelerationX', 0, true);
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Vertical acceleration applied to emitted particles, in pixels per second squared.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#accelerationY
2018-04-18 15:32:03 +00:00
* @type {Phaser.GameObjects.Particles.EmitterOp}
2018-02-06 22:25:23 +00:00
* @default 0
* @since 3.0.0
*/
this.accelerationY = new EmitterOp(config, 'accelerationY', 0, true);
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* The maximum horizontal velocity of emitted particles, in pixels per second squared.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#maxVelocityX
2018-04-18 15:32:03 +00:00
* @type {Phaser.GameObjects.Particles.EmitterOp}
2018-02-06 22:25:23 +00:00
* @default 10000
* @since 3.0.0
*/
this.maxVelocityX = new EmitterOp(config, 'maxVelocityX', 10000, true);
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* The maximum vertical velocity of emitted particles, in pixels per second squared.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#maxVelocityY
2018-04-18 15:32:03 +00:00
* @type {Phaser.GameObjects.Particles.EmitterOp}
2018-02-06 22:25:23 +00:00
* @default 10000
* @since 3.0.0
*/
this.maxVelocityY = new EmitterOp(config, 'maxVelocityY', 10000, true);
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* The initial horizontal speed of emitted particles, in pixels per second.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#speedX
2018-04-18 15:32:03 +00:00
* @type {Phaser.GameObjects.Particles.EmitterOp}
2018-02-06 22:25:23 +00:00
* @default 0
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setSpeedX
2018-02-06 22:25:23 +00:00
*/
this.speedX = new EmitterOp(config, 'speedX', 0, true);
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* The initial vertical speed of emitted particles, in pixels per second.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#speedY
2018-04-18 15:32:03 +00:00
* @type {Phaser.GameObjects.Particles.EmitterOp}
2018-02-06 22:25:23 +00:00
* @default 0
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setSpeedY
2018-02-06 22:25:23 +00:00
*/
this.speedY = new EmitterOp(config, 'speedY', 0, true);
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Whether moveToX and moveToY are nonzero. Set automatically during configuration.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#moveTo
* @type {boolean}
* @default false
* @since 3.0.0
*/
2017-10-27 20:19:21 +00:00
this.moveTo = false;
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* The x-coordinate emitted particles move toward, when {@link Phaser.GameObjects.Particles.ParticleEmitter#moveTo} is true.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#moveToX
2018-04-18 15:32:03 +00:00
* @type {Phaser.GameObjects.Particles.EmitterOp}
2018-02-06 22:25:23 +00:00
* @default 0
* @since 3.0.0
*/
2017-10-27 20:19:21 +00:00
this.moveToX = new EmitterOp(config, 'moveToX', 0, true);
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* The y-coordinate emitted particles move toward, when {@link Phaser.GameObjects.Particles.ParticleEmitter#moveTo} is true.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#moveToY
2018-04-18 15:32:03 +00:00
* @type {Phaser.GameObjects.Particles.EmitterOp}
2018-02-06 22:25:23 +00:00
* @default 0
* @since 3.0.0
*/
2017-10-27 20:19:21 +00:00
this.moveToY = new EmitterOp(config, 'moveToY', 0, true);
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Whether particles will rebound when they meet the emitter bounds.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#bounce
2018-04-18 15:32:03 +00:00
* @type {Phaser.GameObjects.Particles.EmitterOp}
2018-02-06 22:25:23 +00:00
* @default 0
* @since 3.0.0
*/
this.bounce = new EmitterOp(config, 'bounce', 0, true);
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* The horizontal scale of emitted particles.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#scaleX
2018-04-18 15:32:03 +00:00
* @type {Phaser.GameObjects.Particles.EmitterOp}
2018-02-06 22:25:23 +00:00
* @default 1
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setScale
* @see Phaser.GameObjects.Particles.ParticleEmitter#setScaleX
2018-02-06 22:25:23 +00:00
*/
this.scaleX = new EmitterOp(config, 'scaleX', 1);
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* The vertical scale of emitted particles.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#scaleY
2018-04-18 15:32:03 +00:00
* @type {Phaser.GameObjects.Particles.EmitterOp}
2018-02-06 22:25:23 +00:00
* @default 1
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setScale
* @see Phaser.GameObjects.Particles.ParticleEmitter#setScaleY
2018-02-06 22:25:23 +00:00
*/
this.scaleY = new EmitterOp(config, 'scaleY', 1);
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Color tint applied to emitted particles. Any alpha component (0xAA000000) is ignored.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#tint
2018-04-18 15:32:03 +00:00
* @type {Phaser.GameObjects.Particles.EmitterOp}
* @default 0xffffffff
2018-02-06 22:25:23 +00:00
* @since 3.0.0
*/
2017-10-27 20:19:21 +00:00
this.tint = new EmitterOp(config, 'tint', 0xffffffff);
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* The alpha (transparency) of emitted particles.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#alpha
2018-04-18 15:32:03 +00:00
* @type {Phaser.GameObjects.Particles.EmitterOp}
2018-02-06 22:25:23 +00:00
* @default 1
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setAlpha
2018-02-06 22:25:23 +00:00
*/
this.alpha = new EmitterOp(config, 'alpha', 1);
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* The lifespan of emitted particles, in ms.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#lifespan
2018-04-18 15:32:03 +00:00
* @type {Phaser.GameObjects.Particles.EmitterOp}
2018-02-06 22:25:23 +00:00
* @default 1000
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setLifespan
2018-02-06 22:25:23 +00:00
*/
this.lifespan = new EmitterOp(config, 'lifespan', 1000);
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* The angle of the initial velocity of emitted particles, in degrees.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#angle
2018-04-18 15:32:03 +00:00
* @type {Phaser.GameObjects.Particles.EmitterOp}
* @default { min: 0, max: 360 }
2018-02-06 22:25:23 +00:00
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setAngle
2018-02-06 22:25:23 +00:00
*/
2017-10-24 02:31:54 +00:00
this.angle = new EmitterOp(config, 'angle', { min: 0, max: 360 });
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* The rotation of emitted particles, in degrees.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#rotate
2018-04-18 15:32:03 +00:00
* @type {Phaser.GameObjects.Particles.EmitterOp}
2018-02-06 22:25:23 +00:00
* @default 0
* @since 3.0.0
*/
2017-10-24 02:31:54 +00:00
this.rotate = new EmitterOp(config, 'rotate', 0);
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* A function to call when a particle is emitted.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#emitCallback
2018-03-19 21:27:16 +00:00
* @type {?ParticleEmitterCallback}
2018-02-06 22:25:23 +00:00
* @default null
* @since 3.0.0
*/
this.emitCallback = null;
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* The calling context for {@link Phaser.GameObjects.Particles.ParticleEmitter#emitCallback}.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#emitCallbackScope
2018-03-20 16:15:49 +00:00
* @type {?*}
2018-02-06 22:25:23 +00:00
* @default null
* @since 3.0.0
*/
this.emitCallbackScope = null;
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* A function to call when a particle dies.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#deathCallback
2018-03-19 21:27:16 +00:00
* @type {?ParticleDeathCallback}
2018-02-06 22:25:23 +00:00
* @default null
* @since 3.0.0
*/
this.deathCallback = null;
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* The calling context for {@link Phaser.GameObjects.Particles.ParticleEmitter#deathCallback}.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#deathCallbackScope
2018-03-20 16:15:49 +00:00
* @type {?*}
2018-02-06 22:25:23 +00:00
* @default null
* @since 3.0.0
*/
this.deathCallbackScope = null;
2018-02-06 22:25:23 +00:00
/**
* Set to hard limit the amount of particle objects this emitter is allowed to create.
* 0 means unlimited.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#maxParticles
2018-04-18 15:32:03 +00:00
* @type {integer}
2018-02-06 22:25:23 +00:00
* @default 0
* @since 3.0.0
*/
this.maxParticles = 0;
2017-10-18 14:18:42 +00:00
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* How many particles are emitted each time particles are emitted (one explosion or one flow cycle).
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#quantity
2018-04-18 15:32:03 +00:00
* @type {Phaser.GameObjects.Particles.EmitterOp}
2018-02-06 22:25:23 +00:00
* @default 1
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setFrequency
* @see Phaser.GameObjects.Particles.ParticleEmitter#setQuantity
2018-02-06 22:25:23 +00:00
*/
this.quantity = new EmitterOp(config, 'quantity', 1, true);
2018-02-06 22:25:23 +00:00
/**
* How many ms to wait after emission before the particles start updating.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#delay
2018-04-18 15:32:03 +00:00
* @type {Phaser.GameObjects.Particles.EmitterOp}
* @default 0
2018-02-06 22:25:23 +00:00
* @since 3.0.0
*/
2017-10-27 11:31:37 +00:00
this.delay = new EmitterOp(config, 'delay', 0, true);
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* For a flow emitter, the time interval (>= 0) between particle flow cycles in ms.
* A value of 0 means there is one particle flow cycle for each logic update (the maximum flow frequency). This is the default setting.
* For an exploding emitter, this value will be -1.
* Calling {@link Phaser.GameObjects.Particles.ParticleEmitter#flow} also puts the emitter in flow mode (frequency >= 0).
* Calling {@link Phaser.GameObjects.Particles.ParticleEmitter#explode} also puts the emitter in explode mode (frequency = -1).
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#frequency
2018-04-18 15:32:03 +00:00
* @type {float}
2018-02-06 22:25:23 +00:00
* @default 0
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setFrequency
2018-02-06 22:25:23 +00:00
*/
this.frequency = 0;
2017-10-18 14:18:42 +00:00
2018-02-06 22:25:23 +00:00
/**
* Controls if the emitter is currently emitting a particle flow (when frequency >= 0).
2018-02-06 22:25:23 +00:00
* Already alive particles will continue to update until they expire.
2018-04-18 15:32:03 +00:00
* Controlled by {@link Phaser.GameObjects.Particles.ParticleEmitter#start}.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#on
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.on = true;
2017-10-18 14:18:42 +00:00
2018-02-06 22:25:23 +00:00
/**
* Newly emitted particles are added to the top of the particle list, i.e. rendered above those already alive.
* Set to false to send them to the back.
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#particleBringToTop
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.particleBringToTop = true;
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* The time rate applied to active particles, affecting lifespan, movement, and tweens. Values larger than 1 are faster than normal.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#timeScale
2018-04-18 15:32:03 +00:00
* @type {float}
2018-02-06 22:25:23 +00:00
* @default 1
* @since 3.0.0
*/
this.timeScale = 1;
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* An object describing a shape to emit particles from.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#emitZone
2018-04-18 15:32:03 +00:00
* @type {?Phaser.GameObjects.Particles.Zones.EdgeZone|Phaser.GameObjects.Particles.Zones.RandomZone}
2018-02-06 22:25:23 +00:00
* @default null
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setEmitZone
2018-02-06 22:25:23 +00:00
*/
this.emitZone = null;
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* An object describing a shape that deactivates particles when they interact with it.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#deathZone
* @type {?Phaser.GameObjects.Particles.Zones.DeathZone}
2018-02-06 22:25:23 +00:00
* @default null
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setDeathZone
2018-02-06 22:25:23 +00:00
*/
this.deathZone = null;
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* A rectangular boundary constraining particle movement.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#bounds
* @type {?Phaser.Geom.Rectangle}
* @default null
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setBounds
2018-02-06 22:25:23 +00:00
*/
this.bounds = null;
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Whether particles interact with the left edge of the emitter {@link Phaser.GameObjects.Particles.ParticleEmitter#bounds}.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#collideLeft
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.collideLeft = true;
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Whether particles interact with the right edge of the emitter {@link Phaser.GameObjects.Particles.ParticleEmitter#bounds}.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#collideRight
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.collideRight = true;
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Whether particles interact with the top edge of the emitter {@link Phaser.GameObjects.Particles.ParticleEmitter#bounds}.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#collideTop
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.collideTop = true;
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Whether particles interact with the bottom edge of the emitter {@link Phaser.GameObjects.Particles.ParticleEmitter#bounds}.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#collideBottom
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.collideBottom = true;
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Whether this emitter updates itself and its particles.
*
* Controlled by {@link Phaser.GameObjects.Particles.ParticleEmitter#pause}
* and {@link Phaser.GameObjects.Particles.ParticleEmitter#resume}.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#active
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.active = true;
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Set this to false to hide any active particles.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#visible
* @type {boolean}
* @default true
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setVisible
2018-02-06 22:25:23 +00:00
*/
this.visible = true;
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* The blend mode of this emitter's particles.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#blendMode
* @type {integer}
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setBlendMode
2018-02-06 22:25:23 +00:00
*/
this.blendMode = BlendModes.NORMAL;
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* A Game Object whose position is used as the particle origin.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#follow
2018-04-18 15:32:03 +00:00
* @type {?Phaser.GameObjects.GameObject}
2018-02-06 22:25:23 +00:00
* @default null
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#startFollow
* @see Phaser.GameObjects.Particles.ParticleEmitter#stopFollow
2018-02-06 22:25:23 +00:00
*/
this.follow = null;
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* The offset of the particle origin from the {@link Phaser.GameObjects.Particles.ParticleEmitter#follow} target.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#followOffset
* @type {Phaser.Math.Vector2}
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#startFollow
2018-02-06 22:25:23 +00:00
*/
this.followOffset = new Vector2();
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Whether the emitter's {@link Phaser.GameObjects.Particles.ParticleEmitter#visible} state will track
* the {@link Phaser.GameObjects.Particles.ParticleEmitter#follow} target's visibility state.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#trackVisible
* @type {boolean}
* @default false
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#startFollow
2018-02-06 22:25:23 +00:00
*/
this.trackVisible = false;
2018-02-06 22:25:23 +00:00
/**
* The current texture frame, as an index of {@link Phaser.GameObjects.Particles.ParticleEmitter#frames}.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#currentFrame
2018-04-18 15:32:03 +00:00
* @type {integer}
2018-02-06 22:25:23 +00:00
* @default 0
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setFrame
2018-02-06 22:25:23 +00:00
*/
this.currentFrame = 0;
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Whether texture {@link Phaser.GameObjects.Particles.ParticleEmitter#frames} are selected at random.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#randomFrame
* @type {boolean}
* @default true
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setFrame
2018-02-06 22:25:23 +00:00
*/
this.randomFrame = true;
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* The number of consecutive particles that receive a single texture frame (per frame cycle).
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#frameQuantity
2018-04-18 15:32:03 +00:00
* @type {integer}
2018-02-06 22:25:23 +00:00
* @default 1
* @since 3.0.0
* @see Phaser.GameObjects.Particles.ParticleEmitter#setFrame
2018-02-06 22:25:23 +00:00
*/
this.frameQuantity = 1;
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Inactive particles.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#dead
2018-03-19 12:50:32 +00:00
* @type {Phaser.GameObjects.Particles.Particle[]}
2018-02-06 22:25:23 +00:00
* @private
* @since 3.0.0
*/
this.dead = [];
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Active particles
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#alive
2018-03-19 12:50:32 +00:00
* @type {Phaser.GameObjects.Particles.Particle[]}
2018-02-06 22:25:23 +00:00
* @private
* @since 3.0.0
*/
this.alive = [];
2018-02-06 22:25:23 +00:00
/**
* The time until the next flow cycle.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#_counter
2018-04-18 15:32:03 +00:00
* @type {float}
2018-02-06 22:25:23 +00:00
* @private
* @default 0
* @since 3.0.0
*/
2017-10-18 14:18:42 +00:00
this._counter = 0;
2018-02-06 22:25:23 +00:00
/**
* Counts up to {@link Phaser.GameObjects.Particles.ParticleEmitter#frameQuantity}.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitter#_frameCounter
2018-04-18 15:32:03 +00:00
* @type {integer}
2018-02-06 22:25:23 +00:00
* @private
* @default 0
* @since 3.0.0
*/
this._frameCounter = 0;
if (config)
{
this.fromJSON(config);
}
},
2017-10-26 16:02:34 +00:00
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Merges configuration settings into the emitter's current settings.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#fromJSON
* @since 3.0.0
*
2018-04-18 15:32:03 +00:00
* @param {ParticleEmitterConfig} config - Settings for this emitter.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
fromJSON: function (config)
{
if (!config)
2017-10-26 16:02:34 +00:00
{
return this;
}
// Only update properties from their current state if they exist in the given config
var i = 0;
var key = '';
for (i = 0; i < this.configFastMap.length; i++)
{
key = this.configFastMap[i];
if (HasValue(config, key))
{
this[key] = GetFastValue(config, key);
}
2017-10-26 16:02:34 +00:00
}
for (i = 0; i < this.configOpMap.length; i++)
{
key = this.configOpMap[i];
if (HasValue(config, key))
{
this[key].loadConfig(config);
}
}
this.acceleration = (this.accelerationX.propertyValue !== 0 || this.accelerationY.propertyValue !== 0);
this.moveTo = (this.moveToX.propertyValue !== 0 || this.moveToY.propertyValue !== 0);
// Special 'speed' override
if (HasValue(config, 'speed'))
{
this.speedX.loadConfig(config, 'speed');
this.speedY = null;
}
2018-04-18 15:32:03 +00:00
// If you specify speedX, speedY or moveTo then it changes the emitter from radial to a point emitter
if (HasAny(config, [ 'speedX', 'speedY' ]) || this.moveTo)
{
this.radial = false;
}
// Special 'scale' override
if (HasValue(config, 'scale'))
{
this.scaleX.loadConfig(config, 'scale');
this.scaleY = null;
}
if (HasValue(config, 'callbackScope'))
{
var callbackScope = GetFastValue(config, 'callbackScope', null);
this.emitCallbackScope = callbackScope;
this.deathCallbackScope = callbackScope;
}
2017-10-20 02:48:42 +00:00
if (HasValue(config, 'emitZone'))
{
this.setEmitZone(config.emitZone);
}
if (HasValue(config, 'deathZone'))
{
this.setDeathZone(config.deathZone);
}
if (HasValue(config, 'bounds'))
{
this.setBounds(config.bounds);
}
if (HasValue(config, 'followOffset'))
{
this.followOffset.setFromObject(GetFastValue(config, 'followOffset', 0));
}
if (HasValue(config, 'frame'))
{
this.setFrame(config.frame);
}
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Creates a description of this emitter suitable for JSON serialization.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#toJSON
* @since 3.0.0
*
2018-04-18 15:32:03 +00:00
* @param {object} [output] - An object to copy output into.
2018-02-06 22:25:23 +00:00
*
2018-04-18 15:32:03 +00:00
* @return {object} - The output object.
2018-02-06 22:25:23 +00:00
*/
toJSON: function (output)
{
if (output === undefined) { output = {}; }
var i = 0;
var key = '';
for (i = 0; i < this.configFastMap.length; i++)
{
key = this.configFastMap[i];
output[key] = this[key];
}
for (i = 0; i < this.configOpMap.length; i++)
{
key = this.configOpMap[i];
if (this[key])
{
output[key] = this[key].toJSON();
}
}
// special handlers
if (!this.speedY)
{
delete output.speedX;
output.speed = this.speedX.toJSON();
}
if (!this.scaleY)
{
delete output.scaleX;
output.scale = this.scaleX.toJSON();
}
return output;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Continuously moves the particle origin to follow a Game Object's position.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#startFollow
* @since 3.0.0
*
2018-04-18 15:32:03 +00:00
* @param {Phaser.GameObjects.Particles.Particle} target - The Game Object to follow.
* @param {float} [offsetX=0] - Horizontal offset of the particle origin from the Game Object.
* @param {float} [offsetY=0] - Vertical offset of the particle origin from the Game Object.
* @param {boolean} [trackVisible=false] - Whether the emitter's visible state will track the target's visible state.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
startFollow: function (target, offsetX, offsetY, trackVisible)
2017-10-20 02:48:42 +00:00
{
if (offsetX === undefined) { offsetX = 0; }
if (offsetY === undefined) { offsetY = 0; }
if (trackVisible === undefined) { trackVisible = false; }
this.follow = target;
this.followOffset.set(offsetX, offsetY);
this.trackVisible = trackVisible;
2017-10-20 02:48:42 +00:00
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Stops following a Game Object.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#stopFollow
* @since 3.0.0
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
2017-10-20 02:48:42 +00:00
stopFollow: function ()
{
this.follow = null;
this.followOffset.set(0, 0);
this.trackVisible = false;
2017-10-20 02:48:42 +00:00
return this;
},
2018-02-06 22:25:23 +00:00
/**
* Chooses a texture frame from {@link Phaser.GameObjects.Particles.ParticleEmitter#frames}.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#getFrame
* @since 3.0.0
*
2018-04-18 15:32:03 +00:00
* @return {Phaser.Textures.Frame} The texture frame.
2018-02-06 22:25:23 +00:00
*/
2017-10-18 14:18:42 +00:00
getFrame: function ()
{
if (this.frames.length === 1)
{
return this.defaultFrame;
}
else if (this.randomFrame)
{
2018-04-10 03:13:38 +00:00
return GetRandom(this.frames);
}
else
{
var frame = this.frames[this.currentFrame];
this._frameCounter++;
if (this._frameCounter === this.frameQuantity)
{
this._frameCounter = 0;
2018-02-16 18:17:51 +00:00
this.currentFrame = Wrap(this.currentFrame + 1, 0, this._frameLength);
}
return frame;
}
2017-10-18 14:18:42 +00:00
},
// frame: 0
// frame: 'red'
// frame: [ 0, 1, 2, 3 ]
// frame: [ 'red', 'green', 'blue', 'pink', 'white' ]
// frame: { frames: [ 'red', 'green', 'blue', 'pink', 'white' ], [cycle: bool], [quantity: int] }
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Sets a pattern for assigning texture frames to emitted particles.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#setFrame
* @since 3.0.0
*
2018-04-18 15:32:03 +00:00
* @param {(array|string|integer|ParticleEmitterFrameConfig)} frames - One or more texture frames, or a configuration object.
* @param {boolean} [pickRandom=true] - Whether frames should be assigned at random from `frames`.
* @param {integer} [quantity=1] - The number of consecutive particles that will receive each frame.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
setFrame: function (frames, pickRandom, quantity)
{
if (pickRandom === undefined) { pickRandom = true; }
if (quantity === undefined) { quantity = 1; }
this.randomFrame = pickRandom;
this.frameQuantity = quantity;
this.currentFrame = 0;
this._frameCounter = 0;
var t = typeof (frames);
if (Array.isArray(frames) || t === 'string' || t === 'number')
{
this.manager.setEmitterFrames(frames, this);
}
else if (t === 'object')
{
var frameConfig = frames;
2018-03-19 12:50:32 +00:00
2018-02-16 19:08:50 +00:00
frames = GetFastValue(frameConfig, 'frames', null);
if (frames)
{
this.manager.setEmitterFrames(frames, this);
}
var isCycle = GetFastValue(frameConfig, 'cycle', false);
this.randomFrame = (isCycle) ? false : true;
this.frameQuantity = GetFastValue(frameConfig, 'quantity', quantity);
}
this._frameLength = this.frames.length;
if (this._frameLength === 1)
{
this.frameQuantity = 1;
this.randomFrame = false;
}
2017-10-18 14:18:42 +00:00
return this;
},
2018-02-06 22:25:23 +00:00
/**
* Turns {@link Phaser.GameObjects.Particles.ParticleEmitter#radial} particle movement on or off.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#setRadial
* @since 3.0.0
*
2018-04-18 15:32:03 +00:00
* @param {boolean} [value=true] - Radial mode (true) or point mode (true).
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
2017-10-18 14:18:42 +00:00
setRadial: function (value)
{
if (value === undefined) { value = true; }
this.radial = value;
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Sets the position of the emitter's particle origin.
* New particles will be emitted here.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#setPosition
* @since 3.0.0
*
* @param {float|float[]|EmitterOpOnEmitCallback|object} x - The x-coordinate of the particle origin.
* @param {float|float[]|EmitterOpOnEmitCallback|object} y - The y-coordinate of the particle origin.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
setPosition: function (x, y)
{
this.x.onChange(x);
this.y.onChange(y);
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Sets or modifies a rectangular boundary constraining the particles.
*
* To remove the boundary, set {@link Phaser.GameObjects.Particles.ParticleEmitter#bounds} to null.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#setBounds
* @since 3.0.0
*
2018-04-18 15:32:03 +00:00
* @param {(number|ParticleEmitterBounds|ParticleEmitterBoundsAlt)} x - The x-coordinate of the left edge of the boundary, or an object representing a rectangle.
* @param {float} y - The y-coordinate of the top edge of the boundary.
* @param {float} width - The width of the boundary.
* @param {float} height - The height of the boundary.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
setBounds: function (x, y, width, height)
{
if (typeof x === 'object')
{
var obj = x;
2018-02-16 19:08:50 +00:00
x = obj.x;
y = obj.y;
width = (HasValue(obj, 'w')) ? obj.w : obj.width;
height = (HasValue(obj, 'h')) ? obj.h : obj.height;
}
if (this.bounds)
{
this.bounds.setTo(x, y, width, height);
}
else
{
this.bounds = new Rectangle(x, y, width, height);
}
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Sets the initial horizontal speed of emitted particles.
* Changes the emitter to point mode.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#setSpeedX
* @since 3.0.0
*
* @param {float|float[]|EmitterOpOnEmitCallback|object} value - The speed, in pixels per second.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
setSpeedX: function (value)
{
this.speedX.onChange(value);
// If you specify speedX and Y then it changes the emitter from radial to a point emitter
this.radial = false;
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Sets the initial vertical speed of emitted particles.
* Changes the emitter to point mode.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#setSpeedY
* @since 3.0.0
*
* @param {float|float[]|EmitterOpOnEmitCallback|object} value - The speed, in pixels per second.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
setSpeedY: function (value)
{
if (this.speedY)
{
this.speedY.onChange(value);
// If you specify speedX and Y then it changes the emitter from radial to a point emitter
this.radial = false;
}
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Sets the initial radial speed of emitted particles.
* Changes the emitter to radial mode.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#setSpeed
* @since 3.0.0
*
* @param {float|float[]|EmitterOpOnEmitCallback|object} value - The speed, in pixels per second.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
setSpeed: function (value)
{
this.speedX.onChange(value);
this.speedY = null;
// If you specify speedX and Y then it changes the emitter from radial to a point emitter
this.radial = true;
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Sets the horizontal scale of emitted particles.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#setScaleX
* @since 3.0.0
*
* @param {float|float[]|EmitterOpOnUpdateCallback|object} value - The scale, relative to 1.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
setScaleX: function (value)
{
this.scaleX.onChange(value);
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Sets the vertical scale of emitted particles.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#setScaleY
* @since 3.0.0
*
* @param {float|float[]|EmitterOpOnUpdateCallback|object} value - The scale, relative to 1.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
setScaleY: function (value)
{
this.scaleY.onChange(value);
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Sets the scale of emitted particles.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#setScale
* @since 3.0.0
*
* @param {float|float[]|EmitterOpOnUpdateCallback|object} value - The scale, relative to 1.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
setScale: function (value)
{
this.scaleX.onChange(value);
this.scaleY = null;
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Sets the horizontal gravity applied to emitted particles.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#setGravityX
* @since 3.0.0
*
2018-04-18 15:32:03 +00:00
* @param {float} value - Acceleration due to gravity, in pixels per second squared.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
setGravityX: function (value)
{
this.gravityX = value;
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Sets the vertical gravity applied to emitted particles.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#setGravityY
* @since 3.0.0
*
2018-04-18 15:32:03 +00:00
* @param {float} value - Acceleration due to gravity, in pixels per second squared.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
setGravityY: function (value)
{
this.gravityY = value;
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Sets the gravity applied to emitted particles.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#setGravity
* @since 3.0.0
*
2018-04-18 15:32:03 +00:00
* @param {float} x - Horizontal acceleration due to gravity, in pixels per second squared.
* @param {float} y - Vertical acceleration due to gravity, in pixels per second squared.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
2017-10-18 01:26:15 +00:00
setGravity: function (x, y)
{
this.gravityX = x;
this.gravityY = y;
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Sets the opacity of emitted particles.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#setAlpha
* @since 3.0.0
*
* @param {float|float[]|EmitterOpOnUpdateCallback|object} value - A value between 0 (transparent) and 1 (opaque).
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
setAlpha: function (value)
{
this.alpha.onChange(value);
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Sets the angle of a {@link Phaser.GameObjects.Particles.ParticleEmitter#radial} particle stream.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#setEmitterAngle
* @since 3.0.0
*
* @param {float|float[]|EmitterOpOnEmitCallback|object} value - The angle of the initial velocity of emitted particles.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
setEmitterAngle: function (value)
{
2017-10-24 02:31:54 +00:00
this.angle.onChange(value);
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Sets the angle of a {@link Phaser.GameObjects.Particles.ParticleEmitter#radial} particle stream.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#setAngle
* @since 3.0.0
*
* @param {float|float[]|EmitterOpOnEmitCallback|object} value - The angle of the initial velocity of emitted particles.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
setAngle: function (value)
{
this.angle.onChange(value);
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Sets the lifespan of newly emitted particles.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#setLifespan
* @since 3.0.0
*
* @param {float|float[]|EmitterOpOnEmitCallback|object} value - The particle lifespan, in ms.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
setLifespan: function (value)
{
this.lifespan.onChange(value);
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Sets the number of particles released at each flow cycle or explosion.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#setQuantity
* @since 3.0.0
*
* @param {float|float[]|EmitterOpOnEmitCallback|object} quantity - The number of particles to release at each flow cycle or explosion.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
setQuantity: function (quantity)
{
2017-10-24 02:31:54 +00:00
this.quantity.onChange(quantity);
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Sets the emitter's {@link Phaser.GameObjects.Particles.ParticleEmitter#frequency}
* and {@link Phaser.GameObjects.Particles.ParticleEmitter#quantity}.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#setFrequency
* @since 3.0.0
*
2018-04-18 15:32:03 +00:00
* @param {float} frequency - The time interval (>= 0) of each flow cycle, in ms; or -1 to put the emitter in explosion mode.
* @param {float|float[]|EmitterOpOnEmitCallback|object} [quantity] - The number of particles to release at each flow cycle or explosion.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
setFrequency: function (frequency, quantity)
{
2017-10-18 14:18:42 +00:00
this.frequency = frequency;
2017-10-18 14:18:42 +00:00
this._counter = 0;
if (quantity)
{
2017-10-24 02:31:54 +00:00
this.quantity.onChange(quantity);
}
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Sets or removes the {@link Phaser.GameObjects.Particles.ParticleEmitter#emitZone}.
*
* An {@link ParticleEmitterEdgeZoneConfig EdgeZone} places particles on its edges. Its {@link EdgeZoneSource source} can be a Curve, Path, Circle, Ellipse, Line, Polygon, Rectangle, or Triangle; or any object with a suitable {@link EdgeZoneSourceCallback getPoints} method.
2018-04-18 15:32:03 +00:00
*
* A {@link ParticleEmitterRandomZoneConfig RandomZone} places randomly within its interior. Its {@link RandomZoneSource source} can be a Circle, Ellipse, Line, Polygon, Rectangle, or Triangle; or any object with a suitable {@link RandomZoneSourceCallback getRandomPoint} method.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#setEmitZone
* @since 3.0.0
*
2018-04-18 15:32:03 +00:00
* @param {ParticleEmitterEdgeZoneConfig|ParticleEmitterRandomZoneConfig} [zoneConfig] - An object describing the zone, or `undefined` to remove any current emit zone.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
setEmitZone: function (zoneConfig)
{
if (zoneConfig === undefined)
2017-10-18 14:18:42 +00:00
{
this.emitZone = null;
2017-10-18 14:18:42 +00:00
}
2017-10-26 16:02:34 +00:00
else
2017-10-18 14:18:42 +00:00
{
2017-10-26 16:02:34 +00:00
// Where source = Geom like Circle, or a Path or Curve
// emitZone: { type: 'random', source: X }
// emitZone: { type: 'edge', source: X, quantity: 32, [stepRate=0], [yoyo=false], [seamless=true] }
2017-10-26 16:02:34 +00:00
var type = GetFastValue(zoneConfig, 'type', 'random');
var source = GetFastValue(zoneConfig, 'source', null);
2017-10-26 16:02:34 +00:00
2018-04-28 16:07:07 +00:00
switch (type)
2017-10-26 16:02:34 +00:00
{
2018-04-28 16:07:07 +00:00
case 'random':
2018-04-28 16:07:07 +00:00
this.emitZone = new RandomZone(source);
2018-04-28 16:07:07 +00:00
break;
2018-04-28 16:07:07 +00:00
case 'edge':
2018-04-28 16:07:07 +00:00
var quantity = GetFastValue(zoneConfig, 'quantity', 1);
var stepRate = GetFastValue(zoneConfig, 'stepRate', 0);
var yoyo = GetFastValue(zoneConfig, 'yoyo', false);
var seamless = GetFastValue(zoneConfig, 'seamless', true);
2018-04-28 16:07:07 +00:00
this.emitZone = new EdgeZone(source, quantity, stepRate, yoyo, seamless);
2018-04-28 16:07:07 +00:00
break;
2017-10-26 16:02:34 +00:00
}
2017-10-18 14:18:42 +00:00
}
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Sets or removes the {@link Phaser.GameObjects.Particles.ParticleEmitter#deathZone}.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#setDeathZone
* @since 3.0.0
*
* @param {ParticleEmitterDeathZoneConfig} [zoneConfig] - An object describing the zone, or `undefined` to remove any current death zone.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
setDeathZone: function (zoneConfig)
{
if (zoneConfig === undefined)
{
this.deathZone = null;
}
else
{
2018-02-06 22:25:23 +00:00
// Where source = Geom like Circle or Rect that supports a 'contains' function
// deathZone: { type: 'onEnter', source: X }
// deathZone: { type: 'onLeave', source: X }
var type = GetFastValue(zoneConfig, 'type', 'onEnter');
var source = GetFastValue(zoneConfig, 'source', null);
if (source && typeof source.contains === 'function')
{
var killOnEnter = (type === 'onEnter') ? true : false;
this.deathZone = new DeathZone(source, killOnEnter);
}
}
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Creates inactive particles and adds them to this emitter's pool.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#reserve
* @since 3.0.0
*
2018-04-18 15:32:03 +00:00
* @param {integer} particleCount - The number of particles to create.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
reserve: function (particleCount)
{
var dead = this.dead;
2017-10-18 14:18:42 +00:00
for (var i = 0; i < particleCount; i++)
{
dead.push(new this.particleClass(this));
}
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Gets the number of active (in-use) particles in this emitter.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#getAliveParticleCount
* @since 3.0.0
*
2018-04-18 15:32:03 +00:00
* @return {integer} The number of particles with `active=true`.
2018-02-06 22:25:23 +00:00
*/
getAliveParticleCount: function ()
{
return this.alive.length;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Gets the number of inactive (available) particles in this emitter.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#getDeadParticleCount
* @since 3.0.0
*
2018-04-18 15:32:03 +00:00
* @return {integer} The number of particles with `active=false`.
2018-02-06 22:25:23 +00:00
*/
getDeadParticleCount: function ()
{
return this.dead.length;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Gets the total number of particles in this emitter.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#getParticleCount
* @since 3.0.0
*
2018-04-18 15:32:03 +00:00
* @return {integer} The number of particles, including both alive and dead.
2018-02-06 22:25:23 +00:00
*/
getParticleCount: function ()
{
return this.getAliveParticleCount() + this.getDeadParticleCount();
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Whether this emitter is at its limit (if set).
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#atLimit
* @since 3.0.0
*
* @return {boolean} Returns `true` if this Emitter is at its limit, or `false` if no limit, or below the `maxParticles` level.
*/
2017-10-18 14:18:42 +00:00
atLimit: function ()
{
return (this.maxParticles > 0 && this.getParticleCount() === this.maxParticles);
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Sets a function to call for each newly emitted particle.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#onParticleEmit
* @since 3.0.0
*
2018-04-18 15:32:03 +00:00
* @param {ParticleEmitterCallback} callback - The function.
* @param {*} [context] - The calling context.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
onParticleEmit: function (callback, context)
{
if (callback === undefined)
{
// Clear any previously set callback
this.emitCallback = null;
this.emitCallbackScope = null;
}
else if (typeof callback === 'function')
{
this.emitCallback = callback;
if (context)
{
this.emitCallbackScope = context;
}
}
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Sets a function to call for each particle death.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#onParticleDeath
* @since 3.0.0
*
2018-04-18 15:32:03 +00:00
* @param {ParticleDeathCallback} callback - The function.
* @param {*} [context] - The function's calling context.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
onParticleDeath: function (callback, context)
{
if (callback === undefined)
{
// Clear any previously set callback
this.deathCallback = null;
this.deathCallbackScope = null;
}
else if (typeof callback === 'function')
{
this.deathCallback = callback;
if (context)
{
this.deathCallbackScope = context;
}
}
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Deactivates every particle in this emitter.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#killAll
* @since 3.0.0
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
killAll: function ()
{
var dead = this.dead;
var alive = this.alive;
while (alive.length > 0)
{
dead.push(alive.pop());
}
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Calls a function for each active particle in this emitter.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#forEachAlive
* @since 3.0.0
*
2018-04-18 15:32:03 +00:00
* @param {ParticleEmitterCallback} callback - The function.
2018-06-13 07:37:40 +00:00
* @param {*} context - The function's calling context.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
2018-06-13 07:37:40 +00:00
forEachAlive: function (callback, context)
{
var alive = this.alive;
var length = alive.length;
for (var index = 0; index < length; ++index)
{
2017-10-18 14:18:42 +00:00
// Sends the Particle and the Emitter
2018-06-13 07:37:40 +00:00
callback.call(context, alive[index], this);
}
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Calls a function for each inactive particle in this emitter.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#forEachDead
* @since 3.0.0
*
2018-04-18 15:32:03 +00:00
* @param {ParticleEmitterCallback} callback - The function.
2018-06-13 07:37:40 +00:00
* @param {*} context - The function's calling context.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
2018-06-13 07:37:40 +00:00
forEachDead: function (callback, context)
{
var dead = this.dead;
var length = dead.length;
for (var index = 0; index < length; ++index)
{
2017-10-18 14:18:42 +00:00
// Sends the Particle and the Emitter
2018-06-13 07:37:40 +00:00
callback.call(context, dead[index], this);
}
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Turns {@link Phaser.GameObjects.Particles.ParticleEmitter#on} the emitter and resets the flow counter.
*
* If this emitter is in flow mode (frequency >= 0; the default), the particle flow will start (or restart).
*
* If this emitter is in explode mode (frequency = -1), nothing will happen.
* Use {@link Phaser.GameObjects.Particles.ParticleEmitter#explode} or {@link Phaser.GameObjects.Particles.ParticleEmitter#flow} instead.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#start
* @since 3.0.0
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
2017-10-18 14:18:42 +00:00
start: function ()
{
this.on = true;
this._counter = 0;
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* {@link Phaser.GameObjects.Particles.ParticleEmitter#active Deactivates} the emitter.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#pause
* @since 3.0.0
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
pause: function ()
{
this.active = false;
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* {@link Phaser.GameObjects.Particles.ParticleEmitter#active Activates} the emitter.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#resume
* @since 3.0.0
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
resume: function ()
{
this.active = true;
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Sorts active particles with {@link Phaser.GameObjects.Particles.ParticleEmitter#depthSortCallback}.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#depthSort
* @since 3.0.0
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
depthSort: function ()
{
StableSort.inplace(this.alive, this.depthSortCallback);
return this;
},
2018-02-06 22:25:23 +00:00
/**
* Puts the emitter in flow mode (frequency >= 0) and starts (or restarts) a particle flow.
*
* To resume a flow at the current frequency and quantity, use {@link Phaser.GameObjects.Particles.ParticleEmitter#start} instead.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#flow
* @since 3.0.0
*
2018-04-18 15:32:03 +00:00
* @param {float} frequency - The time interval (>= 0) of each flow cycle, in ms.
* @param {float|float[]|EmitterOpOnEmitCallback|object} [count=1] - The number of particles to emit at each flow cycle.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} This Particle Emitter.
*/
2017-10-18 14:18:42 +00:00
flow: function (frequency, count)
{
if (count === undefined) { count = 1; }
2017-10-18 14:18:42 +00:00
this.frequency = frequency;
2017-10-24 02:31:54 +00:00
this.quantity.onChange(count);
2017-10-18 14:18:42 +00:00
return this.start();
},
2018-02-06 22:25:23 +00:00
/**
* Puts the emitter in explode mode (frequency = -1), stopping any current particle flow, and emits several particles all at once.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#explode
* @since 3.0.0
*
* @param {integer} count - The amount of Particles to emit.
2018-04-18 15:32:03 +00:00
* @param {float} x - The x coordinate to emit the Particles from.
* @param {float} y - The y coordinate to emit the Particles from.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.Particle} The most recently emitted Particle.
*/
2017-10-18 14:18:42 +00:00
explode: function (count, x, y)
{
2017-10-18 14:18:42 +00:00
this.frequency = -1;
return this.emitParticle(count, x, y);
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Emits particles at a given position (or the emitter's current position).
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#emitParticleAt
* @since 3.0.0
*
2018-04-18 15:32:03 +00:00
* @param {float} [x=this.x] - The x coordinate to emit the Particles from.
* @param {float} [y=this.x] - The y coordinate to emit the Particles from.
* @param {integer} [count=this.quantity] - The number of Particles to emit.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.Particle} The most recently emitted Particle.
*/
emitParticleAt: function (x, y, count)
{
return this.emitParticle(count, x, y);
2017-10-18 14:18:42 +00:00
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Emits particles at a given position (or the emitter's current position).
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#emitParticle
* @since 3.0.0
*
2018-04-18 15:32:03 +00:00
* @param {integer} [count=this.quantity] - The number of Particles to emit.
* @param {float} [x=this.x] - The x coordinate to emit the Particles from.
* @param {float} [y=this.x] - The y coordinate to emit the Particles from.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.Particle} The most recently emitted Particle.
2018-04-18 15:32:03 +00:00
*
* @see Phaser.GameObjects.Particles.Particle#fire
2018-02-06 22:25:23 +00:00
*/
emitParticle: function (count, x, y)
2017-10-18 14:18:42 +00:00
{
if (this.atLimit())
{
return;
2017-10-18 14:18:42 +00:00
}
2017-10-24 02:31:54 +00:00
if (count === undefined)
{
count = this.quantity.onEmit();
}
var dead = this.dead;
2017-10-18 14:18:42 +00:00
for (var i = 0; i < count; i++)
{
2017-10-18 14:18:42 +00:00
var particle;
if (dead.length > 0)
{
particle = dead.pop();
2017-10-18 14:18:42 +00:00
}
else
{
particle = new this.particleClass(this);
}
2017-10-18 14:18:42 +00:00
particle.fire(x, y);
2017-10-18 14:18:42 +00:00
if (this.particleBringToTop)
{
2017-10-18 14:18:42 +00:00
this.alive.push(particle);
}
else
{
2017-10-18 14:18:42 +00:00
this.alive.unshift(particle);
}
if (this.emitCallback)
{
this.emitCallback.call(this.emitCallbackScope, particle, this);
}
2017-10-18 14:18:42 +00:00
if (this.atLimit())
{
break;
}
}
return particle;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Updates this emitter and its particles.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#preUpdate
* @since 3.0.0
*
2018-03-18 13:43:37 +00:00
* @param {integer} time - The current timestamp as generated by the Request Animation Frame or SetTimeout.
2018-04-18 15:32:03 +00:00
* @param {float} delta - The delta time, in ms, elapsed since the last frame.
2018-02-06 22:25:23 +00:00
*/
preUpdate: function (time, delta)
{
// Scale the delta
delta *= this.timeScale;
2017-10-18 14:18:42 +00:00
var step = (delta / 1000);
2017-10-20 02:48:42 +00:00
if (this.trackVisible)
2017-10-20 02:48:42 +00:00
{
this.visible = this.follow.visible;
2017-10-20 02:48:42 +00:00
}
// Any particle processors?
var processors = this.manager.getProcessors();
var particles = this.alive;
var length = particles.length;
for (var index = 0; index < length; index++)
{
var particle = particles[index];
// update returns `true` if the particle is now dead (lifeStep < 0)
if (particle.update(delta, step, processors))
{
// Moves the dead particle to the end of the particles array (ready for splicing out later)
var last = particles[length - 1];
particles[length - 1] = particle;
particles[index] = last;
2017-10-18 14:18:42 +00:00
index -= 1;
length -= 1;
}
}
// Move dead particles to the dead array
var deadLength = particles.length - length;
if (deadLength > 0)
{
2017-10-18 14:18:42 +00:00
var rip = particles.splice(particles.length - deadLength, deadLength);
var deathCallback = this.deathCallback;
var deathCallbackScope = this.deathCallbackScope;
if (deathCallback)
{
for (var i = 0; i < rip.length; i++)
{
deathCallback.call(deathCallbackScope, rip[i]);
}
}
this.dead.concat(rip);
StableSort.inplace(particles, this.indexSortCallback);
}
2018-03-19 12:50:32 +00:00
if (!this.on)
{
return;
}
if (this.frequency === 0)
{
this.emitParticle();
}
else if (this.frequency > 0)
2017-10-18 14:18:42 +00:00
{
this._counter -= delta;
if (this._counter <= 0)
{
this.emitParticle();
// counter = frequency - remained from previous delta
this._counter = (this.frequency - Math.abs(this._counter));
2017-10-18 14:18:42 +00:00
}
}
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Calculates the difference of two particles, for sorting them by depth.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#depthSortCallback
* @since 3.0.0
*
2018-04-18 15:32:03 +00:00
* @param {object} a - The first particle.
* @param {object} b - The second particle.
2018-02-06 22:25:23 +00:00
*
2018-04-18 15:32:03 +00:00
* @return {integer} The difference of a and b's y coordinates.
2018-02-06 22:25:23 +00:00
*/
depthSortCallback: function (a, b)
{
return a.y - b.y;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Calculates the difference of two particles, for sorting them by index.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitter#indexSortCallback
* @since 3.0.0
*
2018-04-18 15:32:03 +00:00
* @param {object} a - The first particle.
* @param {object} b - The second particle.
2018-02-06 22:25:23 +00:00
*
2018-04-18 15:32:03 +00:00
* @return {integer} The difference of a and b's `index` properties.
2018-02-06 22:25:23 +00:00
*/
indexSortCallback: function (a, b)
{
return a.index - b.index;
}
});
module.exports = ParticleEmitter;