RequestAnimationFrame done and optimised massively. PluginManager added (but needs testing). Game now fleshed out with all the state changing and core loop, also optimised heavily. Also Pixi integration started and the basics are working well :)

This commit is contained in:
Richard Davey
2013-08-29 03:52:59 +01:00
parent b8d3a61c97
commit 3c8cd20b70
9 changed files with 1076 additions and 44 deletions
+79
View File
@@ -0,0 +1,79 @@
/**
* Phaser - Plugin
*
* This is a base Plugin template to use for any Phaser plugin development
*/
Phaser.Plugin = function (game, parent) {
this.game = game;
this.parent = parent;
this.active = false;
this.visible = false;
this.hasPreUpdate = false;
this.hasUpdate = false;
this.hasPostUpdate = false;
this.hasPreRender = false;
this.hasRender = false;
this.hasPostRender = false;
};
Phaser.Plugin.prototype = {,
/**
* Pre-update is called at the start of the update cycle, before any other updates have taken place.
* It is only called if active is set to true.
*/
preUpdate: function () {
},
/**
* Pre-update is called at the start of the update cycle, before any other updates have taken place.
* It is only called if active is set to true.
*/
update: function () {
},
/**
* Post-update is called at the end of the objects update cycle, after other update logic has taken place.
* It is only called if active is set to true.
*/
postUpdate: function () {
},
/**
* Pre-render is called right before the Game Renderer starts and before any custom preRender callbacks have been run.
* It is only called if visible is set to true.
*/
preRender: function () {
},
/**
* Pre-render is called right before the Game Renderer starts and before any custom preRender callbacks have been run.
* It is only called if visible is set to true.
*/
render: function () {
},
/**
* Post-render is called after every camera and game object has been rendered, also after any custom postRender callbacks have been run.
* It is only called if visible is set to true.
*/
postRender: function () {
},
/**
* Clear down this Plugin and null out references
*/
destroy: function () {
this.game = null;
this.parent = null;
this.active = false;
this.visible = false;
}
};
+199
View File
@@ -0,0 +1,199 @@
/**
* Phaser - PluginManager
*
* TODO: We can optimise this a lot by using separate hashes per function (update, render, etc)
*/
Phaser.PluginManager = function(game, parent) {
this.game = game;
this._parent = parent;
this.plugins = [];
this._pluginsLength = 0;
};
Phaser.PluginManager.prototype = {,
/**
* Add a new Plugin to the PluginManager.
* The plugins game and parent reference are set to this game and pluginmanager parent.
* @type {Phaser.Plugin}
*/
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['postUpdate'] === 'function') {
plugin.hasPostUpdate = true;
result = true;
}
if (typeof plugin['preRender'] === 'function') {
plugin.hasPreRender = 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.hasPreRender || plugin.hasRender || plugin.hasPostRender)
{
plugin.visible = true;
}
this._pluginsLength = this.plugins.push(plugin);
return plugin;
}
else
{
return null;
}
},
remove: function (plugin) {
// TODO
this._pluginsLength--;
},
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();
}
}
},
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();
}
}
},
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();
}
}
},
preRender: 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].hasPreRender) {
this.plugins[this._p].preRender();
}
}
},
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();
}
}
},
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();
}
}
},
destroy: function () {
this.plugins.length = 0;
this._pluginsLength = 0;
this.game = null;
this._parent = null;
}
};