fix: impl
This commit is contained in:
parent
28782aaf9b
commit
e58690c9c6
|
|
@ -32,12 +32,15 @@ async function place(game: BoopGame, row: number, col: number, player: PlayerTyp
|
||||||
throw new Error(`No ${type} available in ${player}'s supply`);
|
throw new Error(`No ${type} available in ${player}'s supply`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const partId = part.id;
|
||||||
|
|
||||||
game.produce(state => {
|
game.produce(state => {
|
||||||
// 将棋子从supply移动到棋盘
|
// 将棋子从supply移动到棋盘
|
||||||
|
const part = state.pieces[partId];
|
||||||
moveToRegion(part, state.regions[player], state.regions.board, [row, col]);
|
moveToRegion(part, state.regions[player], state.regions.board, [row, col]);
|
||||||
});
|
});
|
||||||
|
|
||||||
return { row, col, player, type, partId: part.id };
|
return { row, col, player, type, partId };
|
||||||
}
|
}
|
||||||
const placeCommand = registry.register( 'place <row:number> <col:number> <player> <type>', place);
|
const placeCommand = registry.register( 'place <row:number> <col:number> <player> <type>', place);
|
||||||
|
|
||||||
|
|
@ -55,7 +58,8 @@ async function boop(game: BoopGame, row: number, col: number, type: PieceType) {
|
||||||
|
|
||||||
if (!isInBounds(nr, nc)) continue;
|
if (!isInBounds(nr, nc)) continue;
|
||||||
|
|
||||||
const part = findPartAtPosition(game, nr, nc);
|
// 从 state 中查找,而不是 game
|
||||||
|
const part = findPartAtPosition(state, nr, nc);
|
||||||
if (!part) continue;
|
if (!part) continue;
|
||||||
|
|
||||||
// 小猫不能推动猫
|
// 小猫不能推动猫
|
||||||
|
|
@ -70,7 +74,7 @@ async function boop(game: BoopGame, row: number, col: number, type: PieceType) {
|
||||||
// 棋子被推出棋盘,返回玩家supply
|
// 棋子被推出棋盘,返回玩家supply
|
||||||
booped.push(part.id);
|
booped.push(part.id);
|
||||||
moveToRegion(part, state.regions.board, state.regions[part.player]);
|
moveToRegion(part, state.regions.board, state.regions[part.player]);
|
||||||
} else if (!isCellOccupied(game, newRow, newCol)) {
|
} else if (!isCellOccupied(state, newRow, newCol)) {
|
||||||
// 新位置为空,移动过去
|
// 新位置为空,移动过去
|
||||||
booped.push(part.id);
|
booped.push(part.id);
|
||||||
moveToRegion(part, state.regions.board, state.regions.board, [newRow, newCol]);
|
moveToRegion(part, state.regions.board, state.regions.board, [newRow, newCol]);
|
||||||
|
|
@ -130,13 +134,15 @@ async function checkGraduates(game: BoopGame){
|
||||||
}
|
}
|
||||||
|
|
||||||
game.produce(state => {
|
game.produce(state => {
|
||||||
|
// 预先收集所有可用的猫(在盒子里的)
|
||||||
for(const partId of toUpgrade){
|
for(const partId of toUpgrade){
|
||||||
const part = state.pieces[partId];
|
const part = state.pieces[partId];
|
||||||
const [row, col] = part.position;
|
const [row, col] = part.position;
|
||||||
const player = part.player;
|
const player = part.player;
|
||||||
moveToRegion(part, state.regions.board, null);
|
moveToRegion(part, state.regions.board, null);
|
||||||
|
|
||||||
const newPart = findPartInRegion(game, '', 'cat');
|
// 使用下一个可用的猫
|
||||||
|
const newPart = findPartInRegion(state, '', 'kitten', player);
|
||||||
moveToRegion(newPart || part, null, state.regions[player], [row, col]);
|
moveToRegion(newPart || part, null, state.regions[player], [row, col]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,13 @@
|
||||||
import {BOARD_SIZE, BoopGame, BoopPart, BoopState, PieceType, WIN_LENGTH} from "@/samples/boop/data";
|
import {
|
||||||
|
BOARD_SIZE,
|
||||||
|
BoopGame,
|
||||||
|
BoopPart,
|
||||||
|
BoopState,
|
||||||
|
PieceType,
|
||||||
|
PlayerType,
|
||||||
|
RegionType,
|
||||||
|
WIN_LENGTH
|
||||||
|
} from "@/samples/boop/data";
|
||||||
|
|
||||||
const DIRS = [
|
const DIRS = [
|
||||||
[0, 1],
|
[0, 1],
|
||||||
|
|
@ -41,14 +50,17 @@ export function* getNeighborPositions(x: number = 0, y: number = 0){
|
||||||
yield [x + dx, y + dy] as PT;
|
yield [x + dx, y + dy] as PT;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function findPartInRegion(ctx: BoopGame | BoopState, regionId: keyof BoopGame['value']['regions'], type: PieceType): BoopPart | null {
|
export function findPartInRegion(ctx: BoopGame | BoopState, regionId: keyof BoopGame['value']['regions'], type: PieceType, player?: PlayerType): BoopPart | null {
|
||||||
const state = getState(ctx);
|
const state = getState(ctx);
|
||||||
if(!regionId){
|
if(!regionId){
|
||||||
return Object.values(state.pieces).find(part => part.type === type && !part.regionId) || null;
|
return Object.values(state.pieces).find(part => match(regionId, part, type, player)) || null;
|
||||||
}
|
}
|
||||||
const id = state.regions[regionId].childIds.find(id => state.pieces[id].type === type);
|
const id = state.regions[regionId].childIds.find(id => match(regionId, state.pieces[id], type, player));
|
||||||
return id ? state.pieces[id] || null : null;
|
return id ? state.pieces[id] || null : null;
|
||||||
}
|
}
|
||||||
|
function match(regionId: RegionType, part: BoopPart, type: PieceType, player?: PlayerType){
|
||||||
|
return regionId === part.regionId && part.type === type && (!player || part.player === player);
|
||||||
|
}
|
||||||
|
|
||||||
export function findPartAtPosition(ctx: BoopGame | BoopState, row: number, col: number): BoopPart | null {
|
export function findPartAtPosition(ctx: BoopGame | BoopState, row: number, col: number): BoopPart | null {
|
||||||
const state = getState(ctx);
|
const state = getState(ctx);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue