mirror of
https://github.com/wassname/phaser.git
synced 2026-08-15 12:45:11 +08:00
StateManager improving in leaps and bounds.
This commit is contained in:
+3
-4
@@ -13,11 +13,10 @@
|
||||
// Your game will work either from within a closure or not, it doesn't matter
|
||||
(function () {
|
||||
|
||||
// These are all optional parameters now
|
||||
var game = new Phaser.Game();
|
||||
var game = new Phaser.Game(800, 600, Phaser.RENDERER_AUTO, '', { preload: preload, create: create });
|
||||
|
||||
// In this approach we're simply using functions in the current scope to do what we need
|
||||
game.launch(this, preload, create);
|
||||
// game.launch(this, preload, create);
|
||||
|
||||
function preload() {
|
||||
|
||||
@@ -45,7 +44,7 @@
|
||||
|
||||
}
|
||||
|
||||
/})();
|
||||
})();
|
||||
|
||||
</script>
|
||||
|
||||
|
||||
+3
-5
@@ -16,7 +16,7 @@
|
||||
|
||||
state.preload = function() {
|
||||
|
||||
console.log('state preload called');
|
||||
console.log('state preload called', this);
|
||||
|
||||
// When an instance of Phaser.State is added to Phaser.Game it becomes bound to that game
|
||||
// which opens up lots of handy short-cuts to helpers and libs, such as this.math, this.load, this.cache, etc
|
||||
@@ -32,7 +32,7 @@
|
||||
console.log('state create called');
|
||||
|
||||
// Let's try adding an image to the DOM
|
||||
document.body.appendChild(this.cache.getImage('cockpit'));
|
||||
document.body.appendChild(this.cache.getImage('overdose'));
|
||||
|
||||
// And some text
|
||||
var para = document.createElement('pre');
|
||||
@@ -41,11 +41,9 @@
|
||||
|
||||
}
|
||||
|
||||
var megaBlasterGame = new Phaser.Game();
|
||||
|
||||
// In this approach we're using a Phaser.State object and switching to it
|
||||
// This gives us the advantage that from within state functions you can access 'this' and most the properties of Phaser.Game
|
||||
megaBlasterGame.switchState(state);
|
||||
var game = new Phaser.Game(800, 600, Phaser.RENDERER_AUTO, '', state);
|
||||
|
||||
})();
|
||||
|
||||
|
||||
+2
-4
@@ -37,10 +37,8 @@
|
||||
}
|
||||
}
|
||||
|
||||
var game = new Phaser.Game();
|
||||
|
||||
// This method now works either from within a closure or not
|
||||
game.switchState(state);
|
||||
// In this example we're passing in a custom state object, not an instance of Phaser.State
|
||||
var game = new Phaser.Game(800, 600, Phaser.RENDERER_AUTO, '', state);
|
||||
|
||||
})();
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<head>
|
||||
<title>phaser.js - a new beginning</title>
|
||||
<?php
|
||||
require('js.php');
|
||||
?>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<script type="text/javascript">
|
||||
|
||||
var MegaGame = {};
|
||||
|
||||
MegaGame.MainMenu = function (game) {
|
||||
// A reference to the local game will always be passed to your state when created
|
||||
// You should store it somewhere handy for use in your functions, like this ...
|
||||
this.game = game;
|
||||
};
|
||||
|
||||
MegaGame.MainMenu.prototype = {
|
||||
|
||||
preload: function () {
|
||||
|
||||
console.log('MainMenu.preload');
|
||||
this.game.load.image('bladerunner', 'assets/pics/acryl_bladerunner.png');
|
||||
|
||||
},
|
||||
|
||||
create: function () {
|
||||
|
||||
console.log('MainMenu.create');
|
||||
document.body.appendChild(this.game.cache.getImage('bladerunner'));
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
(function () {
|
||||
|
||||
// Finally in this example we pass in a custom State object which will be created upon boot
|
||||
var game = new Phaser.Game(800, 600, Phaser.RENDERER_AUTO, '', MegaGame.MainMenu);
|
||||
|
||||
})();
|
||||
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,50 @@
|
||||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<head>
|
||||
<title>phaser.js - a new beginning</title>
|
||||
<?php
|
||||
require('js.php');
|
||||
?>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<script type="text/javascript">
|
||||
|
||||
(function () {
|
||||
|
||||
var state = {
|
||||
|
||||
preload: function() {
|
||||
|
||||
console.log('state preload called');
|
||||
game.load.image('rememberMe', 'assets/pics/remember-me.jpg');
|
||||
game.load.text('copyright', 'assets/warning - copyright.txt');
|
||||
|
||||
},
|
||||
|
||||
create: function() {
|
||||
|
||||
console.log('state create called');
|
||||
|
||||
// Let's try adding an image to the DOM
|
||||
document.body.appendChild(game.cache.getImage('rememberMe'));
|
||||
|
||||
// And some text
|
||||
var para = document.createElement('pre');
|
||||
para.innerHTML = game.cache.getText('copyright');
|
||||
document.body.appendChild(para);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
var game = new Phaser.Game(800, 600, Phaser.RENDERER_AUTO, '');
|
||||
|
||||
// In this instance we'll change to the state ourselves rather than pass it in the game constructor
|
||||
|
||||
|
||||
})();
|
||||
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -42,6 +42,7 @@
|
||||
<script src="../src/pixi/utils/Polyk.js"></script>
|
||||
<script src="../src/pixi/utils/Utils.js"></script>
|
||||
|
||||
<script src="../src/core/StateManager.js"></script>
|
||||
<script src="../src/core/State.js"></script>
|
||||
<script src="../src/system/RequestAnimationFrame.js"></script>
|
||||
<script src="../src/system/Device.js"></script>
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<head>
|
||||
<title>phaser.js - a new beginning</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<script type="text/javascript">
|
||||
|
||||
var BigGame = function(context) {
|
||||
|
||||
console.log(context);
|
||||
console.log(context['hello']);
|
||||
// this.pendingCallback = callback;
|
||||
this.pendingCallback = context['hello'];
|
||||
|
||||
var _this = this;
|
||||
|
||||
this._onLoop = function () {
|
||||
return _this.boot();
|
||||
}
|
||||
|
||||
if (document.readyState === 'complete' || document.readyState === 'interactive')
|
||||
{
|
||||
window.setTimeout(this._onLoop, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
document.addEventListener('DOMContentLoaded', this._onLoop, false);
|
||||
window.addEventListener('load', this._onLoop, false);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
BigGame.prototype = {
|
||||
|
||||
isBooted: false,
|
||||
pendingCallback: null,
|
||||
_onLoop: null,
|
||||
|
||||
boot: function () {
|
||||
|
||||
if (this.isBooted) {
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
document.removeEventListener('DOMContentLoaded', this._onLoop);
|
||||
window.removeEventListener('load', this._onLoop);
|
||||
|
||||
this.pendingCallback();
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
check: function () {
|
||||
console.log(Math.random());
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
(function () {
|
||||
|
||||
// because this is in a closure we have to pass a reference to the function directly, it can't guess it
|
||||
// var bob = new BigGame(hello);
|
||||
//var bob = new BigGame(this);
|
||||
|
||||
function hello() {
|
||||
console.log('hello world 2');
|
||||
console.log(bob);
|
||||
bob.check();
|
||||
}
|
||||
|
||||
})();
|
||||
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
+74
-220
@@ -24,26 +24,43 @@
|
||||
* @param renderer {number} Which renderer to use (canvas or webgl)
|
||||
* @param parent {string} ID of its parent DOM element.
|
||||
*/
|
||||
Phaser.Game = function (width, height, renderer, parent) {
|
||||
Phaser.Game = function (width, height, renderer, parent, state) {
|
||||
|
||||
if (typeof width === "undefined") { width = 800; }
|
||||
if (typeof height === "undefined") { height = 600; }
|
||||
if (typeof renderer === "undefined") { renderer = Phaser.RENDERER_AUTO; }
|
||||
if (typeof parent === "undefined") { parent = ''; }
|
||||
if (typeof state === "undefined") { state = {}; }
|
||||
|
||||
this.id = Phaser.GAMES.push(this) - 1;
|
||||
this.parent = parent;
|
||||
|
||||
// Do some more intelligent size parsing here, so they can set "100%" for example, maybe pass the scale mode in here too?
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
|
||||
this.renderer = renderer;
|
||||
|
||||
//var _this = this;
|
||||
console.log('Phaser.Game', width, height, renderer, parent);
|
||||
|
||||
// Pass 'state' to the StateManager?
|
||||
this.state = new Phaser.StateManager(this, state);
|
||||
// this._tempState = state;
|
||||
|
||||
var _this = this;
|
||||
|
||||
this._onBoot = function () {
|
||||
return _this.boot();
|
||||
}
|
||||
|
||||
if (document.readyState === 'complete' || document.readyState === 'interactive')
|
||||
{
|
||||
setTimeout(Phaser.GAMES[this.id].boot(parent, width, height), 0);
|
||||
window.setTimeout(this._onBoot, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
document.addEventListener('DOMContentLoaded', Phaser.GAMES[this.id].boot(parent, width, height), false);
|
||||
window.addEventListener('load', Phaser.GAMES[this.id].boot(parent, width, height), false);
|
||||
document.addEventListener('DOMContentLoaded', this._onBoot, false);
|
||||
window.addEventListener('load', this._onBoot, false);
|
||||
}
|
||||
|
||||
return this;
|
||||
@@ -52,12 +69,34 @@ Phaser.Game = function (width, height, renderer, parent) {
|
||||
|
||||
Phaser.Game.prototype = {
|
||||
|
||||
// temps so we can clean-up the event listeneres
|
||||
_tempState: null,
|
||||
_onBoot: null,
|
||||
|
||||
/**
|
||||
* Phaser Game ID.
|
||||
* @type {number}
|
||||
*/
|
||||
id: 0,
|
||||
|
||||
/**
|
||||
* The Game width (in pixels).
|
||||
* @type {number}
|
||||
*/
|
||||
width: 0,
|
||||
|
||||
/**
|
||||
* The Game height (in pixels).
|
||||
* @type {number}
|
||||
*/
|
||||
height: 0,
|
||||
|
||||
/**
|
||||
* The Games DOM parent.
|
||||
* @type {HTMLElement}
|
||||
*/
|
||||
parent: '',
|
||||
|
||||
/**
|
||||
* The Renderer this Phaser.Game will use. Either Phaser.RENDERER_AUTO, Phaser.RENDERER_CANVAS or Phaser.RENDERER_WEBGL
|
||||
* @type {number}
|
||||
@@ -76,72 +115,6 @@ Phaser.Game.prototype = {
|
||||
*/
|
||||
_paused: false,
|
||||
|
||||
/**
|
||||
* The state to be switched to in the next frame.
|
||||
* @type {State}
|
||||
*/
|
||||
_pendingState: null,
|
||||
|
||||
/**
|
||||
* The current State object (defaults to null)
|
||||
* @type {State}
|
||||
*/
|
||||
state: null,
|
||||
|
||||
/**
|
||||
* This will be called when init states. (loading assets...)
|
||||
* @type {function}
|
||||
*/
|
||||
onPreloadCallback: null,
|
||||
|
||||
/**
|
||||
* This will be called when create states. (setup states...)
|
||||
* @type {function}
|
||||
*/
|
||||
onCreateCallback: null,
|
||||
|
||||
/**
|
||||
* This will be called when State is updated, this doesn't happen during load (see onLoadUpdateCallback)
|
||||
* @type {function}
|
||||
*/
|
||||
onUpdateCallback: null,
|
||||
|
||||
/**
|
||||
* This will be called when the State is rendered, this doesn't happen during load (see onLoadRenderCallback)
|
||||
* @type {function}
|
||||
*/
|
||||
onRenderCallback: null,
|
||||
|
||||
/**
|
||||
* This will be called before the State is rendered and before the stage is cleared
|
||||
* @type {function}
|
||||
*/
|
||||
onPreRenderCallback: null,
|
||||
|
||||
/**
|
||||
* This will be called when the State is updated but only during the load process
|
||||
* @type {function}
|
||||
*/
|
||||
onLoadUpdateCallback: null,
|
||||
|
||||
/**
|
||||
* This will be called when the State is rendered but only during the load process
|
||||
* @type {function}
|
||||
*/
|
||||
onLoadRenderCallback: null,
|
||||
|
||||
/**
|
||||
* This will be called when states paused.
|
||||
* @type {function}
|
||||
*/
|
||||
onPausedCallback: null,
|
||||
|
||||
/**
|
||||
* This will be called when the state is destroyed (i.e. swapping to a new state)
|
||||
* @type {function}
|
||||
*/
|
||||
onDestroyCallback: null,
|
||||
|
||||
/**
|
||||
* Whether the game engine is booted, aka available.
|
||||
* @type {bool}
|
||||
@@ -156,9 +129,16 @@ Phaser.Game.prototype = {
|
||||
|
||||
/**
|
||||
* Automatically handles the core game loop via requestAnimationFrame or setTimeout
|
||||
* @type {Phaser.RequestAnimationFrame}
|
||||
*/
|
||||
raf: null,
|
||||
|
||||
/**
|
||||
* The StateManager.
|
||||
* @type {Phaser.StateManager}
|
||||
*/
|
||||
state: null,
|
||||
|
||||
/**
|
||||
* Reference to the GameObject Factory.
|
||||
* @type {Phaser.GameObjectFactory}
|
||||
@@ -249,24 +229,25 @@ Phaser.Game.prototype = {
|
||||
* @param width {number} Width of the game screen.
|
||||
* @param height {number} Height of the game screen.
|
||||
*/
|
||||
boot: function (parent, width, height) {
|
||||
boot: function () {
|
||||
|
||||
if (this.isBooted) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('Phaser.Game.Boot');
|
||||
console.log(this);
|
||||
console.log('Phaser.Game boot');
|
||||
|
||||
var _this = this;
|
||||
// Probably not needed any more
|
||||
// var _this = this;
|
||||
|
||||
if (!document.body) {
|
||||
setTimeout(Phaser.GAMES[_this.id].boot(parent, width, height), 13);
|
||||
window.setTimeout(this._onBoot, 20);
|
||||
// setTimeout(Phaser.GAMES[_this.id].boot(parent, width, height), 13);
|
||||
}
|
||||
else
|
||||
{
|
||||
document.removeEventListener('DOMContentLoaded', Phaser.GAMES[_this.id].boot);
|
||||
window.removeEventListener('load', Phaser.GAMES[_this.id].boot);
|
||||
document.removeEventListener('DOMContentLoaded', this._onBoot);
|
||||
window.removeEventListener('load', this._onBoot);
|
||||
|
||||
this.onPause = new Phaser.Signal();
|
||||
this.onResume = new Phaser.Signal();
|
||||
@@ -286,9 +267,11 @@ Phaser.Game.prototype = {
|
||||
this.rnd = new Phaser.RandomDataGenerator([(Date.now() * Math.random()).toString()]);
|
||||
// this.physics = new Phaser.Physics.PhysicsManager(this);
|
||||
this.plugins = new Phaser.PluginManager(this, this);
|
||||
// this.state = new Phaser.StateManager(this, this._tempState);
|
||||
|
||||
this.load.onLoadComplete.add(this.loadComplete, this);
|
||||
|
||||
this.state.boot();
|
||||
// this.world.boot();
|
||||
// this.stage.boot();
|
||||
// this.input.boot();
|
||||
@@ -299,22 +282,11 @@ Phaser.Game.prototype = {
|
||||
this.isRunning = true;
|
||||
this._loadComplete = false;
|
||||
|
||||
this.raf = new Phaser.RequestAnimationFrame(this);
|
||||
this.raf.start();
|
||||
// this.raf = new Phaser.RequestAnimationFrame(this);
|
||||
// this.raf.start();
|
||||
|
||||
// boot sm
|
||||
|
||||
if (this.onPreloadCallback || this.onCreateCallback || this.onUpdateCallback || this.onRenderCallback || this._pendingState)
|
||||
{
|
||||
if (this._pendingState)
|
||||
{
|
||||
console.log('boot has found a pending state');
|
||||
this.switchState(this._pendingState, false, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
console.log('boot has found enough callbacks to start the state');
|
||||
this.startState();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
@@ -329,6 +301,7 @@ Phaser.Game.prototype = {
|
||||
*/
|
||||
launch: function (context, preload, create, update, render) {
|
||||
|
||||
/*
|
||||
this.callbackContext = context;
|
||||
|
||||
this.onPreloadCallback = preload || null;
|
||||
@@ -363,11 +336,12 @@ Phaser.Game.prototype = {
|
||||
console.log('launch has set the callbacks but cant start because the DOM isnt booted yet');
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Called when the load has finished after preload was run.
|
||||
* Called when the load has finished, after preload was run.
|
||||
*/
|
||||
loadComplete: function () {
|
||||
|
||||
@@ -375,56 +349,14 @@ Phaser.Game.prototype = {
|
||||
|
||||
this._loadComplete = true;
|
||||
|
||||
if (this.onCreateCallback) {
|
||||
this.onCreateCallback.call(this.callbackContext);
|
||||
// this.onCreateCallback.call(this);
|
||||
}
|
||||
this.state.loadComplete();
|
||||
|
||||
},
|
||||
this.load.onLoadComplete.remove(this.loadComplete, this);
|
||||
|
||||
/**
|
||||
* Start the current state
|
||||
*/
|
||||
startState: function () {
|
||||
|
||||
console.log('startState');
|
||||
// console.log(this);
|
||||
// console.log(this.callbackContext);
|
||||
|
||||
if (this.onPreloadCallback !== null)
|
||||
{
|
||||
this.load.reset();
|
||||
this.onPreloadCallback.call(this.callbackContext);
|
||||
// this.onPreloadCallback.call(this.onPreloadCallback);
|
||||
|
||||
// Is the loader empty?
|
||||
if (this.load.queueSize == 0)
|
||||
{
|
||||
if (this.onCreateCallback !== null)
|
||||
{
|
||||
this.onCreateCallback.call(this.callbackContext);
|
||||
// this.onCreateCallback.call(this.onCreateCallback);
|
||||
}
|
||||
|
||||
this._loadComplete = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Start the loader going as we have something in the queue
|
||||
this.load.onLoadComplete.add(this.loadComplete, this);
|
||||
this.load.start();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// No init? Then there was nothing to load either
|
||||
if (this.onCreateCallback !== null) {
|
||||
this.onCreateCallback.call(this.callbackContext);
|
||||
// this.onCreateCallback.call(this.onCreateCallback);
|
||||
}
|
||||
|
||||
this._loadComplete = true;
|
||||
}
|
||||
// if (this.onCreateCallback) {
|
||||
// this.onCreateCallback.call(this.callbackContext);
|
||||
// // this.onCreateCallback.call(this);
|
||||
// }
|
||||
|
||||
},
|
||||
|
||||
@@ -493,84 +425,6 @@ Phaser.Game.prototype = {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Switch to a new State.
|
||||
* @param state {State} The state you want to switch to.
|
||||
* @param [clearWorld] {bool} clear everything in the world? (Default to true)
|
||||
* @param [clearCache] {bool} clear asset cache? (Default to false and ONLY available when clearWorld=true)
|
||||
*/
|
||||
switchState: function (state, clearWorld, clearCache) {
|
||||
|
||||
// console.log('switchState', state, this.isBooted);
|
||||
// console.log(typeof state);
|
||||
// console.log(state instanceof Phaser.State);
|
||||
|
||||
if (typeof clearWorld === "undefined") { clearWorld = true; }
|
||||
if (typeof clearCache === "undefined") { clearCache = false; }
|
||||
|
||||
if (state instanceof Phaser.State) {
|
||||
state.link(this);
|
||||
}
|
||||
|
||||
if (this.isBooted == false) {
|
||||
this._pendingState = state;
|
||||
return;
|
||||
}
|
||||
|
||||
// Destroy current state?
|
||||
if (this.onDestroyCallback !== null) {
|
||||
this.onDestroyCallback.call(this.callbackContext);
|
||||
}
|
||||
|
||||
if (this.input) {
|
||||
this.input.reset(true);
|
||||
}
|
||||
|
||||
// Prototype?
|
||||
if (typeof state === 'function')
|
||||
{
|
||||
this.state = new state(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
// Ok, have we got at least a create or update function?
|
||||
if (this.state['create'] || this.state['update']) {
|
||||
|
||||
this.callbackContext = this.state;
|
||||
|
||||
// Bingo, let's set them up
|
||||
this.onPreloadCallback = this.state['preload'] || null;
|
||||
this.onLoadRenderCallback = this.state['loadRender'] || null;
|
||||
this.onLoadUpdateCallback = this.state['loadUpdate'] || null;
|
||||
this.onCreateCallback = this.state['create'] || null;
|
||||
this.onUpdateCallback = this.state['update'] || null;
|
||||
this.onPreRenderCallback = this.state['preRender'] || null;
|
||||
this.onRenderCallback = this.state['render'] || null;
|
||||
this.onPausedCallback = this.state['paused'] || null;
|
||||
this.onDestroyCallback = this.state['destroy'] || null;
|
||||
|
||||
if (clearWorld) {
|
||||
|
||||
//this.world.destroy();
|
||||
|
||||
if (clearCache == true) {
|
||||
this.cache.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
this._loadComplete = false;
|
||||
|
||||
this.startState();
|
||||
}
|
||||
else
|
||||
{
|
||||
console.warn("Invalid Phaser State object given. Must contain at least a create or update function.");
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Nuke the entire game from orbit
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,516 @@
|
||||
Phaser.StateManager = function (game, pendingState) {
|
||||
|
||||
this.game = game;
|
||||
|
||||
this.states = {};
|
||||
|
||||
this._pendingState = pendingState;
|
||||
|
||||
};
|
||||
|
||||
Phaser.StateManager.prototype = {
|
||||
|
||||
/**
|
||||
* @type {Phaser.Game}
|
||||
*/
|
||||
game: null,
|
||||
|
||||
/**
|
||||
* The state to be switched to in the next frame.
|
||||
* @type {State}
|
||||
*/
|
||||
_pendingState: null,
|
||||
|
||||
/**
|
||||
* The state to be switched to in the next frame.
|
||||
* @type {Object}
|
||||
*/
|
||||
states: {},
|
||||
|
||||
/**
|
||||
* The current active State object (defaults to null)
|
||||
* @type {String}
|
||||
*/
|
||||
current: '',
|
||||
|
||||
/**
|
||||
* This will be called when the state is started (i.e. set as the current active state)
|
||||
* @type {function}
|
||||
*/
|
||||
onInitCallback: null,
|
||||
|
||||
/**
|
||||
* This will be called when init states. (loading assets...)
|
||||
* @type {function}
|
||||
*/
|
||||
onPreloadCallback: null,
|
||||
|
||||
/**
|
||||
* This will be called when create states. (setup states...)
|
||||
* @type {function}
|
||||
*/
|
||||
onCreateCallback: null,
|
||||
|
||||
/**
|
||||
* This will be called when State is updated, this doesn't happen during load (see onLoadUpdateCallback)
|
||||
* @type {function}
|
||||
*/
|
||||
onUpdateCallback: null,
|
||||
|
||||
/**
|
||||
* This will be called when the State is rendered, this doesn't happen during load (see onLoadRenderCallback)
|
||||
* @type {function}
|
||||
*/
|
||||
onRenderCallback: null,
|
||||
|
||||
/**
|
||||
* This will be called before the State is rendered and before the stage is cleared
|
||||
* @type {function}
|
||||
*/
|
||||
onPreRenderCallback: null,
|
||||
|
||||
/**
|
||||
* This will be called when the State is updated but only during the load process
|
||||
* @type {function}
|
||||
*/
|
||||
onLoadUpdateCallback: null,
|
||||
|
||||
/**
|
||||
* This will be called when the State is rendered but only during the load process
|
||||
* @type {function}
|
||||
*/
|
||||
onLoadRenderCallback: null,
|
||||
|
||||
/**
|
||||
* This will be called when states paused.
|
||||
* @type {function}
|
||||
*/
|
||||
onPausedCallback: null,
|
||||
|
||||
/**
|
||||
* This will be called when the state is shut down (i.e. swapped to another state)
|
||||
* @type {function}
|
||||
*/
|
||||
onShutDownCallback: null,
|
||||
|
||||
boot: function () {
|
||||
|
||||
console.log('Phaser.StateManager.boot');
|
||||
console.log(typeof this._pendingState);
|
||||
|
||||
this.add('default', this._pendingState, true);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Add a new State.
|
||||
* @param key {String} A unique key you use to reference this state, i.e. "MainMenu", "Level1".
|
||||
* @param state {State} The state you want to switch to.
|
||||
* @param autoStart {Boolean} Start the state immediately after creating it? (default true)
|
||||
*/
|
||||
add: function (key, state, autoStart) {
|
||||
|
||||
if (typeof autoStart === "undefined") { autoStart = true; }
|
||||
|
||||
console.log('Phaser.StateManager.addState', key);
|
||||
console.log(typeof state);
|
||||
|
||||
var newState;
|
||||
|
||||
if (state instanceof Phaser.State)
|
||||
{
|
||||
console.log('Phaser.StateManager.addState: Phaser.State given');
|
||||
newState = state;
|
||||
newState.link(this.game);
|
||||
}
|
||||
else if (typeof state === 'object')
|
||||
{
|
||||
console.log('Phaser.StateManager.addState: Object given');
|
||||
newState = state;
|
||||
}
|
||||
else if (typeof state === 'function')
|
||||
{
|
||||
console.log('Phaser.StateManager.addState: Function given');
|
||||
newState = new state(this.game);
|
||||
}
|
||||
|
||||
this.states[key] = newState;
|
||||
|
||||
if (autoStart)
|
||||
{
|
||||
this.start(key);
|
||||
}
|
||||
|
||||
return newState;
|
||||
|
||||
},
|
||||
|
||||
remove: function (key) {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Start the given state
|
||||
* @param key {String} The key of the state you want to start.
|
||||
* @param [clearWorld] {bool} clear everything in the world? (Default to true)
|
||||
* @param [clearCache] {bool} clear asset cache? (Default to false and ONLY available when clearWorld=true)
|
||||
*/
|
||||
start: function (key, clearWorld, clearCache) {
|
||||
|
||||
console.log('Phaser.StateManager.start', key);
|
||||
// console.log(this);
|
||||
// console.log(this.callbackContext);
|
||||
|
||||
if (typeof clearWorld === "undefined") { clearWorld = true; }
|
||||
if (typeof clearCache === "undefined") { clearCache = false; }
|
||||
|
||||
if (this.checkState(key) == false)
|
||||
{
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Already got a state running?
|
||||
if (this.current)
|
||||
{
|
||||
this.onShutDownCallback.call(this.callbackContext);
|
||||
}
|
||||
|
||||
if (clearWorld) {
|
||||
|
||||
//this.game.world.destroy();
|
||||
|
||||
if (clearCache == true) {
|
||||
this.game.cache.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
this.setCurrentState(key);
|
||||
}
|
||||
|
||||
if (this.onPreloadCallback)
|
||||
{
|
||||
console.log('Preload Callback found');
|
||||
this.game.load.reset();
|
||||
this.onPreloadCallback.call(this.callbackContext);
|
||||
|
||||
// Is the loader empty?
|
||||
if (this.game.load.queueSize == 0)
|
||||
{
|
||||
console.log('Loader queue empty');
|
||||
this.game.loadComplete();
|
||||
|
||||
if (this.onCreateCallback)
|
||||
{
|
||||
this.onCreateCallback.call(this.callbackContext);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
console.log('Loader started');
|
||||
// Start the loader going as we have something in the queue
|
||||
this.game.load.start();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
console.log('Preload callback not found');
|
||||
// No init? Then there was nothing to load either
|
||||
if (this.onCreateCallback)
|
||||
{
|
||||
console.log('Create callback found');
|
||||
this.onCreateCallback.call(this.callbackContext);
|
||||
}
|
||||
|
||||
this.game.loadComplete();
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
// Used by onInit and onShutdown when those functions don't exist on the state
|
||||
dummy: function () {
|
||||
},
|
||||
|
||||
checkState: function (key) {
|
||||
|
||||
if (this.states[key])
|
||||
{
|
||||
var valid = false;
|
||||
|
||||
if (this.states[key]['preload']) { valid = true; }
|
||||
|
||||
if (valid == false && this.states[key]['loadRender']) { valid = true; }
|
||||
if (valid == false && this.states[key]['loadUpdate']) { valid = true; }
|
||||
if (valid == false && this.states[key]['create']) { valid = true; }
|
||||
if (valid == false && this.states[key]['update']) { valid = true; }
|
||||
if (valid == false && this.states[key]['preRender']) { valid = true; }
|
||||
if (valid == false && this.states[key]['render']) { valid = true; }
|
||||
if (valid == false && this.states[key]['paused']) { valid = true; }
|
||||
|
||||
if (valid == false)
|
||||
{
|
||||
console.warn("Invalid Phaser State object given. Must contain at least a one of the required functions.");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
console.warn("Phaser.StateManager - No state found with the key: " + key);
|
||||
return false;
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
setCurrentState: function (key) {
|
||||
|
||||
this.callbackContext = this.states[key];
|
||||
|
||||
// Used when the state is set as being the current active state
|
||||
this.onInitCallback = this.states[key]['init'] || this.dummy;
|
||||
|
||||
this.onPreloadCallback = this.states[key]['preload'] || null;
|
||||
this.onLoadRenderCallback = this.states[key]['loadRender'] || null;
|
||||
this.onLoadUpdateCallback = this.states[key]['loadUpdate'] || null;
|
||||
this.onCreateCallback = this.states[key]['create'] || null;
|
||||
this.onUpdateCallback = this.states[key]['update'] || null;
|
||||
this.onPreRenderCallback = this.states[key]['preRender'] || null;
|
||||
this.onRenderCallback = this.states[key]['render'] || null;
|
||||
this.onPausedCallback = this.states[key]['paused'] || null;
|
||||
|
||||
// Used when the state is no longer the current active state
|
||||
this.onShutDownCallback = this.states[key]['shutdown'] || this.dummy;
|
||||
|
||||
this.current = key;
|
||||
|
||||
this.onInitCallback.call(this.callbackContext);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Switch to a new State.
|
||||
* @param state {State} The state you want to switch to. If a string will be treated as a reference to an already stored state.
|
||||
* @param [clearWorld] {bool} clear everything in the world? (Default to true)
|
||||
* @param [clearCache] {bool} clear asset cache? (Default to false and ONLY available when clearWorld=true)
|
||||
*/
|
||||
OLDswitchState: function (state, clearWorld, clearCache) {
|
||||
|
||||
if (typeof clearWorld === "undefined") { clearWorld = true; }
|
||||
if (typeof clearCache === "undefined") { clearCache = false; }
|
||||
|
||||
console.log('Phaser.StateManager.switchState');
|
||||
console.log(typeof state);
|
||||
|
||||
var result = false;
|
||||
var newState;
|
||||
|
||||
// Check the pendingState object to see if it contains anything we can use
|
||||
if (typeof state === 'string')
|
||||
{
|
||||
console.log('Phaser.StateManager.boot: string given');
|
||||
}
|
||||
else if (state instanceof Phaser.State)
|
||||
{
|
||||
console.log('Phaser.StateManager.boot: Phaser.State given');
|
||||
newState = state;
|
||||
newState.link(this.game);
|
||||
result = this.setCallbacks();
|
||||
}
|
||||
else if (typeof state === 'object')
|
||||
{
|
||||
console.log('Phaser.StateManager.boot: Object given');
|
||||
newState = state;
|
||||
result = this.setCallbacks();
|
||||
}
|
||||
else if (typeof state === 'function')
|
||||
{
|
||||
console.log('Phaser.StateManager.boot: function reference given');
|
||||
newState = new state(this.game);
|
||||
result = this.setCallbacks();
|
||||
}
|
||||
|
||||
if (result)
|
||||
{
|
||||
console.log("We've been given and successfully parsed a valid state");
|
||||
|
||||
var idx = this.states.push(newState);
|
||||
|
||||
// We've been given and successfully parsed a valid state
|
||||
this.startState();
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
},
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Start the current state
|
||||
*/
|
||||
OLDstartState: function () {
|
||||
result = this.setCallbacks();
|
||||
|
||||
console.log('Phaser.StateManager.startState');
|
||||
// console.log(this);
|
||||
// console.log(this.callbackContext);
|
||||
|
||||
|
||||
|
||||
if (this.onPreloadCallback !== null)
|
||||
{
|
||||
console.log('Preload Callback found');
|
||||
this.game.load.reset();
|
||||
this.onPreloadCallback.call(this.callbackContext);
|
||||
// this.onPreloadCallback.call(this.onPreloadCallback);
|
||||
|
||||
// Is the loader empty?
|
||||
if (this.game.load.queueSize == 0)
|
||||
{
|
||||
console.log('Loader queue empty');
|
||||
// this.game._loadComplete = true;
|
||||
this.game.loadComplete();
|
||||
|
||||
if (this.onCreateCallback !== null)
|
||||
{
|
||||
this.onCreateCallback.call(this.callbackContext);
|
||||
// this.onCreateCallback.call(this.onCreateCallback);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
console.log('Loader started');
|
||||
// Start the loader going as we have something in the queue
|
||||
this.game.load.start();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
console.log('Preload callback not found');
|
||||
// No init? Then there was nothing to load either
|
||||
if (this.onCreateCallback) {
|
||||
console.log('Create callback found');
|
||||
this.onCreateCallback.call(this.callbackContext);
|
||||
// this.onCreateCallback.call(this.onCreateCallback);
|
||||
}
|
||||
|
||||
// this.game._loadComplete = true;
|
||||
this.game.loadComplete();
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
loadComplete: function () {
|
||||
|
||||
console.log('Phaser.StateManager.loadComplete');
|
||||
|
||||
if (this.onCreateCallback) {
|
||||
console.log('Create callback found');
|
||||
this.onCreateCallback.call(this.callbackContext);
|
||||
// this.onCreateCallback.call(this.onCreateCallback);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Switch to a new State.
|
||||
* @param state {State} The state you want to switch to.
|
||||
* @param [clearWorld] {bool} clear everything in the world? (Default to true)
|
||||
* @param [clearCache] {bool} clear asset cache? (Default to false and ONLY available when clearWorld=true)
|
||||
*/
|
||||
|
||||
/*
|
||||
switchState: function (state, clearWorld, clearCache) {
|
||||
|
||||
// console.log('switchState', state, this.isBooted);
|
||||
// console.log(typeof state);
|
||||
// console.log(state instanceof Phaser.State);
|
||||
|
||||
if (typeof clearWorld === "undefined") { clearWorld = true; }
|
||||
if (typeof clearCache === "undefined") { clearCache = false; }
|
||||
|
||||
if (state instanceof Phaser.State) {
|
||||
state.link(this);
|
||||
}
|
||||
|
||||
if (this.isBooted == false) {
|
||||
this._pendingState = state;
|
||||
return;
|
||||
}
|
||||
|
||||
// Destroy current state?
|
||||
if (this.onDestroyCallback !== null) {
|
||||
this.onDestroyCallback.call(this.callbackContext);
|
||||
}
|
||||
|
||||
if (this.input) {
|
||||
this.input.reset(true);
|
||||
}
|
||||
|
||||
// Prototype?
|
||||
if (typeof state === 'function')
|
||||
{
|
||||
this.state = new state(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
// Ok, have we got at least a create or update function?
|
||||
if (this.state['create'] || this.state['update']) {
|
||||
|
||||
this.callbackContext = this.state;
|
||||
|
||||
// Bingo, let's set them up
|
||||
this.onPreloadCallback = this.state['preload'] || null;
|
||||
this.onLoadRenderCallback = this.state['loadRender'] || null;
|
||||
this.onLoadUpdateCallback = this.state['loadUpdate'] || null;
|
||||
this.onCreateCallback = this.state['create'] || null;
|
||||
this.onUpdateCallback = this.state['update'] || null;
|
||||
this.onPreRenderCallback = this.state['preRender'] || null;
|
||||
this.onRenderCallback = this.state['render'] || null;
|
||||
this.onPausedCallback = this.state['paused'] || null;
|
||||
this.onDestroyCallback = this.state['destroy'] || null;
|
||||
|
||||
if (clearWorld) {
|
||||
|
||||
//this.world.destroy();
|
||||
|
||||
if (clearCache == true) {
|
||||
this.cache.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
this._loadComplete = false;
|
||||
|
||||
this.startState();
|
||||
}
|
||||
else
|
||||
{
|
||||
console.warn("Invalid Phaser State object given. Must contain at least a create or update function.");
|
||||
}
|
||||
},
|
||||
|
||||
*/
|
||||
|
||||
/**
|
||||
* Nuke the entire game from orbit
|
||||
*/
|
||||
destroy: function () {
|
||||
|
||||
this.callbackContext = null;
|
||||
this.onPreloadCallback = null;
|
||||
this.onLoadRenderCallback = null;
|
||||
this.onLoadUpdateCallback = null;
|
||||
this.onCreateCallback = null;
|
||||
this.onUpdateCallback = null;
|
||||
this.onRenderCallback = null;
|
||||
this.onPausedCallback = null;
|
||||
this.onDestroyCallback = null;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user