refactor: use GameHost for simplification

This commit is contained in:
2026-04-04 01:11:49 +08:00
parent 32509d7812
commit 4fa06098ba
2 changed files with 65 additions and 72 deletions
+23 -21
View File
@@ -1,5 +1,6 @@
import Phaser from 'phaser';
import type { TicTacToeState, TicTacToePart, PlayerType } from '@/game/tic-tac-toe';
import type { GameHost, CommandSchema, IGameContext, MutableSignal } from 'boardgame-core';
import { ReactiveScene, bindRegion, createInputMapper, InputMapper } from 'boardgame-phaser';
const CELL_SIZE = 120;
@@ -11,13 +12,31 @@ export class GameScene extends ReactiveScene<TicTacToeState> {
private gridGraphics!: Phaser.GameObjects.Graphics;
private inputMapper!: InputMapper;
private turnText!: Phaser.GameObjects.Text;
/** Receives the active prompt from the single PromptHandler in main.tsx */
promptSignal: { current: any } = { current: null };
/** Receives the active prompt schema from main.tsx */
promptSchema: { current: CommandSchema | null } = { current: null };
/** GameHost instance passed from main.tsx */
private gameHost!: GameHost<TicTacToeState>;
constructor() {
super('GameScene');
}
init(data: { gameHost: GameHost<TicTacToeState> } | { gameContext: IGameContext<TicTacToeState> }): void {
if ('gameHost' in data) {
this.gameHost = data.gameHost;
// Create a compatible gameContext from GameHost
this.gameContext = {
state: this.gameHost.state as MutableSignal<TicTacToeState>,
commands: this.gameHost.commands,
} as IGameContext<TicTacToeState>;
this.state = this.gameContext.state;
this.commands = this.gameContext.commands;
} else {
// Fallback for direct gameContext passing
super.init(data);
}
}
protected onStateReady(_state: TicTacToeState): void {
}
@@ -84,10 +103,8 @@ export class GameScene extends ReactiveScene<TicTacToeState> {
private setupInput(): void {
this.inputMapper = createInputMapper(this, {
onSubmit: (cmd: string) => {
// Delegate to the single PromptHandler via the shared commands reference.
// The actual PromptHandler instance lives in main.tsx and is set up once.
// We call through a callback that main.tsx provides via the scene's public interface.
return this.submitToPrompt(cmd);
// Directly submit to GameHost
return this.gameHost.onInput(cmd);
}
});
@@ -106,21 +123,6 @@ export class GameScene extends ReactiveScene<TicTacToeState> {
);
}
/**
* Called by main.tsx to wire up the single PromptHandler's submit function.
*/
private _submitToPrompt: ((cmd: string) => string | null) | null = null;
setSubmitPrompt(fn: (cmd: string) => string | null): void {
this._submitToPrompt = fn;
}
private submitToPrompt(cmd: string): string | null {
return this._submitToPrompt
? this._submitToPrompt(cmd)
: null; // no handler wired yet, accept silently
}
private drawGrid(): void {
const g = this.gridGraphics;
g.lineStyle(3, 0x6b7280);