refactor: entity -> MutableSignal

This commit is contained in:
2026-04-03 14:19:44 +08:00
parent b1b059de8c
commit 86714e7837
8 changed files with 51 additions and 62 deletions
+12 -12
View File
@@ -1,4 +1,4 @@
import {createGameCommandRegistry, Part, Entity, createRegion} from '@/index';
import {createGameCommandRegistry, Part, MutableSignal, createRegion} from '@/index';
const BOARD_SIZE = 6;
const MAX_PIECES_PER_PLAYER = 8;
@@ -48,7 +48,7 @@ export type BoopState = ReturnType<typeof createInitialState>;
const registration = createGameCommandRegistry<BoopState>();
export const registry = registration.registry;
export function getPlayer(host: Entity<BoopState>, player: PlayerType): Player {
export function getPlayer(host: MutableSignal<BoopState>, player: PlayerType): Player {
return host.value.players[player];
}
@@ -152,23 +152,23 @@ function isValidMove(row: number, col: number): boolean {
return !isNaN(row) && !isNaN(col) && row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE;
}
export function getBoardRegion(host: Entity<BoopState>) {
export function getBoardRegion(host: MutableSignal<BoopState>) {
return host.value.board;
}
export function isCellOccupied(host: Entity<BoopState>, row: number, col: number): boolean {
export function isCellOccupied(host: MutableSignal<BoopState>, row: number, col: number): boolean {
const board = getBoardRegion(host);
return board.partMap[`${row},${col}`] !== undefined;
}
export function getPartAt(host: Entity<BoopState>, row: number, col: number): BoopPart | null {
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;
}
export function placePiece(host: Entity<BoopState>, row: number, col: number, player: PlayerType, pieceType: PieceType) {
export function placePiece(host: MutableSignal<BoopState>, row: number, col: number, player: PlayerType, pieceType: PieceType) {
const board = getBoardRegion(host);
const playerData = getPlayer(host, player);
const count = playerData[pieceType].placed + 1;
@@ -188,7 +188,7 @@ export function placePiece(host: Entity<BoopState>, row: number, col: number, pl
decrementSupply(playerData, pieceType);
}
export function applyBoops(host: Entity<BoopState>, placedRow: number, placedCol: number, placedType: PieceType) {
export function applyBoops(host: MutableSignal<BoopState>, placedRow: number, placedCol: number, placedType: PieceType) {
const board = getBoardRegion(host);
const pieces = host.value.pieces;
@@ -237,7 +237,7 @@ export function applyBoops(host: Entity<BoopState>, placedRow: number, placedCol
}
}
export function removePieceFromBoard(host: Entity<BoopState>, part: BoopPart) {
export function removePieceFromBoard(host: MutableSignal<BoopState>, part: BoopPart) {
const board = getBoardRegion(host);
const playerData = getPlayer(host, part.player);
board.childIds = board.childIds.filter(id => id !== part.id);
@@ -297,7 +297,7 @@ export function hasWinningLine(positions: number[][]): boolean {
return false;
}
export function checkGraduation(host: Entity<BoopState>, player: PlayerType): number[][][] {
export function checkGraduation(host: MutableSignal<BoopState>, player: PlayerType): number[][][] {
const pieces = host.value.pieces;
const posSet = new Set<string>();
@@ -316,7 +316,7 @@ export function checkGraduation(host: Entity<BoopState>, player: PlayerType): nu
return winningLines;
}
export function processGraduation(host: Entity<BoopState>, player: PlayerType, lines: number[][][]) {
export function processGraduation(host: MutableSignal<BoopState>, player: PlayerType, lines: number[][][]) {
const allPositions = new Set<string>();
for (const line of lines) {
for (const [r, c] of line) {
@@ -338,12 +338,12 @@ export function processGraduation(host: Entity<BoopState>, player: PlayerType, l
incrementSupply(playerData, 'cat', count);
}
export function countPiecesOnBoard(host: Entity<BoopState>, player: PlayerType): number {
export function countPiecesOnBoard(host: MutableSignal<BoopState>, player: PlayerType): number {
const pieces = host.value.pieces;
return pieces.filter(p => p.player === player).length;
}
export function checkWinner(host: Entity<BoopState>): WinnerType {
export function checkWinner(host: MutableSignal<BoopState>): WinnerType {
const pieces = host.value.pieces;
for (const player of ['white', 'black'] as PlayerType[]) {
+4 -4
View File
@@ -1,4 +1,4 @@
import {createGameCommandRegistry, Part, Entity, createRegion, moveToRegion} from '@/index';
import {createGameCommandRegistry, Part, MutableSignal, createRegion, moveToRegion} from '@/index';
const BOARD_SIZE = 3;
const MAX_TURNS = BOARD_SIZE * BOARD_SIZE;
@@ -90,7 +90,7 @@ function isValidMove(row: number, col: number): boolean {
return !isNaN(row) && !isNaN(col) && row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE;
}
export function isCellOccupied(host: Entity<TicTacToeState>, row: number, col: number): boolean {
export function isCellOccupied(host: MutableSignal<TicTacToeState>, row: number, col: number): boolean {
const board = host.value.board;
return board.partMap[`${row},${col}`] !== undefined;
}
@@ -103,7 +103,7 @@ export function hasWinningLine(positions: number[][]): boolean {
);
}
export function checkWinner(host: Entity<TicTacToeState>): WinnerType {
export function checkWinner(host: MutableSignal<TicTacToeState>): WinnerType {
const parts = Object.values(host.value.parts);
const xPositions = parts.filter((p: TicTacToePart) => p.player === 'X').map((p: TicTacToePart) => p.position);
@@ -116,7 +116,7 @@ export function checkWinner(host: Entity<TicTacToeState>): WinnerType {
return null;
}
export function placePiece(host: Entity<TicTacToeState>, row: number, col: number, player: PlayerType) {
export function placePiece(host: MutableSignal<TicTacToeState>, row: number, col: number, player: PlayerType) {
const board = host.value.board;
const moveNumber = Object.keys(host.value.parts).length + 1;
const piece: TicTacToePart = {