From 70c24a12741b8db29ad6c69fe9eede2a369e2202 Mon Sep 17 00:00:00 2001 From: bruh-9000 Date: Sun, 21 Apr 2024 17:10:41 -0700 Subject: [PATCH] Add smooth scrolling --- src/js/scenes/GameScene.ts | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/js/scenes/GameScene.ts b/src/js/scenes/GameScene.ts index efb3f45..aad2cf0 100644 --- a/src/js/scenes/GameScene.ts +++ b/src/js/scenes/GameScene.ts @@ -216,28 +216,35 @@ export default class GameScene extends Phaser.Scene { indicator.rotation = this.angle; } + private targetZoom: number; + private zoom(deltaY: number, pointer: Phaser.Input.Pointer) { const tilemap = this.tilemap; const camera = this.cameras.main; const maxZoom = (10 * 16) / tilemap.tileWidth; const minZoom = (1.25 * 16) / tilemap.tileWidth; - let targetZoom; + + if (this.targetZoom === undefined) { + this.targetZoom = camera.zoom; + } + if (deltaY < 0) { - targetZoom = camera.zoom * 1.2; - if (targetZoom < maxZoom) { - let xDist = pointer.worldX - camera.midPoint.x; - let yDist = pointer.worldY - camera.midPoint.y; - camera.scrollX += xDist / 6; - camera.scrollY += yDist / 6; - } - } else targetZoom = camera.zoom / 1.2; - if (targetZoom < minZoom) targetZoom = minZoom; - else if (targetZoom > maxZoom) targetZoom = maxZoom; - camera.setZoom(targetZoom); + this.targetZoom = camera.zoom * 1.5; + } else this.targetZoom = camera.zoom / 1.5; + if (this.targetZoom < minZoom) this.targetZoom = minZoom; + else if (this.targetZoom > maxZoom) this.targetZoom = maxZoom; + // camera.setZoom(this.targetZoom); } + public update() { + if (this.targetZoom) { + const camera = this.cameras.main; + const newZoom = camera.zoom + (this.targetZoom - camera.zoom) / 12; + camera.setZoom(newZoom) + } + const tilemap = this.tilemap; this.buildings.forEach((building, index) => { //@ts-ignore