Skip to content

Commit 183f5c4

Browse files
committed
CameraManager.getTotal is a new method that will return the total number of Cameras being managed, with an optional isVisible argument, that only counts visible cameras if set.
1 parent 88eb4f4 commit 183f5c4

2 files changed

Lines changed: 60 additions & 25 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* `CameraManager.currentCameraId` has been removed. IDs are assigned more intelligently now, via the `getNextID` internal method.
2424
* `CameraManager.addExisting` no longer needs to be passed a Camera that already exists in the pool (as the pool has been removed), meaning you can now create your own Cameras and pass them to `addExisting` and have them treated as normal cameras and not be ignored by the manager. They are also assigned a proper ID when added.
2525
* `CameraManager.addExisting` has a new boolean argument `makeMain` which will make the new camera the main one.
26+
* `CameraManager.getTotal` is a new method that will return the total number of Cameras being managed, with an optional `isVisible` argument, that only counts visible cameras if set.
2627

2728
### New Features
2829

@@ -60,6 +61,7 @@
6061
* Fix extra argument passing in Array.Each (thanks @samme)
6162
* TileSprite was using the Size compontent instead of ComputedSize, meaning its `getBounds` and `displayWidth` and `displayHeight` results were incorrect. Fix #3789 (thanks @jjalonso)
6263
* ArrayUtils.AddAt didn't calculate the array offset correctly if you passed an array in to be merged with an existing array. This also caused Container.addAt to fail if an array was passed to it. Fix #3788 (thanks @jjalonso)
64+
* The `Pointer.camera` property would only be set if there was a viable Game Object in the camera view. Now it is set regardless, to always be the Camera the Pointer interacted with.
6365

6466
### Examples, Documentation and TypeScript
6567

src/cameras/2d/CameraManager.js

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,47 @@ var CameraManager = new Class({
237237
return camera;
238238
},
239239

240+
/**
241+
* Adds an existing Camera into the Camera Manager.
242+
*
243+
* The Camera should either be a `Phaser.Cameras.Scene2D.Camera` instance, or a class that extends from it.
244+
*
245+
* The Camera will be assigned an ID, which is used for Game Object exclusion and then added to the
246+
* manager. As long as it doesn't already exist in the manager it will be added then returned.
247+
*
248+
* If this method returns `null` then the Camera already exists in this Camera Manager.
249+
*
250+
* @method Phaser.Cameras.Scene2D.CameraManager#addExisting
251+
* @since 3.0.0
252+
*
253+
* @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera to be added to the Camera Manager.
254+
* @param {boolean} [makeMain=false] - Set this Camera as being the 'main' camera. This just makes the property `main` a reference to it.
255+
*
256+
* @return {?Phaser.Cameras.Scene2D.Camera} The Camera that was added to the Camera Manager, or `null` if it couldn't be added.
257+
*/
258+
addExisting: function (camera, makeMain)
259+
{
260+
if (makeMain === undefined) { makeMain = false; }
261+
262+
var index = this.cameras.indexOf(camera);
263+
264+
if (index === -1)
265+
{
266+
camera.id = this.getNextID();
267+
268+
this.cameras.push(camera);
269+
270+
if (makeMain)
271+
{
272+
this.main = camera;
273+
}
274+
275+
return camera;
276+
}
277+
278+
return null;
279+
},
280+
240281
/**
241282
* Gets the next available Camera ID number.
242283
*
@@ -286,44 +327,36 @@ var CameraManager = new Class({
286327
},
287328

288329
/**
289-
* Adds an existing Camera into the Camera Manager.
290-
*
291-
* The Camera should either be a `Phaser.Cameras.Scene2D.Camera` instance, or a class that extends from it.
292-
*
293-
* The Camera will be assigned an ID, which is used for Game Object exclusion and then added to the
294-
* manager. As long as it doesn't already exist in the manager it will be added then returned.
330+
* Gets the total number of Cameras in this Camera Manager.
295331
*
296-
* If this method returns `null` then the Camera already exists in this Camera Manager.
332+
* If the optional `isVisible` argument is set it will only count Cameras that are currently visible.
297333
*
298-
* @method Phaser.Cameras.Scene2D.CameraManager#addExisting
299-
* @since 3.0.0
300-
*
301-
* @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera to be added to the Camera Manager.
302-
* @param {boolean} [makeMain=false] - Set this Camera as being the 'main' camera. This just makes the property `main` a reference to it.
334+
* @method Phaser.Cameras.Scene2D.CameraManager#getTotal
335+
* @since 3.11.0
336+
*
337+
* @param {boolean} [isVisible=false] - Set the `true` to only include visible Cameras in the total.
303338
*
304-
* @return {?Phaser.Cameras.Scene2D.Camera} The Camera that was added to the Camera Manager, or `null` if it couldn't be added.
339+
* @return {integer} The total number of Cameras in this Camera Manager.
305340
*/
306-
addExisting: function (camera, makeMain)
341+
getTotal: function (isVisible)
307342
{
308-
if (makeMain === undefined) { makeMain = false; }
343+
if (isVisible === undefined) { isVisible = false; }
309344

310-
var index = this.cameras.indexOf(camera);
345+
var total = 0;
311346

312-
if (index === -1)
313-
{
314-
camera.id = this.getNextID();
347+
var cameras = this.cameras;
315348

316-
this.cameras.push(camera);
349+
for (var i = 0; i < cameras.length; i++)
350+
{
351+
var camera = cameras[i];
317352

318-
if (makeMain)
353+
if (!isVisible || (isVisible && camera.visible))
319354
{
320-
this.main = camera;
355+
total++;
321356
}
322-
323-
return camera;
324357
}
325358

326-
return null;
359+
return total;
327360
},
328361

329362
/**

0 commit comments

Comments
 (0)