phaser/src/gameobjects/particles/ParticleEmitterManager.js

467 lines
15 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2019-01-15 16:20:22 +00:00
* @copyright 2019 Photon Storm Ltd.
2018-02-12 16:01:20 +00:00
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Class = require('../../utils/Class');
var Components = require('../components');
var GameObject = require('../GameObject');
var GravityWell = require('./GravityWell');
var List = require('../../structs/List');
var ParticleEmitter = require('./ParticleEmitter');
var Render = require('./ParticleManagerRender');
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 Manager creates and controls {@link Phaser.GameObjects.Particles.ParticleEmitter Particle Emitters} and {@link Phaser.GameObjects.Particles.GravityWell Gravity Wells}.
2018-02-06 22:25:23 +00:00
*
* @class ParticleEmitterManager
2018-04-16 13:43:24 +00:00
* @extends Phaser.GameObjects.GameObject
2018-10-10 09:49:13 +00:00
* @memberof Phaser.GameObjects.Particles
2018-02-06 22:25:23 +00:00
* @constructor
* @since 3.0.0
*
* @extends Phaser.GameObjects.Components.Depth
* @extends Phaser.GameObjects.Components.Pipeline
* @extends Phaser.GameObjects.Components.Transform
* @extends Phaser.GameObjects.Components.Visible
2018-02-06 22:25:23 +00:00
*
2018-04-18 15:32:03 +00:00
* @param {Phaser.Scene} scene - The Scene to which this Emitter Manager belongs.
* @param {string} texture - The key of the Texture this Emitter Manager will use to render particles, as stored in the Texture Manager.
* @param {(string|integer)} [frame] - An optional frame from the Texture this Emitter Manager will use to render particles.
* @param {ParticleEmitterConfig|ParticleEmitterConfig[]} [emitters] - Configuration settings for one or more emitters to create.
2018-02-06 22:25:23 +00:00
*/
var ParticleEmitterManager = new Class({
Extends: GameObject,
Mixins: [
Components.Depth,
2018-01-29 21:46:48 +00:00
Components.Pipeline,
Components.Transform,
Components.Visible,
Render
],
initialize:
// frame is optional and can contain the emitters array or object if skipped
function ParticleEmitterManager (scene, texture, frame, emitters)
{
GameObject.call(this, scene, 'ParticleEmitterManager');
2018-02-06 22:25:23 +00:00
/**
* The blend mode applied to all emitters and particles.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitterManager#blendMode
2018-04-18 15:32:03 +00:00
* @type {integer}
2018-02-06 22:25:23 +00:00
* @default -1
* @private
* @since 3.0.0
*/
this.blendMode = -1;
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* The time scale applied to all emitters and particles, affecting flow rate, lifespan, and movement.
* Values larger than 1 are faster than normal.
* This is multiplied with any timeScale set on each individual emitter.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitterManager#timeScale
* @type {number}
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
* The texture used to render this Emitter Manager's particles.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitterManager#texture
* @type {Phaser.Textures.Texture}
* @default null
* @since 3.0.0
*/
2017-10-18 14:18:42 +00:00
this.texture = null;
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* The texture frame used to render this Emitter Manager's particles.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitterManager#frame
* @type {Phaser.Textures.Frame}
* @default null
* @since 3.0.0
*/
2017-10-18 14:18:42 +00:00
this.frame = null;
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Names of this Emitter Manager's texture frames.
2018-02-06 22:25:23 +00:00
*
* @name Phaser.GameObjects.Particles.ParticleEmitterManager#frameNames
2018-05-22 14:55:36 +00:00
* @type {string[]}
2018-02-06 22:25:23 +00:00
* @since 3.0.0
*/
2017-10-18 14:18:42 +00:00
this.frameNames = [];
// frame is optional and can contain the emitters array or object if skipped
if (frame !== null && (typeof frame === 'object' || Array.isArray(frame)))
{
emitters = frame;
frame = null;
}
this.setTexture(texture, frame);
2018-02-06 22:25:23 +00:00
this.initPipeline();
2018-02-06 22:25:23 +00:00
/**
* A list of Emitters being managed by this Emitter Manager.
*
* @name Phaser.GameObjects.Particles.ParticleEmitterManager#emitters
2018-05-22 14:55:36 +00:00
* @type {Phaser.Structs.List.<Phaser.GameObjects.Particles.ParticleEmitter>}
2018-02-06 22:25:23 +00:00
* @since 3.0.0
*/
this.emitters = new List(this);
2018-02-06 22:25:23 +00:00
/**
* A list of Gravity Wells being managed by this Emitter Manager.
*
* @name Phaser.GameObjects.Particles.ParticleEmitterManager#wells
2018-03-23 15:54:12 +00:00
* @type {Phaser.Structs.List.<Phaser.GameObjects.Particles.GravityWell>}
2018-02-06 22:25:23 +00:00
* @since 3.0.0
*/
this.wells = new List(this);
if (emitters)
{
// An array of emitter configs?
if (!Array.isArray(emitters))
{
emitters = [ emitters ];
}
for (var i = 0; i < emitters.length; i++)
{
this.createEmitter(emitters[i]);
}
}
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Sets the texture and frame this Emitter Manager will use to render with.
2018-02-06 22:25:23 +00:00
*
* Textures are referenced by their string-based keys, as stored in the Texture Manager.
*
* @method Phaser.GameObjects.Particles.ParticleEmitterManager#setTexture
* @since 3.0.0
*
* @param {string} key - The key of the texture to be used, as stored in the Texture Manager.
2018-03-20 14:56:31 +00:00
* @param {(string|integer)} [frame] - The name or index of the frame within the Texture.
2018-02-06 22:25:23 +00:00
*
2018-04-18 15:32:03 +00:00
* @return {Phaser.GameObjects.Particles.ParticleEmitterManager} This Emitter Manager.
2018-02-06 22:25:23 +00:00
*/
2017-10-18 14:18:42 +00:00
setTexture: function (key, frame)
{
this.texture = this.scene.sys.textures.get(key);
return this.setFrame(frame);
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Sets the frame this Emitter Manager will use to render with.
2018-02-06 22:25:23 +00:00
*
* The Frame has to belong to the current Texture being used.
*
* It can be either a string or an index.
*
* @method Phaser.GameObjects.Particles.ParticleEmitterManager#setFrame
* @since 3.0.0
*
2018-03-20 14:56:31 +00:00
* @param {(string|integer)} [frame] - The name or index of the frame within the Texture.
2018-02-06 22:25:23 +00:00
*
2018-04-18 15:32:03 +00:00
* @return {Phaser.GameObjects.Particles.ParticleEmitterManager} This Emitter Manager.
2018-02-06 22:25:23 +00:00
*/
2017-10-18 14:18:42 +00:00
setFrame: function (frame)
{
this.frame = this.texture.get(frame);
2018-09-27 13:15:58 +00:00
var frames = this.texture.getFramesFromTextureSource(this.frame.sourceIndex);
var names = [];
frames.forEach(function (sourceFrame)
{
names.push(sourceFrame.name);
});
this.frameNames = names;
2017-10-18 14:18:42 +00:00
this.defaultFrame = this.frame;
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
* Assigns texture frames to an emitter.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitterManager#setEmitterFrames
* @since 3.0.0
*
2018-04-18 15:32:03 +00:00
* @param {(Phaser.Textures.Frame|Phaser.Textures.Frame[])} frames - The texture frames.
* @param {Phaser.GameObjects.Particles.ParticleEmitter} emitter - The particle emitter to modify.
2018-02-06 22:25:23 +00:00
*
2018-04-18 15:32:03 +00:00
* @return {Phaser.GameObjects.Particles.ParticleEmitterManager} This Emitter Manager.
2018-02-06 22:25:23 +00:00
*/
2017-10-18 14:18:42 +00:00
setEmitterFrames: function (frames, emitter)
{
if (!Array.isArray(frames))
{
frames = [ frames ];
}
var out = emitter.frames;
out.length = 0;
for (var i = 0; i < frames.length; i++)
{
var frame = frames[i];
if (this.frameNames.indexOf(frame) !== -1)
{
out.push(this.texture.get(frame));
}
}
if (out.length > 0)
{
emitter.defaultFrame = out[0];
}
else
{
emitter.defaultFrame = this.defaultFrame;
}
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
* Adds an existing Particle Emitter to this Emitter Manager.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitterManager#addEmitter
* @since 3.0.0
*
* @param {Phaser.GameObjects.Particles.ParticleEmitter} emitter - The Particle Emitter to add to this Emitter Manager.
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} The Particle Emitter that was added to this Emitter Manager.
*/
addEmitter: function (emitter)
{
return this.emitters.add(emitter);
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Creates a new Particle Emitter object, adds it to this Emitter Manager and returns a reference to it.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitterManager#createEmitter
* @since 3.0.0
*
* @param {ParticleEmitterConfig} config - Configuration settings for the Particle Emitter to create.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.ParticleEmitter} The Particle Emitter that was created.
*/
createEmitter: function (config)
{
return this.addEmitter(new ParticleEmitter(this, config));
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Adds an existing Gravity Well object to this Emitter Manager.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitterManager#addGravityWell
* @since 3.0.0
*
* @param {Phaser.GameObjects.Particles.GravityWell} well - The Gravity Well to add to this Emitter Manager.
*
* @return {Phaser.GameObjects.Particles.GravityWell} The Gravity Well that was added to this Emitter Manager.
*/
addGravityWell: function (well)
{
return this.wells.add(well);
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Creates a new Gravity Well, adds it to this Emitter Manager and returns a reference to it.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitterManager#createGravityWell
* @since 3.0.0
*
* @param {GravityWellConfig} config - Configuration settings for the Gravity Well to create.
2018-02-06 22:25:23 +00:00
*
* @return {Phaser.GameObjects.Particles.GravityWell} The Gravity Well that was created.
*/
createGravityWell: function (config)
{
return this.addGravityWell(new GravityWell(config));
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Emits particles from each active emitter.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitterManager#emitParticle
* @since 3.0.0
*
2018-04-18 15:32:03 +00:00
* @param {integer} [count] - The number of particles to release from each emitter. The default is the emitter's own {@link Phaser.GameObjects.Particles.ParticleEmitter#quantity}.
* @param {number} [x] - The x-coordinate to to emit particles from. The default is the x-coordinate of the emitter's current location.
* @param {number} [y] - The y-coordinate to to emit particles from. The default is the y-coordinate of the emitter's current location.
2018-02-06 22:25:23 +00:00
*
2018-04-18 15:32:03 +00:00
* @return {Phaser.GameObjects.Particles.ParticleEmitterManager} This Emitter Manager.
2018-02-06 22:25:23 +00:00
*/
emitParticle: function (count, x, y)
{
var emitters = this.emitters.list;
for (var i = 0; i < emitters.length; i++)
{
var emitter = emitters[i];
if (emitter.active)
{
emitter.emitParticle(count, x, y);
}
}
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Emits particles from each active emitter.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitterManager#emitParticleAt
* @since 3.0.0
*
* @param {number} [x] - The x-coordinate to to emit particles from. The default is the x-coordinate of the emitter's current location.
* @param {number} [y] - The y-coordinate to to emit particles from. The default is the y-coordinate of the emitter's current location.
2018-04-18 15:32:03 +00:00
* @param {integer} [count] - The number of particles to release from each emitter. The default is the emitter's own {@link Phaser.GameObjects.Particles.ParticleEmitter#quantity}.
2018-02-06 22:25:23 +00:00
*
2018-04-18 15:32:03 +00:00
* @return {Phaser.GameObjects.Particles.ParticleEmitterManager} This Emitter Manager.
2018-02-06 22:25:23 +00:00
*/
emitParticleAt: function (x, y, count)
{
return this.emitParticle(count, x, y);
},
2018-02-06 22:25:23 +00:00
/**
* Pauses this Emitter Manager.
*
* This has the effect of pausing all emitters, and all particles of those emitters, currently under its control.
*
* The particles will still render, but they will not have any of their logic updated.
*
* @method Phaser.GameObjects.Particles.ParticleEmitterManager#pause
* @since 3.0.0
*
2018-04-18 15:32:03 +00:00
* @return {Phaser.GameObjects.Particles.ParticleEmitterManager} This Emitter Manager.
2018-02-06 22:25:23 +00:00
*/
pause: function ()
{
this.active = false;
return this;
},
2018-02-06 22:25:23 +00:00
/**
* Resumes this Emitter Manager, should it have been previously paused.
*
* @method Phaser.GameObjects.Particles.ParticleEmitterManager#resume
* @since 3.0.0
*
2018-04-18 15:32:03 +00:00
* @return {Phaser.GameObjects.Particles.ParticleEmitterManager} This Emitter Manager.
2018-02-06 22:25:23 +00:00
*/
resume: function ()
{
this.active = true;
return this;
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Gets all active particle processors (gravity wells).
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitterManager#getProcessors
* @since 3.0.0
*
2018-04-18 15:32:03 +00:00
* @return {Phaser.GameObjects.Particles.GravityWell[]} - The active gravity wells.
2018-02-06 22:25:23 +00:00
*/
getProcessors: function ()
{
return this.wells.getAll('active', true);
},
2018-02-06 22:25:23 +00:00
/**
2018-04-18 15:32:03 +00:00
* Updates all active emitters.
2018-02-06 22:25:23 +00:00
*
* @method Phaser.GameObjects.Particles.ParticleEmitterManager#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.
* @param {number} 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;
var emitters = this.emitters.list;
for (var i = 0; i < emitters.length; i++)
{
var emitter = emitters[i];
if (emitter.active)
{
emitter.preUpdate(time, delta);
}
}
},
/**
* A NOOP method so you can pass an EmitterManager to a Container.
* Calling this method will do nothing. It is intentionally empty.
*
* @method Phaser.GameObjects.Particles.ParticleEmitterManager#setAlpha
* @private
* @since 3.10.0
*/
setAlpha: function ()
{
},
/**
* A NOOP method so you can pass an EmitterManager to a Container.
* Calling this method will do nothing. It is intentionally empty.
*
* @method Phaser.GameObjects.Particles.ParticleEmitterManager#setScrollFactor
* @private
* @since 3.10.0
*/
setScrollFactor: function ()
{
},
/**
* A NOOP method so you can pass an EmitterManager to a Container.
* Calling this method will do nothing. It is intentionally empty.
*
* @method Phaser.GameObjects.Particles.ParticleEmitterManager#setBlendMode
* @private
* @since 3.15.0
*/
setBlendMode: function ()
{
}
});
module.exports = ParticleEmitterManager;