phaser/src/core/PluginManager.js

233 lines
5.5 KiB
JavaScript
Raw Normal View History

/**
2013-10-01 12:54:29 +00:00
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
2013-10-01 12:54:29 +00:00
/**
* Description.
*
* @class Phaser.PluginManager
2013-10-02 14:05:55 +00:00
* @classdesc Phaser - PluginManager
2013-10-01 12:54:29 +00:00
* @constructor
* @param {Phaser.Game} game - A reference to the currently running game.
* @param {Description} parent - Description.
*/
Phaser.PluginManager = function(game, parent) {
2013-10-01 12:54:29 +00:00
/**
* @property {Phaser.Game} game - A reference to the currently running game.
*/
this.game = game;
2013-10-01 12:54:29 +00:00
/**
* @property {Description} _parent - Description.
* @private
*/
this._parent = parent;
2013-10-01 12:54:29 +00:00
/**
* @property {array} plugins - Description.
*/
this.plugins = [];
2013-10-01 12:54:29 +00:00
/**
* @property {array} _pluginsLength - Description.
* @private
* @default
*/
this._pluginsLength = 0;
};
Phaser.PluginManager.prototype = {
/**
* Add a new Plugin to the PluginManager.
2013-10-01 12:54:29 +00:00
* The plugin's game and parent reference are set to this game and pluginmanager parent.
2013-10-02 10:22:20 +00:00
* @method Phaser.PluginManager#add
2013-10-01 12:54:29 +00:00
* @param {Phaser.Plugin} plugin - Description.
* @return {Phaser.Plugin} Description.
*/
add: function (plugin) {
var result = false;
// Prototype?
if (typeof plugin === 'function')
{
plugin = new plugin(this.game, this._parent);
}
else
{
plugin.game = this.game;
plugin.parent = this._parent;
}
// 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['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.active = true;
}
if (plugin.hasRender || plugin.hasPostRender)
{
plugin.visible = true;
}
this._pluginsLength = this.plugins.push(plugin);
return plugin;
}
else
{
return null;
}
},
2013-10-01 12:54:29 +00:00
/**
* Remove a Plugin from the PluginManager.
2013-10-02 10:22:20 +00:00
* @method Phaser.PluginManager#remove
* @param {Phaser.Plugin} plugin - The plugin to be removed.
2013-10-01 12:54:29 +00:00
*/
remove: function (plugin) {
// TODO
this._pluginsLength--;
},
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.
2013-10-01 12:54:29 +00:00
*
2013-10-02 10:22:20 +00:00
* @method Phaser.PluginManager#preUpdate
2013-10-01 12:54:29 +00:00
*/
preUpdate: function () {
if (this._pluginsLength == 0)
{
return;
}
for (this._p = 0; this._p < this._pluginsLength; this._p++)
{
if (this.plugins[this._p].active && this.plugins[this._p].hasPreUpdate)
{
this.plugins[this._p].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.
2013-10-01 12:54:29 +00:00
*
2013-10-02 10:22:20 +00:00
* @method Phaser.PluginManager#update
2013-10-01 12:54:29 +00:00
*/
update: function () {
if (this._pluginsLength == 0)
{
return;
}
for (this._p = 0; this._p < this._pluginsLength; this._p++)
{
if (this.plugins[this._p].active && this.plugins[this._p].hasUpdate)
{
this.plugins[this._p].update();
}
}
},
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.
2013-10-01 12:54:29 +00:00
*
2013-10-02 10:22:20 +00:00
* @method Phaser.PluginManager#render
2013-10-01 12:54:29 +00:00
*/
render: function () {
if (this._pluginsLength == 0)
{
return;
}
for (this._p = 0; this._p < this._pluginsLength; this._p++)
{
if (this.plugins[this._p].visible && this.plugins[this._p].hasRender)
{
this.plugins[this._p].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.
2013-10-01 12:54:29 +00:00
*
2013-10-02 10:22:20 +00:00
* @method Phaser.PluginManager#postRender
2013-10-01 12:54:29 +00:00
*/
postRender: function () {
if (this._pluginsLength == 0)
{
return;
}
for (this._p = 0; this._p < this._pluginsLength; this._p++)
{
if (this.plugins[this._p].visible && this.plugins[this._p].hasPostRender)
{
this.plugins[this._p].postRender();
}
}
},
2013-10-01 12:54:29 +00:00
/**
2013-10-02 10:22:20 +00:00
* Clear down this PluginManager and null out references
2013-10-01 12:54:29 +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.plugins.length = 0;
this._pluginsLength = 0;
this.game = null;
this._parent = null;
}
};