fix: some boop states are not updated in produce
This commit is contained in:
parent
aba035f2ec
commit
0dbb4fc2ba
|
|
@ -193,7 +193,6 @@ export function placePiece(host: MutableSignal<BoopState>, row: number, col: num
|
||||||
}
|
}
|
||||||
|
|
||||||
export function applyBoops(host: MutableSignal<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;
|
const pieces = host.value.pieces;
|
||||||
const piecesArray = Object.values(pieces);
|
const piecesArray = Object.values(pieces);
|
||||||
|
|
||||||
|
|
@ -216,6 +215,10 @@ export function applyBoops(host: MutableSignal<BoopState>, placedRow: number, pl
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
host.produce(state => {
|
||||||
|
const board = state.board;
|
||||||
|
const currentPieces = state.pieces;
|
||||||
|
|
||||||
for (const { part, dr, dc } of piecesToBoop) {
|
for (const { part, dr, dc } of piecesToBoop) {
|
||||||
const [r, c] = part.position;
|
const [r, c] = part.position;
|
||||||
const newRow = r + dr;
|
const newRow = r + dr;
|
||||||
|
|
@ -224,31 +227,37 @@ export function applyBoops(host: MutableSignal<BoopState>, placedRow: number, pl
|
||||||
if (newRow < 0 || newRow >= BOARD_SIZE || newCol < 0 || newCol >= BOARD_SIZE) {
|
if (newRow < 0 || newRow >= BOARD_SIZE || newCol < 0 || newCol >= BOARD_SIZE) {
|
||||||
const pt = part.pieceType;
|
const pt = part.pieceType;
|
||||||
const pl = part.player;
|
const pl = part.player;
|
||||||
const playerData = getPlayer(host, pl);
|
const playerData = state.players[pl];
|
||||||
removePieceFromBoard(host, part);
|
// Remove piece from board
|
||||||
incrementSupply(playerData, pt);
|
board.childIds = board.childIds.filter(id => id !== part.id);
|
||||||
|
delete board.partMap[part.position.join(',')];
|
||||||
|
delete currentPieces[part.id];
|
||||||
|
playerData[pt].placed--;
|
||||||
|
playerData[pt].supply++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isCellOccupied(host, newRow, newCol)) continue;
|
// Check if target cell is occupied
|
||||||
|
const targetPosKey = `${newRow},${newCol}`;
|
||||||
|
if (board.partMap[targetPosKey]) continue;
|
||||||
|
|
||||||
|
// Move piece to new position
|
||||||
|
delete board.partMap[part.position.join(',')];
|
||||||
part.position = [newRow, newCol];
|
part.position = [newRow, newCol];
|
||||||
board.partMap = Object.fromEntries(
|
board.partMap[targetPosKey] = part.id;
|
||||||
board.childIds.map(id => {
|
|
||||||
const p = pieces[id];
|
|
||||||
return [p.position.join(','), id];
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function removePieceFromBoard(host: MutableSignal<BoopState>, part: BoopPart) {
|
export function removePieceFromBoard(host: MutableSignal<BoopState>, part: BoopPart) {
|
||||||
const board = getBoardRegion(host);
|
host.produce(state => {
|
||||||
const playerData = getPlayer(host, part.player);
|
const board = state.board;
|
||||||
|
const playerData = state.players[part.player];
|
||||||
board.childIds = board.childIds.filter(id => id !== part.id);
|
board.childIds = board.childIds.filter(id => id !== part.id);
|
||||||
delete board.partMap[part.position.join(',')];
|
delete board.partMap[part.position.join(',')];
|
||||||
delete host.value.pieces[part.id];
|
delete state.pieces[part.id];
|
||||||
playerData[part.pieceType].placed--;
|
playerData[part.pieceType].placed--;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const DIRECTIONS: [number, number][] = [
|
const DIRECTIONS: [number, number][] = [
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue