2016-11-29 15:25:14 +00:00
|
|
|
|
2017-07-04 00:59:31 +00:00
|
|
|
var CameraManager = require('../plugins/CameraManager');
|
2017-06-28 16:17:54 +00:00
|
|
|
var Clock = require('../time/Clock');
|
2017-07-04 00:59:31 +00:00
|
|
|
// var Component = require('../components');
|
2016-12-05 15:29:35 +00:00
|
|
|
var EventDispatcher = require('../events/EventDispatcher');
|
2017-07-04 00:59:31 +00:00
|
|
|
var DisplayList = require('../plugins/DisplayList');
|
|
|
|
var Data = require('../plugins/Data');
|
|
|
|
var GameObjectCreator = require('../plugins/GameObjectCreator');
|
|
|
|
var GameObjectFactory = require('../plugins/GameObjectFactory');
|
|
|
|
var Loader = require('../plugins/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-07-04 00:59:31 +00:00
|
|
|
var StateManager = require('../plugins/StateManager');
|
2017-05-09 19:24:39 +00:00
|
|
|
var TweenManager = require('../tween/TweenManager');
|
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-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-06-29 16:02:08 +00:00
|
|
|
this.registry;
|
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-06-29 16:02:08 +00:00
|
|
|
this.time;
|
2017-05-09 19:24:39 +00:00
|
|
|
this.tweens;
|
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 = {
|
|
|
|
|
2017-01-25 17:10:19 +00:00
|
|
|
init: function (game)
|
2016-11-29 15:25:14 +00:00
|
|
|
{
|
2017-07-04 00:59:31 +00:00
|
|
|
var state = this.state;
|
|
|
|
|
2017-01-25 17:10:19 +00:00
|
|
|
this.game = game;
|
2016-11-29 15:25:14 +00:00
|
|
|
|
2017-06-29 16:02:08 +00:00
|
|
|
// Game (Global) level managers
|
2017-02-12 13:19:55 +00:00
|
|
|
|
2017-07-04 00:59:31 +00:00
|
|
|
this.anims = game.anims;
|
|
|
|
this.cache = game.cache;
|
|
|
|
this.input = game.input;
|
|
|
|
this.registry = game.registry;
|
|
|
|
this.textures = game.textures;
|
2017-01-26 04:06:10 +00:00
|
|
|
|
2016-11-29 15:25:14 +00:00
|
|
|
// State specific properties (transform, data, children, etc)
|
2017-01-25 17:10:19 +00:00
|
|
|
|
2017-07-04 00:59:31 +00:00
|
|
|
this.children = new DisplayList(state);
|
|
|
|
// this.color = new Component.Color(state);
|
|
|
|
this.data = new Data(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)
|
|
|
|
|
2017-07-04 00:59:31 +00:00
|
|
|
this.add = new GameObjectFactory(state);
|
|
|
|
this.cameras = new CameraManager(state);
|
2017-04-04 22:59:02 +00:00
|
|
|
this.events = new EventDispatcher();
|
2017-07-04 00:59:31 +00:00
|
|
|
this.load = new Loader(state);
|
|
|
|
this.make = new GameObjectCreator(state);
|
|
|
|
this.stateManager = new StateManager(state, game);
|
|
|
|
this.time = new Clock(state);
|
|
|
|
this.tweens = new TweenManager(state);
|
2017-04-04 22:59:02 +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 ()
|
|
|
|
{
|
2017-06-29 23:32:18 +00:00
|
|
|
var map = this.settings.map;
|
|
|
|
|
|
|
|
for (var key in map)
|
|
|
|
{
|
|
|
|
if (key === 'sys')
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.state[map[key]] = this[key];
|
|
|
|
}
|
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
|
|
|
{
|
2017-06-30 03:09:19 +00:00
|
|
|
// Are there any pending StateManager actions?
|
|
|
|
this.stateManager.update();
|
|
|
|
|
2017-06-30 02:31:31 +00:00
|
|
|
if (!this.settings.active)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-28 16:17:54 +00:00
|
|
|
this.time.begin(time);
|
|
|
|
|
2017-05-17 03:37:30 +00:00
|
|
|
this.tweens.begin(time);
|
2017-05-09 19:24:39 +00:00
|
|
|
|
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-06-28 21:19:41 +00:00
|
|
|
// preUpdate TimerEvents
|
2017-06-28 16:17:54 +00:00
|
|
|
this.time.update(time, delta);
|
|
|
|
|
2017-05-17 03:37:30 +00:00
|
|
|
this.tweens.update(time, delta);
|
|
|
|
|
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-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-06-30 02:31:31 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// A paused State still renders, it just doesn't run ANY of its update handlers or systems
|
|
|
|
pause: function ()
|
|
|
|
{
|
|
|
|
// Was paused by the GlobalStateManager
|
|
|
|
|
|
|
|
this.settings.active = false;
|
|
|
|
|
2017-06-30 03:06:53 +00:00
|
|
|
if (this.state.pause)
|
|
|
|
{
|
|
|
|
this.state.pause.call(this.state);
|
|
|
|
}
|
2017-06-30 02:31:31 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
resume: function ()
|
|
|
|
{
|
|
|
|
// Was resumed by the GlobalStateManager
|
|
|
|
|
|
|
|
this.settings.active = true;
|
|
|
|
|
2017-06-30 03:06:53 +00:00
|
|
|
if (this.state.resume)
|
|
|
|
{
|
|
|
|
this.state.resume.call(this.state);
|
|
|
|
}
|
2017-06-30 02:31:31 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
sleep: function ()
|
|
|
|
{
|
|
|
|
// Was sent to sleep by the GlobalStateManager
|
|
|
|
|
|
|
|
this.settings.active = false;
|
|
|
|
this.settings.visible = false;
|
|
|
|
|
2017-06-30 03:06:53 +00:00
|
|
|
if (this.state.sleep)
|
|
|
|
{
|
|
|
|
this.state.sleep.call(this.state);
|
|
|
|
}
|
2017-06-30 02:31:31 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
wake: function ()
|
|
|
|
{
|
|
|
|
// Was woken up by the GlobalStateManager
|
|
|
|
|
|
|
|
this.settings.active = true;
|
|
|
|
this.settings.visible = true;
|
|
|
|
|
2017-06-30 03:06:53 +00:00
|
|
|
if (this.state.wake)
|
|
|
|
{
|
|
|
|
this.state.wake.call(this.state);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
start: function (data)
|
|
|
|
{
|
|
|
|
// Was started by the GlobalStateManager
|
|
|
|
|
|
|
|
this.settings.data = data;
|
|
|
|
|
|
|
|
this.settings.active = true;
|
|
|
|
this.settings.visible = true;
|
2017-06-30 02:31:31 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
shutdown: function ()
|
|
|
|
{
|
|
|
|
// Was stopped by the GlobalStateManager
|
|
|
|
|
|
|
|
this.settings.active = false;
|
2017-06-30 03:06:53 +00:00
|
|
|
this.settings.visible = false;
|
2017-06-30 02:31:31 +00:00
|
|
|
|
|
|
|
// If all State level managers followed the same pattern then we could just iterate
|
|
|
|
// the map and call shutdown on all of them, same for destroy
|
|
|
|
|
|
|
|
// Move to a Plugin based system? Then plugins can look-up other plugins via the State
|
|
|
|
// and store their own references to them to avoid constant look-ups.
|
|
|
|
|
|
|
|
this.children.removeAll();
|
|
|
|
|
|
|
|
this.time.shutdown();
|
|
|
|
this.tweens.shutdown();
|
|
|
|
|
2017-06-30 03:06:53 +00:00
|
|
|
if (this.state.shutdown)
|
|
|
|
{
|
|
|
|
this.state.shutdown.call(this.state);
|
|
|
|
}
|
2017-06-30 02:31:31 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// Game level nuke
|
|
|
|
destroy: function ()
|
|
|
|
{
|
|
|
|
// TODO
|
|
|
|
|
|
|
|
this.time.destroy();
|
|
|
|
this.tweens.destroy();
|
|
|
|
|
|
|
|
// etc
|
2017-06-30 03:06:53 +00:00
|
|
|
if (this.state.destroy)
|
|
|
|
{
|
|
|
|
this.state.destroy.call(this.state);
|
|
|
|
}
|
2017-02-08 00:08:09 +00:00
|
|
|
}
|
2016-11-29 15:25:14 +00:00
|
|
|
};
|
|
|
|
|
2017-06-30 02:31:31 +00:00
|
|
|
Systems.prototype.constructor = Systems;
|
|
|
|
|
2016-11-29 15:25:14 +00:00
|
|
|
module.exports = Systems;
|