2013-11-25 03:13:04 +00:00
|
|
|
/* jshint newcap: false */
|
|
|
|
|
2013-08-29 02:52:59 +00:00
|
|
|
/**
|
2013-10-01 12:54:29 +00:00
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2016-04-04 21:15:01 +00:00
|
|
|
* @copyright 2016 Photon Storm Ltd.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
2013-08-29 02:52:59 +00:00
|
|
|
*/
|
|
|
|
|
2014-03-23 07:59:28 +00:00
|
|
|
/**
|
2013-11-25 03:13:04 +00:00
|
|
|
* The Plugin Manager is responsible for the loading, running and unloading of Phaser Plugins.
|
2014-03-23 07:59:28 +00:00
|
|
|
*
|
2013-10-01 12:54:29 +00:00
|
|
|
* @class Phaser.PluginManager
|
|
|
|
* @constructor
|
|
|
|
* @param {Phaser.Game} game - A reference to the currently running game.
|
|
|
|
*/
|
2014-04-09 15:12:25 +00:00
|
|
|
Phaser.PluginManager = function(game) {
|
2013-08-29 02:52:59 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Game} game - A reference to the currently running game.
|
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
this.game = game;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2014-11-30 11:10:52 +00:00
|
|
|
* @property {Phaser.Plugin[]} plugins - An array of all the plugins being managed by this PluginManager.
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2014-04-09 15:12:25 +00:00
|
|
|
this.plugins = [];
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2014-04-09 15:12:25 +00:00
|
|
|
* @property {number} _len - Internal cache var.
|
|
|
|
* @private
|
2013-11-25 03:13:04 +00:00
|
|
|
*/
|
2014-04-09 15:12:25 +00:00
|
|
|
this._len = 0;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2014-04-09 15:12:25 +00:00
|
|
|
* @property {number} _i - Internal cache var.
|
2013-11-25 03:13:04 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2014-04-09 15:12:25 +00:00
|
|
|
this._i = 0;
|
2013-08-29 02:52:59 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2013-08-29 06:06:16 +00:00
|
|
|
Phaser.PluginManager.prototype = {
|
2013-08-29 02:52:59 +00:00
|
|
|
|
|
|
|
/**
|
2014-04-09 15:12:25 +00:00
|
|
|
* Add a new Plugin into the PluginManager.
|
2014-07-07 16:19:02 +00:00
|
|
|
* The Plugin must have 2 properties: game and parent. Plugin.game is set to the game reference the PluginManager uses, and parent is set to the PluginManager.
|
2014-04-09 15:12:25 +00:00
|
|
|
*
|
2013-10-02 10:22:20 +00:00
|
|
|
* @method Phaser.PluginManager#add
|
2014-04-09 15:12:25 +00:00
|
|
|
* @param {object|Phaser.Plugin} plugin - The Plugin to add into the PluginManager. This can be a function or an existing object.
|
2015-06-16 10:57:28 +00:00
|
|
|
* @param {...*} parameter - Additional arguments that will be passed to the Plugin.init method.
|
2014-04-09 15:12:25 +00:00
|
|
|
* @return {Phaser.Plugin} The Plugin that was added to the manager.
|
2013-08-29 02:52:59 +00:00
|
|
|
*/
|
|
|
|
add: function (plugin) {
|
|
|
|
|
2015-08-27 06:50:16 +00:00
|
|
|
var args = Array.prototype.slice.call(arguments, 1);
|
2013-08-29 02:52:59 +00:00
|
|
|
var result = false;
|
|
|
|
|
|
|
|
// Prototype?
|
|
|
|
if (typeof plugin === 'function')
|
|
|
|
{
|
2014-05-07 15:35:08 +00:00
|
|
|
plugin = new plugin(this.game, this);
|
2013-08-29 02:52:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
plugin.game = this.game;
|
2014-04-09 15:12:25 +00:00
|
|
|
plugin.parent = this;
|
2013-08-29 02:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for methods now to avoid having to do this every loop
|
2013-09-11 16:32:53 +00:00
|
|
|
if (typeof plugin['preUpdate'] === 'function')
|
|
|
|
{
|
2013-08-29 02:52:59 +00:00
|
|
|
plugin.hasPreUpdate = true;
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
|
2013-09-11 16:32:53 +00:00
|
|
|
if (typeof plugin['update'] === 'function')
|
|
|
|
{
|
2013-08-29 02:52:59 +00:00
|
|
|
plugin.hasUpdate = true;
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
|
2013-11-02 06:04:32 +00:00
|
|
|
if (typeof plugin['postUpdate'] === 'function')
|
|
|
|
{
|
|
|
|
plugin.hasPostUpdate = true;
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
|
2013-09-11 16:32:53 +00:00
|
|
|
if (typeof plugin['render'] === 'function')
|
|
|
|
{
|
2013-08-29 02:52:59 +00:00
|
|
|
plugin.hasRender = true;
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
|
2013-09-11 16:32:53 +00:00
|
|
|
if (typeof plugin['postRender'] === 'function')
|
|
|
|
{
|
2013-08-29 02:52:59 +00:00
|
|
|
plugin.hasPostRender = true;
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The plugin must have at least one of the above functions to be added to the PluginManager.
|
2013-09-11 16:32:53 +00:00
|
|
|
if (result)
|
|
|
|
{
|
2013-11-27 16:33:49 +00:00
|
|
|
if (plugin.hasPreUpdate || plugin.hasUpdate || plugin.hasPostUpdate)
|
2013-08-29 02:52:59 +00:00
|
|
|
{
|
|
|
|
plugin.active = true;
|
|
|
|
}
|
|
|
|
|
2013-09-11 16:32:53 +00:00
|
|
|
if (plugin.hasRender || plugin.hasPostRender)
|
2013-08-29 02:52:59 +00:00
|
|
|
{
|
|
|
|
plugin.visible = true;
|
|
|
|
}
|
|
|
|
|
2014-04-09 15:12:25 +00:00
|
|
|
this._len = this.plugins.push(plugin);
|
2013-10-27 22:30:01 +00:00
|
|
|
|
|
|
|
// Allows plugins to run potentially destructive code outside of the constructor, and only if being added to the PluginManager
|
|
|
|
if (typeof plugin['init'] === 'function')
|
|
|
|
{
|
2014-05-07 15:35:08 +00:00
|
|
|
plugin.init.apply(plugin, args);
|
2013-10-27 22:30:01 +00:00
|
|
|
}
|
|
|
|
|
2013-08-29 02:52:59 +00:00
|
|
|
return plugin;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2014-04-09 15:12:25 +00:00
|
|
|
* Remove a Plugin from the PluginManager. It calls Plugin.destroy on the plugin before removing it from the manager.
|
|
|
|
*
|
2013-11-25 04:40:04 +00:00
|
|
|
* @method Phaser.PluginManager#remove
|
|
|
|
* @param {Phaser.Plugin} plugin - The plugin to be removed.
|
2016-06-03 14:52:17 +00:00
|
|
|
* @param {boolean} [destroy=true] - Call destroy on the plugin that is removed?
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2016-06-03 14:52:17 +00:00
|
|
|
remove: function (plugin, destroy) {
|
|
|
|
|
|
|
|
if (destroy === undefined) { destroy = true; }
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-04-09 15:12:25 +00:00
|
|
|
this._i = this._len;
|
2013-08-29 02:52:59 +00:00
|
|
|
|
2014-04-09 15:12:25 +00:00
|
|
|
while (this._i--)
|
2013-11-08 06:11:36 +00:00
|
|
|
{
|
2014-04-09 15:12:25 +00:00
|
|
|
if (this.plugins[this._i] === plugin)
|
2013-11-08 06:11:36 +00:00
|
|
|
{
|
2016-06-03 14:52:17 +00:00
|
|
|
if (destroy)
|
|
|
|
{
|
|
|
|
plugin.destroy();
|
|
|
|
}
|
|
|
|
|
2014-04-09 15:12:25 +00:00
|
|
|
this.plugins.splice(this._i, 1);
|
|
|
|
this._len--;
|
2013-11-08 06:11:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-04-09 15:12:25 +00:00
|
|
|
|
2013-11-08 06:11:36 +00:00
|
|
|
},
|
2013-08-29 02:52:59 +00:00
|
|
|
|
2013-11-08 06:11:36 +00:00
|
|
|
/**
|
2014-04-09 15:12:25 +00:00
|
|
|
* Remove all Plugins from the PluginManager. It calls Plugin.destroy on every plugin before removing it from the manager.
|
|
|
|
*
|
2013-11-25 04:40:04 +00:00
|
|
|
* @method Phaser.PluginManager#removeAll
|
|
|
|
*/
|
2013-11-08 06:11:36 +00:00
|
|
|
removeAll: function() {
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-04-09 15:12:25 +00:00
|
|
|
this._i = this._len;
|
|
|
|
|
|
|
|
while (this._i--)
|
2013-11-08 06:11:36 +00:00
|
|
|
{
|
2014-04-09 23:57:46 +00:00
|
|
|
this.plugins[this._i].destroy();
|
2013-11-08 06:11:36 +00:00
|
|
|
}
|
2014-04-09 15:12:25 +00:00
|
|
|
|
2013-11-08 06:11:36 +00:00
|
|
|
this.plugins.length = 0;
|
2014-04-09 15:12:25 +00:00
|
|
|
this._len = 0;
|
|
|
|
|
2013-08-29 02:52:59 +00:00
|
|
|
},
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-10-02 10:22:20 +00:00
|
|
|
* Pre-update is called at the very start of the update cycle, before any other subsystems have been updated (including Physics).
|
|
|
|
* It only calls plugins who have active=true.
|
2014-03-23 07:59:28 +00:00
|
|
|
*
|
2013-10-02 10:22:20 +00:00
|
|
|
* @method Phaser.PluginManager#preUpdate
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
preUpdate: function () {
|
|
|
|
|
2014-04-09 15:12:25 +00:00
|
|
|
this._i = this._len;
|
2013-08-29 02:52:59 +00:00
|
|
|
|
2014-04-09 15:12:25 +00:00
|
|
|
while (this._i--)
|
2013-09-11 16:32:53 +00:00
|
|
|
{
|
2014-04-09 15:12:25 +00:00
|
|
|
if (this.plugins[this._i].active && this.plugins[this._i].hasPreUpdate)
|
2013-09-11 16:32:53 +00:00
|
|
|
{
|
2014-04-09 15:12:25 +00:00
|
|
|
this.plugins[this._i].preUpdate();
|
2013-08-29 02:52:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-10-02 10:22:20 +00:00
|
|
|
* Update is called after all the core subsystems (Input, Tweens, Sound, etc) and the State have updated, but before the render.
|
|
|
|
* It only calls plugins who have active=true.
|
2014-03-23 07:59:28 +00:00
|
|
|
*
|
2013-10-02 10:22:20 +00:00
|
|
|
* @method Phaser.PluginManager#update
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
update: function () {
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-04-09 15:12:25 +00:00
|
|
|
this._i = this._len;
|
2013-08-29 02:52:59 +00:00
|
|
|
|
2014-04-09 15:12:25 +00:00
|
|
|
while (this._i--)
|
2013-08-29 02:52:59 +00:00
|
|
|
{
|
2014-04-09 15:12:25 +00:00
|
|
|
if (this.plugins[this._i].active && this.plugins[this._i].hasUpdate)
|
2013-09-11 16:32:53 +00:00
|
|
|
{
|
2014-04-09 15:12:25 +00:00
|
|
|
this.plugins[this._i].update();
|
2013-08-29 02:52:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-02 06:04:32 +00:00
|
|
|
/**
|
|
|
|
* PostUpdate is the last thing to be called before the world render.
|
|
|
|
* In particular, it is called after the world postUpdate, which means the camera has been adjusted.
|
|
|
|
* It only calls plugins who have active=true.
|
2014-03-23 07:59:28 +00:00
|
|
|
*
|
2013-11-02 06:04:32 +00:00
|
|
|
* @method Phaser.PluginManager#postUpdate
|
|
|
|
*/
|
|
|
|
postUpdate: function () {
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-04-09 15:12:25 +00:00
|
|
|
this._i = this._len;
|
2013-11-02 06:04:32 +00:00
|
|
|
|
2014-04-09 15:12:25 +00:00
|
|
|
while (this._i--)
|
2013-11-02 06:04:32 +00:00
|
|
|
{
|
2014-04-09 15:12:25 +00:00
|
|
|
if (this.plugins[this._i].active && this.plugins[this._i].hasPostUpdate)
|
2013-11-02 06:04:32 +00:00
|
|
|
{
|
2014-04-09 15:12:25 +00:00
|
|
|
this.plugins[this._i].postUpdate();
|
2013-11-02 06:04:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-10-02 10:22:20 +00:00
|
|
|
* Render is called right after the Game Renderer completes, but before the State.render.
|
|
|
|
* It only calls plugins who have visible=true.
|
2014-03-23 07:59:28 +00:00
|
|
|
*
|
2013-10-02 10:22:20 +00:00
|
|
|
* @method Phaser.PluginManager#render
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
render: function () {
|
|
|
|
|
2014-04-09 15:12:25 +00:00
|
|
|
this._i = this._len;
|
2013-08-29 02:52:59 +00:00
|
|
|
|
2014-04-09 15:12:25 +00:00
|
|
|
while (this._i--)
|
2013-09-11 16:32:53 +00:00
|
|
|
{
|
2014-04-09 15:12:25 +00:00
|
|
|
if (this.plugins[this._i].visible && this.plugins[this._i].hasRender)
|
2013-09-11 16:32:53 +00:00
|
|
|
{
|
2014-04-09 15:12:25 +00:00
|
|
|
this.plugins[this._i].render();
|
2013-08-29 02:52:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2013-10-02 10:22:20 +00:00
|
|
|
* Post-render is called after the Game Renderer and State.render have run.
|
|
|
|
* It only calls plugins who have visible=true.
|
2014-03-23 07:59:28 +00:00
|
|
|
*
|
2013-10-02 10:22:20 +00:00
|
|
|
* @method Phaser.PluginManager#postRender
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
postRender: function () {
|
|
|
|
|
2014-04-09 15:12:25 +00:00
|
|
|
this._i = this._len;
|
2013-08-29 02:52:59 +00:00
|
|
|
|
2014-04-09 15:12:25 +00:00
|
|
|
while (this._i--)
|
2013-09-11 16:32:53 +00:00
|
|
|
{
|
2014-04-09 15:12:25 +00:00
|
|
|
if (this.plugins[this._i].visible && this.plugins[this._i].hasPostRender)
|
2013-09-11 16:32:53 +00:00
|
|
|
{
|
2014-04-09 15:12:25 +00:00
|
|
|
this.plugins[this._i].postRender();
|
2013-08-29 02:52:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
2014-04-09 15:12:25 +00:00
|
|
|
* Clear down this PluginManager, calls destroy on every plugin and nulls out references.
|
2014-03-23 07:59:28 +00:00
|
|
|
*
|
2013-10-02 10:22:20 +00:00
|
|
|
* @method Phaser.PluginManager#destroy
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
destroy: function () {
|
|
|
|
|
2014-04-09 15:12:25 +00:00
|
|
|
this.removeAll();
|
|
|
|
|
2013-08-29 02:52:59 +00:00
|
|
|
this.game = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2013-12-30 16:54:00 +00:00
|
|
|
|
|
|
|
Phaser.PluginManager.prototype.constructor = Phaser.PluginManager;
|