var Class = require('../../utils/Class'); var Light = require('./Light'); var Utils = require('../../renderer/webgl/Utils'); var LightsManager = new Class({ initialize: function LightsManager(){ this.lightPool = [] ; this.lights = [] ; this.culledLights = [] ; this.ambientColor = { r: 0.1, g: 0.1, b: 0.1} ; this.active = false ; this.maxLights = -1; } , enable: function (){ if (this.maxLights === -1) { this.maxLights = this.scene.sys.game.renderer.config.maxLights; } this.active = true ; return this; } , disable: function (){ this.active = false ; return this; } , cull: function (camera){ var lights = this.lights; var culledLights = this.culledLights; var length = _AN_Read_length('length', lights); var cameraCenterX = camera.x + camera.width / 2; var cameraCenterY = camera.y + camera.height / 2; var cameraRadius = (camera.width + camera.height) / 2; var point = { x: 0, y: 0} ; var cameraMatrix = camera.matrix; var viewportHeight = this.systems.game.renderer.height; culledLights.length = 0; for (var i = 0; i < length && (_AN_Read_length('length', culledLights)) < this.maxLights; i++ ){ var light = lights[i]; cameraMatrix.transformPoint(light.x, light.y, point); var dx = cameraCenterX - (point.x - (camera.scrollX * light.scrollFactorX * camera.zoom)); var dy = cameraCenterY - (viewportHeight - (point.y - (camera.scrollY * light.scrollFactorY) * camera.zoom)); var distance = Math.sqrt(dx * dx + dy * dy); if (distance < light.radius + cameraRadius) { culledLights.push(lights[i]); } } return culledLights; } , forEachLight: function (callback){ if (!callback) { return ; } var lights = this.lights; var length = _AN_Read_length('length', lights); for (var i = 0; i < length; i++ ){ callback(lights[i]); } return this; } , setAmbientColor: function (rgb){ var color = Utils.getFloatsFromUintRGB(rgb); this.ambientColor.r = color[0]; this.ambientColor.g = color[1]; this.ambientColor.b = color[2]; return this; } , getMaxVisibleLights: function (){ return 10; } , getLightCount: function (){ return (_AN_Read_length('length', this.lights)); } , addLight: function (x, y, radius, rgb, intensity){ if (x === undefined) { x = 0; } if (y === undefined) { y = 0; } if (radius === undefined) { radius = 100; } if (rgb === undefined) { rgb = 16777215; } if (intensity === undefined) { intensity = 1; } var color = null ; var light = null ; color = Utils.getFloatsFromUintRGB(rgb); light = null ; var pool = this.lightPool; if ((_AN_Read_length('length', pool)) > 0) { light = pool.pop(); light.set(x, y, radius, color[0], color[1], color[2], intensity); } else { light = new Light(x, y, radius, color[0], color[1], color[2], intensity); } this.lights.push(light); return light; } , removeLight: function (light){ var index = this.lights.indexOf(light); if (index >= 0) { this.lightPool.push(light); this.lights.splice(index, 1); } return this; } , shutdown: function (){ while ((_AN_Read_length('length', this.lights)) > 0){ this.lightPool.push(this.lights.pop()); } this.ambientColor = { r: 0.1, g: 0.1, b: 0.1} ; this.culledLights.length = 0; this.lights.length = 0; } , destroy: function (){ this.shutdown(); this.lightPool.length = 0; } } ); module.exports = LightsManager;