fix(minigames): pick up placed emplacements on click instead of overwriting
Clicking a grid cell always placed the currently selected emplacement there and silently deleted whatever it overlapped, so clicking an earlier emplacement (after selection auto-advanced) clobbered it. Now clicking a placed emplacement selects it so it can be moved, and placing onto an occupied cell is refused with a message instead of deleting the other emplacement. Adds the pick-up hint to the editor copy.
This commit is contained in:
parent
3ce9677fd0
commit
d4bf0e9742
1 changed files with 38 additions and 20 deletions
|
|
@ -144,32 +144,50 @@ function PlacementPhase({
|
||||||
|
|
||||||
const placeAt = useCallback(
|
const placeAt = useCallback(
|
||||||
(x: number, y: number) => {
|
(x: number, y: number) => {
|
||||||
|
// Clicking a placed emplacement picks it up instead of overwriting it.
|
||||||
|
const existing = occupied.get(`${x},${y}`);
|
||||||
|
if (existing && existing !== selectedId) {
|
||||||
|
setSelectedId(existing);
|
||||||
|
setError(null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const definition = FOB_FLEET.find((f) => f.id === selectedId);
|
const definition = FOB_FLEET.find((f) => f.id === selectedId);
|
||||||
if (!definition) return;
|
if (!definition) return;
|
||||||
const origin: GridPoint = { x, y };
|
const origin: GridPoint = { x, y };
|
||||||
const cells = cellsForFob(definition, origin, orientation, BOARD);
|
const cells = cellsForFob(definition, origin, orientation, BOARD);
|
||||||
setError(cells ? null : `${definition.label} does not fit there.`);
|
if (!cells) {
|
||||||
setPlacements((current) => {
|
setError(`${definition.label} does not fit there.`);
|
||||||
const next = { ...current };
|
return;
|
||||||
// Drop any other asset occupying these cells.
|
|
||||||
if (cells) {
|
|
||||||
for (const cell of cells) {
|
|
||||||
for (const otherId of Object.keys(next)) {
|
|
||||||
if (otherId === selectedId) continue;
|
|
||||||
const other = next[otherId];
|
|
||||||
const otherDefinition = FOB_FLEET.find((f) => f.id === otherId)!;
|
|
||||||
const otherCells = cellsForFob(otherDefinition, other.origin, other.orientation, BOARD);
|
|
||||||
if (otherCells?.some((c) => c.x === cell.x && c.y === cell.y)) delete next[otherId];
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
// Refuse overlaps rather than silently deleting another emplacement.
|
||||||
next[selectedId] = { assetId: selectedId, origin, orientation };
|
const conflict = FOB_FLEET.find((other) => {
|
||||||
return next;
|
if (other.id === selectedId) return false;
|
||||||
|
const otherPlacement = placements[other.id];
|
||||||
|
if (!otherPlacement) return false;
|
||||||
|
const otherCells = cellsForFob(
|
||||||
|
other,
|
||||||
|
otherPlacement.origin,
|
||||||
|
otherPlacement.orientation,
|
||||||
|
BOARD,
|
||||||
|
);
|
||||||
|
return Boolean(otherCells?.some((c) => cells.some((n) => n.x === c.x && n.y === c.y)));
|
||||||
});
|
});
|
||||||
|
if (conflict) {
|
||||||
|
setError(`That would overlap the ${conflict.label}.`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setError(null);
|
||||||
|
setPlacements((current) => ({
|
||||||
|
...current,
|
||||||
|
[selectedId]: { assetId: selectedId, origin, orientation },
|
||||||
|
}));
|
||||||
const nextUnplaced = FOB_FLEET.find((f) => f.id !== selectedId && !placements[f.id]);
|
const nextUnplaced = FOB_FLEET.find((f) => f.id !== selectedId && !placements[f.id]);
|
||||||
if (nextUnplaced) setSelectedId(nextUnplaced.id);
|
if (nextUnplaced) setSelectedId(nextUnplaced.id);
|
||||||
},
|
},
|
||||||
[orientation, placements, selectedId],
|
[occupied, orientation, placements, selectedId],
|
||||||
);
|
);
|
||||||
|
|
||||||
const allPlaced = FOB_FLEET.every((definition) => placements[definition.id]);
|
const allPlaced = FOB_FLEET.every((definition) => placements[definition.id]);
|
||||||
|
|
@ -301,8 +319,8 @@ function PlacementPhase({
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p className="text-xs text-muted-foreground">
|
<p className="text-xs text-muted-foreground">
|
||||||
Select an emplacement, then click a cell to set its anchor. Toggle orientation for the
|
Select an emplacement, then click a cell to set its anchor. Click a placed emplacement to
|
||||||
longer assets.
|
pick it up and move it. Toggle orientation for the longer assets.
|
||||||
</p>
|
</p>
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue