phaser/src/core/PluginManager.js

237 lines
5 KiB
JavaScript
Raw Normal View History

/**
2013-10-01 13:54:29 +01: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}
* @module Phaser.PluginManager
*/
2013-10-01 13:54:29 +01:00
/**
* Description.
*
* @class Phaser.PluginManager
* @classdesc PPhaser - PluginManager
* @constructor
* @param {Phaser.Game} game - A reference to the currently running game.
* @param {Description} parent - Description.
*/
Phaser.PluginManager = function(game, parent) {
2013-10-01 13:54:29 +01:00
/**
* @property {Phaser.Game} game - A reference to the currently running game.
*/
this.game = game;
2013-10-01 13:54:29 +01:00
/**
* @property {Description} _parent - Description.
* @private
*/
this._parent = parent;
2013-10-01 13:54:29 +01:00
/**
* @property {array} plugins - Description.
*/
this.plugins = [];
2013-10-01 13:54:29 +01:00
/**
* @property {array} _pluginsLength - Description.
* @private
* @default
*/
this._pluginsLength = 0;
};
Phaser.PluginManager.prototype = {
/**
* Add a new Plugin to the PluginManager.
2013-10-01 13:54:29 +01:00
* The plugin's game and parent reference are set to this game and pluginmanager parent.
2013-10-02 01:16:40 +01:00
* @method add
* @memberof Phaser.PluginManager
2013-10-01 13:54:29 +01: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 13:54:29 +01:00
/**
* Remove a Plugin from the PluginManager.
2013-10-02 01:16:40 +01:00
* @method remove
* @memberof Phaser.PluginManager
2013-10-01 13:54:29 +01:00
* @param {Phaser.Plugin} plugin - Description.
*/
remove: function (plugin) {
// TODO
this._pluginsLength--;
},
2013-10-01 13:54:29 +01:00
/**
* Description.
*
* @method preUpdate
2013-10-02 01:16:40 +01:00
* @memberof Phaser.PluginManager
2013-10-01 13:54:29 +01: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 13:54:29 +01:00
/**
* Description.
*
* @method update
2013-10-02 01:16:40 +01:00
* @memberof Phaser.PluginManager
2013-10-01 13:54:29 +01: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 13:54:29 +01:00
/**
* Description.
*
* @method render
2013-10-02 01:16:40 +01:00
* @memberof Phaser.PluginManager
2013-10-01 13:54:29 +01: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 13:54:29 +01:00
/**
* Description.
*
* @method postRender
2013-10-02 01:16:40 +01:00
* @memberof Phaser.PluginManager
2013-10-01 13:54:29 +01: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 13:54:29 +01:00
/**
* Description.
*
* @method destroy
2013-10-02 01:16:40 +01:00
* @memberof Phaser.PluginManager
2013-10-01 13:54:29 +01:00
*/
destroy: function () {
this.plugins.length = 0;
this._pluginsLength = 0;
this.game = null;
this._parent = null;
}
};