boardgame-core/docs/parts-regions-rng.md

61 lines
1.3 KiB
Markdown
Raw Normal View History

2026-04-04 15:27:37 +08:00
# 棋子、区域与 RNG
## 创建和放置 Part
```ts
import { createPart, createRegion } from 'boardgame-core';
const board = createRegion('board', [
{ name: 'row', min: 0, max: 2 },
{ name: 'col', min: 0, max: 2 },
]);
const piece = createPart<{ owner: string }>(
{ regionId: 'board', position: [1, 1], owner: 'white' },
'piece-1'
);
state.produce(draft => {
draft.parts[piece.id] = piece;
draft.board.childIds.push(piece.id);
draft.board.partMap['1,1'] = piece.id;
});
```
## Part 池
```ts
const pool = createPartPool<{ type: string }>(
{ regionId: 'supply', type: 'kitten' },
10,
'kitten'
);
const piece = pool.draw(); // 取出一个
pool.return(piece); // 放回
pool.remaining(); // 剩余数量
```
## 区域操作
```ts
import { applyAlign, shuffle, moveToRegion, isCellOccupied } from 'boardgame-core';
isCellOccupied(state.parts, 'board', [1, 1]);
applyAlign(handRegion, state.parts); // 紧凑排列
shuffle(deckRegion, state.parts, rng); // 打乱
moveToRegion(piece, sourceRegion, targetRegion, [0, 0]);
```
## RNG
```ts
import { createRNG } from 'boardgame-core';
const rng = createRNG(12345);
rng.nextInt(6); // 0-5
rng.next(); // [0, 1)
```
完整 API 列表详见 [API 参考](./api-reference.md)。