phaser/src/scene/SceneManager.js

1394 lines
32 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
2018-01-16 16:33:23 +00:00
var Class = require('../utils/Class');
var CONST = require('./const');
2018-01-16 16:33:23 +00:00
var GetValue = require('../utils/object/GetValue');
var NOOP = require('../utils/NOOP');
var Scene = require('./Scene');
var Systems = require('./Systems');
2016-11-29 13:01:16 +00:00
2018-02-12 16:18:34 +00:00
/**
* @classdesc
2018-02-13 01:39:22 +00:00
* The Scene Manager.
*
* The Scene Manager is a Game level system, responsible for creating, processing and updating all of the
* Scenes in a Game instance.
2018-02-12 16:18:34 +00:00
*
*
* @class SceneManager
* @memberOf Phaser.Scenes
* @constructor
* @since 3.0.0
*
* @param {Phaser.Game} game - The Phaser.Game instance this Scene Manager belongs to.
* @param {object} sceneConfig - Scene specific configuration settings.
*/
2018-01-16 16:33:23 +00:00
var SceneManager = new Class({
2016-11-29 13:01:16 +00:00
initialize:
2016-11-29 13:01:16 +00:00
2018-01-16 16:33:23 +00:00
function SceneManager (game, sceneConfig)
{
2018-02-12 16:18:34 +00:00
/**
* [description]
*
* @name Phaser.Scenes.SceneManager#game
* @type {Phaser.Game}
* @since 3.0.0
*/
this.game = game;
2016-11-29 13:01:16 +00:00
2018-02-12 16:18:34 +00:00
/**
* An object that maps the keys to the scene so we can quickly get a scene from a key without iteration.
*
* @name Phaser.Scenes.SceneManager#keys
* @type {object}
* @since 3.0.0
*/
this.keys = {};
2016-11-29 13:01:16 +00:00
2018-02-12 16:18:34 +00:00
/**
* The array in which all of the scenes are kept.
*
* @name Phaser.Scenes.SceneManager#scenes
* @type {array}
* @since 3.0.0
*/
this.scenes = [];
2018-02-12 16:18:34 +00:00
/**
* Scenes pending to be added are stored in here until the manager has time to add it.
*
* @name Phaser.Scenes.SceneManager#_pending
* @type {array}
* @private
* @since 3.0.0
*/
this._pending = [];
2018-02-12 16:18:34 +00:00
/**
* An array of scenes waiting to be started once the game has booted.
*
* @name Phaser.Scenes.SceneManager#_start
* @type {array}
* @private
* @since 3.0.0
*/
this._start = [];
2018-02-12 16:18:34 +00:00
/**
* An operations queue, because we don't manipulate the scenes array during processing.
*
* @name Phaser.Scenes.SceneManager#_queue
* @type {array}
* @private
* @since 3.0.0
*/
this._queue = [];
2018-02-12 16:18:34 +00:00
/**
* The number of Scenes to process.
*
* @name Phaser.Scenes.SceneManager#_processing
* @type {integer}
* @private
* @since 3.0.0
*/
this._processing = 0;
if (sceneConfig)
2016-11-29 13:01:16 +00:00
{
if (!Array.isArray(sceneConfig))
{
sceneConfig = [ sceneConfig ];
}
for (var i = 0; i < sceneConfig.length; i++)
2016-11-29 13:01:16 +00:00
{
// The i === 0 part just autostarts the first Scene given (unless it says otherwise in its config)
this._pending.push({
key: 'default',
scene: sceneConfig[i],
autoStart: (i === 0),
2017-02-16 17:18:50 +00:00
data: {}
});
2016-11-29 13:01:16 +00:00
}
// Only need to wait for the boot event if we've scenes to actually boot
game.events.once('ready', this.bootQueue, this);
}
},
/**
* Internal first-time Scene boot handler.
*
* @method Phaser.Scenes.SceneManager#bootQueue
* @since 3.2.0
*/
bootQueue: function ()
{
var i;
var entry;
var key;
var sceneConfig;
for (i = 0; i < this._pending.length; i++)
{
entry = this._pending[i];
key = entry.key;
sceneConfig = entry.scene;
var newScene;
if (sceneConfig instanceof Scene)
{
newScene = this.createSceneFromInstance(key, sceneConfig);
}
else if (typeof sceneConfig === 'object')
{
sceneConfig.key = key;
newScene = this.createSceneFromObject(key, sceneConfig);
}
else if (typeof sceneConfig === 'function')
{
newScene = this.createSceneFromFunction(key, sceneConfig);
}
// Replace key in case the scene changed it
key = newScene.sys.settings.key;
this.keys[key] = newScene;
this.scenes.push(newScene);
if (entry.autoStart || newScene.sys.settings.active)
{
this._start.push(key);
}
}
// Clear the pending lists
this._pending.length = 0;
// _start might have been populated by the above
for (i = 0; i < this._start.length; i++)
{
entry = this._start[i];
this.start(entry);
}
this._start.length = 0;
},
/**
* [description]
*
* @method Phaser.Scenes.SceneManager#processQueue
* @since 3.0.0
*/
processQueue: function ()
{
var pendingLength = this._pending.length;
var queueLength = this._queue.length;
if (pendingLength === 0 && queueLength === 0)
{
return;
}
var i;
var entry;
if (pendingLength)
{
for (i = 0; i < pendingLength; i++)
{
entry = this._pending[i];
this.add(entry.key, entry.scene, entry.autoStart);
}
// _start might have been populated by this.add
for (i = 0; i < this._start.length; i++)
{
entry = this._start[i];
this.start(entry);
}
// Clear the pending lists
this._start.length = 0;
this._pending.length = 0;
return;
}
for (i = 0; i < this._queue.length; i++)
{
entry = this._queue[i];
this[entry.op](entry.keyA, entry.keyB);
}
this._queue.length = 0;
},
2016-11-29 13:01:16 +00:00
2018-01-16 16:29:58 +00:00
/**
* Adds a new Scene into the SceneManager.
* You must give each Scene a unique key by which you'll identify it.
*
* The `sceneConfig` can be:
*
* * A `Phaser.Scene` object, or an object that extends it.
* * A plain JavaScript object
* * A JavaScript ES6 Class that extends `Phaser.Scene`
* * A JavaScript ES5 prototype based Class
* * A JavaScript function
*
* If a function is given then a new Scene will be created by calling it.
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#add
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string} key - A unique key used to reference the Scene, i.e. `MainMenu` or `Level1`.
* @param {Phaser.Scene|object|function} sceneConfig - [description]
* @param {boolean} [autoStart=false] - If `true` the Scene will be started immediately after being added.
*
* @return {Phaser.Scene|null} [description]
2018-01-16 16:29:58 +00:00
*/
add: function (key, sceneConfig, autoStart)
{
if (autoStart === undefined) { autoStart = false; }
// if not booted, then put scene into a holding pattern
if (this._processing === 1 || !this.game.isBooted)
2018-01-16 16:29:58 +00:00
{
this._pending.push({
key: key,
scene: sceneConfig,
autoStart: autoStart,
data: {}
2018-01-16 16:29:58 +00:00
});
return null;
2018-01-16 16:29:58 +00:00
}
key = this.getKey(key, sceneConfig);
var newScene;
if (sceneConfig instanceof Scene)
{
newScene = this.createSceneFromInstance(key, sceneConfig);
}
else if (typeof sceneConfig === 'object')
{
sceneConfig.key = key;
newScene = this.createSceneFromObject(key, sceneConfig);
}
else if (typeof sceneConfig === 'function')
{
newScene = this.createSceneFromFunction(key, sceneConfig);
}
// Replace key in case the scene changed it
key = newScene.sys.settings.key;
this.keys[key] = newScene;
this.scenes.push(newScene);
if (autoStart || newScene.sys.settings.active)
{
if (this.game.isBooted)
{
this.start(key);
}
else
{
this._start.push(key);
}
}
return newScene;
},
/**
* Removes a Scene from the SceneManager.
*
* The Scene is removed from the local scenes array, it's key is cleared from the keys
* cache and Scene.Systems.destroy is then called on it.
*
* If the SceneManager is processing the Scenes when this method is called it wil
* queue the operation for the next update sequence.
*
* @method Phaser.Scenes.SceneManager#remove
* @since 3.2.0
*
* @param {string|Phaser.Scene} scene - The Scene to be removed.
*
* @return {Phaser.Scenes.SceneManager} This SceneManager.
*/
remove: function (key)
{
if (this._processing)
{
this._queue.push({ op: 'remove', keyA: key, keyB: null });
}
else
{
var sceneToRemove = this.getScene(key);
if (!sceneToRemove)
{
return this;
}
var index = this.scenes.indexOf(sceneToRemove);
var sceneKey = sceneToRemove.sys.settings.key;
if (index > -1)
{
this.keys[sceneKey] = undefined;
this.scenes.splice(index, 1);
if (this._start.indexOf(sceneKey) > -1)
{
index = this._start.indexOf(sceneKey);
this._start.splice(index, 1);
}
sceneToRemove.sys.destroy();
}
}
return this;
},
2018-01-16 16:29:58 +00:00
/**
* [description]
*
* @method Phaser.Scenes.SceneManager#bootScene
* @private
2018-02-12 16:18:34 +00:00
* @since 3.0.0
*
* @param {Phaser.Scene} scene - [description]
2018-01-16 16:29:58 +00:00
*/
bootScene: function (scene)
2018-01-16 16:29:58 +00:00
{
if (scene.init)
{
scene.init.call(scene, scene.sys.settings.data);
}
var loader;
2018-01-16 16:29:58 +00:00
if (scene.sys.load)
{
loader = scene.sys.load;
loader.reset();
}
if (loader && scene.preload)
2018-01-16 16:29:58 +00:00
{
scene.preload.call(scene);
2018-01-16 16:29:58 +00:00
// Is the loader empty?
if (loader.list.size === 0)
2018-01-16 16:29:58 +00:00
{
this.create(scene);
2018-01-16 16:29:58 +00:00
}
else
{
scene.sys.settings.status = CONST.LOADING;
// Start the loader going as we have something in the queue
loader.once('complete', this.loadComplete, this);
2018-01-16 16:29:58 +00:00
loader.start();
}
}
else
{
// No preload? Then there was nothing to load either
this.create(scene);
2018-01-16 16:29:58 +00:00
}
},
/**
* [description]
*
* @method Phaser.Scenes.SceneManager#loadComplete
* @private
2018-02-12 16:18:34 +00:00
* @since 3.0.0
*
2018-02-12 16:18:34 +00:00
* @param {object} loader - [description]
2018-01-16 16:29:58 +00:00
*/
loadComplete: function (loader)
2018-01-16 16:29:58 +00:00
{
var scene = loader.scene;
this.create(scene);
2018-01-16 16:29:58 +00:00
},
/**
* [description]
*
* @method Phaser.Scenes.SceneManager#payloadComplete
* @private
2018-02-12 16:18:34 +00:00
* @since 3.0.0
*
2018-02-12 16:18:34 +00:00
* @param {object} loader - [description]
2018-01-16 16:29:58 +00:00
*/
payloadComplete: function (loader)
2018-01-16 16:29:58 +00:00
{
this.bootScene(loader.scene);
},
2018-01-16 16:29:58 +00:00
/**
* [description]
*
* @method Phaser.Scenes.SceneManager#update
* @since 3.0.0
*
* @param {number} time - [description]
* @param {number} delta - [description]
*/
update: function (time, delta)
{
this.processQueue();
2018-01-16 16:29:58 +00:00
this._processing = 1;
2018-01-16 16:29:58 +00:00
// Loop through the active scenes in reverse order
for (var i = this.scenes.length - 1; i >= 0; i--)
2018-01-16 16:29:58 +00:00
{
var sys = this.scenes[i].sys;
if (sys.settings.status === CONST.RUNNING)
2018-01-16 16:29:58 +00:00
{
sys.step(time, delta);
2018-01-16 16:29:58 +00:00
}
}
},
/**
* [description]
*
* @method Phaser.Scenes.SceneManager#resize
* @since 3.2.0
*
* @param {number} width - The new width of the game.
* @param {number} height - The new height of the game.
*/
resize: function (width, height)
{
// Loop through the scenes in forward order
for (var i = 0; i < this.scenes.length; i++)
{
var sys = this.scenes[i].sys;
sys.resize(width, height);
}
},
/**
* [description]
*
* @method Phaser.Scenes.SceneManager#render
* @since 3.0.0
*
* @param {any} renderer - [description]
*/
render: function (renderer)
{
// Loop through the scenes in forward order
for (var i = 0; i < this.scenes.length; i++)
{
var sys = this.scenes[i].sys;
2018-01-16 16:29:58 +00:00
if (sys.settings.visible && (sys.settings.status === CONST.RUNNING || sys.settings.status === CONST.PAUSED))
2018-01-16 16:29:58 +00:00
{
sys.render(renderer);
2018-01-16 16:29:58 +00:00
}
}
this._processing = 0;
},
/**
* [description]
*
* @method Phaser.Scenes.SceneManager#create
* @private
2018-02-12 16:18:34 +00:00
* @since 3.0.0
*
* @param {Phaser.Scene} scene - [description]
*/
create: function (scene)
{
if (scene.create)
{
scene.sys.settings.status = CONST.CREATING;
scene.create.call(scene, scene.sys.settings.data);
}
scene.sys.settings.status = CONST.RUNNING;
2018-01-16 16:29:58 +00:00
},
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#createSceneFromFunction
* @private
2018-02-12 16:18:34 +00:00
* @since 3.0.0
*
2018-01-16 16:29:58 +00:00
* @param {string} key - [description]
* @param {function} scene - [description]
*
* @return {Phaser.Scene} [description]
*/
createSceneFromFunction: function (key, scene)
{
var newScene = new scene();
if (newScene instanceof Scene)
{
var configKey = newScene.sys.settings.key;
if (configKey !== '')
{
key = configKey;
}
if (this.keys.hasOwnProperty(key))
{
throw new Error('Cannot add a Scene with duplicate key: ' + key);
}
return this.createSceneFromInstance(key, newScene);
}
else
{
newScene.sys = new Systems(newScene);
newScene.sys.settings.key = key;
newScene.sys.init(this.game);
if (!newScene.update)
{
newScene.update = NOOP;
}
return newScene;
}
},
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#createSceneFromInstance
* @private
2018-02-12 16:18:34 +00:00
* @since 3.0.0
*
2018-01-16 16:29:58 +00:00
* @param {string} key - [description]
* @param {Phaser.Scene} newScene - [description]
*
* @return {Phaser.Scene} [description]
*/
createSceneFromInstance: function (key, newScene)
{
var configKey = newScene.sys.settings.key;
if (configKey !== '')
{
key = configKey;
}
else
{
newScene.sys.settings.key = key;
}
newScene.sys.init(this.game);
return newScene;
},
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#createSceneFromObject
* @private
2018-02-12 16:18:34 +00:00
* @since 3.0.0
*
2018-01-16 16:29:58 +00:00
* @param {string} key - [description]
* @param {object} sceneConfig - [description]
*
* @return {Phaser.Scene} [description]
*/
createSceneFromObject: function (key, sceneConfig)
{
var newScene = new Scene(sceneConfig);
var configKey = newScene.sys.settings.key;
if (configKey !== '')
{
key = configKey;
}
else
{
newScene.sys.settings.key = key;
}
newScene.sys.init(this.game);
// Extract callbacks
2018-01-16 16:29:58 +00:00
var defaults = [ 'init', 'preload', 'create', 'update', 'render', 'shutdown', 'destroy' ];
2018-01-16 16:29:58 +00:00
for (var i = 0; i < defaults.length; i++)
{
var sceneCallback = GetValue(sceneConfig, defaults[i], null);
// Must always have an update function, no matter what (the rest are optional)
if (defaults[i] === 'update' && !sceneCallback)
{
sceneCallback = NOOP;
}
if (sceneCallback)
{
newScene[defaults[i]] = sceneCallback;
}
2018-01-16 16:29:58 +00:00
}
// Now let's move across any other functions or properties that may exist in the extend object:
2018-01-16 16:29:58 +00:00
/*
scene: {
preload: preload,
create: create,
extend: {
hello: 1,
test: 'atari',
addImage: addImage
}
}
*/
if (sceneConfig.hasOwnProperty('extend'))
{
for (var propertyKey in sceneConfig.extend)
{
newScene[propertyKey] = sceneConfig.extend[propertyKey];
2018-01-16 16:29:58 +00:00
}
}
return newScene;
},
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#getKey
* @private
2018-02-12 16:18:34 +00:00
* @since 3.0.0
2018-01-16 16:29:58 +00:00
*
* @param {string} key - [description]
* @param {Phaser.Scene|object|function} sceneConfig - [description]
*
* @return {string} [description]
*/
getKey: function (key, sceneConfig)
{
if (!key) { key = 'default'; }
if (typeof sceneConfig === 'function')
{
return key;
}
else if (sceneConfig instanceof Scene)
{
key = sceneConfig.sys.settings.key;
}
else if (typeof sceneConfig === 'object' && sceneConfig.hasOwnProperty('key'))
{
key = sceneConfig.key;
}
// By this point it's either 'default' or extracted from the Scene
if (this.keys.hasOwnProperty(key))
{
throw new Error('Cannot add a Scene with duplicate key: ' + key);
}
else
{
return key;
}
},
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#getScene
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string} key - [description]
*
* @return {Phaser.Scene|null} [description]
2018-01-16 16:29:58 +00:00
*/
getScene: function (key)
{
if (typeof key === 'string')
{
if (this.keys[key])
{
return this.keys[key];
}
}
else
{
for (var i = 0; i < this.scenes.length; i++)
{
if (key === this.scenes[i])
{
return key;
}
}
}
2018-01-16 16:29:58 +00:00
return null;
},
2018-01-16 16:29:58 +00:00
/**
* [description]
*
* @method Phaser.Scenes.SceneManager#isActive
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string} key - [description]
2018-01-16 16:29:58 +00:00
*
* @return {boolean} [description]
2018-01-16 16:29:58 +00:00
*/
isActive: function (key)
2018-01-16 16:29:58 +00:00
{
var scene = this.getScene(key);
if (scene)
2018-01-16 16:29:58 +00:00
{
return scene.sys.isActive();
2018-01-16 16:29:58 +00:00
}
return null;
2018-01-16 16:29:58 +00:00
},
/**
* [description]
*
* @method Phaser.Scenes.SceneManager#isVisible
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string} key - [description]
2018-01-16 16:29:58 +00:00
*
* @return {boolean} [description]
2018-01-16 16:29:58 +00:00
*/
isVisible: function (key)
2018-01-16 16:29:58 +00:00
{
var scene = this.getScene(key);
2018-01-16 16:29:58 +00:00
if (scene)
{
return scene.sys.isVisible();
}
2018-01-16 16:29:58 +00:00
return null;
2018-01-16 16:29:58 +00:00
},
/**
* [description]
*
* @method Phaser.Scenes.SceneManager#isSleeping
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string} key - [description]
*
* @return {boolean} [description]
*/
isSleeping: function (key)
2018-01-16 16:29:58 +00:00
{
var scene = this.getScene(key);
if (scene)
{
return scene.sys.isSleeping();
}
2018-01-16 16:29:58 +00:00
return null;
2018-01-16 16:29:58 +00:00
},
/**
* [description]
*
* @method Phaser.Scenes.SceneManager#pause
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string} key - [description]
*
* @return {Phaser.Scenes.SceneManager} [description]
2018-01-16 16:29:58 +00:00
*/
pause: function (key)
2018-01-16 16:29:58 +00:00
{
var scene = this.getScene(key);
2018-01-16 16:29:58 +00:00
if (scene)
2018-01-16 16:29:58 +00:00
{
scene.sys.pause();
2018-01-16 16:29:58 +00:00
}
return this;
2018-01-16 16:29:58 +00:00
},
/**
* [description]
*
* @method Phaser.Scenes.SceneManager#resume
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string} key - [description]
*
* @return {Phaser.Scenes.SceneManager} [description]
2018-01-16 16:29:58 +00:00
*/
resume: function (key)
2018-01-16 16:29:58 +00:00
{
var scene = this.getScene(key);
2018-01-16 16:29:58 +00:00
if (scene)
{
scene.sys.resume();
}
2018-01-16 16:29:58 +00:00
return this;
},
2018-01-16 16:29:58 +00:00
/**
* [description]
*
* @method Phaser.Scenes.SceneManager#sleep
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string} key - [description]
*
* @return {Phaser.Scenes.SceneManager} [description]
2018-01-16 16:29:58 +00:00
*/
sleep: function (key)
2018-01-16 16:29:58 +00:00
{
var scene = this.getScene(key);
2018-01-16 16:29:58 +00:00
if (scene)
2018-01-16 16:29:58 +00:00
{
scene.sys.sleep();
2018-01-16 16:29:58 +00:00
}
return this;
},
2018-01-16 16:29:58 +00:00
/**
* [description]
*
* @method Phaser.Scenes.SceneManager#wake
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string} key - [description]
*
* @return {Phaser.Scenes.SceneManager} [description]
2018-01-16 16:29:58 +00:00
*/
wake: function (key)
2018-01-16 16:29:58 +00:00
{
var scene = this.getScene(key);
2018-01-16 16:29:58 +00:00
if (scene)
2018-01-16 16:29:58 +00:00
{
scene.sys.wake();
2018-01-16 16:29:58 +00:00
}
return this;
2018-01-16 16:29:58 +00:00
},
/**
* [description]
*
* @method Phaser.Scenes.SceneManager#start
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string} key - [description]
* @param {object} [data] - [description]
*
* @return {Phaser.Scenes.SceneManager} [description]
2018-01-16 16:29:58 +00:00
*/
start: function (key, data)
2018-01-16 16:29:58 +00:00
{
if (data === undefined) { data = {}; }
2018-01-16 16:29:58 +00:00
// if not booted, then put scene into a holding pattern
if (!this.game.isBooted)
2018-01-16 16:29:58 +00:00
{
for (var i = 0; i < this._pending.length; i++)
{
var entry = this._pending[i];
if (entry.key === key)
{
entry.autoStart = true;
entry.data = data;
}
}
return this;
2018-01-16 16:29:58 +00:00
}
var scene = this.getScene(key);
if (scene)
{
scene.sys.start(data);
var loader;
if (scene.sys.load)
{
loader = scene.sys.load;
}
// Files payload?
if (loader && Array.isArray(scene.sys.settings.files))
{
loader.reset();
if (loader.loadArray(scene.sys.settings.files))
{
scene.sys.settings.status = CONST.LOADING;
loader.once('complete', this.payloadComplete, this);
loader.start();
return this;
}
}
this.bootScene(scene);
}
return this;
2018-01-16 16:29:58 +00:00
},
/**
* [description]
*
* @method Phaser.Scenes.SceneManager#stop
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string} key - [description]
*
* @return {Phaser.Scenes.SceneManager} [description]
2018-01-16 16:29:58 +00:00
*/
stop: function (key)
2018-01-16 16:29:58 +00:00
{
var scene = this.getScene(key);
if (scene)
{
scene.sys.shutdown();
}
return this;
2018-01-16 16:29:58 +00:00
},
/**
* [description]
*
* @method Phaser.Scenes.SceneManager#switch
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string} from - [description]
* @param {string} to - [description]
*
* @return {Phaser.Scenes.SceneManager} [description]
2018-01-16 16:29:58 +00:00
*/
switch: function (from, to)
2018-01-16 16:29:58 +00:00
{
var sceneA = this.getScene(from);
var sceneB = this.getScene(to);
2018-01-16 16:29:58 +00:00
if (sceneA && sceneB && sceneA !== sceneB)
2018-01-16 16:29:58 +00:00
{
this.sleep(from);
if (this.isSleeping(to))
{
this.wake(to);
}
else
{
this.start(to);
}
2018-01-16 16:29:58 +00:00
}
return this;
},
2018-01-16 16:29:58 +00:00
/**
* [description]
*
* @method Phaser.Scenes.SceneManager#getAt
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {integer} index - [description]
*
* @return {Phaser.Scene|undefined} [description]
2018-01-16 16:29:58 +00:00
*/
getAt: function (index)
2018-01-16 16:29:58 +00:00
{
return this.scenes[index];
2018-01-16 16:29:58 +00:00
},
/**
* [description]
*
* @method Phaser.Scenes.SceneManager#getIndex
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string|Phaser.Scene} key - [description]
*
* @return {integer} [description]
2018-01-16 16:29:58 +00:00
*/
getIndex: function (key)
2018-01-16 16:29:58 +00:00
{
var scene = this.getScene(key);
2018-01-16 16:29:58 +00:00
return this.scenes.indexOf(scene);
2018-01-16 16:29:58 +00:00
},
/**
* [description]
*
* @method Phaser.Scenes.SceneManager#bringToTop
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string|Phaser.Scene} key - [description]
2018-01-16 16:29:58 +00:00
*
* @return {Phaser.Scenes.SceneManager} [description]
2018-01-16 16:29:58 +00:00
*/
bringToTop: function (key)
2018-01-16 16:29:58 +00:00
{
if (this._processing)
2018-01-16 16:29:58 +00:00
{
2018-02-12 16:18:34 +00:00
this._queue.push({ op: 'bringToTop', keyA: key, keyB: null });
2018-01-16 16:29:58 +00:00
}
else
{
var index = this.getIndex(key);
if (index !== -1 && index < this.scenes.length)
{
var scene = this.getScene(key);
this.scenes.splice(index, 1);
this.scenes.push(scene);
}
2018-01-16 16:29:58 +00:00
}
return this;
2018-01-16 16:29:58 +00:00
},
/**
* [description]
*
* @method Phaser.Scenes.SceneManager#sendToBack
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string|Phaser.Scene} key - [description]
*
* @return {Phaser.Scenes.SceneManager} [description]
2018-01-16 16:29:58 +00:00
*/
sendToBack: function (key)
2018-01-16 16:29:58 +00:00
{
if (this._processing)
2018-01-16 16:29:58 +00:00
{
2018-02-12 16:18:34 +00:00
this._queue.push({ op: 'sendToBack', keyA: key, keyB: null });
2018-01-16 16:29:58 +00:00
}
else
2018-01-16 16:29:58 +00:00
{
var index = this.getIndex(key);
if (index !== -1 && index > 0)
{
var scene = this.getScene(key);
2018-01-16 16:29:58 +00:00
this.scenes.splice(index, 1);
this.scenes.unshift(scene);
2018-01-16 16:29:58 +00:00
}
}
return this;
2018-01-16 16:29:58 +00:00
},
/**
* [description]
*
* @method Phaser.Scenes.SceneManager#moveDown
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string|Phaser.Scene} key - [description]
*
* @return {Phaser.Scenes.SceneManager} [description]
2018-01-16 16:29:58 +00:00
*/
moveDown: function (key)
2018-01-16 16:29:58 +00:00
{
if (this._processing)
2018-01-16 16:29:58 +00:00
{
2018-02-12 16:18:34 +00:00
this._queue.push({ op: 'moveDown', keyA: key, keyB: null });
}
else
{
var indexA = this.getIndex(key);
2018-01-16 16:29:58 +00:00
if (indexA > 0)
2018-01-16 16:29:58 +00:00
{
var indexB = indexA - 1;
var sceneA = this.getScene(key);
var sceneB = this.getAt(indexB);
2018-01-16 16:29:58 +00:00
this.scenes[indexA] = sceneB;
this.scenes[indexB] = sceneA;
2018-01-16 16:29:58 +00:00
}
}
return this;
2018-01-16 16:29:58 +00:00
},
/**
* [description]
*
* @method Phaser.Scenes.SceneManager#moveUp
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string|Phaser.Scene} key - [description]
*
* @return {Phaser.Scenes.SceneManager} [description]
2018-01-16 16:29:58 +00:00
*/
moveUp: function (key)
2018-01-16 16:29:58 +00:00
{
if (this._processing)
2018-01-16 16:29:58 +00:00
{
2018-02-12 16:18:34 +00:00
this._queue.push({ op: 'moveUp', keyA: key, keyB: null });
2018-01-16 16:29:58 +00:00
}
else
{
var indexA = this.getIndex(key);
if (indexA < this.scenes.length - 1)
{
var indexB = indexA + 1;
var sceneA = this.getScene(key);
var sceneB = this.getAt(indexB);
this.scenes[indexA] = sceneB;
this.scenes[indexB] = sceneA;
}
2018-01-16 16:29:58 +00:00
}
return this;
},
2018-01-16 16:29:58 +00:00
/**
* Moves a Scene so it is immediately above another Scene in the Scenes list.
* This means it will render over the top of the other Scene.
*
* @method Phaser.Scenes.SceneManager#moveAbove
* @since 3.2.0
*
* @param {string|Phaser.Scene} keyA - The Scene that Scene B will be moved above.
* @param {string|Phaser.Scene} keyB - The Scene to be moved.
*
* @return {Phaser.Scenes.SceneManager} [description]
*/
moveAbove: function (keyA, keyB)
{
if (keyA === keyB)
{
return this;
}
if (this._processing)
{
this._queue.push({ op: 'moveAbove', keyA: keyA, keyB: keyB });
}
else
{
var indexA = this.getIndex(keyA);
var indexB = this.getIndex(keyB);
if (indexA > indexB && indexA !== -1 && indexB !== -1)
{
var tempScene = this.getAt(indexB);
// Remove
this.scenes.splice(indexB, 1);
// Add in new location
this.scenes.splice(indexA, 0, tempScene);
}
}
return this;
},
/**
* Moves a Scene so it is immediately below another Scene in the Scenes list.
* This means it will render behind the other Scene.
*
* @method Phaser.Scenes.SceneManager#moveBelow
* @since 3.2.0
*
* @param {string|Phaser.Scene} keyA - The Scene that Scene B will be moved above.
* @param {string|Phaser.Scene} keyB - The Scene to be moved.
*
* @return {Phaser.Scenes.SceneManager} [description]
*/
moveBelow: function (keyA, keyB)
{
if (keyA === keyB)
{
return this;
}
if (this._processing)
{
this._queue.push({ op: 'moveBelow', keyA: keyA, keyB: keyB });
}
else
{
var indexA = this.getIndex(keyA);
var indexB = this.getIndex(keyB);
if (indexA < indexB && indexA !== -1 && indexB !== -1)
{
var tempScene = this.getAt(indexB);
// Remove
this.scenes.splice(indexB, 1);
// Add in new location
this.scenes.splice(indexA, 0, tempScene);
}
}
return this;
},
queueOp: function (op, keyA, keyB)
{
2018-02-12 16:18:34 +00:00
this._queue.push({ op: op, keyA: keyA, keyB: keyB });
return this;
},
2018-01-16 16:29:58 +00:00
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#swapPosition
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string|Phaser.Scene} keyA - [description]
* @param {string|Phaser.Scene} keyB - [description]
*
* @return {Phaser.Scenes.SceneManager} [description]
2018-01-16 16:29:58 +00:00
*/
swapPosition: function (keyA, keyB)
2018-01-16 16:29:58 +00:00
{
if (keyA === keyB)
2018-01-16 16:29:58 +00:00
{
return this;
2018-01-16 16:29:58 +00:00
}
if (this._processing)
{
2018-02-12 16:18:34 +00:00
this._queue.push({ op: 'swapPosition', keyA: keyA, keyB: keyB });
}
else
2018-01-16 16:29:58 +00:00
{
var indexA = this.getIndex(keyA);
var indexB = this.getIndex(keyB);
2018-01-16 16:29:58 +00:00
if (indexA !== indexB && indexA !== -1 && indexB !== -1)
{
var tempScene = this.getAt(indexA);
this.scenes[indexA] = this.scenes[indexB];
this.scenes[indexB] = tempScene;
}
2018-01-16 16:29:58 +00:00
}
return this;
2018-01-16 16:29:58 +00:00
},
dump: function ()
{
2018-03-05 01:40:11 +00:00
var out = [];
var map = [ 'pending', 'init', 'start', 'loading', 'creating', 'running', 'paused', 'sleeping', 'shutdown', 'destroyed' ];
for (var i = 0; i < this.scenes.length; i++)
{
var sys = this.scenes[i].sys;
var key = (sys.settings.visible && (sys.settings.status === CONST.RUNNING || sys.settings.status === CONST.PAUSED)) ? '[*] ' : '[-] ';
key += sys.settings.key + ' (' + map[sys.settings.status] + ')';
out.push(key);
}
console.log(out.join('\n'));
},
2018-01-16 16:29:58 +00:00
/**
* [description]
*
* @method Phaser.Scenes.SceneManager#destroy
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*/
destroy: function ()
2018-01-16 16:29:58 +00:00
{
for (var i = this.scenes.length - 1; i >= 0; i--)
{
var sys = this.scenes[i].sys;
sys.destroy();
}
this.scenes = [];
this._pending = [];
this._start = [];
this._queue = [];
this.game = null;
2018-01-16 16:29:58 +00:00
}
2016-11-29 13:01:16 +00:00
});
2016-11-29 13:01:16 +00:00
2018-01-16 16:33:23 +00:00
module.exports = SceneManager;