Skip to content

Commit f2b7fd0

Browse files
committed
Removed the cameraX properties because they fall out of sync on camera remove
1 parent da2b91b commit f2b7fd0

2 files changed

Lines changed: 58 additions & 218 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
* `Camera.deadzone` (and its related method `Camera.setDeadzone`) allows you to specify the deadzone for a camera. The deadzone is a rectangular region used when a camera is following a target. If the target is within the deadzone then the camera will not scroll. As soon as the target leaves the deadzone, the camera will begin tracking it (applying lerp if needed.) It allows you to set a region of the camera in which a player can move freely before tracking begins. The deadzone is re-centered on the camera mid point every frame, meaning you can also use the rectangle for other in-game checks as needed.
1010
* `Camera.pan` is a new Camera Effect that allows you to control automatic camera pans between points in your game world. You can specify a duration and ease type for the pan, and it'll emit events just like all other camera effects, so you can hook into the start, update and completion of the pan. See the examples and docs for more details.
1111
* `Camera.zoom` is a new Camera Effect that allows you to control automatic camera zooming. You can specify a duration and ease type for the zoom, as well as the zoom factor of course, and it'll emit events just like all other camera effects, so you can hook into the start, update and completion of the zoom. Used in combination with the new Pan effect you can zoom and pan around with ease. See the examples and docs for more details.
12-
* The Camera Manager has 10 new read-only properties: `camera1`, `camera2` and so on, up to `camera10` so you can now quickly access the first 10 cameras created in the Camera Manager without needing to hold your own references too.
1312
* `Camera.midPoint` is a new Vec2 property that is updated every frame. Use it to obtain exactly where in the world the center of the camera is currently looking.
1413
* `Camera.displayWidth` is a new property that returns the display width of the camera, factoring in the current zoom level.
1514
* `Camera.displayHeight` is a new property that returns the display height of the camera, factoring in the current zoom level.
1615
* `Camera.centerOn` is a new method that will move the camera so its viewport is centered on the given coordinates. A handy way of jumping to different points around a map without needing to calculate the scroll offsets.
1716
* The Camera bounds didn't factor in the camera zoom properly, meaning you would often not be able to reach the corners of a camera bound world at a zoom level other than 1. The bounds are now calculated each frame to ensure they match the zoom level and it will no longer allow you to scroll off the edge of the bounds. Fix #3547 (thanks @nkholski)
1817
* `Camera.centerToBounds` didn't take the bounds offset into account, so bounds at non-zero positions wouldn't center properly. All bounds now center correctly. Fix #3706 (thanks @cyantree)
1918
* `Camera.setBounds` has a new optional argument `centerOn`. If specified it will automatically center the camera on the new bounds given.
19+
* The Camera will no longer stutter when following Game Objects at high zoom levels.
2020
* `Camera._id` has been renamed to `Camera.id`, a read-only bitmask used for camera exclusion from Game Objects.
2121
* The Camera Manager `cameraPool` has been removed entirely. It was mostly pointless in practise as Cameras are not regenerated frequently enough to need pooling. It also didn't maintain the bitmask list correctly before.
2222
* `CameraManager.resetAll` now destroys all current Cameras, resets the camera ID marker to 1 and adds a single new Camera.
23-
* `CameraManager.currentCameraId` has been renamed to `nextID` and marked as read-only.
23+
* `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.
2626

src/cameras/2d/CameraManager.js

Lines changed: 56 additions & 216 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ var RectangleContains = require('../../geom/rectangle/Contains');
3838
* By default you can access the Camera Manager from within a Scene using `this.cameras`, although this can be changed
3939
* in your game config.
4040
*
41-
* The Camera Manager can manage up to 31 unique Cameras. You can use the properties `camera1` through to `camera10`
42-
* for quick and easy access to the first 10 cameras you define.
43-
*
4441
* Create new Cameras using the `add` method. Or extend the Camera class with your own addition code and then add
4542
* the new Camera in using the `addExisting` method.
4643
*
@@ -58,9 +55,12 @@ var RectangleContains = require('../../geom/rectangle/Contains');
5855
* viewport, and changing the viewport has no impact on the scrolling.
5956
*
6057
* By default a Camera will render all Game Objects it can see. You can change this using the `ignore` method,
61-
* allowing you to filter Game Objects out on a per-Camera basis.
58+
* allowing you to filter Game Objects out on a per-Camera basis. The Camera Manager can manage up to 31 unique
59+
* 'Game Object ignore capable' Cameras. Any Cameras beyond 31 that you create will all be given a Camera ID of
60+
* zero, meaning that they cannot be used for Game Object exclusion. This means if you need your Camera to ignore
61+
* Game Objects, make sure it's one of the first 31 created.
6262
*
63-
* A Camera also has built-in special effects including Fade, Flash and Camera Shake.
63+
* A Camera also has built-in special effects including Fade, Flash, Camera Shake, Pan and Zoom.
6464
*
6565
* @class CameraManager
6666
* @memberOf Phaser.Cameras.Scene2D
@@ -93,18 +93,6 @@ var CameraManager = new Class({
9393
*/
9494
this.systems = scene.sys;
9595

96-
/**
97-
* The ID that will be assigned to the next Camera that is created.
98-
* This is a bitmask value, meaning only up to 31 cameras can be created in total.
99-
*
100-
* @name Phaser.Cameras.Scene2D.CameraManager#nextID
101-
* @type {integer}
102-
* @default 1
103-
* @readOnly
104-
* @since 3.0.0
105-
*/
106-
this.nextID = 1;
107-
10896
/**
10997
* An Array of the Camera objects being managed by this Camera Manager.
11098
* The Cameras are updated and rendered in the same order in which they appear in this array.
@@ -237,18 +225,64 @@ var CameraManager = new Class({
237225
camera.setName(name);
238226
camera.setScene(this.scene);
239227

228+
camera.id = this.getNextID();
229+
240230
this.cameras.push(camera);
241231

242232
if (makeMain)
243233
{
244234
this.main = camera;
245235
}
246236

247-
camera.id = this.nextID;
237+
return camera;
238+
},
248239

249-
this.nextID = this.nextID << 1;
240+
/**
241+
* Gets the next available Camera ID number.
242+
*
243+
* The Camera Manager supports up to 31 unique cameras, after which the ID returned will always be zero.
244+
* You can create additional cameras beyond 31, but they cannot be used for Game Object exclusion.
245+
*
246+
* @method Phaser.Cameras.Scene2D.CameraManager#getNextID
247+
* @private
248+
* @since 3.11.0
249+
*
250+
* @return {number} The next available Camera ID, or 0 if they're all already in use.
251+
*/
252+
getNextID: function ()
253+
{
254+
var cameras = this.cameras;
250255

251-
return camera;
256+
var testID = 1;
257+
258+
// Find the first free camera ID we can use
259+
260+
for (var t = 0; t < 32; t++)
261+
{
262+
var found = false;
263+
264+
for (var i = 0; i < cameras.length; i++)
265+
{
266+
var camera = cameras[i];
267+
268+
if (camera && camera.id === testID)
269+
{
270+
found = true;
271+
continue;
272+
}
273+
}
274+
275+
if (found)
276+
{
277+
testID = testID << 1;
278+
}
279+
else
280+
{
281+
return testID;
282+
}
283+
}
284+
285+
return 0;
252286
},
253287

254288
/**
@@ -277,11 +311,9 @@ var CameraManager = new Class({
277311

278312
if (index === -1)
279313
{
280-
this.cameras.push(camera);
281-
282-
camera.id = this.nextID;
314+
camera.id = this.getNextID();
283315

284-
this.nextID = this.nextID << 1;
316+
this.cameras.push(camera);
285317

286318
if (makeMain)
287319
{
@@ -509,8 +541,6 @@ var CameraManager = new Class({
509541

510542
this.cameras = [];
511543

512-
this.nextID = 1;
513-
514544
this.main = this.add();
515545

516546
return this.main;
@@ -592,196 +622,6 @@ var CameraManager = new Class({
592622

593623
this.scene = null;
594624
this.systems = null;
595-
},
596-
597-
/**
598-
* A reference to Camera 1 in the Camera Manager.
599-
*
600-
* Create additional cameras using the `add` method.
601-
*
602-
* @name Phaser.Cameras.Scene2D.CameraManager#camera1
603-
* @type {Phaser.Cameras.Scene2D.Camera}
604-
* @readOnly
605-
* @since 3.11.0
606-
*/
607-
camera1: {
608-
609-
get: function ()
610-
{
611-
return this.cameras[0];
612-
}
613-
614-
},
615-
616-
/**
617-
* A reference to Camera 2 in the Camera Manager.
618-
*
619-
* This will be `undefined` by default unless you have created new cameras via `add` or `addExisting`.
620-
*
621-
* @name Phaser.Cameras.Scene2D.CameraManager#camera2
622-
* @type {Phaser.Cameras.Scene2D.Camera}
623-
* @readOnly
624-
* @since 3.11.0
625-
*/
626-
camera2: {
627-
628-
get: function ()
629-
{
630-
return this.cameras[1];
631-
}
632-
633-
},
634-
635-
/**
636-
* A reference to Camera 3 in the Camera Manager.
637-
*
638-
* This will be `undefined` by default unless you have created new cameras via `add` or `addExisting`.
639-
*
640-
* @name Phaser.Cameras.Scene2D.CameraManager#camera3
641-
* @type {Phaser.Cameras.Scene2D.Camera}
642-
* @readOnly
643-
* @since 3.11.0
644-
*/
645-
camera3: {
646-
647-
get: function ()
648-
{
649-
return this.cameras[2];
650-
}
651-
652-
},
653-
654-
/**
655-
* A reference to Camera 4 in the Camera Manager.
656-
*
657-
* This will be `undefined` by default unless you have created new cameras via `add` or `addExisting`.
658-
*
659-
* @name Phaser.Cameras.Scene2D.CameraManager#camera4
660-
* @type {Phaser.Cameras.Scene2D.Camera}
661-
* @readOnly
662-
* @since 3.11.0
663-
*/
664-
camera4: {
665-
666-
get: function ()
667-
{
668-
return this.cameras[3];
669-
}
670-
671-
},
672-
673-
/**
674-
* A reference to Camera 5 in the Camera Manager.
675-
*
676-
* This will be `undefined` by default unless you have created new cameras via `add` or `addExisting`.
677-
*
678-
* @name Phaser.Cameras.Scene2D.CameraManager#camera5
679-
* @type {Phaser.Cameras.Scene2D.Camera}
680-
* @readOnly
681-
* @since 3.11.0
682-
*/
683-
camera5: {
684-
685-
get: function ()
686-
{
687-
return this.cameras[4];
688-
}
689-
690-
},
691-
692-
/**
693-
* A reference to Camera 6 in the Camera Manager.
694-
*
695-
* This will be `undefined` by default unless you have created new cameras via `add` or `addExisting`.
696-
*
697-
* @name Phaser.Cameras.Scene2D.CameraManager#camera6
698-
* @type {Phaser.Cameras.Scene2D.Camera}
699-
* @readOnly
700-
* @since 3.11.0
701-
*/
702-
camera6: {
703-
704-
get: function ()
705-
{
706-
return this.cameras[5];
707-
}
708-
709-
},
710-
711-
/**
712-
* A reference to Camera 7 in the Camera Manager.
713-
*
714-
* This will be `undefined` by default unless you have created new cameras via `add` or `addExisting`.
715-
*
716-
* @name Phaser.Cameras.Scene2D.CameraManager#camera7
717-
* @type {Phaser.Cameras.Scene2D.Camera}
718-
* @readOnly
719-
* @since 3.11.0
720-
*/
721-
camera7: {
722-
723-
get: function ()
724-
{
725-
return this.cameras[6];
726-
}
727-
728-
},
729-
730-
/**
731-
* A reference to Camera 8 in the Camera Manager.
732-
*
733-
* This will be `undefined` by default unless you have created new cameras via `add` or `addExisting`.
734-
*
735-
* @name Phaser.Cameras.Scene2D.CameraManager#camera8
736-
* @type {Phaser.Cameras.Scene2D.Camera}
737-
* @readOnly
738-
* @since 3.11.0
739-
*/
740-
camera8: {
741-
742-
get: function ()
743-
{
744-
return this.cameras[7];
745-
}
746-
747-
},
748-
749-
/**
750-
* A reference to Camera 9 in the Camera Manager.
751-
*
752-
* This will be `undefined` by default unless you have created new cameras via `add` or `addExisting`.
753-
*
754-
* @name Phaser.Cameras.Scene2D.CameraManager#camera9
755-
* @type {Phaser.Cameras.Scene2D.Camera}
756-
* @readOnly
757-
* @since 3.11.0
758-
*/
759-
camera9: {
760-
761-
get: function ()
762-
{
763-
return this.cameras[8];
764-
}
765-
766-
},
767-
768-
/**
769-
* A reference to Camera 10 in the Camera Manager.
770-
*
771-
* This will be `undefined` by default unless you have created new cameras via `add` or `addExisting`.
772-
*
773-
* @name Phaser.Cameras.Scene2D.CameraManager#camera10
774-
* @type {Phaser.Cameras.Scene2D.Camera}
775-
* @readOnly
776-
* @since 3.11.0
777-
*/
778-
camera10: {
779-
780-
get: function ()
781-
{
782-
return this.cameras[9];
783-
}
784-
785625
}
786626

787627
});

0 commit comments

Comments
 (0)