refactor game scene

This commit is contained in:
Peter Stockings
2026-01-27 20:30:07 +11:00
parent 2493d37c7a
commit 34554aa051
6 changed files with 719 additions and 492 deletions

View File

@@ -0,0 +1,121 @@
import Phaser from "phaser";
import { TILE_SIZE } from "../../core/constants";
export interface GameInputEvents {
"toggle-menu": () => void;
"close-menu": () => void;
"toggle-inventory": () => void;
"toggle-character": () => void;
"toggle-minimap": () => void;
"reload": () => void;
"wait": () => void;
"zoom": (deltaY: number) => void;
"pan": (dx: number, dy: number) => void;
"cancel-target": () => void;
"confirm-target": () => void; // Left click while targeting
"tile-click": (tileX: number, tileY: number, button: number) => void;
"cursor-move": (worldX: number, worldY: number) => void;
}
export class GameInput extends Phaser.Events.EventEmitter {
private scene: Phaser.Scene;
private cursors: Phaser.Types.Input.Keyboard.CursorKeys;
constructor(scene: Phaser.Scene) {
super();
this.scene = scene;
this.cursors = this.scene.input.keyboard!.createCursorKeys();
this.setupKeyboard();
this.setupMouse();
}
private setupKeyboard() {
if (!this.scene.input.keyboard) return;
this.scene.input.keyboard.on("keydown-I", () => this.emit("toggle-menu"));
this.scene.input.keyboard.on("keydown-ESC", () => this.emit("close-menu"));
this.scene.input.keyboard.on("keydown-M", () => this.emit("toggle-minimap"));
this.scene.input.keyboard.on("keydown-B", () => this.emit("toggle-inventory"));
this.scene.input.keyboard.on("keydown-C", () => this.emit("toggle-character"));
this.scene.input.keyboard.on("keydown-R", () => this.emit("reload"));
this.scene.input.keyboard.on("keydown-SPACE", () => this.emit("wait"));
}
private setupMouse() {
this.scene.input.on("wheel", (_p: any, _g: any, _x: any, deltaY: number) => {
this.emit("zoom", deltaY);
});
this.scene.input.mouse?.disableContextMenu();
this.scene.input.on("pointerdown", (p: Phaser.Input.Pointer) => {
if (p.rightButtonDown()) {
this.emit("cancel-target");
}
// For general clicks, we emit tile-click
// Logic for "confirm-target" vs "move" happens in Scene for now,
// or we can distinguish based on internal state if we moved targeting here.
// For now, let's just emit generic events or specific if clear.
// Actually, GameScene has specific logic:
// "If targeting active -> Left Click = throw"
// "Else -> Left Click = move/attack"
// To keep GameInput "dumb", we just emit the click details.
// EXCEPT: Panning logic is computed from pointer movement.
const tx = Math.floor(p.worldX / TILE_SIZE);
const ty = Math.floor(p.worldY / TILE_SIZE);
this.emit("tile-click", tx, ty, p.button);
});
this.scene.input.on("pointermove", (p: Phaser.Input.Pointer) => {
this.emit("cursor-move", p.worldX, p.worldY);
// Panning logic
if (p.isDown) {
const isRightDrag = p.rightButtonDown();
const isMiddleDrag = p.middleButtonDown();
const isShiftDrag = p.isDown && p.event.shiftKey;
if (isRightDrag || isMiddleDrag || isShiftDrag) {
const { x, y } = p.position;
const { x: prevX, y: prevY } = p.prevPosition;
const dx = (x - prevX); // Zoom factor needs to be handled by receiver or passed here
const dy = (y - prevY);
this.emit("pan", dx, dy);
}
}
});
}
public getCursorState() {
// Return simplified cursor state for movement
let dx = 0;
let dy = 0;
const left = this.cursors.left?.isDown;
const right = this.cursors.right?.isDown;
const up = this.cursors.up?.isDown;
const down = this.cursors.down?.isDown;
if (left) dx -= 1;
if (right) dx += 1;
if (up) dy -= 1;
if (down) dy += 1;
const anyJustDown = Phaser.Input.Keyboard.JustDown(this.cursors.left!) ||
Phaser.Input.Keyboard.JustDown(this.cursors.right!) ||
Phaser.Input.Keyboard.JustDown(this.cursors.up!) ||
Phaser.Input.Keyboard.JustDown(this.cursors.down!);
return { dx, dy, anyJustDown };
}
public cleanup() {
this.removeAllListeners();
// Determine is scene specific cleanup is needed for inputs
}
}