Adding camera reference and removing camera reference

This commit is contained in:
Felipe Alfonso 2017-02-10 10:49:18 -03:00
parent 3e3b327bda
commit e4f72c99c8
2 changed files with 49 additions and 7 deletions

View file

@ -32,6 +32,14 @@ var Camera = function (x, y, width, height)
this._flashAlpha = 0.0;
};
Camera.prototype.setViewport = function (x, y, width, height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
};
Camera.prototype.setState = function (state)
{
this.state = state;

View file

@ -91,6 +91,7 @@ Systems.prototype = {
this.transform = new Component.Transform(this.state);
this.cameras = [];
this.cameraPool = [];
this.mainCamera = new Camera(0, 0, this.game.config.width, this.game.config.height);
this.cameras.push(this.mainCamera);
this.inject();
@ -174,20 +175,53 @@ Systems.prototype = {
addCamera: function (x, y, width, height)
{
var camera = new Camera(x, y, width, height);
var camera = null;
if (this.cameraPool.length > 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;
},
addCameraReference: 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;
},
removeCamera: function (camera)
{
var cameraIndex = this.cameras.indexOf(camera);
if (cameraIndex >= 0)
{
this.cameraPool.push(this.cameras[cameraIndex]);
this.cameras.splice(cameraIndex, 1);
}
},
resetCameras: function ()
{
this.cameras.length = 1;
this.mainCamera = this.cameras[0];
this.mainCamera.x = 0;
this.mainCamera.y = 0;
this.mainCamera.width = this.game.config.width;
this.mainCamera.height = this.game.config.height;
while(this.cameras.length > 0)
{
this.cameraPool.push(this.cameras.pop());
}
this.mainCamera = this.addCamera(0, 0, this.game.config.width, this.game.config.height);
}
};