2016-11-29 15:25:14 +00:00
|
|
|
|
2017-04-04 22:59:02 +00:00
|
|
|
var CameraManager = require('./systems/CameraManager');
|
|
|
|
var Component = require('../components');
|
2016-12-05 15:29:35 +00:00
|
|
|
var EventDispatcher = require('../events/EventDispatcher');
|
2017-01-25 17:10:19 +00:00
|
|
|
var GameObjectCreator = require('./systems/GameObjectCreator');
|
2017-04-04 22:59:02 +00:00
|
|
|
var GameObjectFactory = require('./systems/GameObjectFactory');
|
2016-11-30 17:16:45 +00:00
|
|
|
var Loader = require('./systems/Loader');
|
2017-01-25 17:10:19 +00:00
|
|
|
var Settings = require('./Settings');
|
2017-03-23 19:51:02 +00:00
|
|
|
var StableSort = require('../utils/array/StableSort');
|
2017-04-04 22:59:02 +00:00
|
|
|
var StateManager = require('./systems/StateManager');
|
|
|
|
var UpdateManager = require('./systems/UpdateManager');
|
2016-11-29 15:25:14 +00:00
|
|
|
|
|
|
|
var Systems = function (state, config)
|
|
|
|
{
|
|
|
|
this.state = state;
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-04-04 22:59:02 +00:00
|
|
|
this.sortChildrenFlag = false;
|
|
|
|
|
|
|
|
// Set by the GlobalStateManager
|
2017-02-07 18:44:26 +00:00
|
|
|
this.mask = null;
|
|
|
|
this.canvas;
|
|
|
|
this.context;
|
|
|
|
|
2017-04-04 22:59:02 +00:00
|
|
|
// CORE (GLOBAL) SYSTEMS / PROPERTIES
|
|
|
|
|
|
|
|
this.game;
|
2017-01-25 17:10:19 +00:00
|
|
|
|
2017-04-04 22:59:02 +00:00
|
|
|
this.anims;
|
2017-01-30 16:56:04 +00:00
|
|
|
this.cache;
|
2017-04-04 22:59:02 +00:00
|
|
|
this.input;
|
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;
|
2017-04-04 22:59:02 +00:00
|
|
|
this.cameras;
|
2017-01-25 17:10:19 +00:00
|
|
|
this.events;
|
2017-04-04 22:59:02 +00:00
|
|
|
this.load;
|
|
|
|
this.make;
|
2017-02-08 01:07:01 +00:00
|
|
|
this.stateManager;
|
2017-04-04 22:59:02 +00:00
|
|
|
this.updates;
|
2016-11-29 15:25:14 +00:00
|
|
|
|
2017-01-25 17:10:19 +00:00
|
|
|
// State properties
|
2016-11-29 15:25:14 +00:00
|
|
|
this.children;
|
|
|
|
this.color;
|
|
|
|
this.data;
|
|
|
|
};
|
|
|
|
|
|
|
|
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-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-04-04 22:59:02 +00:00
|
|
|
this.anims = this.game.anims;
|
2017-01-30 16:56:04 +00:00
|
|
|
this.cache = this.game.cache;
|
2017-04-04 22:59:02 +00:00
|
|
|
this.input = this.game.input;
|
2017-01-26 04:06:10 +00:00
|
|
|
this.textures = this.game.textures;
|
|
|
|
|
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-04-04 22:59:02 +00:00
|
|
|
// State specific managers (Factory, Tweens, Loader, Physics, etc)
|
|
|
|
|
|
|
|
this.add = new GameObjectFactory(this.state);
|
|
|
|
this.cameras = new CameraManager(this.state);
|
|
|
|
this.events = new EventDispatcher();
|
|
|
|
this.load = new Loader(this.state);
|
|
|
|
this.make = new GameObjectCreator(this.state);
|
|
|
|
this.stateManager = new StateManager(this.state, game);
|
|
|
|
this.updates = new UpdateManager(this.state);
|
|
|
|
|
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;
|
|
|
|
|
2017-04-04 22:59:02 +00:00
|
|
|
this.state.anims = this.anims;
|
|
|
|
this.state.cache = this.cache;
|
|
|
|
this.state.input = this.input;
|
|
|
|
this.state.textures = this.textures;
|
|
|
|
|
2016-11-29 15:25:14 +00:00
|
|
|
this.state.add = this.add;
|
2017-04-11 01:48:55 +00:00
|
|
|
this.state.make = this.make;
|
2017-04-04 22:59:02 +00:00
|
|
|
this.state.cameras = this.cameras;
|
|
|
|
this.state.events = this.events;
|
2016-11-30 17:16:45 +00:00
|
|
|
this.state.load = this.load;
|
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-01-26 04:06:10 +00:00
|
|
|
|
2017-04-04 22:59:02 +00:00
|
|
|
this.state.children = this.children;
|
|
|
|
this.state.color = this.color;
|
|
|
|
this.state.data = this.data;
|
2016-11-29 15:25:14 +00:00
|
|
|
},
|
|
|
|
|
2017-04-27 16:03:19 +00:00
|
|
|
step: function (time, delta)
|
2017-04-27 02:11:56 +00:00
|
|
|
{
|
|
|
|
var list = this.children.list;
|
|
|
|
|
|
|
|
for (var i = 0; i < list.length; i++)
|
|
|
|
{
|
2017-04-27 16:03:19 +00:00
|
|
|
list[i].preUpdate(time, delta);
|
2017-04-27 02:11:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-27 16:03:19 +00:00
|
|
|
this.cameras.update(time, delta);
|
2017-04-27 02:11:56 +00:00
|
|
|
|
2017-04-27 16:03:19 +00:00
|
|
|
this.state.update.call(this.state, time, delta);
|
2017-04-27 02:11:56 +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-04-04 22:59:02 +00:00
|
|
|
var list = this.children.list;
|
|
|
|
|
|
|
|
for (var i = 0; i < list.length; i++)
|
2017-03-23 19:51:02 +00:00
|
|
|
{
|
2017-04-04 22:59:02 +00:00
|
|
|
list[i].preUpdate(timestamp, frameDelta);
|
2017-03-23 19:51:02 +00:00
|
|
|
}
|
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-04-04 22:59:02 +00:00
|
|
|
// Called just once per frame
|
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-04-04 22:59:02 +00:00
|
|
|
if (this.sortChildrenFlag)
|
|
|
|
{
|
|
|
|
StableSort.inplace(this.children.list, this.sortZ);
|
|
|
|
|
|
|
|
this.sortChildrenFlag = false;
|
|
|
|
}
|
|
|
|
|
2017-02-21 00:38:22 +00:00
|
|
|
this.cameras.render(renderer, this.children, interpolation);
|
2017-04-04 22:59:02 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
sortZ: function (childA, childB)
|
|
|
|
{
|
|
|
|
return childA._z - childB._z;
|
2017-02-08 00:08:09 +00:00
|
|
|
}
|
2016-11-29 15:25:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Systems;
|