phaser/wip/core/PluginManager.js

295 lines
7.5 KiB
JavaScript
Raw Normal View History

/* jshint newcap: false */
/**
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}
*/
2014-03-23 07:59:28 +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.
*/
Phaser.PluginManager = function(game) {
/**
* @property {Phaser.Game} game - A reference to the currently running game.
*/
this.game = game;
2014-03-23 07:59:28 +00:00
2013-10-01 12:54:29 +00:00
/**
* @property {Phaser.Plugin[]} plugins - An array of all the plugins being managed by this PluginManager.
*/
this.plugins = [];
2014-03-23 07:59:28 +00:00
2013-10-01 12:54:29 +00:00
/**
* @property {number} _len - Internal cache var.
* @private
*/
this._len = 0;
2014-03-23 07:59:28 +00:00
2013-10-01 12:54:29 +00:00
/**
* @property {number} _i - Internal cache var.
* @private
*/
this._i = 0;
};
Phaser.PluginManager.prototype = {
/**
* 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.
*
2013-10-02 10:22:20 +00:00
* @method Phaser.PluginManager#add
* @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.
* @return {Phaser.Plugin} The Plugin that was added to the manager.
*/
add: function (plugin) {
var args = Array.prototype.slice.call(arguments, 1);
var result = false;
// Prototype?
if (typeof plugin === 'function')
{
plugin = new plugin(this.game, this);
}
else
{
plugin.game = this.game;
plugin.parent = this;
}
// Check for methods now to avoid having to do this every loop
if (typeof plugin['preUpdate'] === 'function')
{
plugin.hasPreUpdate = true;
result = true;
}
if (typeof plugin['update'] === 'function')
{
plugin.hasUpdate = true;
result = true;
}
if (typeof plugin['postUpdate'] === 'function')
{
plugin.hasPostUpdate = true;
result = true;
}
if (typeof plugin['render'] === 'function')
{
plugin.hasRender = true;
result = true;
}
if (typeof plugin['postRender'] === 'function')
{
plugin.hasPostRender = true;
result = true;
}
// The plugin must have at least one of the above functions to be added to the PluginManager.
if (result)
{
if (plugin.hasPreUpdate || plugin.hasUpdate || plugin.hasPostUpdate)
{
plugin.active = true;
}
if (plugin.hasRender || plugin.hasPostRender)
{
plugin.visible = true;
}
this._len = this.plugins.push(plugin);
// Allows plugins to run potentially destructive code outside of the constructor, and only if being added to the PluginManager
if (typeof plugin['init'] === 'function')
{
plugin.init.apply(plugin, args);
}
return plugin;
}
else
{
return null;
}
},
2013-10-01 12:54:29 +00:00
/**
* Remove a Plugin from the PluginManager. It calls Plugin.destroy on the plugin before removing it from the manager.
*
* @method Phaser.PluginManager#remove
* @param {Phaser.Plugin} plugin - The plugin to be removed.
* @param {boolean} [destroy=true] - Call destroy on the plugin that is removed?
*/
remove: function (plugin, destroy) {
if (destroy === undefined) { destroy = true; }
2014-03-23 07:59:28 +00:00
this._i = this._len;
while (this._i--)
{
if (this.plugins[this._i] === plugin)
{
if (destroy)
{
plugin.destroy();
}
this.plugins.splice(this._i, 1);
this._len--;
return;
}
}
},
/**
* Remove all Plugins from the PluginManager. It calls Plugin.destroy on every plugin before removing it from the manager.
*
* @method Phaser.PluginManager#removeAll
*/
removeAll: function() {
2014-03-23 07:59:28 +00:00
this._i = this._len;
while (this._i--)
{
2014-04-09 23:57:46 +00:00
this.plugins[this._i].destroy();
}
this.plugins.length = 0;
this._len = 0;
},
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
*/
preUpdate: function () {
this._i = this._len;
while (this._i--)
{
if (this.plugins[this._i].active && this.plugins[this._i].hasPreUpdate)
{
this.plugins[this._i].preUpdate();
}
}
},
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
*/
update: function () {
2014-03-23 07:59:28 +00:00
this._i = this._len;
while (this._i--)
{
if (this.plugins[this._i].active && this.plugins[this._i].hasUpdate)
{
this.plugins[this._i].update();
}
}
},
/**
* 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
*
* @method Phaser.PluginManager#postUpdate
*/
postUpdate: function () {
2014-03-23 07:59:28 +00:00
this._i = this._len;
while (this._i--)
{
if (this.plugins[this._i].active && this.plugins[this._i].hasPostUpdate)
{
this.plugins[this._i].postUpdate();
}
}
},
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
*/
render: function () {
this._i = this._len;
while (this._i--)
{
if (this.plugins[this._i].visible && this.plugins[this._i].hasRender)
{
this.plugins[this._i].render();
}
}
},
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
*/
postRender: function () {
this._i = this._len;
while (this._i--)
{
if (this.plugins[this._i].visible && this.plugins[this._i].hasPostRender)
{
this.plugins[this._i].postRender();
}
}
},
2013-10-01 12:54:29 +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
*/
destroy: function () {
this.removeAll();
this.game = null;
}
};
Phaser.PluginManager.prototype.constructor = Phaser.PluginManager;