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 smooth scrolling #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions src/js/scenes/GameScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down