phaser/src/scene/SceneManager.js

1084 lines
26 KiB
JavaScript
Raw Normal View History

2018-01-16 16:33:23 +00:00
var CanvasInterpolation = require('../display/canvas/CanvasInterpolation');
var CanvasPool = require('../display/canvas/CanvasPool');
var Class = require('../utils/Class');
var CONST = require('../const');
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-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)
{
this.game = game;
2016-11-29 13:01:16 +00:00
// Everything kept in here
this.keys = {};
this.scenes = [];
2016-11-29 13:01:16 +00:00
// Only active scenes are kept in here. They are moved here when started, and moved out when not.
// All scenes are stored in the scenes array, regardless of being active or not.
this.active = [];
// A scene pending to be added to the Scene Manager is stored in here until the manager has time to add it.
this._pending = [];
// An array of scenes waiting to be started once the game has booted
this._start = [];
if (sceneConfig)
2016-11-29 13:01:16 +00:00
{
if (Array.isArray(sceneConfig))
{
for (var i = 0; i < sceneConfig.length; i++)
{
// The i === 0 part just starts the first Scene given
this._pending.push({
index: i,
key: 'default',
scene: sceneConfig[i],
autoStart: (i === 0),
data: {}
});
}
}
else
2016-11-29 13:01:16 +00:00
{
this._pending.push({
index: 0,
key: 'default',
scene: sceneConfig,
autoStart: true,
2017-02-16 17:18:50 +00:00
data: {}
});
2016-11-29 13:01:16 +00:00
}
}
game.events.once('boot', this.boot, this);
},
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} [description]
*/
add: function (key, sceneConfig, autoStart)
{
if (autoStart === undefined) { autoStart = false; }
// if not booted, then put scene into a holding pattern
if (!this.game.isBooted)
{
this._pending.push({
index: this._pending.length,
key: key,
scene: sceneConfig,
autoStart: autoStart
});
return;
}
// var ok = key;
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;
},
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#boot
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*/
boot: function ()
{
var i;
var entry;
for (i = 0; i < this._pending.length; i++)
{
entry = this._pending[i];
this.add(entry.key, entry.scene, entry.autoStart);
}
for (i = 0; i < this._start.length; i++)
{
entry = this._start[i];
this.start(entry);
}
// Clear the pending lists
this._start = [];
this._pending = [];
},
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#bootScene
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {Phaser.Scene} scene - [description]
*/
bootScene: function (scene)
{
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(this.game);
// Is the loader empty?
if (loader.list.size === 0)
{
this.create(scene);
}
else
{
// Start the loader going as we have something in the queue
loader.once('complete', this.loadComplete, this);
loader.start();
}
}
else
{
// No preload? Then there was nothing to load either
this.create(scene);
}
},
// If the arguments are strings they are assumed to be keys, otherwise they are Scene objects
// You can only swap the positions of Active (rendering / updating) Scenes. If a Scene is not active it cannot be moved.
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#bringToTop
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string|Phaser.Scene} scene - [description]
*/
bringToTop: function (scene)
{
var index = (typeof scene === 'string') ? this.getActiveSceneIndexByKey(scene) : this.getActiveSceneIndex(scene);
if (index < this.active.length)
{
var i = 0;
var entry = this.active.splice(index, 1);
for (i = 0; i < this.active.length; i++)
{
this.active[i].index = i;
}
this.active.push({ index: i, scene: entry[0].scene });
}
},
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#create
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {Phaser.Scene} scene - [description]
*/
create: function (scene)
{
// Insert at the correct index, or it just all goes wrong :)
var i = this.getSceneIndex(scene);
this.active.push({ index: i, scene: scene });
// Sort the 'active' array based on the index property
this.active.sort(this.sortScenes);
if (scene.create)
{
scene.create.call(scene, scene.sys.settings.data);
}
},
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#createSceneDisplay
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {Phaser.Scene} scene - [description]
*/
createSceneDisplay: function (scene)
{
var settings = scene.sys.settings;
var width = settings.width;
var height = settings.height;
var config = this.game.config;
if (config.renderType === CONST.CANVAS)
{
if (settings.renderToTexture)
{
scene.sys.canvas = CanvasPool.create(scene, width, height);
scene.sys.context = scene.sys.canvas.getContext('2d');
}
else
{
scene.sys.canvas = this.game.canvas;
scene.sys.context = this.game.context;
}
// Pixel Art mode?
if (config.pixelArt)
{
CanvasInterpolation.setCrisp(scene.sys.canvas);
}
}
},
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#createSceneFromFunction
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @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);
this.createSceneDisplay(newScene);
// Default required functions
/*
2018-01-16 16:29:58 +00:00
if (!newScene.init)
{
newScene.init = NOOP;
}
if (!newScene.preload)
{
newScene.preload = NOOP;
}
if (!newScene.create)
{
newScene.create = NOOP;
}
if (!newScene.shutdown)
{
newScene.shutdown = NOOP;
}
*/
2018-01-16 16:29:58 +00:00
if (!newScene.update)
{
newScene.update = NOOP;
}
/*
2018-01-16 16:29:58 +00:00
if (!newScene.render)
{
newScene.render = NOOP;
}
*/
2018-01-16 16:29:58 +00:00
return newScene;
}
},
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#createSceneFromInstance
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @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);
this.createSceneDisplay(newScene);
return newScene;
},
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#createSceneFromObject
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @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);
this.createSceneDisplay(newScene);
// Extract callbacks or set NOOP
var defaults = [ 'init', 'preload', 'create', 'update', 'render' ];
2018-01-16 16:29:58 +00:00
for (var i = 0; i < defaults.length; i++)
{
var sceneCallback = GetValue(sceneConfig, defaults[i], null);
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
/*
scene: {
preload: preload,
create: create,
extend: {
hello: 1,
test: 'atari',
addImage: addImage
}
}
*/
if (sceneConfig.hasOwnProperty('extend'))
{
for (var propertyKey in sceneConfig.extend)
{
if (defaults.indexOf(propertyKey) === -1)
{
newScene[propertyKey] = sceneConfig.extend[propertyKey];
}
}
}
return newScene;
},
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#getActiveScene
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string} key - [description]
*
* @return {Phaser.Scene} [description]
*/
getActiveScene: function (key)
{
var scene = this.getScene(key);
for (var i = 0; i < this.active.length; i++)
{
if (this.active[i].scene === scene)
{
return this.active[i];
}
}
},
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#getActiveSceneIndex
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {Phaser.Scene} scene - [description]
*
* @return {integer} [description]
*/
getActiveSceneIndex: function (scene)
{
for (var i = 0; i < this.active.length; i++)
{
if (this.active[i].scene === scene)
{
return this.active[i].index;
}
}
return -1;
},
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#getActiveSceneIndexByKey
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string} key - [description]
*
* @return {integer} [description]
*/
getActiveSceneIndexByKey: function (key)
{
var scene = this.keys[key];
for (var i = 0; i < this.active.length; i++)
{
if (this.active[i].scene === scene)
{
return this.active[i].index;
}
}
return -1;
},
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#getKey
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @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} [description]
*/
getScene: function (key)
{
return this.keys[key];
},
// Gets the Active scene at the given position
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#getSceneAt
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {integer} index - [description]
*
* @return {Phaser.Scene} [description]
*/
getSceneAt: function (index)
{
if (this.active[index])
{
return this.active[index].scene;
}
},
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#getSceneIndex
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {Phaser.Scene} scene - [description]
*
* @return {integer} [description]
*/
getSceneIndex: function (scene)
{
return this.scenes.indexOf(scene);
},
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#getSceneIndexByKey
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string} key - [description]
*
* @return {integer} [description]
*/
getSceneIndexByKey: function (key)
{
var scene = this.keys[key];
return this.scenes.indexOf(scene);
},
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#isActive
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string} key - [description]
*
* @return {boolean} [description]
*/
isActive: function (key)
{
var entry = this.getActiveScene(key);
return (entry && entry.scene.sys.settings.active);
},
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @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)
{
var entry = this.getActiveScene(key);
if (entry)
{
return (!entry.scene.sys.settings.active && !entry.scene.sys.settings.visible);
}
return false;
},
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#loadComplete
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {object} event - [description]
*/
loadComplete: function (loader)
{
var scene = loader.scene;
this.create(scene);
},
// If the arguments are strings they are assumed to be keys, otherwise they are Scene objects
// You can only swap the positions of Active (rendering / updating) Scenes. If a Scene is not active it cannot be moved.
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#moveDown
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string|Phaser.Scene} scene - [description]
*/
moveDown: function (scene)
{
var index = (typeof scene === 'string') ? this.getActiveSceneIndexByKey(scene) : this.getActiveSceneIndex(scene);
if (index > 0)
{
var sceneB = this.getSceneAt(index - 1);
if (sceneB)
{
this.swapPosition(scene, sceneB);
}
}
},
// If the arguments are strings they are assumed to be keys, otherwise they are Scene objects
// You can only swap the positions of Active (rendering / updating) Scenes. If a Scene is not active it cannot be moved.
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#moveUp
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string|Phaser.Scene} scene - [description]
*/
moveUp: function (scene)
{
var index = (typeof scene === 'string') ? this.getActiveSceneIndexByKey(scene) : this.getActiveSceneIndex(scene);
if (index !== -1 && index < this.active.length - 1)
{
var sceneB = this.getSceneAt(index + 1);
if (sceneB)
{
this.swapPosition(scene, sceneB);
}
}
},
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#pause
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string} key - [description]
*/
pause: function (key)
{
var entry = this.getActiveScene(key);
if (entry)
{
entry.scene.sys.pause();
}
},
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#payloadComplete
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {object} event - [description]
*/
payloadComplete: function (event)
{
var scene = event.loader.scene;
this.bootScene(scene);
},
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#resume
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string} key - [description]
*/
resume: function (key)
{
var entry = this.getActiveScene(key);
if (entry)
{
entry.scene.sys.resume();
}
},
// If the arguments are strings they are assumed to be keys, otherwise they are Scene objects
// You can only swap the positions of Active (rendering / updating) Scenes. If a Scene is not active it cannot be moved.
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#sendToBack
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string|Phaser.Scene} scene - [description]
*/
sendToBack: function (scene)
{
var index = (typeof scene === 'string') ? this.getActiveSceneIndexByKey(scene) : this.getActiveSceneIndex(scene);
if (index > 0)
{
var entry = this.active.splice(index, 1);
this.active.unshift({ index: 0, scene: entry[0].scene });
for (var i = 0; i < this.active.length; i++)
{
this.active[i].index = i;
}
}
},
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#sleep
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string} key - [description]
*/
sleep: function (key)
{
var entry = this.getActiveScene(key);
if (entry)
{
entry.scene.sys.sleep();
}
},
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#sortScenes
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {object} sceneA - [description]
* @param {object} sceneB - [description]
*
* @return {integer} [description]
*/
sortScenes: function (sceneA, sceneB)
{
// Sort descending
if (sceneA.index < sceneB.index)
{
return -1;
}
else if (sceneA.index > sceneB.index)
{
return 1;
}
else
{
return 0;
}
},
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @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]
*/
start: function (key, data)
{
if (data === undefined) { data = {}; }
// if not booted, then put scene into a holding pattern
if (!this.game.isBooted)
{
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;
}
var scene = this.getScene(key);
if (scene)
{
// Already started? Nothing more to do here ...
if (this.isActive(key))
{
return;
}
scene.sys.start(data);
var loader;
if (scene.sys.load)
{
loader = scene.sys.load;
}
2018-01-16 16:29:58 +00:00
// Files payload?
if (loader && Array.isArray(scene.sys.settings.files))
{
loader.reset();
if (loader.loadArray(scene.sys.settings.files))
{
loader.once('complete', this.payloadComplete, this);
loader.start();
}
else
{
this.bootScene(scene);
}
}
else
{
this.bootScene(scene);
}
}
},
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#stop
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string} key - [description]
*/
stop: function (key)
{
var entry = this.getActiveScene(key);
if (entry)
{
entry.scene.sys.shutdown();
// Remove from the active list
var index = this.active.indexOf(entry);
if (index !== -1)
{
this.active.splice(index, 1);
this.active.sort(this.sortScenes);
}
}
},
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#swap
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string} from - [description]
* @param {string} to - [description]
*/
swap: function (from, to)
{
this.sleep(from);
if (this.isSleeping(to))
{
this.wake(to);
}
else
{
this.start(to);
}
},
// If the arguments are strings they are assumed to be keys, otherwise they are Scene objects
// You can only swap the positions of Active (rendering / updating) Scenes. If a Scene is not active it cannot be moved.
/**
* [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} scene1 - [description]
* @param {string|Phaser.Scene} scene2 - [description]
*/
swapPosition: function (scene1, scene2)
{
if (scene1 === scene2)
{
return;
}
var index1 = (typeof scene1 === 'string') ? this.getActiveSceneIndexByKey(scene1) : this.getActiveSceneIndex(scene1);
var index2 = (typeof scene2 === 'string') ? this.getActiveSceneIndexByKey(scene2) : this.getActiveSceneIndex(scene2);
if (index1 !== -1 && index2 !== -1 && index1 !== index2)
{
this.active[index1].index = index2;
this.active[index2].index = index1;
this.active.sort(this.sortScenes);
}
},
/**
* [description]
*
2018-01-16 16:33:23 +00:00
* @method Phaser.Scenes.SceneManager#wake
2018-01-16 16:29:58 +00:00
* @since 3.0.0
*
* @param {string} key - [description]
*/
wake: function (key)
{
var entry = this.getActiveScene(key);
if (entry)
{
entry.scene.sys.wake();
}
}
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;