Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exclusive camera feature #6379

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/cameras/2d/BaseCamera.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,18 @@ var BaseCamera = new Class({
* @since 3.60.0
*/
this.isSceneCamera = true;

/**
* Exclusive camera will ignore all game objects,
* only accepted game objects will be rendered.
*
* @name Phaser.Cameras.Scene2D.BaseCamera#exclusive
* @type {boolean}
* @default false
* @since 3.60.0
*/
this.exclusive = false;

},

/**
Expand Down Expand Up @@ -912,6 +924,65 @@ var BaseCamera = new Class({
return this;
},

/**
* Enable exclusive camera, only accepted game objects will be rendered.
*
* @method Phaser.Cameras.Scene2D.BaseCamera#setExclusiveEnable
* @since 3.60.0
*
* @return {this} This Camera instance.
*/
setExclusive: function()
{
this.exclusive = true;

return this;
},

/**
* For exclusive camera, given a Game Object, or an array of Game Objects, it will update all of their camera filter2 settings
* so that they are accepted by this Camera. This means they will be rendered by this Camera.
*
* @method Phaser.Cameras.Scene2D.BaseCamera#accept
* @since 3.60.0
*
* @param {(Phaser.GameObjects.GameObject|Phaser.GameObjects.GameObject[]|Phaser.GameObjects.Group)} entries - The Game Object, or array of Game Objects, to be ignored by this Camera.
*
* @return {this} This Camera instance.
*/
accept: function (entries)
{
var id = this.id;

if (!Array.isArray(entries))
{
entries = [ entries ];
}

for (var i = 0; i < entries.length; i++)
{
var entry = entries[i];

if (Array.isArray(entry))
{
this.ignore(entry);
}
else if (entry.isParent)
{
this.ignore(entry.getChildren());
}
else
{
entry.cameraFilter2 |= id;

// Don't render by all other cameras
entry.cameraFilter = 0xffffffff;
}
}

return this;
},

/**
* Internal preRender step.
*
Expand Down
1 change: 1 addition & 0 deletions src/cameras/2d/CameraManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ var CameraManager = new Class({
camera.scrollY = GetFastValue(cameraConfig, 'scrollY', 0);
camera.roundPixels = GetFastValue(cameraConfig, 'roundPixels', false);
camera.visible = GetFastValue(cameraConfig, 'visible', true);
camera.exclusive = GetFastValue(cameraConfig, 'exclusive', false);

// Background Color

Expand Down
2 changes: 2 additions & 0 deletions src/cameras/2d/typedefs/JSONCamera.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* @property {number} zoom - The zoom of camera
* @property {number} rotation - The rotation of camera
* @property {boolean} roundPixels - The round pixels st status of camera
* @property {boolean} visible - Visible of camera
* @property {boolean} exclusive - The exclusive flag of camera
* @property {number} scrollX - The horizontal scroll of camera
* @property {number} scrollY - The vertical scroll of camera
* @property {string} backgroundColor - The background color of camera
Expand Down
34 changes: 33 additions & 1 deletion src/gameobjects/GameObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,21 @@ var GameObject = new Class({
*/
this.cameraFilter = 0;

/**
* A bitmask that controls if this Game Object is drawn by an exclusive Camera or not.
* Not usually set directly, instead call `Camera.accept`, however you can
* set this property directly using the Camera.id property:
*
* @example
* this.cameraFilter2 |= camera.id
*
* @name Phaser.GameObjects.GameObject#cameraFilter2
* @type {number}
* @default 0
* @since 3.60.0
*/
this.cameraFilter2 = 0;

/**
* If this Game Object is enabled for input then this property will contain an InteractiveObject instance.
* Not usually set directly. Instead call `GameObject.setInteractive()`.
Expand Down Expand Up @@ -622,7 +637,24 @@ var GameObject = new Class({
{
var listWillRender = (this.displayList && this.displayList.active) ? this.displayList.willRender(camera) : true;

return !(!listWillRender || GameObject.RENDER_MASK !== this.renderFlags || (this.cameraFilter !== 0 && (this.cameraFilter & camera.id)));
if (!listWillRender)
{
return false;
}

if (GameObject.RENDER_MASK !== this.renderFlags)
{
return false;
}

if (!camera.exclusive)
{
return (this.cameraFilter === 0) || ((this.cameraFilter & camera.id) === 0);
}
else
{
return (this.cameraFilter2 !== 0) && ((this.cameraFilter2 & camera.id) !== 0);
}
},

/**
Expand Down