phaser/src/scene/Systems.js

547 lines
12 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 GetPhysicsPlugins = require('./GetPhysicsPlugins');
var GetScenePlugins = require('./GetScenePlugins');
var Plugins = require('../plugins');
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.
* @param {object} config - Scene specific configuration settings.
*/
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
* @type {object}
* @since 3.0.0
*/
this.config = config;
2018-02-12 15:18:31 +00:00
/**
* [description]
*
* @name Phaser.Scenes.Systems#settings
* @type {[type]}
* @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
* @type {Phaser.Plugins.PluginManager}
* @since 3.0.0
*/
this.plugins;
2018-02-12 15:18:31 +00:00
/**
* [description]
*
* @name Phaser.Scenes.Systems#registry
* @type {[type]}
* @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
* @type {null}
* @since 3.0.0
*/
this.displayList;
2018-02-12 15:18:31 +00:00
/**
* [description]
*
* @name Phaser.Scenes.Systems#events
* @type {EventEmitter3}
* @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
* @type {[type]}
* @since 3.0.0
*/
this.updateList;
},
2018-02-12 15:18:31 +00:00
/**
* [description]
*
* @method Phaser.Scenes.Systems#init
* @since 3.0.0
*
* @param {Phaser.Game} game - A reference to the Phaser Game
*/
init: function (game)
{
this.settings.status = CONST.INIT;
this.game = game;
this.canvas = game.canvas;
this.context = game.context;
var pluginManager = game.plugins;
this.plugins = pluginManager;
pluginManager.installGlobal(this, Plugins.Global);
pluginManager.installLocal(this, Plugins.CoreScene);
pluginManager.installLocal(this, GetScenePlugins(this));
pluginManager.installLocal(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
/**
* [description]
*
* @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
/**
* [description]
*
* @method Phaser.Scenes.Systems#step
* @since 3.0.0
*
* @param {number} time - [description]
* @param {number} delta - [description]
*/
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.scene.update.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
/**
* [description]
*
* @method Phaser.Scenes.Systems#render
* @since 3.0.0
*
* @param {Phaser.Renderer.Canvas.CanvasRenderer|Phaser.Renderer.WebGL.WebGLRenderer} renderer - [description]
*/
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
*
* @return {Phaser.Scenes.Systems} This Systems object.
*/
pause: function ()
{
if (this.settings.active)
{
this.settings.status = CONST.PAUSED;
this.settings.active = false;
this.events.emit('pause', this);
}
2018-02-12 15:18:31 +00:00
return this;
},
2018-02-12 15:18:31 +00:00
/**
* Resume this Scene.
*
* @method Phaser.Scenes.Systems#resume
* @since 3.0.0
*
* @return {Phaser.Scenes.Systems} This Systems object.
*/
resume: function ()
{
if (!this.settings.active)
{
this.settings.status = CONST.RUNNING;
this.settings.active = true;
this.events.emit('resume', this);
}
2018-02-12 15:18:31 +00:00
return this;
},
2018-02-12 15:18:31 +00:00
/**
* Send this Scene to sleep.
*
* A sleeping Scene doesn't run it's update step or render anything, but it also isn't destroyed,
* or have any of its systems or children removed, meaning it can be re-activated at any point.
*
* @method Phaser.Scenes.Systems#sleep
* @since 3.0.0
*
* @return {Phaser.Scenes.Systems} This Systems object.
*/
sleep: function ()
{
this.settings.status = CONST.SLEEPING;
this.settings.active = false;
this.settings.visible = false;
this.events.emit('sleep', this);
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
*
* @return {Phaser.Scenes.Systems} This Systems object.
*/
wake: function ()
{
this.settings.status = CONST.RUNNING;
this.settings.active = true;
this.settings.visible = true;
this.events.emit('wake', this);
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);
},
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
/**
* [description]
*
* @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
/**
* [description]
*
* @method Phaser.Scenes.Systems#setActive
* @since 3.0.0
*
* @param {boolean} value - [description]
*
* @return {Phaser.Scenes.Systems} This Systems object.
*/
setActive: function (value)
2017-06-30 03:06:53 +00:00
{
if (value)
{
return this.resume();
}
else
{
return this.pause();
}
},
2018-02-12 15:18:31 +00:00
/**
* Start this Scene running and rendering.
*
* @method Phaser.Scenes.Systems#start
* @since 3.0.0
*
* @param {object} data - [description]
*/
start: function (data)
{
this.settings.status = CONST.START;
2017-06-30 03:06:53 +00:00
this.settings.data = data;
this.settings.active = true;
this.settings.visible = true;
this.events.emit('start', this);
},
2018-02-12 15:18:31 +00:00
/**
* Shutdown this Scene and send a shutdown event to all of its systems.
*
* @method Phaser.Scenes.Systems#shutdown
* @since 3.0.0
*/
shutdown: function ()
{
this.settings.status = CONST.SHUTDOWN;
this.settings.active = false;
2017-06-30 03:06:53 +00:00
this.settings.visible = false;
2018-01-16 02:08:22 +00:00
this.events.emit('shutdown', this);
},
2018-02-12 15:18:31 +00:00
/**
* Destroy this Scene and send a destroy event all of its systems.
*
* @method Phaser.Scenes.Systems#destroy
* @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);
2017-02-08 00:08:09 +00:00
}
});
module.exports = Systems;