phaser/src/scene/Systems.js

669 lines
18 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 19:49:13 +00:00
var Class = require('../utils/Class');
var CONST = require('./const');
var DefaultPlugins = require('../plugins/DefaultPlugins');
var GetPhysicsPlugins = require('./GetPhysicsPlugins');
var GetScenePlugins = require('./GetScenePlugins');
var NOOP = require('../utils/NOOP');
var Settings = require('./Settings');
2018-02-12 15:18:31 +00:00
/**
* @classdesc
* The Scene Systems class.
*
* This class is available from within a Scene under the property `sys`.
* It is responsible for managing all of the plugins a Scene has running, including the display list, and
* handling the update step and renderer. It also contains references to global systems belonging to Game.
*
* @class Systems
* @memberOf Phaser.Scenes
* @constructor
* @since 3.0.0
*
* @param {Phaser.Scene} scene - The Scene that owns this Systems instance.
2018-04-18 11:13:49 +00:00
* @param {(string|Phaser.Scenes.Settings.Config)} config - Scene specific configuration settings.
2018-02-12 15:18:31 +00:00
*/
var Systems = new Class({
initialize:
function Systems (scene, config)
{
2018-02-12 15:18:31 +00:00
/**
* [description]
*
* @name Phaser.Scenes.Systems#scene
* @type {Phaser.Scene}
* @since 3.0.0
*/
this.scene = scene;
2018-02-12 15:18:31 +00:00
/**
* [description]
*
* @name Phaser.Scenes.Systems#game
* @type {Phaser.Game}
* @since 3.0.0
*/
this.game;
2018-02-12 15:18:31 +00:00
/**
* [description]
*
* @name Phaser.Scenes.Systems#config
2018-04-18 11:13:49 +00:00
* @type {(string|Phaser.Scenes.Settings.Config)}
2018-02-12 15:18:31 +00:00
* @since 3.0.0
*/
this.config = config;
2018-02-12 15:18:31 +00:00
/**
* [description]
*
* @name Phaser.Scenes.Systems#settings
2018-04-18 11:13:49 +00:00
* @type {Phaser.Scenes.Settings.Object}
2018-02-12 15:18:31 +00:00
* @since 3.0.0
*/
this.settings = Settings.create(config);
2018-02-12 15:18:31 +00:00
/**
* A handy reference to the Scene canvas / context.
*
* @name Phaser.Scenes.Systems#canvas
* @type {HTMLCanvasElement}
* @since 3.0.0
*/
this.canvas;
2018-02-12 15:18:31 +00:00
/**
* [description]
*
* @name Phaser.Scenes.Systems#context
* @type {CanvasRenderingContext2D}
* @since 3.0.0
*/
this.context;
// Global Systems - these are single-instance global managers that belong to Game
2018-02-12 15:18:31 +00:00
/**
* [description]
*
* @name Phaser.Scenes.Systems#anims
* @type {Phaser.Animations.AnimationManager}
* @since 3.0.0
*/
this.anims;
2018-02-12 15:18:31 +00:00
/**
* [description]
*
* @name Phaser.Scenes.Systems#cache
* @type {Phaser.Cache.CacheManager}
* @since 3.0.0
*/
this.cache;
2018-02-12 15:18:31 +00:00
/**
* [description]
*
* @name Phaser.Scenes.Systems#plugins
2018-05-10 16:14:33 +00:00
* @type {Phaser.Plugins.PluginManager}
2018-02-12 15:18:31 +00:00
* @since 3.0.0
*/
this.plugins;
2018-02-12 15:18:31 +00:00
/**
* [description]
*
* @name Phaser.Scenes.Systems#registry
2018-03-19 11:25:46 +00:00
* @type {Phaser.Data.DataManager}
2018-02-12 15:18:31 +00:00
* @since 3.0.0
*/
this.registry;
2018-02-12 15:18:31 +00:00
/**
* [description]
*
* @name Phaser.Scenes.Systems#sound
* @type {Phaser.Sound.BaseSoundManager}
* @since 3.0.0
*/
2018-01-11 14:48:43 +00:00
this.sound;
2018-02-12 15:18:31 +00:00
/**
* [description]
*
* @name Phaser.Scenes.Systems#textures
* @type {Phaser.Textures.TextureManager}
* @since 3.0.0
*/
this.textures;
// Core Plugins - these are non-optional Scene plugins, needed by lots of the other systems
2018-02-12 15:18:31 +00:00
/**
* [description]
*
* @name Phaser.Scenes.Systems#add
* @type {Phaser.GameObjects.GameObjectFactory}
* @since 3.0.0
*/
2018-01-16 02:08:22 +00:00
this.add;
2018-02-12 15:18:31 +00:00
/**
* [description]
*
* @name Phaser.Scenes.Systems#cameras
* @type {Phaser.Cameras.Scene2D.CameraManager}
* @since 3.0.0
*/
this.cameras;
2018-02-12 15:18:31 +00:00
/**
* [description]
*
* @name Phaser.Scenes.Systems#displayList
2018-04-03 14:08:48 +00:00
* @type {Phaser.GameObjects.DisplayList}
2018-02-12 15:18:31 +00:00
* @since 3.0.0
*/
this.displayList;
2018-02-12 15:18:31 +00:00
/**
* [description]
*
* @name Phaser.Scenes.Systems#events
2018-03-29 12:12:07 +00:00
* @type {Phaser.Events.EventEmitter}
2018-02-12 15:18:31 +00:00
* @since 3.0.0
*/
this.events;
2018-02-12 15:18:31 +00:00
/**
* [description]
*
* @name Phaser.Scenes.Systems#make
* @type {Phaser.GameObjects.GameObjectCreator}
* @since 3.0.0
*/
2018-01-16 02:08:22 +00:00
this.make;
2018-02-12 15:18:31 +00:00
/**
* [description]
*
* @name Phaser.Scenes.Systems#scenePlugin
* @type {Phaser.Scenes.ScenePlugin}
* @since 3.0.0
*/
this.scenePlugin;
2018-02-12 15:18:31 +00:00
/**
* [description]
*
* @name Phaser.Scenes.Systems#updateList
2018-03-19 00:59:59 +00:00
* @type {Phaser.GameObjects.UpdateList}
2018-02-12 15:18:31 +00:00
* @since 3.0.0
*/
this.updateList;
/**
* The Scene Update function.
*
* This starts out as NOOP during init, preload and create, and at the end of create
* it swaps to be whatever the Scene.update function is.
*
* @name Phaser.Scenes.Systems#sceneUpdate
* @type {function}
* @private
* @since 3.10.0
*/
this.sceneUpdate = NOOP;
},
2018-02-12 15:18:31 +00:00
/**
* This method is called only once by the Scene Manager when the Scene is instantiated.
* It is responsible for setting up all of the Scene plugins and references.
* It should never be called directly.
2018-02-12 15:18:31 +00:00
*
* @method Phaser.Scenes.Systems#init
* @protected
2018-02-12 15:18:31 +00:00
* @since 3.0.0
*
* @param {Phaser.Game} game - A reference to the Phaser Game instance.
2018-02-12 15:18:31 +00:00
*/
init: function (game)
{
this.settings.status = CONST.INIT;
// This will get replaced by the SceneManager with the actual update function, if it exists, once create is over.
this.sceneUpdate = NOOP;
this.game = game;
this.canvas = game.canvas;
this.context = game.context;
var pluginManager = game.plugins;
this.plugins = pluginManager;
pluginManager.addToScene(this, DefaultPlugins.Global, [ DefaultPlugins.CoreScene, GetScenePlugins(this), GetPhysicsPlugins(this) ]);
2018-01-16 02:08:22 +00:00
this.events.emit('boot', this);
this.settings.isBooted = true;
},
2018-02-12 15:18:31 +00:00
/**
* Called by a plugin, it tells the System to install the plugin locally.
2018-02-12 15:18:31 +00:00
*
* @method Phaser.Scenes.Systems#install
* @private
* @since 3.0.0
*
* @param {array} plugin - An array of plugins to install into this Scene.
*/
install: function (plugin)
{
if (!Array.isArray(plugin))
{
plugin = [ plugin ];
}
this.plugins.installLocal(this, plugin);
2018-01-16 02:08:22 +00:00
},
2018-02-12 15:18:31 +00:00
/**
* A single game step. Called automatically by the Scene Manager as a result of a Request Animation
* Frame or Set Timeout call to the main Game instance.
2018-02-12 15:18:31 +00:00
*
* @method Phaser.Scenes.Systems#step
* @since 3.0.0
*
2018-06-04 14:19:25 +00:00
* @param {number} time - The time value from the most recent Game step. Typically a high-resolution timer value, or Date.now().
* @param {number} delta - The delta value since the last frame. This is smoothed to avoid delta spikes by the TimeStep class.
2018-02-12 15:18:31 +00:00
*/
step: function (time, delta)
{
this.events.emit('preupdate', time, delta);
2017-06-30 03:09:19 +00:00
2018-01-16 02:08:22 +00:00
this.events.emit('update', time, delta);
this.sceneUpdate.call(this.scene, time, delta);
2017-11-08 17:18:10 +00:00
2018-01-16 02:08:22 +00:00
this.events.emit('postupdate', time, delta);
},
2018-02-12 15:18:31 +00:00
/**
* Called automatically by the Scene Manager. Instructs the Scene to render itself via
* its Camera Manager to the renderer given.
2018-02-12 15:18:31 +00:00
*
* @method Phaser.Scenes.Systems#render
* @since 3.0.0
*
2018-03-20 15:12:42 +00:00
* @param {(Phaser.Renderer.Canvas.CanvasRenderer|Phaser.Renderer.WebGL.WebGLRenderer)} renderer - [description]
2018-02-12 15:18:31 +00:00
*/
render: function (renderer)
{
var displayList = this.displayList;
2017-12-15 04:07:32 +00:00
displayList.depthSort();
this.cameras.render(renderer, displayList);
this.events.emit('render', renderer);
},
2018-02-12 15:18:31 +00:00
/**
* Force a sort of the display list on the next render.
*
* @method Phaser.Scenes.Systems#queueDepthSort
* @since 3.0.0
*/
queueDepthSort: function ()
{
this.displayList.queueDepthSort();
},
2018-02-12 15:18:31 +00:00
/**
* Immediately sorts the display list if the flag is set.
*
* @method Phaser.Scenes.Systems#depthSort
* @since 3.0.0
*/
depthSort: function ()
{
this.displayList.depthSort();
},
2018-02-12 15:18:31 +00:00
/**
* Pause this Scene.
* A paused Scene still renders, it just doesn't run ANY of its update handlers or systems.
*
* @method Phaser.Scenes.Systems#pause
* @since 3.0.0
*
* @param {object} [data] - A data object that will be passed in the 'pause' event.
2018-02-12 15:18:31 +00:00
*
* @return {Phaser.Scenes.Systems} This Systems object.
*/
pause: function (data)
{
if (this.settings.active)
{
this.settings.status = CONST.PAUSED;
this.settings.active = false;
this.events.emit('pause', this, data);
}
2018-02-12 15:18:31 +00:00
return this;
},
2018-02-12 15:18:31 +00:00
/**
* Resume this Scene from a paused state.
2018-02-12 15:18:31 +00:00
*
* @method Phaser.Scenes.Systems#resume
* @since 3.0.0
*
* @param {object} [data] - A data object that will be passed in the 'resume' event.
*
2018-02-12 15:18:31 +00:00
* @return {Phaser.Scenes.Systems} This Systems object.
*/
resume: function (data)
{
if (!this.settings.active)
{
this.settings.status = CONST.RUNNING;
this.settings.active = true;
this.events.emit('resume', this, data);
}
2018-02-12 15:18:31 +00:00
return this;
},
2018-02-12 15:18:31 +00:00
/**
* Send this Scene to sleep.
2018-03-19 00:59:59 +00:00
*
* A sleeping Scene doesn't run it's update step or render anything, but it also isn't shut down
* or have any of its systems or children removed, meaning it can be re-activated at any point and
* will carry on from where it left off. It also keeps everything in memory and events and callbacks
* from other Scenes may still invoke changes within it, so be careful what is left active.
2018-02-12 15:18:31 +00:00
*
* @method Phaser.Scenes.Systems#sleep
* @since 3.0.0
*
* @param {object} [data] - A data object that will be passed in the 'sleep' event.
2018-02-12 15:18:31 +00:00
*
* @return {Phaser.Scenes.Systems} This Systems object.
*/
sleep: function (data)
{
this.settings.status = CONST.SLEEPING;
this.settings.active = false;
this.settings.visible = false;
this.events.emit('sleep', this, data);
2018-02-12 15:18:31 +00:00
return this;
},
2018-02-12 15:18:31 +00:00
/**
* Wake-up this Scene if it was previously asleep.
*
* @method Phaser.Scenes.Systems#wake
* @since 3.0.0
*
* @param {object} [data] - A data object that will be passed in the 'wake' event.
*
2018-02-12 15:18:31 +00:00
* @return {Phaser.Scenes.Systems} This Systems object.
*/
wake: function (data)
{
var settings = this.settings;
settings.status = CONST.RUNNING;
settings.active = true;
settings.visible = true;
this.events.emit('wake', this, data);
2018-02-12 15:18:31 +00:00
if (settings.isTransition)
{
this.events.emit('transitionwake', settings.transitionFrom, settings.transitionDuration);
}
2018-02-12 15:18:31 +00:00
return this;
2017-06-30 03:06:53 +00:00
},
2018-02-12 15:18:31 +00:00
/**
* Is this Scene sleeping?
*
* @method Phaser.Scenes.Systems#isSleeping
* @since 3.0.0
*
* @return {boolean} [description]
*/
isSleeping: function ()
{
return (this.settings.status === CONST.SLEEPING);
},
2018-02-12 15:18:31 +00:00
/**
* Is this Scene active?
*
* @method Phaser.Scenes.Systems#isActive
* @since 3.0.0
*
* @return {boolean} [description]
*/
isActive: function ()
{
return (this.settings.status === CONST.RUNNING);
},
/**
* Is this Scene currently transitioning out to, or in from another Scene?
*
* @method Phaser.Scenes.Systems#isTransitioning
* @since 3.5.0
*
* @return {boolean} `true` if this Scene is currently transitioning, otherwise `false`.
*/
isTransitioning: function ()
{
return (this.settings.isTransition || this.scenePlugin._target !== null);
},
/**
* Is this Scene currently transitioning out from itself to another Scene?
*
* @method Phaser.Scenes.Systems#isTransitionOut
* @since 3.5.0
*
* @return {boolean} `true` if this Scene is in transition to another Scene, otherwise `false`.
*/
isTransitionOut: function ()
{
return (this.scenePlugin._target !== null && this.scenePlugin._duration > 0);
},
/**
* Is this Scene currently transitioning in from another Scene?
*
* @method Phaser.Scenes.Systems#isTransitionIn
* @since 3.5.0
*
* @return {boolean} `true` if this Scene is transitioning in from another Scene, otherwise `false`.
*/
isTransitionIn: function ()
{
return (this.settings.isTransition);
},
2018-02-12 15:18:31 +00:00
/**
* Is this Scene visible and rendering?
*
* @method Phaser.Scenes.Systems#isVisible
* @since 3.0.0
*
* @return {boolean} [description]
*/
isVisible: function ()
{
return this.settings.visible;
},
2018-02-12 15:18:31 +00:00
/**
* Sets the visible state of this Scene.
* An invisible Scene will not render, but will still process updates.
2018-02-12 15:18:31 +00:00
*
* @method Phaser.Scenes.Systems#setVisible
* @since 3.0.0
*
* @param {boolean} value - [description]
*
* @return {Phaser.Scenes.Systems} This Systems object.
*/
setVisible: function (value)
{
this.settings.visible = value;
return this;
},
2018-02-12 15:18:31 +00:00
/**
* Set the active state of this Scene.
*
* An active Scene will run its core update loop.
2018-02-12 15:18:31 +00:00
*
* @method Phaser.Scenes.Systems#setActive
* @since 3.0.0
*
* @param {boolean} value - If `true` the Scene will be resumed, if previously paused. If `false` it will be paused.
* @param {object} [data] - A data object that will be passed in the 'resume' or 'pause' events.
2018-02-12 15:18:31 +00:00
*
* @return {Phaser.Scenes.Systems} This Systems object.
*/
setActive: function (value, data)
2017-06-30 03:06:53 +00:00
{
if (value)
{
return this.resume(data);
}
else
{
return this.pause(data);
}
},
2018-02-12 15:18:31 +00:00
/**
* Start this Scene running and rendering.
2018-04-09 11:57:03 +00:00
* Called automatically by the SceneManager.
2018-02-12 15:18:31 +00:00
*
* @method Phaser.Scenes.Systems#start
* @since 3.0.0
*
* @param {object} data - Optional data object that may have been passed to this Scene from another.
2018-02-12 15:18:31 +00:00
*/
start: function (data)
{
2018-04-09 11:57:03 +00:00
if (data)
{
this.settings.data = data;
}
2018-04-09 11:57:03 +00:00
this.settings.status = CONST.START;
2017-06-30 03:06:53 +00:00
this.settings.active = true;
this.settings.visible = true;
// For plugins to listen out for
this.events.emit('start', this);
// For user-land code to listen out for
this.events.emit('ready', this, data);
},
2018-02-28 17:18:40 +00:00
/**
* Called automatically by the SceneManager if the Game resizes.
* Dispatches an event you can respond to in your game code.
*
* @method Phaser.Scenes.Systems#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)
{
this.events.emit('resize', width, height);
},
2018-02-12 15:18:31 +00:00
/**
* Shutdown this Scene and send a shutdown event to all of its systems.
* A Scene that has been shutdown will not run its update loop or render, but it does
* not destroy any of its plugins or references. It is put into hibernation for later use.
* If you don't ever plan to use this Scene again, then it should be destroyed instead
* to free-up resources.
2018-02-12 15:18:31 +00:00
*
* @method Phaser.Scenes.Systems#shutdown
* @since 3.0.0
*
* @param {object} [data] - A data object that will be passed in the 'shutdown' event.
2018-02-12 15:18:31 +00:00
*/
shutdown: function (data)
{
this.events.off('transitioninit');
this.events.off('transitionstart');
this.events.off('transitioncomplete');
this.events.off('transitionout');
this.settings.status = CONST.SHUTDOWN;
this.settings.active = false;
2017-06-30 03:06:53 +00:00
this.settings.visible = false;
this.events.emit('shutdown', this, data);
},
2018-02-12 15:18:31 +00:00
/**
* Destroy this Scene and send a destroy event all of its systems.
* A destroyed Scene cannot be restarted.
* You should not call this directly, instead use `SceneManager.remove`.
2018-02-12 15:18:31 +00:00
*
* @method Phaser.Scenes.Systems#destroy
* @private
2018-02-12 15:18:31 +00:00
* @since 3.0.0
*/
destroy: function ()
{
this.settings.status = CONST.DESTROYED;
this.settings.active = false;
this.settings.visible = false;
2018-01-16 02:08:22 +00:00
this.events.emit('destroy', this);
this.events.removeAllListeners();
var props = [ 'scene', 'game', 'anims', 'cache', 'plugins', 'registry', 'sound', 'textures', 'add', 'camera', 'displayList', 'events', 'make', 'scenePlugin', 'updateList' ];
for (var i = 0; i < props.length; i++)
{
this[props[i]] = null;
}
2017-02-08 00:08:09 +00:00
}
});
module.exports = Systems;