Skip to content

Commit e4f72c9

Browse files
committed
Adding camera reference and removing camera reference
1 parent 3e3b327 commit e4f72c9

2 files changed

Lines changed: 49 additions & 7 deletions

File tree

v3/src/camera/Camera-2.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ var Camera = function (x, y, width, height)
3232
this._flashAlpha = 0.0;
3333
};
3434

35+
Camera.prototype.setViewport = function (x, y, width, height)
36+
{
37+
this.x = x;
38+
this.y = y;
39+
this.width = width;
40+
this.height = height;
41+
};
42+
3543
Camera.prototype.setState = function (state)
3644
{
3745
this.state = state;

v3/src/state/Systems.js

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Systems.prototype = {
9191
this.transform = new Component.Transform(this.state);
9292

9393
this.cameras = [];
94+
this.cameraPool = [];
9495
this.mainCamera = new Camera(0, 0, this.game.config.width, this.game.config.height);
9596
this.cameras.push(this.mainCamera);
9697
this.inject();
@@ -174,20 +175,53 @@ Systems.prototype = {
174175

175176
addCamera: function (x, y, width, height)
176177
{
177-
var camera = new Camera(x, y, width, height);
178+
var camera = null;
179+
if (this.cameraPool.length > 0)
180+
{
181+
camera = this.cameraPool.pop();
182+
camera.setViewport(x, y, width, height);
183+
}
184+
else
185+
{
186+
camera = new Camera(x, y, width, height);
187+
}
178188
camera.setState(this.state);
179189
this.cameras.push(camera);
180190
return camera;
181191
},
182192

193+
addCameraReference: function (camera)
194+
{
195+
var index = this.cameras.indexOf(camera);
196+
var poolIndex = this.cameraPool.indexOf(camera);
197+
198+
if (index < 0 && poolIndex >= 0)
199+
{
200+
this.cameras.push(camera);
201+
this.cameraPool.slice(poolIndex, 1);
202+
return camera;
203+
}
204+
205+
return null;
206+
},
207+
208+
removeCamera: function (camera)
209+
{
210+
var cameraIndex = this.cameras.indexOf(camera);
211+
if (cameraIndex >= 0)
212+
{
213+
this.cameraPool.push(this.cameras[cameraIndex]);
214+
this.cameras.splice(cameraIndex, 1);
215+
}
216+
},
217+
183218
resetCameras: function ()
184219
{
185-
this.cameras.length = 1;
186-
this.mainCamera = this.cameras[0];
187-
this.mainCamera.x = 0;
188-
this.mainCamera.y = 0;
189-
this.mainCamera.width = this.game.config.width;
190-
this.mainCamera.height = this.game.config.height;
220+
while(this.cameras.length > 0)
221+
{
222+
this.cameraPool.push(this.cameras.pop());
223+
}
224+
this.mainCamera = this.addCamera(0, 0, this.game.config.width, this.game.config.height);
191225
}
192226

193227
};

0 commit comments

Comments
 (0)