// A close-up of one building, for its own screen: the building larger, the ground and // props of its kind around it, forest beyond, and its workers walking between the // building and the spots where they work (trees for a timber camp, pits for a clay pit, // rocks for an iron mine, fields for a farm, the wall walk for the wall). import type { BuildingType } from '~/types/game' import { type Face, hash, plane, type Poly, project, rect, scale, toPolys, type V2, type V3 } from './iso' import { broadleaf, buildingModel, bush, C, conifer, rock } from './models' import { disc, ground, shadow, U, wallSegments } from './scene' export interface Closeup { viewBox: string width: number height: number terrain: Poly[] decals: Poly[] objects: { key: string; polys: Poly[] }[] /** One per worker shown: a screen path walked there and back, with what they carry. */ walks: { d: string; seconds: number; delay: number; carry: string | null }[] } type Kind = 'timber' | 'clay' | 'iron' | 'farm' | 'wall' | 'core' const KIND: Partial> = { timber_camp: 'timber', clay_pit: 'clay', iron_mine: 'iron', farm: 'farm', wall: 'wall' } const GROUND: Record = { timber: '#76703f', clay: '#8d5a3b', iron: '#7c776a', farm: '#7f8f4b', wall: '#8c7350', core: '#a3885c', } /** What a worker carries home: logs, clay, ore, sheaves; core buildings move crates. */ const CARRY: Record = { timber: C.wood, clay: C.clay, iron: C.ore, farm: C.wheat, wall: null, core: C.plank } /** Workers drawn at most; more would crowd the scene. */ const MAX_WALKERS = 8 const S = U * 1.7 const deg = (d: number) => (d * Math.PI) / 180 const at = (a: number, r: number): V2 => [Math.cos(a) * r, Math.sin(a) * r] const screen = ([x, y]: V2, z = 1.2) => project([x, y, z]) const line = (pts: V2[]) => pts.map((p, i) => `${i ? 'L' : 'M'}${screen(p).map((v) => v.toFixed(1)).join(' ')}`).join(' ') export function buildCloseup(type: BuildingType, level: number, workers: number, aspect = 2): Closeup { const kind = KIND[type] ?? 'core' const objects: { key: string; depth: number; polys: Poly[] }[] = [] const decals: Face[] = [plane(disc(0, 0, S * 2.3, 16, 7, 0.5), GROUND[kind])] const shadows: Face[] = [] const taken: { x: number; y: number; r: number }[] = [{ x: 0, y: 0, r: S * 1.5 }] /** Where workers go; the door is at the front, towards the viewer. */ const spots: V2[] = [] const door: V2 = at(deg(45), S * 0.95) const put = (key: string, x: number, y: number, faces: Face[], r: number) => { objects.push({ key, depth: x + y, polys: toPolys(faces) }) taken.push({ x, y, r }) } if (kind === 'wall') { // A small fort: the wall round an empty yard, guards walking along it. const Rw = S * 2.2 wallSegments(Rw, level, [deg(45)]).forEach((seg, i) => objects.push({ key: `w${i}`, depth: seg.depth, polys: toPolys(seg.faces) })) taken[0] = { x: 0, y: 0, r: Rw + U } } else { const model = buildingModel(type, level, S) objects.push({ key: 'building', depth: 0, polys: toPolys(model.faces) }) shadows.push(shadow(0, 0, S * 0.95, scale(GROUND[kind], 0.8), 3)) } if (kind === 'timber') { for (let i = 0; i < 6; i++) { const [x, y] = at(deg(-80 + i * 34 + (hash(i, 1) - 0.5) * 14), S * (2.4 + 0.5 * hash(i, 2))) put(`tree${i}`, x, y, conifer(x, y, U * (1.4 + 0.4 * hash(i, 3)), i), U * 0.7) spots.push([x * 0.85, y * 0.85]) } } else if (kind === 'clay') { for (let i = 0; i < 4; i++) { const [x, y] = at(deg(-20 + i * 42), S * 2.2) decals.push(plane(disc(x, y, U * 0.75, 9, 20 + i, 0.8), C.clayWet)) spots.push([x, y]) } } else if (kind === 'iron') { for (let i = 0; i < 5; i++) { const [x, y] = at(deg(150 + i * 26), S * (2.5 + 0.3 * hash(i, 4))) put(`rock${i}`, x, y, rock(x, y, U * (1.5 + 0.6 * hash(i, 5)), 40 + i, i % 2 ? C.stoneDark : C.rock), U * 0.9) spots.push([x * 0.8, y * 0.8]) } } else if (kind === 'farm') { for (let i = 0; i < 4; i++) { const [x, y] = at(deg(-40 + i * 50), S * 2.5) decals.push(plane(rect(x, y, U * 2.2, U * 1.5).map(([px, py]): V3 => [px, py, 0.7]), i % 2 ? C.wheat : C.field)) taken.push({ x, y, r: U * 1.4 }) spots.push([x, y]) } } else if (kind === 'wall') { // Guards walk stretches of the wall walk. for (let i = 0; i < 4; i++) spots.push(at(deg(i * 90 + 20), S * 2.0)) } else { for (let i = 0; i < 4; i++) spots.push(at(deg(-10 + i * 55), S * 2.0)) } // Forest and bushes beyond, thicker further out. const R = S * 2.75 const f = U * 1.25 for (let i = -14; i <= 14; i++) { for (let j = -14; j <= 14; j++) { const x = (i + hash(i, j, 61) * 0.8) * f const y = (j + hash(i, j, 62) * 0.8) * f const r = Math.hypot(x, y) if (r < S * 2.6 || r > S * 7) continue if (taken.some((t) => Math.hypot(x - t.x, y - t.y) < t.r + U * 0.4)) continue if (Math.abs(Math.atan2(y, x) - deg(45)) < 0.25 && r < S * 5) continue const density = 0.15 + 0.6 * Math.min(1, (r - S * 2.6) / (S * 2.5)) if (hash(i, j, 63) > density) continue const kindRoll = hash(i, j, 64) const size = U * (1.0 + 0.5 * hash(i, j, 65)) const seed = i * 131 + j put(`t${i}:${j}`, x, y, kindRoll < 0.15 ? bush(x, y, size * 0.8, seed) : kindRoll < 0.7 ? conifer(x, y, size, seed) : broadleaf(x, y, size, seed), size * 0.3) } } // Frame: the building, its spots and a ring of land round them, widened to the stage. const pts: V2[] = [] for (let i = 0; i < 24; i++) pts.push(screen(at((i / 24) * Math.PI * 2, R), 0)) let minX = Math.min(...pts.map((p) => p[0])) let maxX = Math.max(...pts.map((p) => p[0])) let minY = Math.min(...pts.map((p) => p[1])) - S * 1.2 let maxY = Math.max(...pts.map((p) => p[1])) let w = maxX - minX let h = maxY - minY if (w / h < aspect) { minX -= (h * aspect - w) / 2 w = h * aspect } else { minY -= (w / aspect - h) / 2 h = w / aspect } maxX = minX + w maxY = minY + h const view = { x: minX, y: minY, w, h } const shown = Math.min(MAX_WALKERS, Math.max(0, workers)) const walks: Closeup['walks'] = [] for (let i = 0; i < shown && spots.length; i++) { const spot = spots[i % spots.length]! const path = kind === 'wall' ? [at(deg(i * 90), S * 2.0), at(deg(i * 90 + 45), S * 2.0), at(deg(i * 90 + 90), S * 2.0)] : [door, spot] const length = Math.hypot(spot[0] - door[0], spot[1] - door[1]) const seconds = Math.max(7, (length / U) * 2.4) walks.push({ d: line(path), seconds, delay: -(i * seconds) / Math.max(1, shown), carry: CARRY[kind] }) } objects.sort((a, b) => a.depth - b.depth) return { viewBox: `${view.x.toFixed(1)} ${view.y.toFixed(1)} ${w.toFixed(1)} ${h.toFixed(1)}`, width: w, height: h, terrain: ground(view), decals: [...decals, ...shadows].flatMap((face) => toPolys([face])), objects: objects.map(({ key, polys }) => ({ key, polys })), walks, } }