refactor: add some part management utilities

This commit is contained in:
2026-04-03 15:00:25 +08:00
parent 86714e7837
commit 4bf6eb2f6b
9 changed files with 170 additions and 63 deletions
+8 -15
View File
@@ -1,4 +1,4 @@
import {createGameCommandRegistry, Part, MutableSignal, createRegion} from '@/index';
import {createGameCommandRegistry, Part, MutableSignal, createRegion, createPart, isCellOccupied as isCellOccupiedUtil, getPartAtPosition} from '@/index';
const BOARD_SIZE = 6;
const MAX_PIECES_PER_PLAYER = 8;
@@ -8,7 +8,7 @@ export type PlayerType = 'white' | 'black';
export type PieceType = 'kitten' | 'cat';
export type WinnerType = PlayerType | 'draw' | null;
type BoopPart = Part & { player: PlayerType; pieceType: PieceType };
type BoopPart = Part<{ player: PlayerType; pieceType: PieceType }>;
type PieceSupply = { supply: number; placed: number };
@@ -157,15 +157,11 @@ export function getBoardRegion(host: MutableSignal<BoopState>) {
}
export function isCellOccupied(host: MutableSignal<BoopState>, row: number, col: number): boolean {
const board = getBoardRegion(host);
return board.partMap[`${row},${col}`] !== undefined;
return isCellOccupiedUtil(host.value.pieces, 'board', [row, col]);
}
export function getPartAt(host: MutableSignal<BoopState>, row: number, col: number): BoopPart | null {
const board = getBoardRegion(host);
const partId = board.partMap[`${row},${col}`];
if (!partId) return null;
return host.value.pieces.find(p => p.id === partId) || null;
return getPartAtPosition(host.value.pieces, 'board', [row, col]) || null;
}
export function placePiece(host: MutableSignal<BoopState>, row: number, col: number, player: PlayerType, pieceType: PieceType) {
@@ -173,13 +169,10 @@ export function placePiece(host: MutableSignal<BoopState>, row: number, col: num
const playerData = getPlayer(host, player);
const count = playerData[pieceType].placed + 1;
const piece: BoopPart = {
id: `${player}-${pieceType}-${count}`,
regionId: 'board',
position: [row, col],
player,
pieceType,
};
const piece = createPart<{ player: PlayerType; pieceType: PieceType }>(
{ regionId: 'board', position: [row, col], player, pieceType },
`${player}-${pieceType}-${count}`
);
host.produce(s => {
s.pieces.push(piece);
board.childIds.push(piece.id);