phaser/v3/src/state/Systems.js

156 lines
4.2 KiB
JavaScript
Raw Normal View History

/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var EventDispatcher = require('../events/EventDispatcher');
var GameObjectFactory = require('./systems/GameObjectFactory');
var GameObjectCreator = require('./systems/GameObjectCreator');
var StateManager = require('./systems/StateManager');
var Loader = require('./systems/Loader');
var UpdateManager = require('./systems/UpdateManager');
2016-12-07 02:40:07 +00:00
var Component = require('../components');
var Settings = require('./Settings');
var RTree = require('../structs/RTree');
var CameraManager = require('./systems/CameraManager');
2017-03-23 19:51:02 +00:00
var StableSort = require('../utils/array/StableSort');
var Systems = function (state, config)
{
this.state = state;
this.game = null;
this.config = config;
2017-02-06 23:59:15 +00:00
this.settings = Settings.create(config);
this.x = this.settings.x;
this.y = this.settings.y;
2017-02-07 22:00:55 +00:00
this.width = this.settings.width;
this.height = this.settings.height;
this.mask = null;
this.canvas;
this.context;
// CORE SYSTEMS / PROPERTIES
this.cache;
this.textures;
// Reference to State specific managers (Factory, Tweens, Loader, Physics, etc)
this.add;
this.make;
this.load;
this.events;
this.updates;
this.tree;
this.stateManager;
// State properties
this.cameras;
this.children;
this.color;
this.data;
// this.fbo;
this.time;
this.transform;
};
Systems.prototype.constructor = Systems;
Systems.prototype = {
init: function (game)
{
// console.log('State.Systems.init');
this.game = game;
2017-02-06 23:59:15 +00:00
Settings.init(this.settings, this.game.config);
this.width = this.settings.width;
this.height = this.settings.height;
this.cache = this.game.cache;
this.textures = this.game.textures;
// State specific managers (Factory, Tweens, Loader, Physics, etc)
this.tree = RTree(16);
this.events = new EventDispatcher();
this.add = new GameObjectFactory(this.state);
this.make = new GameObjectCreator(this.state);
this.updates = new UpdateManager(this.state);
this.load = new Loader(this.state);
this.stateManager = new StateManager(this.state, game);
this.cameras = new CameraManager(this.state);
// State specific properties (transform, data, children, etc)
2016-12-07 02:40:07 +00:00
this.children = new Component.Children(this.state);
this.color = new Component.Color(this.state);
this.data = new Component.Data(this.state);
this.inject();
},
inject: function ()
{
// Defaults properties injected into the State
this.state.game = this.game;
this.state.events = this.events;
this.state.add = this.add;
this.state.load = this.load;
2016-12-07 02:40:07 +00:00
this.state.children = this.children;
this.state.color = this.color;
this.state.data = this.data;
this.state.settings = this.settings;
this.state.state = this.stateManager;
this.state.cameras = this.cameras;
this.state.cache = this.game.cache;
this.state.input = this.game.input;
this.state.textures = this.game.textures;
2017-03-23 19:51:02 +00:00
this.state.sortChildrenFlag = false;
},
// Called just once per frame, regardless of speed
begin: function (timestamp, frameDelta)
{
2017-03-23 19:51:02 +00:00
var state = this.state;
if (state.sortChildrenFlag)
{
/* Sort the current state children */
StableSort.inplace(state.children.list, function (childA, childB) {
return childA._z - childB._z;
});
state.sortChildrenFlag = false;
}
},
// Potentially called multiple times per frame (on super-fast systems)
update: function (timestep, physicsStep)
{
this.cameras.update(timestep);
2017-02-07 20:47:41 +00:00
this.state.update.call(this.state, timestep, physicsStep);
},
render: function (interpolation, renderer)
{
if (!this.settings.visible)
{
return;
}
this.cameras.render(renderer, this.children, interpolation);
2017-02-08 00:08:09 +00:00
}
};
module.exports = Systems;