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');
|
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');
|
|
|
|
var Camera = require('../camera/Camera');
|
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');
|
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-01-26 04:06:10 +00:00
|
|
|
this.settings;
|
2016-12-06 16:49:29 +00:00
|
|
|
|
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;
|
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.camera;
|
|
|
|
this.children;
|
|
|
|
this.color;
|
|
|
|
this.data;
|
|
|
|
this.fbo;
|
|
|
|
this.time;
|
2017-01-27 00:07:57 +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
|
|
|
{
|
|
|
|
console.log('State.Systems.init');
|
|
|
|
|
2017-01-25 17:10:19 +00:00
|
|
|
this.game = game;
|
2016-11-29 15:25:14 +00:00
|
|
|
|
2017-01-26 04:06:10 +00:00
|
|
|
this.settings = Settings(this.config, this.game.config);
|
|
|
|
|
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-01-25 17:10:19 +00:00
|
|
|
this.make = 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);
|
2016-11-29 15:25:14 +00:00
|
|
|
|
|
|
|
// State specific properties (transform, data, children, etc)
|
2017-01-25 17:10:19 +00:00
|
|
|
|
2017-01-26 04:06:10 +00:00
|
|
|
this.camera = new Camera(this.state, 0, 0, this.settings.width, this.settings.height);
|
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-01-27 00:07:57 +00:00
|
|
|
// this.transform = this.camera.transform;
|
2016-11-29 15:25:14 +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
|
|
|
|
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-01-25 17:10:19 +00:00
|
|
|
|
2016-12-07 02:40:07 +00:00
|
|
|
this.state.camera = this.camera;
|
2017-01-27 00:07:57 +00:00
|
|
|
// this.state.transform = this.camera.transform;
|
2017-01-26 04:06:10 +00:00
|
|
|
|
|
|
|
this.state.state = this.game.state;
|
2017-01-30 16:56:04 +00:00
|
|
|
this.state.cache = this.game.cache;
|
2017-01-26 04:06:10 +00:00
|
|
|
this.state.textures = this.game.textures;
|
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-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-01-25 17:10:19 +00:00
|
|
|
for (var c = 0; c < this.children.list.length; c++)
|
|
|
|
{
|
|
|
|
var child = this.children.list[c];
|
2016-11-29 15:25:14 +00:00
|
|
|
|
2017-01-25 17:10:19 +00:00
|
|
|
if (child.exists)
|
|
|
|
{
|
|
|
|
child.update(timestep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.state.update(timestep, physicsStep);
|
2016-11-29 15:25:14 +00:00
|
|
|
},
|
|
|
|
|
2017-01-25 17:10:19 +00:00
|
|
|
// Called just once per frame, regardless of speed
|
2017-01-26 04:06:10 +00:00
|
|
|
render: function (interpolation, renderer)
|
2016-11-29 15:25:14 +00:00
|
|
|
{
|
2017-01-25 17:10:19 +00:00
|
|
|
this.updates.start();
|
2016-11-29 15:25:14 +00:00
|
|
|
|
2017-01-25 17:10:19 +00:00
|
|
|
if (this.settings.visible && this.color.alpha !== 0)
|
|
|
|
{
|
2017-01-26 04:06:10 +00:00
|
|
|
var list = this.tree.search({
|
|
|
|
minX: this.camera.x,
|
|
|
|
minY: this.camera.y,
|
|
|
|
maxX: this.camera.right,
|
|
|
|
maxY: this.camera.bottom
|
|
|
|
});
|
|
|
|
|
|
|
|
renderer.render(this.state, list, interpolation);
|
2016-11-29 15:25:14 +00:00
|
|
|
}
|
2017-01-25 17:10:19 +00:00
|
|
|
|
|
|
|
this.updates.stop();
|
|
|
|
|
2017-01-26 04:06:10 +00:00
|
|
|
this.state.render(interpolation);
|
2016-11-29 15:25:14 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Systems;
|