phaser/v3/src/state/Systems.js

263 lines
6 KiB
JavaScript
Raw Normal View History

var CameraManager = require('../plugins/CameraManager');
var Class = require('../utils/Class');
2017-06-28 16:17:54 +00:00
var Clock = require('../time/Clock');
var Data = require('../plugins/Data');
var DisplayList = require('../plugins/DisplayList');
var EventDispatcher = require('../events/EventDispatcher');
var GameObjectCreator = require('../plugins/GameObjectCreator');
var GameObjectFactory = require('../plugins/GameObjectFactory');
var Loader = require('../plugins/Loader');
var Settings = require('./Settings');
2017-03-23 19:51:02 +00:00
var StableSort = require('../utils/array/StableSort');
var StateManager = require('../plugins/StateManager');
var TweenManager = require('../tween/TweenManager');
var Systems = new Class({
initialize:
function Systems (state, config)
{
this.state = state;
this.config = config;
this.settings = Settings.create(config);
this.sortChildrenFlag = false;
// Set by the GlobalStateManager
this.mask = null;
this.canvas;
this.context;
// CORE (GLOBAL) SYSTEMS / PROPERTIES
this.game;
this.anims;
this.cache;
this.input;
this.registry;
this.textures;
// Reference to State specific managers (Factory, Tweens, Loader, Physics, etc)
this.add;
this.cameras;
this.events;
this.load;
this.make;
this.stateManager;
this.time;
this.tweens;
// State properties
this.children;
this.data;
},
init: function (game)
{
var state = this.state;
this.game = game;
// Game (Global) level managers
this.anims = game.anims;
this.cache = game.cache;
this.input = game.input;
this.registry = game.registry;
this.textures = game.textures;
// State specific properties (transform, data, children, etc)
this.children = new DisplayList(state);
this.data = new Data(state);
// State specific managers (Factory, Tweens, Loader, Physics, etc)
this.add = new GameObjectFactory(state);
this.cameras = new CameraManager(state);
this.events = new EventDispatcher();
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);
this.inject();
},
inject: function ()
{
var map = this.settings.map;
for (var key in map)
{
if (key === 'sys')
{
continue;
}
this.state[map[key]] = this[key];
}
},
step: function (time, delta)
{
2017-06-30 03:09:19 +00:00
// Are there any pending StateManager actions?
this.stateManager.update();
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);
var list = this.children.list;
for (var i = 0; i < list.length; i++)
{
list[i].preUpdate(time, delta);
}
// 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);
this.cameras.update(time, delta);
this.state.update.call(this.state, time, delta);
},
render: function (interpolation, renderer)
{
if (!this.settings.visible)
{
return;
}
if (this.sortChildrenFlag)
{
StableSort.inplace(this.children.list, this.sortZ);
this.sortChildrenFlag = false;
}
this.cameras.render(renderer, this.children, interpolation);
},
sortZ: function (childA, childB)
{
return childA._z - childB._z;
},
// 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);
}
},
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);
}
},
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);
}
},
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;
},
shutdown: function ()
{
// Was stopped by the GlobalStateManager
this.settings.active = false;
2017-06-30 03:06:53 +00:00
this.settings.visible = false;
// 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);
}
},
// 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
}
});
module.exports = Systems;