phaser/src/scene/Systems.js

283 lines
6.8 KiB
JavaScript
Raw Normal View History

// var CameraManager = require('../../camera/local/CameraManager');
var Class = require('../../utils/Class');
var Clock = require('../../time/Clock');
var Data = require('../../data/Data');
var DataStore = require('../../data/DataStore');
var DisplayList = require('../plugins/DisplayList');
var EventEmitter = require('eventemitter3');
var GameObjectCreator = require('../plugins/GameObjectCreator');
var GameObjectFactory = require('../plugins/GameObjectFactory');
2018-01-16 16:14:21 +00:00
var InputManager = require('../../input/InputPlugin');
var Loader = require('../plugins/Loader');
var PhysicsManager = require('../plugins/PhysicsManager');
var SceneManager = require('../plugins/SceneManager');
var Settings = require('./Settings');
2017-10-12 14:14:34 +00:00
var TweenManager = require('../../tweens/manager/TweenManager');
var UpdateList = require('../plugins/UpdateList');
2018-01-16 02:08:22 +00:00
var TestPlugin = require('../../plugins/TestPlugin');
var Systems = new Class({
initialize:
function Systems (scene, config)
{
this.scene = scene;
this.game;
this.config = config;
this.settings = Settings.create(config);
2018-01-16 02:08:22 +00:00
// Set by the GlobalSceneManager - a reference to the Scene canvas / context
this.canvas;
this.context;
// Global Systems - these are global managers (belonging to Game)
this.anims;
this.cache;
this.registry;
2018-01-11 14:48:43 +00:00
this.sound;
this.textures;
// These are core Scene plugins, needed by lots of the global systems (and each other)
2018-01-16 02:08:22 +00:00
this.add;
this.cameras;
this.displayList;
this.events;
2018-01-16 02:08:22 +00:00
this.make;
this.sceneManager;
this.time;
this.updateList;
},
init: function (game)
{
var scene = this.scene;
this.game = game;
// Global Systems - these are global managers (belonging to Game)
this.anims = game.anims;
this.cache = game.cache;
this.registry = game.registry;
2018-01-11 14:48:43 +00:00
this.sound = game.sound;
this.textures = game.textures;
// These are core Scene plugins, needed by lots of the global systems (and each other)
this.events = new EventEmitter();
2018-01-16 02:08:22 +00:00
game.plugins.install(scene, [ 'displayList', 'updateList', 'sceneManager', 'time', 'cameras', 'add', 'make' ]);
// Optional Scene plugins - not referenced by core systems, can be overridden with user code
2018-01-16 02:08:22 +00:00
// game.plugins.install(scene, [ , 'test' ]);
2017-09-08 01:41:00 +00:00
this.data = new Data(scene);
this.dataStore = new DataStore(scene);
this.inputManager = new InputManager(scene);
this.load = new Loader(scene);
this.physicsManager = new PhysicsManager(scene);
this.tweens = new TweenManager(scene);
// Sometimes the managers need access to a system created after them
2018-01-16 02:08:22 +00:00
this.events.emit('boot', this);
this.inputManager.boot();
this.physicsManager.boot();
2018-01-16 02:08:22 +00:00
this.inject2();
},
inject: function (plugin)
{
var map = this.settings.map;
if (plugin.mapping && map.hasOwnProperty(plugin.mapping))
{
this.scene[plugin.mapping] = plugin;
}
},
2018-01-16 02:08:22 +00:00
inject2: function ()
{
var map = this.settings.map;
for (var key in map)
{
if (key === 'sys')
{
continue;
}
this.scene[map[key]] = this[key];
}
},
step: function (time, delta)
{
// Are there any pending SceneManager actions?
2018-01-16 02:08:22 +00:00
// This plugin is a special case, as it can literally modify this Scene, so we update it directly.
this.sceneManager.update();
2017-06-30 03:09:19 +00:00
if (!this.settings.active)
{
return;
}
2018-01-16 02:08:22 +00:00
this.events.emit('preupdate', time, delta);
2017-09-26 01:17:31 +00:00
2017-05-17 03:37:30 +00:00
this.tweens.begin(time);
this.inputManager.begin(time);
2018-01-16 02:08:22 +00:00
this.events.emit('update', time, delta);
this.physicsManager.update(time, delta);
2017-05-17 03:37:30 +00:00
this.tweens.update(time, delta);
this.inputManager.update(time, delta);
this.scene.update.call(this.scene, time, delta);
2017-11-08 17:18:10 +00:00
2018-01-16 02:08:22 +00:00
this.events.emit('postupdate', time, delta);
2017-11-08 17:18:10 +00:00
this.physicsManager.postUpdate();
},
render: function (interpolation, renderer)
{
if (!this.settings.visible)
{
return;
}
var displayList = this.displayList;
2017-12-15 04:07:32 +00:00
displayList.process();
this.cameras.render(renderer, displayList, interpolation);
},
// Force a sort of the display list on the next render
queueDepthSort: function ()
{
this.displayList.queueDepthSort();
},
// Immediately sorts the display list if the flag is set
depthSort: function ()
{
this.displayList.depthSort();
},
// A paused Scene still renders, it just doesn't run ANY of its update handlers or systems
pause: function ()
{
// Was paused by the GlobalSceneManager
this.settings.active = false;
if (this.scene.pause)
2017-06-30 03:06:53 +00:00
{
this.scene.pause.call(this.scene);
2017-06-30 03:06:53 +00:00
}
},
resume: function ()
{
// Was resumed by the GlobalSceneManager
this.settings.active = true;
if (this.scene.resume)
2017-06-30 03:06:53 +00:00
{
this.scene.resume.call(this.scene);
2017-06-30 03:06:53 +00:00
}
},
sleep: function ()
{
// Was sent to sleep by the GlobalSceneManager
this.settings.active = false;
this.settings.visible = false;
if (this.scene.sleep)
2017-06-30 03:06:53 +00:00
{
this.scene.sleep.call(this.scene);
2017-06-30 03:06:53 +00:00
}
},
wake: function ()
{
// Was woken up by the GlobalSceneManager
this.settings.active = true;
this.settings.visible = true;
if (this.scene.wake)
2017-06-30 03:06:53 +00:00
{
this.scene.wake.call(this.scene);
2017-06-30 03:06:53 +00:00
}
},
start: function (data)
{
// Was started by the GlobalSceneManager
2017-06-30 03:06:53 +00:00
this.settings.data = data;
this.settings.active = true;
this.settings.visible = true;
},
shutdown: function ()
{
// Was stopped by the GlobalSceneManager
this.settings.active = false;
2017-06-30 03:06:53 +00:00
this.settings.visible = false;
2018-01-16 02:08:22 +00:00
this.events.emit('shutdown', this);
// this.displayList.shutdown();
// this.updateList.shutdown();
// this.time.shutdown();
this.tweens.shutdown();
this.physicsManager.shutdown();
if (this.scene.shutdown)
2017-06-30 03:06:53 +00:00
{
this.scene.shutdown.call(this.scene);
2017-06-30 03:06:53 +00:00
}
},
// TODO: Game level nuke
destroy: function ()
{
2018-01-16 02:08:22 +00:00
this.events.emit('destroy', this);
this.add.destroy();
2018-01-16 02:08:22 +00:00
// this.time.destroy();
this.tweens.destroy();
this.physicsManager.destroy();
// etc
if (this.scene.destroy)
2017-06-30 03:06:53 +00:00
{
this.scene.destroy.call(this.scene);
2017-06-30 03:06:53 +00:00
}
2017-02-08 00:08:09 +00:00
}
});
module.exports = Systems;