diff --git a/app/diorama/scene.ts b/app/diorama/scene.ts index 9330b83..4aa04e0 100644 --- a/app/diorama/scene.ts +++ b/app/diorama/scene.ts @@ -194,6 +194,29 @@ function hitOutline(polys: Poly[], ring: { cx: number; cy: number; rx: number; r return hull.map(([x, y]) => `${(cx + (x - cx) * grow).toFixed(1)},${(cy + (y - cy) * grow).toFixed(1)}`).join(' ') } +/** Distance from point p to the segment a–b. */ +function segmentDistance([px, py]: V2, [ax, ay]: V2, [bx, by]: V2): number { + const dx = bx - ax + const dy = by - ay + const t = Math.max(0, Math.min(1, ((px - ax) * dx + (py - ay) * dy) / (dx * dx + dy * dy || 1))) + return Math.hypot(px - ax - dx * t, py - ay - dy * t) +} + +/** A footpath along the points: a strip per stretch and a round patch at every bend. */ +function trail(pts: V2[], half: number, seed: number): Face[] { + const faces: Face[] = [] + for (let i = 0; i < pts.length - 1; i++) { + const [ax, ay] = pts[i]! + const [bx, by] = pts[i + 1]! + const l = Math.hypot(bx - ax, by - ay) || 1 + const nx = (-(by - ay) / l) * half + const ny = ((bx - ax) / l) * half + faces.push(plane([[ax + nx, ay + ny, 1.1], [bx + nx, by + ny, 1.1], [bx - nx, by - ny, 1.1], [ax - nx, ay - ny, 1.1]], DIRT)) + } + for (const [i, [x, y]] of pts.slice(1, -1).entries()) faces.push(plane(disc(x, y, half * 1.05, 8, seed + i, 1.1), DIRT)) + return faces +} + function screenRing(x: number, y: number, r: number) { const [cx, cy] = project([x, y, 0]) return { cx, cy, rx: r * Math.SQRT2, ry: r * Math.SQRT1_2 } @@ -607,6 +630,46 @@ export function buildScene(village: VillageState, frame: number | { width: numbe } decalFaces.push(road(FRONT, hqRadius * 0.9, Rz * 3, PATH_HALF, 1)) for (const zone of activeZones) decalFaces.push(road(ZONES[zone].angle, hqRadius * 0.9, zoneEnd(zone) + U * 0.5, ZONE_PATH_HALF, ZONE_NAMES.indexOf(zone) + 2)) + // Footpaths: from every door to the nearest road or the plaza, around other buildings, + // with a trodden patch in front of the door; from every site outside to its zone road. + const blockers = [{ key: 'hq', x: 0, y: 0, r: hqRadius * 0.8 }, ...allSlots.filter((sl) => sl.key !== 'hq').map((sl) => ({ key: sl.key, x: sl.x, y: sl.y, r: sl.r * 0.85 }))] + const length = (pts: V2[]) => pts.slice(1).reduce((n, p, i) => n + Math.hypot(p[0] - pts[i]![0], p[1] - pts[i]![1]), 0) + for (const b of village.buildings) { + if (b.type === 'headquarters' || b.type === 'wall') continue + const slot = slotOf(`b${b.id}`) + const zone = ZONE_OF[b.type] + if (zone) { + const [dx, dy] = dir(ZONES[zone].angle) + const t = slot.x * dx + slot.y * dy + if (Math.abs(slot.x * dy - slot.y * dx) > U * 0.6) decalFaces.push(...trail([[slot.x, slot.y], [dx * t, dy * t]], U * 0.17, b.id)) + continue + } + const door = doorOf.get(b.id) + const from: V2 = door ?? [slot.x, slot.y] + const step: V2 = door ? [door[0] + U * 0.35, door[1]] : from + const targets: V2[] = [] + const r = Math.hypot(step[0], step[1]) || 1 + targets.push([(step[0] / r) * hqRadius * 1.15, (step[1] / r) * hqRadius * 1.15]) + for (const a of roads) { + const [dx, dy] = dir(a) + const t = step[0] * dx + step[1] * dy + if (t > hqRadius && t < Rv + U) targets.push([dx * t, dy * t]) + } + const clear = (pts: V2[]) => + blockers.every((o) => { + const radius = o.key === slot.key ? slot.s * 0.85 : o.r + return pts.slice(1).every((p, i) => segmentDistance([o.x, o.y], pts[i]!, p) >= radius) + }) + const shapes = targets.flatMap((t): V2[][] => [ + [step, t], + [step, [step[0], t[1]], t], + [step, [t[0], step[1]], t], + ]) + shapes.sort((p, q) => length(p) - length(q)) + const best = shapes.find(clear) ?? shapes[0]! + if (door) decalFaces.push(plane(disc(step[0], step[1], U * 0.42, 9, b.id + 50, 1.1), DIRT)) + decalFaces.push(...trail(door ? [from, ...best] : best, U * 0.2, b.id)) + } decalFaces.push(...ringBand(Rv, U * (wall || wallPlanned ? 0.55 : 0.4), wall || wallPlanned ? WALL_EARTH : DIRT)) decalFaces.push(plane(disc(0, 0, hqRadius * 1.3, 11, 3, 1.6), PLAZA))