import { useComputed, signal } from '@preact/signals'; import { createGameHost, type GameModule } from 'boardgame-core'; import Phaser from 'phaser'; import { h } from 'preact'; import { PhaserGame, PhaserScene } from 'boardgame-phaser'; // 全局信号:从 GameScene 传递反击阶段的状态到 UI export const counterattackInfo = signal<{ phase: string; selectedCards: string[]; totalValue: number; maxHandValue: number; // 手牌最大可提供的点数 requiredValue: number; canSubmit: boolean; canWin: boolean; // 手牌是否足以获胜 error: string | null; }>({ phase: '', selectedCards: [], totalValue: 0, maxHandValue: 0, requiredValue: 0, canSubmit: false, canWin: false, error: null, }); // 全局信号:牌堆信息 export const deckInfo = signal<{ castleDeck: number; // 敌人牌堆剩余 tavernDeck: number; // 酒馆牌堆剩余 discardPile: number; // 弃牌堆数量 hand: number; // 手牌数 defeatedEnemies: number; // 已击败敌人数 jestersUsed: number; // 已用小丑牌数 }>({ castleDeck: 0, tavernDeck: 0, discardPile: 0, hand: 0, defeatedEnemies: 0, jestersUsed: 0, }); // 存储当前场景引用(由 GameScene 在 create 时设置) let currentGameScene: any = null; export function setGameSceneRef(scene: any) { currentGameScene = scene; } export default function App>(props: { gameModule: GameModule, gameScene: { new(): Phaser.Scene } }) { const gameHost = useComputed(() => { const gameHost = createGameHost(props.gameModule); return { gameHost }; }); const scene = useComputed(() => new props.gameScene()); const handleReset = async () => { const result = await gameHost.value.gameHost.start(); console.log('Game finished!', result); }; const label = useComputed(() => gameHost.value.gameHost.status.value === 'running' ? '重新开始' : '开始游戏' ); // 反击阶段提交 const handleSubmitCounterattack = () => { if (currentGameScene && typeof currentGameScene.submitCounterattack === 'function') { currentGameScene.submitCounterattack(); } }; // 反击阶段认输 const handleSurrender = () => { if (currentGameScene && typeof currentGameScene.surrenderCounterattack === 'function') { currentGameScene.surrenderCounterattack(); } }; // Phaser 画布配置 const phaserConfig = { type: Phaser.AUTO, width: 800, height: 700, backgroundColor: '#111827', }; return (
{/* Phaser 游戏场景 */}
{/* 底部控制栏 */}
{/* 牌堆信息 */}
🏰 城堡牌堆: {deckInfo.value.castleDeck}
🍺 酒馆牌堆: {deckInfo.value.tavernDeck}
🗑️ 弃牌堆: {deckInfo.value.discardPile}
🃏 手牌: {deckInfo.value.hand}
💀 已击败: {deckInfo.value.defeatedEnemies}/12
🃏 小丑牌: {2 - deckInfo.value.jestersUsed}/2
{/* 反击阶段信息 + 按钮 */}
⚔️ Regicide - 击败所有12个敌人
{/* 反击阶段信息面板 */} {counterattackInfo.value.phase === 'enemyCounterattack' && (
💥 反击阶段 需要: {counterattackInfo.value.requiredValue} | 手牌最多: {counterattackInfo.value.maxHandValue}
{counterattackInfo.value.selectedCards.length > 0 && (
已选: {counterattackInfo.value.selectedCards.length}张 点数: {counterattackInfo.value.totalValue}
)} {counterattackInfo.value.error && (
❌ {counterattackInfo.value.error}
)}
)}
{/* 反击确认按钮 */} {counterattackInfo.value.phase === 'enemyCounterattack' && counterattackInfo.value.selectedCards.length > 0 && ( )} {/* 认输按钮 */} {counterattackInfo.value.phase === 'enemyCounterattack' && !counterattackInfo.value.canWin && ( )}
); }