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

1.3 KiB

棋子、区域与 RNG

创建和放置 Part

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 池

const pool = createPartPool<{ type: string }>(
    { regionId: 'supply', type: 'kitten' },
    10,
    'kitten'
);

const piece = pool.draw();       // 取出一个
pool.return(piece);              // 放回
pool.remaining();                // 剩余数量

区域操作

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

import { createRNG } from 'boardgame-core';

const rng = createRNG(12345);
rng.nextInt(6);   // 0-5
rng.next();       // [0, 1)

完整 API 列表详见 API 参考