Skip to content

Commit

Permalink
Minor refactor of CameraComponent (#7209)
Browse files Browse the repository at this point in the history
* Minor refactor of CameraComponent

* Lint fixes
  • Loading branch information
willeastcott authored Dec 18, 2024
1 parent 680d0d5 commit e736811
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions src/framework/components/camera/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
}

Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down

0 comments on commit e736811

Please sign in to comment.