import type { BoopState } from '@/game'; import { GameHostScene } from 'boardgame-phaser'; import { commands } from '@/game'; import { BoardRenderer } from './BoardRenderer'; import { createPieceSpawner } from './PieceSpawner'; import { SupplyUI } from './SupplyUI'; import { PieceTypeSelector } from './PieceTypeSelector'; import { WinnerOverlay } from './WinnerOverlay'; import { StartOverlay } from './StartOverlay'; import { ErrorOverlay } from './ErrorOverlay'; export class GameScene extends GameHostScene { private boardRenderer!: BoardRenderer; private supplyUI!: SupplyUI; private pieceTypeSelector!: PieceTypeSelector; private winnerOverlay!: WinnerOverlay; private startOverlay!: StartOverlay; private errorOverlay!: ErrorOverlay; constructor() { super('GameScene'); } create(): void { super.create(); // 初始化 UI 组件 this.boardRenderer = new BoardRenderer(this); this.supplyUI = new SupplyUI(this); this.pieceTypeSelector = new PieceTypeSelector(this); this.winnerOverlay = new WinnerOverlay(this, () => this.restartGame()); this.startOverlay = new StartOverlay(this, () => this.startGame()); this.errorOverlay = new ErrorOverlay(this); // 设置棋子生成器 this.disposables.add(createPieceSpawner(this)); // 设置输入处理 this.boardRenderer.setupInput( () => this.state, (row, col) => this.handleCellClick(row, col), () => this.gameHost.status.value !== 'running' || !!this.state.winner ); // 设置棋子点击处理(用于棋盘满时选择要升级的棋子) this.boardRenderer.setupPieceInput( () => this.state, (row, col) => this.handlePieceClick(row, col), () => this.gameHost.status.value !== 'running' || !!this.state.winner ); // 监听游戏状态变化 this.addEffect(() => { const status = this.gameHost.status.value; if (status === 'running') { this.startOverlay.hide(); } else if (status === 'created') { this.startOverlay.show(); } }); // 监听胜负状态 this.addEffect(() => { const winner = this.state.winner; if (winner) { this.winnerOverlay.show(winner); } else { this.winnerOverlay.hide(); } }); this.addEffect(() => { const currentPlayer = this.state.currentPlayer; this.boardRenderer.updateTurnText(currentPlayer, this.state); this.supplyUI.update(this.state); this.pieceTypeSelector.update(this.state); }); } private handleCellClick(row: number, col: number): void { const selectedType = this.pieceTypeSelector.getSelectedType(); const cmd = commands.play(this.state.currentPlayer, row, col, selectedType); const error = this.gameHost.onInput(cmd); if (error) { this.errorOverlay.show(error); } } private handlePieceClick(row: number, col: number): void { // 棋盘满时,点击棋子触发升级 const cmd = commands.play(this.state.currentPlayer, row, col); const error = this.gameHost.onInput(cmd); if (error) { this.errorOverlay.show(error); } } private startGame(): void { this.gameHost.setup('setup'); } private restartGame(): void { this.gameHost.setup('setup'); } }