From e736811d5ebd1bf22a9b3ced4fd75c77576f73da Mon Sep 17 00:00:00 2001 From: Will Eastcott Date: Wed, 18 Dec 2024 15:03:20 +0000 Subject: [PATCH] Minor refactor of CameraComponent (#7209) * Minor refactor of CameraComponent * Lint fixes --- src/framework/components/camera/component.js | 36 ++++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/framework/components/camera/component.js b/src/framework/components/camera/component.js index c04ee45f20f..8d70aaf2339 100644 --- a/src/framework/components/camera/component.js +++ b/src/framework/components/camera/component.js @@ -645,21 +645,23 @@ class CameraComponent extends Component { * @type {number[]} */ set layers(newValue) { - const layers = this._camera.layers; - for (let i = 0; i < layers.length; i++) { - const layer = this.system.app.scene.layers.getLayerById(layers[i]); - if (!layer) continue; - layer.removeCamera(this); - } + const oldLayers = this._camera.layers; + const scene = this.system.app.scene; - this._camera.layers = newValue; + // Remove from old layers + oldLayers.forEach((layerId) => { + const layer = scene.layers.getLayerById(layerId); + layer?.removeCamera(this); + }); - if (!this.enabled || !this.entity.enabled) return; + this._camera.layers = newValue; - for (let i = 0; i < newValue.length; i++) { - const layer = this.system.app.scene.layers.getLayerById(newValue[i]); - if (!layer) continue; - layer.addCamera(this); + // Only add to new layers if enabled + if (this.enabled && this.entity.enabled) { + newValue.forEach((layerId) => { + const layer = scene.layers.getLayerById(layerId); + layer?.addCamera(this); + }); } } @@ -1033,9 +1035,8 @@ class CameraComponent extends Component { */ screenToWorld(screenx, screeny, cameraz, worldCoord) { const device = this.system.app.graphicsDevice; - const w = device.clientRect.width; - const h = device.clientRect.height; - return this._camera.screenToWorld(screenx, screeny, cameraz, w, h, worldCoord); + const { width, height } = device.clientRect; + return this._camera.screenToWorld(screenx, screeny, cameraz, width, height, worldCoord); } /** @@ -1047,9 +1048,8 @@ class CameraComponent extends Component { */ worldToScreen(worldCoord, screenCoord) { const device = this.system.app.graphicsDevice; - const w = device.clientRect.width; - const h = device.clientRect.height; - return this._camera.worldToScreen(worldCoord, w, h, screenCoord); + const { width, height } = device.clientRect; + return this._camera.worldToScreen(worldCoord, width, height, screenCoord); } /**