boardgame-phaser/docs/GameModule.md

108 lines
2.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## 编写GameModule
游戏逻辑以GameModule的形式定义
```typescript
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>>`来记录桌游物件的摆放。
```typescript
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`读取游戏当前状态
```typescript
async function gameEnd(game: IGameContext<GameState>) {
return game.value.winner;
}
```
需要修改状态时,使用`game.produce`或`game.produceAsync`。
```typescript
async function start(game: IGameContext<GameState>) {
await game.produceAsync(state => {
state.currentPlayer = 'white';
});
}
```
需要等待玩家交互时,使用`await game.prompt(promptDef, validator, player)`。
```typescript
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`来模拟游戏环境。