phaser/v3/src/state/Systems.js

162 lines
4.3 KiB
JavaScript
Raw Normal View History

/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var EventDispatcher = require('../events/EventDispatcher');
var GameObjectFactory = require('./systems/GameObjectFactory');
var GameObjectCreator = require('./systems/GameObjectCreator');
var Loader = require('./systems/Loader');
var UpdateManager = require('./systems/UpdateManager');
2016-12-07 02:40:07 +00:00
var Component = require('../components');
// var Camera = require('../camera/Camera');
var Settings = require('./Settings');
var RTree = require('../structs/RTree');
2017-02-07 15:47:27 +00:00
var Camera = require('../camera/Camera-2')
var Systems = function (state, config)
{
this.state = state;
this.game = null;
this.config = config;
2017-02-06 23:59:15 +00:00
this.settings = Settings.create(config);
this.x = this.settings.x;
this.y = this.settings.y;
this.mask = null;
this.canvas;
this.context;
// CORE SYSTEMS / PROPERTIES
this.cache;
this.textures;
// Reference to State specific managers (Factory, Tweens, Loader, Physics, etc)
this.add;
this.make;
this.load;
this.events;
this.updates;
this.tree;
// State properties
this.cameras;
this.children;
this.color;
this.data;
this.fbo;
this.time;
this.transform;
};
Systems.prototype.constructor = Systems;
Systems.prototype = {
init: function (game)
{
console.log('State.Systems.init');
this.game = game;
2017-02-06 23:59:15 +00:00
Settings.init(this.settings, this.game.config);
this.cache = this.game.cache;
this.textures = this.game.textures;
// State specific managers (Factory, Tweens, Loader, Physics, etc)
this.tree = RTree(16);
this.events = new EventDispatcher();
this.add = new GameObjectFactory(this.state);
this.make = GameObjectCreator(this.state);
this.updates = new UpdateManager(this.state);
this.load = new Loader(this.state);
// State specific properties (transform, data, children, etc)
// 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);
this.transform = new Component.Transform(this.state);
2017-02-07 16:12:20 +00:00
this.cameras = [];
this.cameras[0] = new Camera(0, 0, this.game.config.width, this.game.config.height);
this.inject();
},
inject: function ()
{
// Defaults properties injected into the State
this.state.game = this.game;
this.state.events = this.events;
this.state.add = this.add;
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;
this.state.settings = this.settings;
// this.state.camera = this.camera;
this.state.transform = this.transform;
this.state.state = this.game.state;
this.state.cache = this.game.cache;
this.state.textures = this.game.textures;
2017-02-07 15:47:27 +00:00
2017-02-07 16:12:20 +00:00
for (var i = 0, l = this.cameras.length; i < l; ++i)
{
this.cameras[i].setState(this.state);
}
},
// Called just once per frame, regardless of speed
begin: function (timestamp, frameDelta)
{
},
// Potentially called multiple times per frame (on super-fast systems)
update: function (timestep, physicsStep)
{
for (var c = 0; c < this.children.list.length; c++)
{
var child = this.children.list[c];
2017-01-31 17:04:31 +00:00
// if (child.exists)
// {
child.update(timestep);
2017-01-31 17:04:31 +00:00
// }
}
this.state.update(timestep, physicsStep);
},
render: function (interpolation, renderer)
{
2017-02-07 16:12:20 +00:00
var state = this.state;
var transform = this.transform;
2017-02-07 16:12:20 +00:00
var cameras = this.cameras;
for (var i = 0, l = cameras.length; i < l; ++i)
{
var camera = cameras[i];
camera.preRender();
state.camera = camera;
renderer.render(state, transform.flatRenderArray, interpolation, camera);
//state.render(interpolation);
2017-02-07 16:12:20 +00:00
camera.postRender();
}
}
};
module.exports = Systems;