Phase 29 — Map Toggles #20

Merged
developer merged 8 commits from feature/ui-map-toggles into development 2026-05-19 22:37:30 +00:00
2 changed files with 130 additions and 117 deletions
Showing only changes of commit 53b892ae00 - Show all commits
+67 -76
View File
@@ -243,29 +243,26 @@ export type FogPaintOp =
/**
* fogPaintOps returns the ordered sequence of paint operations that
* draw the Phase 29 visible-hyperspace overlay on a single torus
* copy. The first op is the fog-coloured rectangle covering the
* full world; subsequent ops are background-coloured circles, one
* per visibility circle, painted on top of the fog rectangle. The
* natural rendering order unions overlapping circles for free —
* earlier iterations relied on Pixi v8's `Graphics.cut()` to
* subtract holes, but `cut()` produced incorrect unions for
* overlapping circles (the symptom was a handful of disconnected
* arc segments instead of a clean union).
* draw the Phase 29 visible-hyperspace overlay. The renderer
* dispatches each op onto its own Pixi `Graphics` inside a single
* `fogLayer` that sits below every primitive copy, so the natural
* rendering order paints fog underneath the world.
*
* Coordinates are in world space (the `fogLayer` has no transform),
* which means the wrap offsets are baked directly into the
* positions — there is no per-tile dispatch on the renderer side.
*
* `mode` controls the torus-wrap behaviour:
*
* - `"torus"`: every visibility circle is also drawn at the eight
* wrapped positions (±width, ±height) so the circle remains
* visually continuous when its painted area extends past the
* world rectangle into a neighbouring tile — without the wraps
* the next tile's fog rectangle overpaints the bleed, producing
* a "sector" artifact at the seam.
* - `"no-wrap"`: only the planet's own position is drawn. The
* wrapped positions would create extra holes inside the world
* rectangle when a planet sits near an edge (the user can never
* pan past the boundary in no-wrap mode, but the wrapped circle
* could still leak into the visible area).
* - `"torus"`: every fog rect AND every visibility circle is
* emitted at the nine offsets (`(dx, dy) ∈ {-1, 0, 1}²`), so
* the fog covers all nine torus tiles and a planet near a seam
* keeps a continuous visibility hole across it.
* - `"no-wrap"`: only the central tile is emitted. The user can
* never pan past the boundary in no-wrap mode, so the
* additional wraps would just be wasted paint — worse, a
* wrapped circle from a planet near an edge would leak into
* the visible world rectangle as an unwanted hole.
*
* Empty `circles` returns an empty list — the caller skips fog
* rendering entirely. Width/height ≤ 0 also returns empty so a
@@ -282,17 +279,18 @@ export function fogPaintOps(
if (world.width <= 0 || world.height <= 0) return [];
const offsets: ReadonlyArray<readonly [number, number]> =
mode === "torus" ? TORUS_OFFSETS : ORIGIN_ONLY_OFFSET;
const ops: FogPaintOp[] = [
{
const ops: FogPaintOp[] = [];
for (const [dx, dy] of offsets) {
ops.push({
kind: "fillRect",
x: 0,
y: 0,
x: dx * world.width,
y: dy * world.height,
width: world.width,
height: world.height,
color: fogColor,
alpha: 1,
},
];
});
}
for (const c of circles) {
for (const [dx, dy] of offsets) {
ops.push({
@@ -343,6 +341,17 @@ export async function createRenderer(opts: RendererOptions): Promise<RendererHan
app.stage.addChild(viewport);
// Phase 29 fog layer: a single Container sharing the viewport's
// coordinate space, populated by `setVisibilityFog`. Added to
// the viewport BEFORE the nine torus copies so the layered
// repaint (fog rectangles + background-coloured circles) always
// renders underneath every primitive. An earlier per-copy
// approach with `copy.addChildAt(fog, 0)` ended up with fog on
// top in practice — moving the fog to a sibling of the copies
// avoids any reorder ambiguity.
const fogLayer = new Container();
viewport.addChild(fogLayer);
// Create nine torus copies, each holding its own primitive
// graphics. Origin copy is always visible; the other eight
// follow the active wrap mode.
@@ -368,17 +377,10 @@ export async function createRenderer(opts: RendererOptions): Promise<RendererHan
// renderer-internal hit-test sites (pointer-move, clicked) and the
// external `handle.hitAt` thread it through `hitTest`.
let hiddenIds: ReadonlySet<PrimitiveID> = EMPTY_HIDDEN_IDS;
// Per-copy fog Containers for the Phase 29 visibility fog
// overlay. Each container holds one `Graphics` per
// `FogPaintOp` (fog rect + one bg-coloured circle per
// visibility circle × wrap position), inserted at index 0 of
// the torus copy so primitives paint on top. Created lazily
// when `setVisibilityFog` first receives a non-empty list and
// destroyed wholesale on every subsequent call — Pixi v8's
// multi-shape Graphics is supported in theory, but stacking
// each fill on its own Graphics removes any risk of an
// internal-state regression dropping a layer.
let fogGraphics: Container[] = [];
// `fogLayer` (declared above) is repopulated every time
// `setVisibilityFog` runs. We track the dispatched ops only
// implicitly via the layer's children; on every flip we drop
// the previous children and rebuild from the new op list.
const applyHiddenStateTo = (id: PrimitiveID, list: Graphics[]): void => {
const visible = !hiddenIds.has(id);
for (const g of list) g.visible = visible;
@@ -804,16 +806,13 @@ export async function createRenderer(opts: RendererOptions): Promise<RendererHan
},
isPrimitiveHidden: (id) => hiddenIds.has(id),
setVisibilityFog: (circles) => {
// Drop the old fog Containers first — every flip rebuilds
// from scratch instead of mutating in place, so the
// implementation stays simple and Pixi-v8-residue-free.
// `destroy({children: true})` propagates to every owned
// Graphics inside the Container.
for (const c of fogGraphics) {
c.parent?.removeChild(c);
c.destroy({ children: true });
// Drop the previous fog children — every flip rebuilds
// from scratch instead of mutating in place. Pixi v8's
// `Container.removeChildren()` returns the detached
// children so we can destroy each one explicitly.
for (const old of fogLayer.removeChildren()) {
old.destroy({ children: true });
}
fogGraphics = [];
const ops = fogPaintOps(
opts.world,
circles,
@@ -822,29 +821,21 @@ export async function createRenderer(opts: RendererOptions): Promise<RendererHan
mode,
);
if (ops.length === 0) return;
for (const copy of copies) {
const container = new Container();
// One Graphics per op — the fog rect first, then every
// background-coloured circle on top. Per-shape Graphics
// removes any risk of multi-shape Pixi quirks dropping a
// layer (the previous all-in-one Graphics implementation
// surfaced exactly that symptom in DEV — only the last
// planet's glyph stayed visible inside the bg holes).
for (const op of ops) {
const g = new Graphics();
if (op.kind === "fillRect") {
g.rect(op.x, op.y, op.width, op.height);
} else {
g.circle(op.x, op.y, op.radius);
}
g.fill({ color: op.color, alpha: op.alpha });
container.addChild(g);
// Each op gets its own Graphics so any multi-shape Pixi
// quirks cannot drop a layer (an earlier all-in-one
// implementation surfaced exactly that symptom in DEV —
// only the last planet's glyph stayed visible inside the
// bg holes). The ops carry world-space positions; the
// `fogLayer` has no transform.
for (const op of ops) {
const g = new Graphics();
if (op.kind === "fillRect") {
g.rect(op.x, op.y, op.width, op.height);
} else {
g.circle(op.x, op.y, op.radius);
}
// Fog sits below every primitive on the same copy so
// planet glyphs paint on top. `addChildAt(g, 0)` keeps
// the rest of the children's order intact.
copy.addChildAt(container, 0);
fogGraphics.push(container);
g.fill({ color: op.color, alpha: op.alpha });
fogLayer.addChild(g);
}
},
resize: (w, h) => {
@@ -869,15 +860,15 @@ export async function createRenderer(opts: RendererOptions): Promise<RendererHan
teardownPickMode();
previous?.onPick(null);
}
// `app.destroy({...children: true})` below would also walk
// fog containers, but we drop them eagerly so the closure
// reference clears even if a future caller queries the
// renderer mid-dispose.
for (const c of fogGraphics) {
c.parent?.removeChild(c);
c.destroy({ children: true });
// `app.destroy({...children: true})` below recursively
// destroys every container in the scene graph, fogLayer
// included. The explicit removeChildren()/destroy here
// drops the fog children eagerly so a future caller
// querying the renderer mid-dispose does not see stale
// fog instances still parented under the layer.
for (const old of fogLayer.removeChildren()) {
old.destroy({ children: true });
}
fogGraphics = [];
viewport.off("moved", enforceCentreWhenLarger);
viewport.off("moved", wrapTorusCamera);
viewport.off("clicked", handleViewportClicked);
+63 -41
View File
@@ -1,14 +1,15 @@
// Phase 29 unit coverage for the Phase 29 fog overlay's layered
// overpaint logic. `fogPaintOps` lives in `src/map/render.ts` next
// to its sole consumer (`RendererHandle.setVisibilityFog`) — the
// renderer dispatches each op straight onto its own Pixi `Graphics`
// (one per shape) inside a per-copy `Container`, so the unit test
// exercises the public ordering contract: a single fog-coloured
// rectangle followed by one background-coloured circle per
// visibility entry (multiplied by the torus wrap offsets when the
// renderer is in torus mode). The natural rendering order unions
// overlapping circles for free, replacing the earlier `cut()`
// implementation that produced disconnected arc segments.
// Phase 29 unit coverage for the visible-hyperspace overlay's
// layered overpaint logic. `fogPaintOps` lives in `src/map/render.ts`
// next to its sole consumer (`RendererHandle.setVisibilityFog`) —
// the renderer dispatches each op onto its own Pixi `Graphics`
// inside a `fogLayer` container that sits below every primitive
// copy. The natural rendering order paints fog underneath the
// world, replacing the earlier `cut()` implementation that
// produced disconnected arc segments.
//
// Coordinates returned by `fogPaintOps` are in world space because
// `fogLayer` has no transform — wraps for torus mode are baked
// into the ops directly.
import { describe, expect, test } from "vitest";
@@ -22,7 +23,7 @@ describe("fogPaintOps — no-wrap mode", () => {
expect(fogPaintOps(WORLD, [], FOG_COLOR, BG_COLOR, "no-wrap")).toEqual([]);
});
test("single circle emits fog rect + one bg circle in that order", () => {
test("single circle emits a single fog rect + one bg circle", () => {
const ops = fogPaintOps(
WORLD,
[{ x: 100, y: 200, radius: 50 }],
@@ -98,7 +99,7 @@ describe("fogPaintOps — no-wrap mode", () => {
});
describe("fogPaintOps — torus mode", () => {
test("each circle is emitted at nine wrapped positions", () => {
test("single circle expands to 9 fog rects + 9 bg circles in world space", () => {
const ops = fogPaintOps(
WORLD,
[{ x: 100, y: 200, radius: 50 }],
@@ -106,25 +107,43 @@ describe("fogPaintOps — torus mode", () => {
BG_COLOR,
"torus",
);
// 1 fog rect + 9 wrapped circles.
expect(ops.length).toBe(10);
expect(ops[0].kind).toBe("fillRect");
const positions = ops
.slice(1)
.map((op) => (op.kind === "fillCircle" ? `${op.x},${op.y}` : ""))
// 9 fog rects + 9 wrapped circles.
expect(ops.length).toBe(18);
// The first 9 ops are fog rects, one per neighbour tile.
const rectPositions = ops
.slice(0, 9)
.map((op) =>
op.kind === "fillRect" ? `${op.x},${op.y}` : "non-rect",
)
.sort();
// Every neighbour offset is emitted with width=1000 / height=800.
const expected: string[] = [];
const expectedRectPositions: string[] = [];
for (const dx of [-1, 0, 1]) {
for (const dy of [-1, 0, 1]) {
expected.push(`${100 + dx * 1000},${200 + dy * 800}`);
expectedRectPositions.push(`${dx * 1000},${dy * 800}`);
}
}
expected.sort();
expect(positions).toEqual(expected);
expectedRectPositions.sort();
expect(rectPositions).toEqual(expectedRectPositions);
// The next 9 ops are bg circles at every wrapped planet position.
const circlePositions = ops
.slice(9)
.map((op) =>
op.kind === "fillCircle" ? `${op.x},${op.y}` : "non-circle",
)
.sort();
const expectedCirclePositions: string[] = [];
for (const dx of [-1, 0, 1]) {
for (const dy of [-1, 0, 1]) {
expectedCirclePositions.push(
`${100 + dx * 1000},${200 + dy * 800}`,
);
}
}
expectedCirclePositions.sort();
expect(circlePositions).toEqual(expectedCirclePositions);
});
test("multiple circles produce 9 × N wrapped circles after the fog rect", () => {
test("multiple circles produce 9 fog rects + 9N bg circles", () => {
const ops = fogPaintOps(
WORLD,
[
@@ -135,14 +154,17 @@ describe("fogPaintOps — torus mode", () => {
BG_COLOR,
"torus",
);
// 1 fog rect + (9 wraps × 2 circles) = 19 ops.
expect(ops.length).toBe(19);
expect(ops[0].kind).toBe("fillRect");
// Each circle keeps its own radius across every wrap.
// 9 fog rects + (9 wraps × 2 circles) = 27 ops.
expect(ops.length).toBe(27);
expect(
ops.slice(0, 9).every((op) => op.kind === "fillRect"),
).toBe(true);
expect(
ops.slice(9).every((op) => op.kind === "fillCircle"),
).toBe(true);
const radii = ops
.slice(1)
.map((op) => (op.kind === "fillCircle" ? op.radius : 0))
.filter((r) => r > 0);
.slice(9)
.map((op) => (op.kind === "fillCircle" ? op.radius : 0));
expect(radii.filter((r) => r === 50).length).toBe(9);
expect(radii.filter((r) => r === 30).length).toBe(9);
});
@@ -150,10 +172,10 @@ describe("fogPaintOps — torus mode", () => {
test("a circle near the right edge produces a wrapped copy past the seam", () => {
// Planet at (950, 400) with radius 300 — the painted area
// extends to x = 1250 in the central tile. In torus mode the
// renderer also draws a wrapped circle at (950 - 1000, 400) =
// (-50, 400) so the next tile (with its own fog rect) keeps a
// matching unfogged hole at the seam — this is the fix for
// the "sector" artifact at the wrap boundary.
// renderer also draws wrapped circles at (-50, 400) and
// (1950, 400) so the circle stays continuous across the seam
// instead of appearing as a sector clipped by the neighbour
// tile's fog rectangle.
const ops = fogPaintOps(
WORLD,
[{ x: 950, y: 400, radius: 300 }],
@@ -161,12 +183,12 @@ describe("fogPaintOps — torus mode", () => {
BG_COLOR,
"torus",
);
const xs = ops
.slice(1)
const circleXs = ops
.filter((op) => op.kind === "fillCircle")
.map((op) => (op.kind === "fillCircle" ? op.x : 0));
expect(xs).toContain(-50);
expect(xs).toContain(950);
expect(xs).toContain(1950);
expect(circleXs).toContain(-50);
expect(circleXs).toContain(950);
expect(circleXs).toContain(1950);
});
test("empty input still returns no ops in torus mode", () => {