From 29ccb52c931be933fb0928b4addb72a2d0720977 Mon Sep 17 00:00:00 2001 From: Hanusiak Piotr Date: Tue, 7 Dec 2021 13:18:36 +0100 Subject: [PATCH] cleanup --- front/src/Phaser/Game/CameraManager.ts | 25 +++++++++++++++------ front/src/Phaser/Game/GameScene.ts | 1 + front/src/Phaser/Services/WaScaleManager.ts | 6 +---- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/front/src/Phaser/Game/CameraManager.ts b/front/src/Phaser/Game/CameraManager.ts index 15fa76c2..c4e4b382 100644 --- a/front/src/Phaser/Game/CameraManager.ts +++ b/front/src/Phaser/Game/CameraManager.ts @@ -32,13 +32,12 @@ export class CameraManager extends Phaser.Events.EventEmitter { this.initCamera(); - this.scene.game.events.on("wa-scale-manager:refresh-focus-on-target", () => { - const focusOn = this.waScaleManager.getFocusTarget(); - if (!focusOn) { - return; - } - this.camera.centerOn(focusOn.x + focusOn.width * 0.5, focusOn.y + focusOn.height * 0.5); - }); + this.bindEventHandlers(); + } + + public destroy(): void { + this.scene.game.events.off("wa-scale-manager:refresh-focus-on-target"); + super.destroy(); } public getCamera(): Phaser.Cameras.Scene2D.Camera { @@ -128,4 +127,16 @@ export class CameraManager extends Phaser.Events.EventEmitter { this.camera = this.scene.cameras.main; this.camera.setBounds(0, 0, this.cameraBounds.x, this.cameraBounds.y); } + + private bindEventHandlers(): void { + this.scene.game.events.on( + "wa-scale-manager:refresh-focus-on-target", + (focusOn: { x: number; y: number; width: number; height: number }) => { + if (!focusOn) { + return; + } + this.camera.centerOn(focusOn.x + focusOn.width * 0.5, focusOn.y + focusOn.height * 0.5); + } + ); + } } diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index 78b56327..47967037 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -1410,6 +1410,7 @@ ${escapedMessage} this.userInputManager.destroy(); this.pinchManager?.destroy(); this.emoteManager.destroy(); + this.cameraManager.destroy(); this.peerStoreUnsubscribe(); this.emoteUnsubscribe(); this.emoteMenuUnsubscribe(); diff --git a/front/src/Phaser/Services/WaScaleManager.ts b/front/src/Phaser/Services/WaScaleManager.ts index 574f3b99..447b6a1f 100644 --- a/front/src/Phaser/Services/WaScaleManager.ts +++ b/front/src/Phaser/Services/WaScaleManager.ts @@ -65,17 +65,13 @@ export class WaScaleManager { return; } this.zoomModifier = this.getTargetZoomModifierFor(this.focusTarget.width, this.focusTarget.height); - this.game.events.emit("wa-scale-manager:refresh-focus-on-target"); + this.game.events.emit("wa-scale-manager:refresh-focus-on-target", this.focusTarget); } public setFocusTarget(targetDimensions?: { x: number; y: number; width: number; height: number }): void { this.focusTarget = targetDimensions; } - public getFocusTarget(): { x: number; y: number; width: number; height: number } | undefined { - return this.focusTarget; - } - public getTargetZoomModifierFor(viewportWidth: number, viewportHeight: number) { const { width: gameWidth, height: gameHeight } = coWebsiteManager.getGameSize(); const devicePixelRatio = window.devicePixelRatio ?? 1;