fix(minigames): square versus grid cells and make the Command Post a 2x2 block
The placement and battle grids inherited a 32px line-height, so any cell
holding the emplacement glyph grew taller than its square width (29x34),
making the row gaps look inconsistent once emplacements were placed. Set
leading-none on the grids so cells stay square.
Add a shape to emplacements ("line" | "square") and lay out the Command
Post as a 2x2 block (size 4) instead of a 3-cell line. cellsForFob resolves
cells per definition; the placement editor, preview, and tests use it.
This commit is contained in:
parent
065018cf3c
commit
3ce9677fd0
5 changed files with 71 additions and 33 deletions
|
|
@ -18,7 +18,7 @@ import {
|
|||
type FobOrientation,
|
||||
type FobPlacement,
|
||||
} from "@/lib/minigames/call-for-fire-versus/constants";
|
||||
import { cellsForPlacement } from "@/lib/minigames/call-for-fire-versus/engine";
|
||||
import { cellsForFob } from "@/lib/minigames/call-for-fire-versus/engine";
|
||||
import type { VersusFobView, VersusShotView } from "@/lib/minigames/call-for-fire-versus/types";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
|
|
@ -88,7 +88,7 @@ function randomFleet(): Record<string, FobPlacement> {
|
|||
x: Math.floor(Math.random() * BOARD),
|
||||
y: Math.floor(Math.random() * BOARD),
|
||||
};
|
||||
const cells = cellsForPlacement(origin, orientation, definition.size, BOARD);
|
||||
const cells = cellsForFob(definition, origin, orientation, BOARD);
|
||||
if (!cells) continue;
|
||||
if (cells.some((cell) => occupied.has(key(cell)))) continue;
|
||||
cells.forEach((cell) => occupied.add(key(cell)));
|
||||
|
|
@ -129,16 +129,17 @@ function PlacementPhase({
|
|||
for (const definition of FOB_FLEET) {
|
||||
const placement = placements[definition.id];
|
||||
if (!placement) continue;
|
||||
const cells = cellsForPlacement(placement.origin, placement.orientation, definition.size, BOARD);
|
||||
const cells = cellsForFob(definition, placement.origin, placement.orientation, BOARD);
|
||||
if (!cells) continue;
|
||||
for (const cell of cells) map.set(key(cell), definition.id);
|
||||
}
|
||||
return map;
|
||||
}, [placements]);
|
||||
|
||||
const selectedDefinition = FOB_FLEET.find((f) => f.id === selectedId)!;
|
||||
const selectedPlacement = placements[selectedId];
|
||||
const previewCells = selectedPlacement
|
||||
? (cellsForPlacement(selectedPlacement.origin, selectedPlacement.orientation, FOB_FLEET.find((f) => f.id === selectedId)!.size, BOARD) ?? [])
|
||||
? (cellsForFob(selectedDefinition, selectedPlacement.origin, selectedPlacement.orientation, BOARD) ?? [])
|
||||
: [];
|
||||
|
||||
const placeAt = useCallback(
|
||||
|
|
@ -146,7 +147,7 @@ function PlacementPhase({
|
|||
const definition = FOB_FLEET.find((f) => f.id === selectedId);
|
||||
if (!definition) return;
|
||||
const origin: GridPoint = { x, y };
|
||||
const cells = cellsForPlacement(origin, orientation, definition.size, BOARD);
|
||||
const cells = cellsForFob(definition, origin, orientation, BOARD);
|
||||
setError(cells ? null : `${definition.label} does not fit there.`);
|
||||
setPlacements((current) => {
|
||||
const next = { ...current };
|
||||
|
|
@ -156,12 +157,8 @@ function PlacementPhase({
|
|||
for (const otherId of Object.keys(next)) {
|
||||
if (otherId === selectedId) continue;
|
||||
const other = next[otherId];
|
||||
const otherCells = cellsForPlacement(
|
||||
other.origin,
|
||||
other.orientation,
|
||||
FOB_FLEET.find((f) => f.id === otherId)!.size,
|
||||
BOARD,
|
||||
);
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
|
@ -277,7 +274,7 @@ function PlacementPhase({
|
|||
|
||||
<div className="mx-auto w-full max-w-sm overflow-auto rounded-lg border border-border bg-background p-3">
|
||||
<div
|
||||
className="grid gap-1"
|
||||
className="grid gap-1 leading-none"
|
||||
style={{ gridTemplateColumns: `repeat(${BOARD + 1}, minmax(0, 1fr))` }}
|
||||
>
|
||||
<div />
|
||||
|
|
@ -598,7 +595,7 @@ function BoardPanel({
|
|||
</CardHeader>
|
||||
<CardContent className="p-3">
|
||||
<div
|
||||
className="grid gap-1"
|
||||
className="grid gap-1 leading-none"
|
||||
style={{ gridTemplateColumns: `repeat(${BOARD + 1}, minmax(0, 1fr))` }}
|
||||
>
|
||||
<div />
|
||||
|
|
|
|||
|
|
@ -26,22 +26,26 @@ export const VERSUS_MAX_NAME_LENGTH = 40;
|
|||
/** Sentinel userId for the empty seat in a waiting match (never a real user). */
|
||||
export const VERSUS_WAITING_USER_ID = 0;
|
||||
|
||||
export type FobShape = "line" | "square";
|
||||
|
||||
export interface FobDefinition {
|
||||
readonly id: string;
|
||||
readonly label: string;
|
||||
/** Occupied cells when placed, laid out in a straight line. */
|
||||
/** Occupied cells when placed. */
|
||||
readonly size: number;
|
||||
/** "line" lays the cells straight (orientation applies); "square" is an NxN block. */
|
||||
readonly shape: FobShape;
|
||||
}
|
||||
|
||||
/**
|
||||
* The emplacement fleet each player hides. A shorter list keeps a duel brisk;
|
||||
* total occupied cells are 8 of 100, so rounds matter without dragging.
|
||||
* total occupied cells are 9 of 100, so rounds matter without dragging.
|
||||
*/
|
||||
export const FOB_FLEET: readonly FobDefinition[] = [
|
||||
{ id: "command-post", label: "Command Post", size: 3 },
|
||||
{ id: "ammo-depot", label: "Ammo Depot", size: 2 },
|
||||
{ id: "radar-site", label: "Radar Site", size: 2 },
|
||||
{ id: "fob", label: "Forward Operating Base", size: 1 },
|
||||
{ id: "command-post", label: "Command Post", size: 4, shape: "square" },
|
||||
{ id: "ammo-depot", label: "Ammo Depot", size: 2, shape: "line" },
|
||||
{ id: "radar-site", label: "Radar Site", size: 2, shape: "line" },
|
||||
{ id: "fob", label: "Forward Operating Base", size: 1, shape: "line" },
|
||||
];
|
||||
|
||||
export type FobOrientation = "h" | "v";
|
||||
|
|
|
|||
|
|
@ -13,8 +13,10 @@ import {
|
|||
VERSUS_MAX_SHOTS_PER_PLAYER,
|
||||
VERSUS_SCORE_PER_CELL,
|
||||
VERSUS_SCORE_PER_FOB,
|
||||
type FobDefinition,
|
||||
type FobOrientation,
|
||||
type FobPlacement,
|
||||
type FobShape,
|
||||
} from "./constants";
|
||||
import type {
|
||||
ShotOutcome,
|
||||
|
|
@ -60,13 +62,29 @@ export function createVersusState(
|
|||
};
|
||||
}
|
||||
|
||||
/** Cells a straight-line emplacement covers, or null when it runs off the board. */
|
||||
/** Cells a straight-line or square emplacement covers, or null when it runs off the board. */
|
||||
export function cellsForPlacement(
|
||||
origin: GridPoint,
|
||||
orientation: FobOrientation,
|
||||
size: number,
|
||||
boardSize: number,
|
||||
shape: FobShape = "line",
|
||||
): GridPoint[] | null {
|
||||
if (shape === "square") {
|
||||
const side = Math.round(Math.sqrt(size));
|
||||
if (side * side !== size) return null;
|
||||
const cells: GridPoint[] = [];
|
||||
for (let dy = 0; dy < side; dy += 1) {
|
||||
for (let dx = 0; dx < side; dx += 1) {
|
||||
const x = origin.x + dx;
|
||||
const y = origin.y + dy;
|
||||
if (x < 0 || y < 0 || x >= boardSize || y >= boardSize) return null;
|
||||
cells.push({ x, y });
|
||||
}
|
||||
}
|
||||
return cells;
|
||||
}
|
||||
|
||||
const cells: GridPoint[] = [];
|
||||
for (let i = 0; i < size; i += 1) {
|
||||
const x = orientation === "h" ? origin.x + i : origin.x;
|
||||
|
|
@ -77,6 +95,16 @@ export function cellsForPlacement(
|
|||
return cells;
|
||||
}
|
||||
|
||||
/** Cells for a named emplacement, honoring its shape. */
|
||||
export function cellsForFob(
|
||||
definition: FobDefinition,
|
||||
origin: GridPoint,
|
||||
orientation: FobOrientation,
|
||||
boardSize: number,
|
||||
): GridPoint[] | null {
|
||||
return cellsForPlacement(origin, orientation, definition.size, boardSize, definition.shape);
|
||||
}
|
||||
|
||||
function cellKey(point: GridPoint): string {
|
||||
return `${point.x},${point.y}`;
|
||||
}
|
||||
|
|
@ -93,12 +121,7 @@ export function buildFleet(
|
|||
for (const definition of FOB_FLEET) {
|
||||
const placement = byId.get(definition.id);
|
||||
if (!placement) return { error: `Missing emplacement: ${definition.label}.` };
|
||||
const cells = cellsForPlacement(
|
||||
placement.origin,
|
||||
placement.orientation,
|
||||
definition.size,
|
||||
boardSize,
|
||||
);
|
||||
const cells = cellsForFob(definition, placement.origin, placement.orientation, boardSize);
|
||||
if (!cells) return { error: `${definition.label} does not fit there.` };
|
||||
for (const cell of cells) {
|
||||
const key = cellKey(cell);
|
||||
|
|
|
|||
|
|
@ -16,9 +16,9 @@ import { computeVersusStatsDelta, readVersusStats, xpForVersus } from "@/lib/min
|
|||
function fleetAt(x: number, y: number): FobPlacement[] {
|
||||
return [
|
||||
{ assetId: "command-post", origin: { x, y }, orientation: "h" },
|
||||
{ assetId: "ammo-depot", origin: { x, y: y + 1 }, orientation: "h" },
|
||||
{ assetId: "radar-site", origin: { x, y: y + 2 }, orientation: "h" },
|
||||
{ assetId: "fob", origin: { x, y: y + 3 }, orientation: "h" },
|
||||
{ assetId: "ammo-depot", origin: { x, y: y + 2 }, orientation: "h" },
|
||||
{ assetId: "radar-site", origin: { x, y: y + 3 }, orientation: "h" },
|
||||
{ assetId: "fob", origin: { x, y: y + 4 }, orientation: "h" },
|
||||
];
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,9 +18,9 @@ import {
|
|||
function fleetAt(x: number, y: number): FobPlacement[] {
|
||||
return [
|
||||
{ assetId: "command-post", origin: { x, y }, orientation: "h" },
|
||||
{ assetId: "ammo-depot", origin: { x, y: y + 1 }, orientation: "h" },
|
||||
{ assetId: "radar-site", origin: { x, y: y + 2 }, orientation: "h" },
|
||||
{ assetId: "fob", origin: { x, y: y + 3 }, orientation: "h" },
|
||||
{ assetId: "ammo-depot", origin: { x, y: y + 2 }, orientation: "h" },
|
||||
{ assetId: "radar-site", origin: { x, y: y + 3 }, orientation: "h" },
|
||||
{ assetId: "fob", origin: { x, y: y + 4 }, orientation: "h" },
|
||||
];
|
||||
}
|
||||
|
||||
|
|
@ -46,6 +46,18 @@ describe("call for fire versus: fleet placement", () => {
|
|||
]);
|
||||
});
|
||||
|
||||
it("lays out a 2x2 square emplacement regardless of orientation", () => {
|
||||
const square = [
|
||||
{ x: 0, y: 0 },
|
||||
{ x: 1, y: 0 },
|
||||
{ x: 0, y: 1 },
|
||||
{ x: 1, y: 1 },
|
||||
];
|
||||
expect(cellsForPlacement({ x: 0, y: 0 }, "h", 4, 10, "square")).toEqual(square);
|
||||
expect(cellsForPlacement({ x: 0, y: 0 }, "v", 4, 10, "square")).toEqual(square);
|
||||
expect(cellsForPlacement({ x: 9, y: 0 }, "h", 4, 10, "square")).toBeNull();
|
||||
});
|
||||
|
||||
it("rejects missing, overlapping, and unknown emplacements", () => {
|
||||
expect(buildFleet([fleetAt(0, 0)[0]], 10)).toEqual({ error: "Missing emplacement: Ammo Depot." });
|
||||
|
||||
|
|
@ -116,9 +128,11 @@ describe("call for fire versus: firing", () => {
|
|||
{ i: 1, input: { kind: "grid", grid: { x: 9, y: 9 } } },
|
||||
{ i: 0, input: { kind: "correction", rangeM: 0, deflectionM: 50 } },
|
||||
{ i: 1, input: { kind: "correction", rangeM: 0, deflectionM: 0 } },
|
||||
{ i: 0, input: { kind: "correction", rangeM: -50, deflectionM: -50 } },
|
||||
{ i: 1, input: { kind: "correction", rangeM: 0, deflectionM: 0 } },
|
||||
{ i: 0, input: { kind: "correction", rangeM: 0, deflectionM: 50 } },
|
||||
{ i: 1, input: { kind: "correction", rangeM: 0, deflectionM: 0 } },
|
||||
{ i: 0, input: { kind: "correction", rangeM: -50, deflectionM: -100 } },
|
||||
{ i: 0, input: { kind: "correction", rangeM: -50, deflectionM: -50 } },
|
||||
{ i: 1, input: { kind: "correction", rangeM: 0, deflectionM: 0 } },
|
||||
{ i: 0, input: { kind: "correction", rangeM: 0, deflectionM: 50 } },
|
||||
{ i: 1, input: { kind: "correction", rangeM: 0, deflectionM: 0 } },
|
||||
|
|
|
|||
Loading…
Reference in a new issue