var Camera = require('../camera/Camera'); var Class = require('../utils/Class'); var GetFastValue = require('../utils/object/GetFastValue'); var KeyControl = require('../camera/KeyControl'); var SmoothedKeyControl = require('../camera/SmoothedKeyControl'); var CameraManager = new Class({ initialize: function CameraManager(state){ this.state = state; this.cameras = [] ; this.cameraPool = [] ; if (state.sys.settings.cameras) { this.fromJSON(state.sys.settings.cameras); } else { this.add(); } this.main = this.cameras[0]; } , fromJSON: function (config){ if (!Array.isArray(config)) { config = [config] ; } var gameWidth = this.state.sys.game.config.width; var gameHeight = this.state.sys.game.config.height; for (var i = 0; i < (_AN_Read_length('length', config)); i++ ){ var cameraConfig = config[i]; var x = GetFastValue(cameraConfig, 'x', 0); var y = GetFastValue(cameraConfig, 'y', 0); var width = GetFastValue(cameraConfig, 'width', gameWidth); var height = GetFastValue(cameraConfig, 'height', gameHeight); var camera = this.add(x, y, width, height); camera.name = GetFastValue(cameraConfig, 'name', ''); camera.zoom = GetFastValue(cameraConfig, 'zoom', 1); camera.rotation = GetFastValue(cameraConfig, 'rotation', 0); camera.scrollX = GetFastValue(cameraConfig, 'scrollX', 0); camera.scrollY = GetFastValue(cameraConfig, 'scrollY', 0); camera.roundPixels = GetFastValue(cameraConfig, 'roundPixels', false ); var backgroundColor = GetFastValue(cameraConfig, 'backgroundColor', false ); if (backgroundColor) { camera.setBackgroundColor(backgroundColor); } var boundsConfig = GetFastValue(cameraConfig, 'bounds', null ); if (boundsConfig) { var bx = GetFastValue(boundsConfig, 'x', 0); var by = GetFastValue(boundsConfig, 'y', 0); var bwidth = GetFastValue(boundsConfig, 'width', gameWidth); var bheight = GetFastValue(boundsConfig, 'height', gameHeight); camera.setBounds(bx, by, bwidth, bheight); } } return this; } , add: function (x, y, width, height){ if (x === undefined) { x = 0; } if (y === undefined) { y = 0; } if (width === undefined) { width = this.state.sys.game.config.width; } if (height === undefined) { height = this.state.sys.game.config.height; } var camera = null ; if ((_AN_Read_length('length', this.cameraPool)) > 0) { camera = this.cameraPool.pop(); camera.setViewport(x, y, width, height); } else { camera = new Camera(x, y, width, height); } camera.setState(this.state); this.cameras.push(camera); return camera; } , addKeyControl: function (config){ return new KeyControl(config); } , addSmoothedKeyControl: function (config){ return new SmoothedKeyControl(config); } , addReference: function (camera){ var index = this.cameras.indexOf(camera); var poolIndex = this.cameraPool.indexOf(camera); if (index < 0 && poolIndex >= 0) { this.cameras.push(camera); this.cameraPool.slice(poolIndex, 1); return camera; } return null ; } , remove: function (camera){ var cameraIndex = this.cameras.indexOf(camera); if (cameraIndex >= 0) { this.cameraPool.push(this.cameras[cameraIndex]); this.cameras.splice(cameraIndex, 1); } } , resetAll: function (){ while ((_AN_Read_length('length', this.cameras)) > 0){ this.cameraPool.push(this.cameras.pop()); } this.main = this.add(); } , update: function (timestep, delta){ for (var i = 0, l = _AN_Read_length('length', this.cameras); i < l; ++i){ this.cameras[i].update(timestep, delta); } } , render: function (renderer, children, interpolation){ var cameras = this.cameras; for (var i = 0, l = _AN_Read_length('length', cameras); i < l; ++i){ var camera = cameras[i]; camera.preRender(); renderer.render(this.state, children, interpolation, camera); } } , destroy: function (){ this.main = undefined; for (var i = 0; i < (_AN_Read_length('length', this.cameras)); i++ ){ this.cameras[i].destroy(); } for (i = 0; i < (_AN_Read_length('length', this.cameraPool)); i++ ){ this.cameraPool[i].destroy(); } this.cameras = [] ; this.cameraPool = [] ; this.state = undefined; } } ); module.exports = CameraManager;