1
0
Fork 0

feat(minigames): highlight the selected emplacement and add rotation controls

Ring the selected emplacement's grid cells and document the controls in
the editor help text. Rotate a placed emplacement in place by
double-clicking it or by pressing the Horizontal/Vertical toggle (the
toggle also sets the orientation for a new placement, and is disabled for
the fixed 2x2 Command Post).

Fix: moving an emplacement after rotating it kept the default orientation
instead of the rotated one, so the rotation was lost on the next click.
placeAt now preserves the emplacement's stored orientation when moving it.
This commit is contained in:
Jason Fraley 2026-10-11 22:18:20 -04:00
parent d4bf0e9742
commit a1a2dbdcac

View file

@ -144,10 +144,11 @@ function PlacementPhase({
const placeAt = useCallback(
(x: number, y: number) => {
// Clicking a placed emplacement picks it up instead of overwriting it.
// Clicking a placed emplacement picks it up; its own cells stay inert so a
// double-click can rotate it without also nudging its anchor.
const existing = occupied.get(`${x},${y}`);
if (existing && existing !== selectedId) {
setSelectedId(existing);
if (existing) {
if (existing !== selectedId) setSelectedId(existing);
setError(null);
return;
}
@ -155,7 +156,10 @@ function PlacementPhase({
const definition = FOB_FLEET.find((f) => f.id === selectedId);
if (!definition) return;
const origin: GridPoint = { x, y };
const cells = cellsForFob(definition, origin, orientation, BOARD);
// Keep an already-placed emplacement's orientation when moving it; only
// fall back to the toggle default for a brand-new placement.
const placeOrientation = placements[selectedId]?.orientation ?? orientation;
const cells = cellsForFob(definition, origin, placeOrientation, BOARD);
if (!cells) {
setError(`${definition.label} does not fit there.`);
return;
@ -182,7 +186,7 @@ function PlacementPhase({
setError(null);
setPlacements((current) => ({
...current,
[selectedId]: { assetId: selectedId, origin, orientation },
[selectedId]: { assetId: selectedId, origin, orientation: placeOrientation },
}));
const nextUnplaced = FOB_FLEET.find((f) => f.id !== selectedId && !placements[f.id]);
if (nextUnplaced) setSelectedId(nextUnplaced.id);
@ -190,6 +194,44 @@ function PlacementPhase({
[occupied, orientation, placements, selectedId],
);
/** Double-click a placed line emplacement to flip its orientation in place. */
const rotateAsset = useCallback(
(assetId: string) => {
const definition = FOB_FLEET.find((f) => f.id === assetId);
const placement = placements[assetId];
if (!definition || !placement || definition.shape === "square") return;
const nextOrientation: FobOrientation = placement.orientation === "h" ? "v" : "h";
const cells = cellsForFob(definition, placement.origin, nextOrientation, BOARD);
if (!cells) {
setError(`${definition.label} does not fit rotated there.`);
return;
}
const conflict = FOB_FLEET.find((other) => {
if (other.id === assetId) 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(`Cannot rotate: it would overlap the ${conflict.label}.`);
return;
}
setError(null);
setSelectedId(assetId);
setPlacements((current) => ({
...current,
[assetId]: { ...placement, orientation: nextOrientation },
}));
},
[placements],
);
const allPlaced = FOB_FLEET.every((definition) => placements[definition.id]);
const confirm = useCallback(async () => {
@ -206,6 +248,18 @@ function PlacementPhase({
else setError(result.error ?? "Could not confirm your emplacements.");
}, [allPlaced, match.matchId, onMatch, placements]);
const selectedIsSquare = selectedDefinition.shape === "square";
const effectiveOrientation = selectedPlacement ? selectedPlacement.orientation : orientation;
const toggleOrientation = useCallback(() => {
if (selectedIsSquare) return;
if (selectedPlacement) {
rotateAsset(selectedId);
return;
}
setOrientation((value) => (value === "h" ? "v" : "h"));
}, [rotateAsset, selectedId, selectedIsSquare, selectedPlacement]);
return (
<Card>
<CardHeader className="border-b p-4">
@ -281,12 +335,15 @@ function PlacementPhase({
);
})}
<Button
type="button"
variant="outline"
size="sm"
className="h-auto"
onClick={() => setOrientation((value) => (value === "h" ? "v" : "h"))}
disabled={selectedIsSquare}
title={selectedIsSquare ? "The Command Post is a fixed 2x2 block." : undefined}
onClick={toggleOrientation}
>
<RotateCcw /> {orientation === "h" ? "Horizontal" : "Vertical"}
<RotateCcw /> {effectiveOrientation === "h" ? "Horizontal" : "Vertical"}
</Button>
</div>
@ -311,17 +368,28 @@ function PlacementPhase({
occupied={occupied}
preview={new Set(previewCells.map(key))}
previewClass={fobStyle(selectedId).cell}
selectedId={selectedId}
disabled={false}
onPlace={placeAt}
onRotate={rotateAsset}
/>
))}
</div>
</div>
<p className="text-xs text-muted-foreground">
Select an emplacement, then click a cell to set its anchor. Click a placed emplacement to
pick it up and move it. Toggle orientation for the longer assets.
</p>
<div className="rounded-lg border border-border/60 bg-background/40 p-3 text-xs text-muted-foreground">
<p className="mb-1 font-medium text-foreground">How to emplace</p>
<ul className="list-disc space-y-0.5 pl-4">
<li>Select an emplacement above, then click a cell to anchor it there.</li>
<li>Click a placed emplacement to pick it up, then click a new cell to move it.</li>
<li>
Double-click a placed emplacement, or press the Horizontal/Vertical toggle, to rotate
it in place. The Command Post is a fixed 2x2 block and does not rotate.
</li>
<li>The toggle also sets the orientation used for your next placement.</li>
<li>Emplacements cannot overlap, and your opponent never sees your layout.</li>
</ul>
</div>
</CardContent>
</Card>
);
@ -332,15 +400,19 @@ function PlacementRow({
occupied,
preview,
previewClass,
selectedId,
disabled,
onPlace,
onRotate,
}: {
y: number;
occupied: Map<string, string>;
preview: Set<string>;
previewClass: string;
selectedId: string;
disabled: boolean;
onPlace: (x: number, y: number) => void;
onRotate: (assetId: string) => void;
}) {
return (
<>
@ -350,17 +422,23 @@ function PlacementRow({
{Array.from({ length: BOARD }, (_, x) => {
const assetId = occupied.get(`${x},${y}`);
const isPreview = preview.has(`${x},${y}`);
const selected = assetId !== undefined && assetId === selectedId;
return (
<button
key={`${x}-${y}`}
type="button"
disabled={disabled}
aria-label={`grid ${cellLabel(x, y)}`}
aria-pressed={selected}
onClick={() => onPlace(x, y)}
onDoubleClick={() => {
if (assetId) onRotate(assetId);
}}
className={cn(
"flex aspect-square items-center justify-center rounded-sm border text-[10px] font-semibold transition-colors",
assetId ? fobStyle(assetId).cell : "border-border bg-card hover:border-primary/60",
isPreview && !assetId && cn(previewClass, "opacity-60"),
selected && "ring-2 ring-inset ring-white/80",
disabled && "cursor-not-allowed opacity-60",
)}
>