2026-04-04 14:13:41 +08:00
|
|
|
|
import {useComputed} from '@preact/signals';
|
2026-04-04 13:08:28 +08:00
|
|
|
|
import { createGameHost, type GameModule } from 'boardgame-core';
|
|
|
|
|
|
import Phaser from 'phaser';
|
|
|
|
|
|
import { h } from 'preact';
|
|
|
|
|
|
import { PhaserGame, PhaserScene } from 'boardgame-phaser';
|
|
|
|
|
|
|
|
|
|
|
|
export default function App<TState extends Record<string, unknown>>(props: { gameModule: GameModule<TState>, gameScene: { new(): Phaser.Scene } }) {
|
2026-04-04 12:14:26 +08:00
|
|
|
|
|
|
|
|
|
|
const gameHost = useComputed(() => {
|
2026-04-04 12:52:14 +08:00
|
|
|
|
const gameHost = createGameHost(props.gameModule);
|
2026-04-04 13:08:28 +08:00
|
|
|
|
return { gameHost };
|
2026-04-04 12:14:26 +08:00
|
|
|
|
});
|
2026-04-04 13:08:28 +08:00
|
|
|
|
|
2026-04-04 12:14:26 +08:00
|
|
|
|
const scene = useComputed(() => new props.gameScene());
|
2026-04-04 13:08:28 +08:00
|
|
|
|
|
2026-04-04 14:13:41 +08:00
|
|
|
|
const handleReset = async () => {
|
|
|
|
|
|
gameHost.value.gameHost.setup('setup');
|
|
|
|
|
|
};
|
|
|
|
|
|
const label = useComputed(() => gameHost.value.gameHost.status.value === 'running' ? 'Restart' : 'Start');
|
|
|
|
|
|
|
2026-04-04 12:14:26 +08:00
|
|
|
|
return (
|
|
|
|
|
|
<div className="flex flex-col h-screen">
|
|
|
|
|
|
<div className="flex-1 relative">
|
|
|
|
|
|
<PhaserGame>
|
2026-04-04 13:08:28 +08:00
|
|
|
|
<PhaserScene sceneKey="GameScene" scene={scene.value} autoStart data={gameHost.value} />
|
2026-04-04 12:14:26 +08:00
|
|
|
|
</PhaserGame>
|
|
|
|
|
|
</div>
|
2026-04-04 14:13:41 +08:00
|
|
|
|
<div className="p-4 bg-gray-100 border-t border-gray-200">
|
|
|
|
|
|
<button
|
|
|
|
|
|
onClick={handleReset}
|
|
|
|
|
|
className="px-6 py-2 bg-blue-500 text-white rounded hover:bg-blue-600 disabled:bg-gray-400 disabled:cursor-not-allowed transition-colors font-medium"
|
|
|
|
|
|
>
|
|
|
|
|
|
{label}
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
2026-04-04 12:14:26 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|