boardgame-phaser/packages/boop-game/src/scenes/GameScene.ts

85 lines
2.6 KiB
TypeScript
Raw Normal View History

import type { BoopState } from '@/game';
2026-04-04 14:22:35 +08:00
import { GameHostScene } from 'boardgame-phaser';
2026-04-06 11:22:33 +08:00
import { prompts } from '@/game';
2026-04-04 16:25:36 +08:00
import { BoardRenderer } from './BoardRenderer';
import { createPieceSpawner } from './PieceSpawner';
import { SupplyUI } from './SupplyUI';
import { PieceTypeSelector } from './PieceTypeSelector';
import { WinnerOverlay } from './WinnerOverlay';
2026-04-05 00:24:06 +08:00
import { ErrorOverlay } from './ErrorOverlay';
2026-04-04 14:22:35 +08:00
export class GameScene extends GameHostScene<BoopState> {
2026-04-04 16:25:36 +08:00
private boardRenderer!: BoardRenderer;
private supplyUI!: SupplyUI;
private pieceTypeSelector!: PieceTypeSelector;
private winnerOverlay!: WinnerOverlay;
2026-04-05 00:24:06 +08:00
private errorOverlay!: ErrorOverlay;
2026-04-04 14:22:35 +08:00
constructor() {
super('GameScene');
}
create(): void {
super.create();
2026-04-04 16:25:36 +08:00
// 初始化 UI 组件
this.boardRenderer = new BoardRenderer(this);
this.supplyUI = new SupplyUI(this);
this.pieceTypeSelector = new PieceTypeSelector(this);
2026-04-12 18:47:04 +08:00
this.winnerOverlay = new WinnerOverlay(this, () => this.gameHost.start());
2026-04-05 00:24:06 +08:00
this.errorOverlay = new ErrorOverlay(this);
2026-04-04 16:25:36 +08:00
// 设置棋子生成器
this.disposables.add(createPieceSpawner(this));
2026-04-04 14:22:35 +08:00
2026-04-05 00:30:21 +08:00
// 设置输入处理(空单元格和已有棋子统一处理)
2026-04-04 23:25:43 +08:00
this.boardRenderer.setupInput(
() => this.state,
(row, col) => this.handleCellClick(row, col),
2026-04-05 00:24:06 +08:00
(row, col) => this.handlePieceClick(row, col),
() => this.gameHost.status.value !== 'running' || !!this.state.winner
);
2026-04-05 00:12:05 +08:00
// 监听胜负状态
2026-04-04 16:34:17 +08:00
this.addEffect(() => {
2026-04-04 14:22:35 +08:00
const winner = this.state.winner;
if (winner) {
2026-04-04 16:25:36 +08:00
this.winnerOverlay.show(winner);
} else {
this.winnerOverlay.hide();
2026-04-04 14:22:35 +08:00
}
});
2026-04-04 16:34:17 +08:00
this.addEffect(() => {
2026-04-04 14:22:35 +08:00
const currentPlayer = this.state.currentPlayer;
2026-04-04 16:25:36 +08:00
this.boardRenderer.updateTurnText(currentPlayer, this.state);
this.supplyUI.update(this.state);
this.pieceTypeSelector.update(this.state);
2026-04-04 14:22:35 +08:00
});
2026-04-12 18:47:04 +08:00
// 启动游戏
this.gameHost.start();
2026-04-04 14:22:35 +08:00
}
2026-04-04 16:25:36 +08:00
private handleCellClick(row: number, col: number): void {
const selectedType = this.pieceTypeSelector.getSelectedType();
2026-04-06 16:17:37 +08:00
const error = this.gameHost.tryAnswerPrompt(prompts.play, this.state.currentPlayer, row, col, selectedType);
2026-04-04 16:25:36 +08:00
if (error) {
2026-04-05 00:24:06 +08:00
this.errorOverlay.show(error);
}
}
private handlePieceClick(row: number, col: number): void {
// 棋盘满时,点击棋子触发升级
2026-04-12 18:47:04 +08:00
const error = this.gameHost.tryAnswerPrompt(prompts.choose, this.state.currentPlayer, row, col);
2026-04-05 00:24:06 +08:00
if (error) {
this.errorOverlay.show(error);
2026-04-04 14:22:35 +08:00
}
}
2026-04-12 18:47:04 +08:00
/** 跳转到菜单场景 */
private async goToMenu(): Promise<void> {
await this.sceneController.launch('MenuScene');
2026-04-04 14:22:35 +08:00
}
}