2026-10-29 13:38:34 +00:00
|
|
|
// 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[]
|
2026-10-29 14:27:40 +00:00
|
|
|
/**
|
|
|
|
|
* Back to front: objects, and one entry per worker shown (a screen path walked there
|
|
|
|
|
* and back, with what they carry), sorted in among the objects by depth.
|
|
|
|
|
*/
|
|
|
|
|
layers: ({ key: string; polys: Poly[]; walk?: undefined } | { key: string; walk: Walk; polys?: undefined })[]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface Walk {
|
|
|
|
|
d: string
|
|
|
|
|
seconds: number
|
|
|
|
|
delay: number
|
|
|
|
|
carry: string | null
|
2026-10-29 13:38:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Kind = 'timber' | 'clay' | 'iron' | 'farm' | 'wall' | 'core'
|
|
|
|
|
const KIND: Partial<Record<BuildingType, Kind>> = { timber_camp: 'timber', clay_pit: 'clay', iron_mine: 'iron', farm: 'farm', wall: 'wall' }
|
|
|
|
|
const GROUND: Record<Kind, string> = {
|
|
|
|
|
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<Kind, string | null> = { 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 }]
|
2026-10-29 14:27:40 +00:00
|
|
|
/** Where workers go; each leaves the building on the side that faces their spot. */
|
2026-10-29 13:38:34 +00:00
|
|
|
const spots: V2[] = []
|
|
|
|
|
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 }
|
|
|
|
|
|
2026-10-29 14:27:40 +00:00
|
|
|
// A worker sorts at the depth of the point of their path furthest back, so the building
|
|
|
|
|
// hides those who work behind it and those in front walk over it.
|
2026-10-29 13:38:34 +00:00
|
|
|
const shown = Math.min(MAX_WALKERS, Math.max(0, workers))
|
2026-10-29 14:27:40 +00:00
|
|
|
const walkers: { key: string; depth: number; walk: Walk }[] = []
|
2026-10-29 13:38:34 +00:00
|
|
|
for (let i = 0; i < shown && spots.length; i++) {
|
|
|
|
|
const spot = spots[i % spots.length]!
|
2026-10-29 14:27:40 +00:00
|
|
|
const path: V2[] =
|
|
|
|
|
kind === 'wall'
|
|
|
|
|
? [0, 15, 30, 45, 60, 75, 90].map((d) => at(deg(i * 90 + d), S * 2.0))
|
|
|
|
|
: [at(Math.atan2(spot[1], spot[0]), S * 1.5), spot]
|
|
|
|
|
const length = path.slice(1).reduce((n, p, k) => n + Math.hypot(p[0] - path[k]![0], p[1] - path[k]![1]), 0)
|
2026-10-29 13:38:34 +00:00
|
|
|
const seconds = Math.max(7, (length / U) * 2.4)
|
2026-10-29 14:27:40 +00:00
|
|
|
walkers.push({
|
|
|
|
|
key: `walker${i}`,
|
|
|
|
|
depth: Math.min(...path.map(([x, y]) => x + y)),
|
|
|
|
|
walk: { d: line(path), seconds, delay: -(i * seconds) / Math.max(1, shown), carry: CARRY[kind] },
|
|
|
|
|
})
|
2026-10-29 13:38:34 +00:00
|
|
|
}
|
|
|
|
|
|
2026-10-29 14:27:40 +00:00
|
|
|
const layers = [...objects, ...walkers].sort((a, b) => a.depth - b.depth)
|
2026-10-29 13:38:34 +00:00
|
|
|
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])),
|
2026-10-29 14:27:40 +00:00
|
|
|
layers: layers.map((l) => ('walk' in l ? { key: l.key, walk: l.walk } : { key: l.key, polys: l.polys })),
|
2026-10-29 13:38:34 +00:00
|
|
|
}
|
|
|
|
|
}
|