2017-10-17 03:19:29 +00:00
|
|
|
var Class = require('../../utils/Class');
|
|
|
|
var Components = require('../components');
|
|
|
|
var GameObject = require('../GameObject');
|
2017-10-25 15:05:48 +00:00
|
|
|
var GravityWell = require('./GravityWell');
|
2017-10-20 13:14:22 +00:00
|
|
|
var List = require('../../structs/List');
|
2017-10-17 03:19:29 +00:00
|
|
|
var ParticleEmitter = require('./ParticleEmitter');
|
|
|
|
var Render = require('./ParticleManagerRender');
|
|
|
|
|
|
|
|
var ParticleEmitterManager = new Class({
|
|
|
|
|
|
|
|
Extends: GameObject,
|
|
|
|
|
|
|
|
Mixins: [
|
2017-10-20 13:14:22 +00:00
|
|
|
Components.Depth,
|
2017-10-19 23:54:47 +00:00
|
|
|
Components.RenderTarget,
|
2017-10-17 03:19:29 +00:00
|
|
|
Components.Visible,
|
|
|
|
Render
|
|
|
|
],
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2017-10-20 02:20:39 +00:00
|
|
|
// frame is optional and can contain the emitters array or object if skipped
|
2017-10-17 03:19:29 +00:00
|
|
|
function ParticleEmitterManager (scene, texture, frame, emitters)
|
|
|
|
{
|
|
|
|
GameObject.call(this, scene, 'ParticleEmitterManager');
|
|
|
|
|
|
|
|
// private
|
|
|
|
this.blendMode = -1;
|
|
|
|
|
2017-10-17 20:32:45 +00:00
|
|
|
this.timeScale = 1;
|
|
|
|
|
2017-10-18 14:18:42 +00:00
|
|
|
this.texture = null;
|
|
|
|
this.frame = null;
|
|
|
|
this.frameNames = [];
|
|
|
|
|
2017-11-17 13:30:07 +00:00
|
|
|
// frame is optional and can contain the emitters array or object if skipped
|
|
|
|
if (frame !== null && (typeof frame === 'object' || Array.isArray(frame)))
|
2017-10-20 02:20:39 +00:00
|
|
|
{
|
|
|
|
emitters = frame;
|
|
|
|
frame = null;
|
|
|
|
}
|
|
|
|
|
2017-10-17 03:19:29 +00:00
|
|
|
this.setTexture(texture, frame);
|
|
|
|
|
2017-10-20 13:14:22 +00:00
|
|
|
this.emitters = new List(this);
|
2017-10-17 03:19:29 +00:00
|
|
|
|
2017-10-25 15:05:48 +00:00
|
|
|
this.wells = new List(this);
|
|
|
|
|
2017-11-17 13:30:07 +00:00
|
|
|
if (emitters)
|
2017-10-17 03:19:29 +00:00
|
|
|
{
|
|
|
|
// An array of emitter configs?
|
|
|
|
if (!Array.isArray(emitters))
|
|
|
|
{
|
|
|
|
emitters = [ emitters ];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < emitters.length; i++)
|
|
|
|
{
|
|
|
|
this.createEmitter(emitters[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-10-18 14:18:42 +00:00
|
|
|
setTexture: function (key, frame)
|
|
|
|
{
|
|
|
|
this.texture = this.scene.sys.textures.get(key);
|
|
|
|
|
|
|
|
return this.setFrame(frame);
|
|
|
|
},
|
|
|
|
|
|
|
|
setFrame: function (frame)
|
|
|
|
{
|
|
|
|
this.frame = this.texture.get(frame);
|
|
|
|
|
|
|
|
this.frameNames = this.texture.getFramesFromTextureSource(this.frame.sourceIndex);
|
|
|
|
|
2017-10-19 23:54:47 +00:00
|
|
|
this.defaultFrame = this.frame;
|
|
|
|
|
2017-10-18 14:18:42 +00:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-19 23:54:47 +00:00
|
|
|
if (out.length > 0)
|
|
|
|
{
|
|
|
|
emitter.defaultFrame = out[0];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
emitter.defaultFrame = this.defaultFrame;
|
|
|
|
}
|
|
|
|
|
2017-10-18 14:18:42 +00:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-10-17 03:19:29 +00:00
|
|
|
addEmitter: function (emitter)
|
|
|
|
{
|
2017-10-20 13:14:22 +00:00
|
|
|
return this.emitters.add(emitter);
|
2017-10-17 03:19:29 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
createEmitter: function (config)
|
|
|
|
{
|
|
|
|
return this.addEmitter(new ParticleEmitter(this, config));
|
|
|
|
},
|
|
|
|
|
2017-10-25 15:05:48 +00:00
|
|
|
addGravityWell: function (well)
|
|
|
|
{
|
|
|
|
return this.wells.add(well);
|
|
|
|
},
|
|
|
|
|
|
|
|
createGravityWell: function (config)
|
|
|
|
{
|
|
|
|
return this.addGravityWell(new GravityWell(config));
|
|
|
|
},
|
|
|
|
|
2017-10-20 13:14:22 +00:00
|
|
|
emit: 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.emit(count, x, y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
emitAt: function (x, y, count)
|
|
|
|
{
|
|
|
|
return this.emit(count, x, y);
|
|
|
|
},
|
|
|
|
|
2017-10-17 03:19:29 +00:00
|
|
|
pause: function ()
|
|
|
|
{
|
|
|
|
this.active = false;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
resume: function ()
|
|
|
|
{
|
|
|
|
this.active = true;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-10-25 15:05:48 +00:00
|
|
|
getProcessors: function ()
|
|
|
|
{
|
|
|
|
return this.wells.getAll('active', true);
|
|
|
|
},
|
|
|
|
|
2017-10-17 03:19:29 +00:00
|
|
|
preUpdate: function (time, delta)
|
|
|
|
{
|
2017-10-17 20:32:45 +00:00
|
|
|
// Scale the delta
|
|
|
|
delta *= this.timeScale;
|
|
|
|
|
2017-10-20 13:14:22 +00:00
|
|
|
var emitters = this.emitters.list;
|
2017-10-17 03:19:29 +00:00
|
|
|
|
|
|
|
for (var i = 0; i < emitters.length; i++)
|
|
|
|
{
|
|
|
|
var emitter = emitters[i];
|
|
|
|
|
|
|
|
if (emitter.active)
|
|
|
|
{
|
|
|
|
emitter.preUpdate(time, delta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = ParticleEmitterManager;
|