2016-11-29 15:25:14 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2016 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2016-12-05 15:29:35 +00:00
|
|
|
var EventDispatcher = require('../events/EventDispatcher');
|
2016-11-29 15:25:14 +00:00
|
|
|
var GameObjectFactory = require('./systems/GameObjectFactory');
|
2017-01-25 17:10:19 +00:00
|
|
|
var GameObjectCreator = require('./systems/GameObjectCreator');
|
2017-02-08 01:07:01 +00:00
|
|
|
var StateManager = require('./systems/StateManager');
|
2016-11-30 17:16:45 +00:00
|
|
|
var Loader = require('./systems/Loader');
|
2016-11-29 15:25:14 +00:00
|
|
|
var UpdateManager = require('./systems/UpdateManager');
|
2016-12-07 02:40:07 +00:00
|
|
|
var Component = require('../components');
|
2017-01-25 17:10:19 +00:00
|
|
|
var Settings = require('./Settings');
|
2017-01-26 04:06:10 +00:00
|
|
|
var RTree = require('../structs/RTree');
|
2017-02-12 13:19:55 +00:00
|
|
|
var CameraManager = require('./systems/CameraManager');
|
2017-03-23 19:51:02 +00:00
|
|
|
var StableSort = require('../utils/array/StableSort');
|
2016-11-29 15:25:14 +00:00
|
|
|
|
|
|
|
var Systems = function (state, config)
|
|
|
|
{
|
|
|
|
this.state = state;
|
|
|
|
|
2017-01-25 17:10:19 +00:00
|
|
|
this.game = null;
|
|
|
|
|
2016-11-29 15:25:14 +00:00
|
|
|
this.config = config;
|
|
|
|
|
2017-02-06 23:59:15 +00:00
|
|
|
this.settings = Settings.create(config);
|
2016-12-06 16:49:29 +00:00
|
|
|
|
2017-02-07 18:44:26 +00:00
|
|
|
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;
|
2017-02-07 18:44:26 +00:00
|
|
|
|
|
|
|
this.mask = null;
|
|
|
|
this.canvas;
|
|
|
|
this.context;
|
|
|
|
|
2017-01-25 17:10:19 +00:00
|
|
|
// CORE SYSTEMS / PROPERTIES
|
|
|
|
|
2017-01-30 16:56:04 +00:00
|
|
|
this.cache;
|
2017-01-26 04:06:10 +00:00
|
|
|
this.textures;
|
|
|
|
|
2017-01-25 17:10:19 +00:00
|
|
|
// Reference to State specific managers (Factory, Tweens, Loader, Physics, etc)
|
2016-11-29 15:25:14 +00:00
|
|
|
this.add;
|
|
|
|
this.make;
|
|
|
|
this.load;
|
2017-01-25 17:10:19 +00:00
|
|
|
this.events;
|
2016-11-29 15:25:14 +00:00
|
|
|
this.updates;
|
2017-01-26 04:06:10 +00:00
|
|
|
this.tree;
|
2017-02-08 01:07:01 +00:00
|
|
|
this.stateManager;
|
2016-11-29 15:25:14 +00:00
|
|
|
|
2017-01-25 17:10:19 +00:00
|
|
|
// State properties
|
2017-02-07 18:44:26 +00:00
|
|
|
this.cameras;
|
2016-11-29 15:25:14 +00:00
|
|
|
this.children;
|
|
|
|
this.color;
|
|
|
|
this.data;
|
2017-02-12 13:19:55 +00:00
|
|
|
// this.fbo;
|
2016-11-29 15:25:14 +00:00
|
|
|
this.time;
|
2017-01-30 23:58:29 +00:00
|
|
|
this.transform;
|
2016-11-29 15:25:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Systems.prototype.constructor = Systems;
|
|
|
|
|
|
|
|
Systems.prototype = {
|
|
|
|
|
2017-01-25 17:10:19 +00:00
|
|
|
init: function (game)
|
2016-11-29 15:25:14 +00:00
|
|
|
{
|
2017-02-07 21:49:25 +00:00
|
|
|
// console.log('State.Systems.init');
|
2016-11-29 15:25:14 +00:00
|
|
|
|
2017-01-25 17:10:19 +00:00
|
|
|
this.game = game;
|
2016-11-29 15:25:14 +00:00
|
|
|
|
2017-02-06 23:59:15 +00:00
|
|
|
Settings.init(this.settings, this.game.config);
|
2017-01-26 04:06:10 +00:00
|
|
|
|
2017-02-12 13:19:55 +00:00
|
|
|
this.width = this.settings.width;
|
|
|
|
this.height = this.settings.height;
|
|
|
|
|
2017-01-30 16:56:04 +00:00
|
|
|
this.cache = this.game.cache;
|
2017-01-26 04:06:10 +00:00
|
|
|
this.textures = this.game.textures;
|
|
|
|
|
2016-12-05 15:29:35 +00:00
|
|
|
// State specific managers (Factory, Tweens, Loader, Physics, etc)
|
2016-11-29 15:25:14 +00:00
|
|
|
|
2017-01-26 04:06:10 +00:00
|
|
|
this.tree = RTree(16);
|
2017-01-25 17:10:19 +00:00
|
|
|
this.events = new EventDispatcher();
|
2016-11-29 15:25:14 +00:00
|
|
|
this.add = new GameObjectFactory(this.state);
|
2017-02-08 01:07:01 +00:00
|
|
|
this.make = new GameObjectCreator(this.state);
|
2016-11-29 15:25:14 +00:00
|
|
|
this.updates = new UpdateManager(this.state);
|
2016-11-30 17:16:45 +00:00
|
|
|
this.load = new Loader(this.state);
|
2017-02-08 01:07:01 +00:00
|
|
|
this.stateManager = new StateManager(this.state, game);
|
2017-02-12 13:19:55 +00:00
|
|
|
this.cameras = new CameraManager(this.state);
|
2016-11-29 15:25:14 +00:00
|
|
|
|
|
|
|
// State specific properties (transform, data, children, etc)
|
2017-01-25 17:10:19 +00:00
|
|
|
|
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);
|
2017-02-08 01:07:01 +00:00
|
|
|
|
2017-01-25 17:10:19 +00:00
|
|
|
this.inject();
|
|
|
|
},
|
2016-11-29 15:25:14 +00:00
|
|
|
|
2017-01-25 17:10:19 +00:00
|
|
|
inject: function ()
|
|
|
|
{
|
|
|
|
// Defaults properties injected into the State
|
2016-11-29 15:25:14 +00:00
|
|
|
|
2017-01-31 14:55:15 +00:00
|
|
|
this.state.game = this.game;
|
|
|
|
|
2016-12-05 15:29:35 +00:00
|
|
|
this.state.events = this.events;
|
2016-11-29 15:25:14 +00:00
|
|
|
this.state.add = this.add;
|
2016-11-30 17:16:45 +00:00
|
|
|
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;
|
2017-01-26 04:06:10 +00:00
|
|
|
this.state.settings = this.settings;
|
2017-02-08 01:07:01 +00:00
|
|
|
this.state.state = this.stateManager;
|
2017-02-12 13:19:55 +00:00
|
|
|
this.state.cameras = this.cameras;
|
2017-01-26 04:06:10 +00:00
|
|
|
|
2017-01-30 16:56:04 +00:00
|
|
|
this.state.cache = this.game.cache;
|
2017-02-21 16:05:36 +00:00
|
|
|
this.state.input = this.game.input;
|
2017-01-26 04:06:10 +00:00
|
|
|
this.state.textures = this.game.textures;
|
2017-03-23 19:51:02 +00:00
|
|
|
this.state.sortChildrenFlag = false;
|
2016-11-29 15:25:14 +00:00
|
|
|
},
|
|
|
|
|
2017-01-25 17:10:19 +00:00
|
|
|
// Called just once per frame, regardless of speed
|
2016-11-29 15:25:14 +00:00
|
|
|
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;
|
|
|
|
}
|
2016-11-29 15:25:14 +00:00
|
|
|
},
|
|
|
|
|
2017-01-25 17:10:19 +00:00
|
|
|
// Potentially called multiple times per frame (on super-fast systems)
|
2016-11-29 15:25:14 +00:00
|
|
|
update: function (timestep, physicsStep)
|
|
|
|
{
|
2017-02-12 13:19:55 +00:00
|
|
|
this.cameras.update(timestep);
|
2016-11-29 15:25:14 +00:00
|
|
|
|
2017-02-07 20:47:41 +00:00
|
|
|
this.state.update.call(this.state, timestep, physicsStep);
|
2016-11-29 15:25:14 +00:00
|
|
|
},
|
|
|
|
|
2017-01-26 04:06:10 +00:00
|
|
|
render: function (interpolation, renderer)
|
2017-01-30 23:58:29 +00:00
|
|
|
{
|
2017-02-08 01:07:01 +00:00
|
|
|
if (!this.settings.visible)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-21 00:38:22 +00:00
|
|
|
this.cameras.render(renderer, this.children, interpolation);
|
2017-02-08 00:08:09 +00:00
|
|
|
}
|
2016-11-29 15:25:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Systems;
|