2013-08-29 02:52:59 +00:00
|
|
|
/**
|
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-08-29 02:52:59 +00:00
|
|
|
*/
|
|
|
|
|
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.
|
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
Phaser.PluginManager = function(game, parent) {
|
|
|
|
|
2013-10-01 12:54:29 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Game} game - A reference to the currently running game.
|
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
this.game = game;
|
2013-10-01 12:54:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Description} _parent - Description.
|
|
|
|
* @private
|
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
this._parent = parent;
|
2013-10-01 12:54:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {array} plugins - Description.
|
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
this.plugins = [];
|
2013-10-01 12:54:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {array} _pluginsLength - Description.
|
|
|
|
* @private
|
|
|
|
* @default
|
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
this._pluginsLength = 0;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2013-08-29 06:06:16 +00:00
|
|
|
Phaser.PluginManager.prototype = {
|
2013-08-29 02:52:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2013-08-29 02:52:59 +00:00
|
|
|
*/
|
|
|
|
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
|
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)
|
|
|
|
{
|
|
|
|
if (plugin.hasPreUpdate || plugin.hasUpdate)
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._pluginsLength = 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')
|
|
|
|
{
|
|
|
|
plugin.init();
|
|
|
|
}
|
|
|
|
|
2013-08-29 02:52:59 +00:00
|
|
|
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
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
remove: function (plugin) {
|
2013-11-08 06:11:36 +00:00
|
|
|
|
|
|
|
if (this._pluginsLength == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-08-29 02:52:59 +00:00
|
|
|
|
2013-11-08 06:11:36 +00:00
|
|
|
for (this._p = 0; this._p < this._pluginsLength; this._p++)
|
|
|
|
{
|
|
|
|
if (this.plugins[this._p] === plugin)
|
|
|
|
{
|
|
|
|
plugin.destroy();
|
|
|
|
this.plugins.splice(this._p, 1);
|
|
|
|
this._pluginsLength--;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2013-08-29 02:52:59 +00:00
|
|
|
|
2013-11-08 06:11:36 +00:00
|
|
|
/**
|
|
|
|
* Removes all Plugins from the PluginManager.
|
|
|
|
* @method Phaser.PluginManager#removeAll
|
|
|
|
*/
|
|
|
|
removeAll: function() {
|
|
|
|
|
|
|
|
for (this._p = 0; this._p < this._pluginsLength; this._p++)
|
|
|
|
{
|
|
|
|
this.plugins[this._p].destroy();
|
|
|
|
}
|
|
|
|
this.plugins.length = 0;
|
|
|
|
this._pluginsLength = 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.
|
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
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
preUpdate: function () {
|
|
|
|
|
|
|
|
if (this._pluginsLength == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-11 16:32:53 +00:00
|
|
|
for (this._p = 0; this._p < this._pluginsLength; this._p++)
|
|
|
|
{
|
|
|
|
if (this.plugins[this._p].active && this.plugins[this._p].hasPreUpdate)
|
|
|
|
{
|
2013-08-29 02:52:59 +00:00
|
|
|
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
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
update: function () {
|
|
|
|
|
|
|
|
if (this._pluginsLength == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-11 16:32:53 +00:00
|
|
|
for (this._p = 0; this._p < this._pluginsLength; this._p++)
|
2013-08-29 02:52:59 +00:00
|
|
|
{
|
2013-09-11 16:32:53 +00:00
|
|
|
if (this.plugins[this._p].active && this.plugins[this._p].hasUpdate)
|
|
|
|
{
|
|
|
|
this.plugins[this._p].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.
|
|
|
|
*
|
|
|
|
* @method Phaser.PluginManager#postUpdate
|
|
|
|
*/
|
|
|
|
postUpdate: 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].hasPostUpdate)
|
|
|
|
{
|
|
|
|
this.plugins[this._p].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.
|
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
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
render: function () {
|
|
|
|
|
|
|
|
if (this._pluginsLength == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-11 16:32:53 +00:00
|
|
|
for (this._p = 0; this._p < this._pluginsLength; this._p++)
|
|
|
|
{
|
|
|
|
if (this.plugins[this._p].visible && this.plugins[this._p].hasRender)
|
|
|
|
{
|
2013-08-29 02:52:59 +00:00
|
|
|
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
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
postRender: function () {
|
|
|
|
|
|
|
|
if (this._pluginsLength == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-11 16:32:53 +00:00
|
|
|
for (this._p = 0; this._p < this._pluginsLength; this._p++)
|
|
|
|
{
|
|
|
|
if (this.plugins[this._p].visible && this.plugins[this._p].hasPostRender)
|
|
|
|
{
|
2013-08-29 02:52:59 +00:00
|
|
|
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
|
|
|
*/
|
2013-08-29 02:52:59 +00:00
|
|
|
destroy: function () {
|
|
|
|
|
|
|
|
this.plugins.length = 0;
|
|
|
|
this._pluginsLength = 0;
|
|
|
|
this.game = null;
|
|
|
|
this._parent = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|