From 7501b5f592640c6defcc44167bcc3f7f15476e73 Mon Sep 17 00:00:00 2001 From: hypercross Date: Sat, 4 Apr 2026 00:10:26 +0800 Subject: [PATCH] fix: fix bugs --- packages/framework/src/bindings/index.ts | 5 ++-- packages/framework/src/input/index.ts | 30 ++++++++++++++------ packages/sample-game/src/scenes/GameScene.ts | 18 +++--------- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/packages/framework/src/bindings/index.ts b/packages/framework/src/bindings/index.ts index 2985de2..37266a3 100644 --- a/packages/framework/src/bindings/index.ts +++ b/packages/framework/src/bindings/index.ts @@ -63,9 +63,10 @@ export function bindRegion( if (!part) continue; // 支持动态维度:取前两个维度作为 x, y + // position[0] = row (y轴), position[1] = col (x轴) const pos = new Phaser.Math.Vector2( - (part.position[0] ?? 0) * options.cellSize.x + offset.x, - (part.position[1] ?? 0) * options.cellSize.y + offset.y, + (part.position[1] ?? 0) * options.cellSize.x + offset.x, + (part.position[0] ?? 0) * options.cellSize.y + offset.y, ); let obj = objects.get(childId); diff --git a/packages/framework/src/input/index.ts b/packages/framework/src/input/index.ts index d8ffeba..79fe288 100644 --- a/packages/framework/src/input/index.ts +++ b/packages/framework/src/input/index.ts @@ -14,6 +14,7 @@ export class InputMapper> { private activePrompt: { current: PromptEvent | null }; private onSubmitPrompt: (input: string) => string | null; private pointerDownCallback: ((pointer: Phaser.Input.Pointer) => void) | null = null; + private isWaitingForPrompt = false; constructor(options: InputMapperOptions) { this.scene = options.scene; @@ -41,12 +42,9 @@ export class InputMapper> { const cmd = onCellClick(col, row); if (cmd) { - // 如果有活跃的 prompt,通过 submit 提交 - if (this.activePrompt.current) { - this.onSubmitPrompt(cmd); - } else { - this.commands.run(cmd); - } + // 总是尝试提交到 prompt + // 如果没有活跃 prompt,onSubmitPrompt 会返回错误,我们忽略它 + this.onSubmitPrompt(cmd); } }; @@ -94,6 +92,7 @@ export class PromptHandler> { private onCancel: (reason?: string) => void; private activePrompt: PromptEvent | null = null; private isListening = false; + private pendingInput: string | null = null; constructor(options: PromptHandlerOptions) { this.scene = options.scene; @@ -113,6 +112,19 @@ export class PromptHandler> { this.commands.promptQueue.pop() .then((promptEvent) => { this.activePrompt = promptEvent; + + // 如果有等待的输入,自动提交 + if (this.pendingInput) { + const input = this.pendingInput; + this.pendingInput = null; + const error = this.activePrompt.tryCommit(input); + if (error === null) { + this.activePrompt = null; + this.listenForPrompt(); + } + return; + } + this.onPrompt(promptEvent); }) .catch((reason) => { @@ -123,9 +135,11 @@ export class PromptHandler> { submit(input: string): string | null { if (!this.activePrompt) { - return 'No active prompt'; + // 没有活跃 prompt,保存为待处理输入 + this.pendingInput = input; + return null; // 返回 null 表示已接受,会等待 } - + const error = this.activePrompt.tryCommit(input); if (error === null) { // 提交成功,重置并监听下一个 prompt diff --git a/packages/sample-game/src/scenes/GameScene.ts b/packages/sample-game/src/scenes/GameScene.ts index 1e998c4..07b7c3e 100644 --- a/packages/sample-game/src/scenes/GameScene.ts +++ b/packages/sample-game/src/scenes/GameScene.ts @@ -83,24 +83,14 @@ export class GameScene extends ReactiveScene { } private setupInput(): void { - const scene = this; - const activePromptRef = { - get current() { return scene.activePrompt; } - }; - this.inputMapper = createInputMapper( this, this.commands, - activePromptRef, + { current: null }, // 不再需要,保留以兼容接口 (cmd: string) => { - const activePrompt = this.activePrompt; - if (!activePrompt) return 'No active prompt'; - const error = activePrompt.tryCommit(cmd); - if (error === null) { - this.activePrompt = null; - this.promptHandler.start(); - } - return error; + // 使用 PromptHandler.submit() 而不是直接 tryCommit + // 这样会自动处理没有活跃 prompt 时的排队逻辑 + return this.promptHandler.submit(cmd); } );