diff --git a/packages/framework/src/scenes/GameHostScene.ts b/packages/framework/src/scenes/GameHostScene.ts index 6250ebe..d994676 100644 --- a/packages/framework/src/scenes/GameHostScene.ts +++ b/packages/framework/src/scenes/GameHostScene.ts @@ -18,11 +18,11 @@ export abstract class GameHostScene> public get gameHost(): GameHost { return this._gameHost; } - + addInterruption(promise: Promise){ this.gameHost?.addInterruption(promise); } - + addTweenInterruption(tween: Phaser.Tweens.Tween){ this.addInterruption(new Promise( resolve => tween.once('complete', resolve) @@ -43,7 +43,7 @@ export abstract class GameHostScene> /** 获取当前状态的只读快照 */ public get state(): TState { - return this.gameHost.state.value; + return this.gameHost.context.value; } public addDisposable(disposable: IDisposable){ diff --git a/packages/sample-game/src/game/tic-tac-toe.ts b/packages/sample-game/src/game/tic-tac-toe.ts index 23f8fdf..3cc0e0a 100644 --- a/packages/sample-game/src/game/tic-tac-toe.ts +++ b/packages/sample-game/src/game/tic-tac-toe.ts @@ -1,7 +1,7 @@ import { createGameCommandRegistry, Part, createRegion, createPart, isCellOccupied as isCellOccupiedUtil, - IGameContext -} from '@/index'; + IGameContext, Command +} from 'boardgame-core'; const BOARD_SIZE = 3; const MAX_TURNS = BOARD_SIZE * BOARD_SIZE; @@ -19,7 +19,7 @@ const WINNING_LINES: number[][][] = [ export type PlayerType = 'X' | 'O'; export type WinnerType = PlayerType | 'draw' | null; -type TicTacToePart = Part<{ player: PlayerType }>; +export type TicTacToePart = Part<{ player: PlayerType }>; export function createInitialState() { return { @@ -44,7 +44,7 @@ async function setup(game: TicTacToeGame) { const turnOutput = await turnCommand(game, currentPlayer, turnNumber); if (!turnOutput.success) throw new Error(turnOutput.error); - game.produce(state => { + game.produce((state: TicTacToeState) => { state.winner = turnOutput.result.winner; if (!state.winner) { state.currentPlayer = state.currentPlayer === 'X' ? 'O' : 'X'; @@ -61,7 +61,7 @@ registry.register('setup', setup); async function turn(game: TicTacToeGame, turnPlayer: PlayerType, turnNumber: number) { const {player, row, col} = await game.prompt( 'play ', - (command) => { + (command: Command) => { const [player, row, col] = command.params as [PlayerType, number, number]; if (player !== turnPlayer) { @@ -124,9 +124,20 @@ export function placePiece(host: TicTacToeGame, row: number, col: number, player { regionId: 'board', position: [row, col], player }, `piece-${player}-${moveNumber}` ); - host.produce(state => { + host.produce((state: TicTacToeState) => { state.parts[piece.id] = piece; board.childIds.push(piece.id); board.partMap[`${row},${col}`] = piece.id; }); } + +export const gameModule = { + registry, + createInitialState, +}; + +export const commands = { + play: (player: PlayerType, row: number, col: number) => { + return `play ${player} ${row} ${col}`; + }, +}; diff --git a/packages/sample-game/src/scenes/GameScene.ts b/packages/sample-game/src/scenes/GameScene.ts index b981a06..8148aef 100644 --- a/packages/sample-game/src/scenes/GameScene.ts +++ b/packages/sample-game/src/scenes/GameScene.ts @@ -2,7 +2,6 @@ import Phaser from 'phaser'; import type {TicTacToeState, TicTacToePart} from '@/game/tic-tac-toe'; import { GameHostScene } from 'boardgame-phaser'; import { spawnEffect, type Spawner } from 'boardgame-phaser'; -import type { ReadonlySignal } from '@preact/signals-core'; import {commands} from "@/game/tic-tac-toe"; const CELL_SIZE = 120; @@ -26,9 +25,9 @@ export class GameScene extends GameHostScene { this.gridGraphics = this.add.graphics(); this.drawGrid(); - this.disposables.add(spawnEffect(new TicTacToePartSpawner(this, this.gameHost.state))); + this.disposables.add(spawnEffect(new TicTacToePartSpawner(this))); - this.watch(() => { + this.addEffect(() => { const winner = this.state.winner; if (winner) { this.showWinner(winner); @@ -38,7 +37,7 @@ export class GameScene extends GameHostScene { } }); - this.watch(() => { + this.addEffect(() => { const currentPlayer = this.state.currentPlayer; this.updateTurnText(currentPlayer); }); @@ -163,10 +162,10 @@ export class GameScene extends GameHostScene { } class TicTacToePartSpawner implements Spawner { - constructor(public readonly scene: GameScene, public readonly state: ReadonlySignal) {} + constructor(public readonly scene: GameScene) {} *getData() { - for (const part of Object.values(this.state.value.parts)) { + for (const part of Object.values(this.scene.state.parts)) { yield part; } }