boardgame-phaser/docs/GameModule.md

2.4 KiB
Raw Permalink Blame History

编写GameModule

游戏逻辑以GameModule的形式定义

import {createGameCommandRegistry, IGameContext} from "boardgame-core";

// 创建mutative游戏初始状态
export function createInitialState(): GameState {
    //...
}

// 运行游戏
export async function start(game: IGameContext<GameState>) {
    // ...
}

// 可选
export const registry = createGameCommandRegistry();

使用以下步骤创建GameModule

1. 定义状态

通常使用一个regions: Record<string, Region>和一个parts: Record<string, Part<TMeta>>来记录桌游物件的摆放。

import {Region} from "boardgame-core";

type GameState = {
    regions: {
        white: Region,
        black: Region,
        board: Region,
    },
    pieces: Record<string, Part<PartsTable['0']>>,
    currentPlayer: PlayerType,
    winner: WinnerType,
};

游戏的部件可以从csv加载。详情见boop-game/node_modules/inline-schema/

/// parts.csv
type, player, count
string, string, number
cat, white, 8
cat, black, 8

/// parts.csv.d.ts
type PartsTable = {
    type: string;
    player: string;
    count: number;
  }[];

declare const data: PartsTable;
export default data;

2. 定义流程

使用async function start(game: IGameContext<GameState>)作为入口。

使用game.value读取游戏当前状态

async function gameEnd(game: IGameContext<GameState>) {
    return game.value.winner;
}

需要修改状态时,使用game.producegame.produceAsync

async function start(game: IGameContext<GameState>) {
    await game.produceAsync(state => {
        state.currentPlayer = 'white';
    });
}

需要等待玩家交互时,使用await game.prompt(promptDef, validator, player)

import {createPromptDef} from "boardgame-core";

export const prompts = {
    play: createPromptDef<[PlayerType, number, number]>('play <player> <row:number> <col:number>'),
}

async function turn(game: IGameContext<GameState>, currentPlayer: PlayerType) {
    const {player, row, col} = await game.prompt(
        prompts.play,
        (player, row, col) => {
            if (player !== currentPlayer)
                throw `Wrong player!`
            return {player, row, col};
        }
    )
}

3. 编写测试

使用vitest编写测试,测试应当使用GameHost来模拟游戏环境。