import { hex, mix, type RGB } from '../../engine/color'; import { hash } from '../../engine/rng'; import { sampleShape, Stamp } from '../../engine/stamp'; import type { Surface } from '../../engine/Surface'; import { clamp01, ease, pathTrack, track } from '../../engine/Timeline'; import { floorSunAt } from './stream'; import { sunlit, TIMES, type Frame, type Thing } from './timeline'; import { F, project, streamHalf, streamX } from './world'; /** * A mule deer doe and her family come down the canyon in the blue before dawn. They stop at the * stream to drink, walk on toward the camera, drink again as the sun breaks through the arch, * then wade across and wander off to the left. */ /** A point on the right bank, `off` meters back from the water's edge. */ const bank = (z: number, off: number): [number, number] => [streamX(z) + streamHalf(z) + off, z]; interface Move { t0: number; t1: number; pts: [number, number][]; } const T = TIMES.deer; const MOVES: Move[] = [ { t0: T, t1: T + 46, pts: [bank(74, 1.2), bank(52, 1.0), bank(38, 0.7), bank(29, 0.18)] }, { t0: T + 71, t1: T + 108, pts: [bank(29, 0.18), bank(20, 0.9), bank(12, 0.8), bank(7.2, 0.12)] }, { t0: T + 134, t1: T + 156, pts: [bank(7.2, 0.12), [streamX(6.4), 6.4], [streamX(5.9) - streamHalf(5.9) - 0.5, 5.9], [-3.5, 5.6], [-7.5, 5.4]], }, ]; /** Head down to drink (1) or raised (0), for the doe; the others follow her lead with a lag. */ const DRINK = track([ [T + 46, 0], [T + 49, 1], [T + 57, 1], [T + 59, 0.15], [T + 62, 0.15], [T + 64, 1], [T + 69, 1], [T + 71, 0], [T + 108, 0], [T + 111, 1], [T + 117, 1], // The sun breaks through the arch: she lifts her head and looks up the canyon at it. [T + 119, 0], [T + 125, 0], [T + 127, 1], [T + 132, 1], [T + 134, 0], ]); /** Progress 0–1 along a move, easing in and out over about a second at each end. */ function along(t: number, m: Move): number { const d = m.t1 - m.t0; const a = Math.min(1.2, d / 3); const u = clamp01((t - m.t0) / d) * d; // Distance under a trapezoid speed profile. let s: number; if (u < a) s = (u * u) / (2 * a); else if (u > d - a) s = d - a - (d - u) ** 2 / (2 * a); else s = u - a / 2; return s / (d - a); } interface Deer { lag: number; dz: number; dOff: number; size: number; coat: RGB; paths: ((u: number) => [number, number])[]; seed: number; } function makeDeer(lag: number, dz: number, dOff: number, size: number, coat: string, seed: number): Deer { return { lag, dz, dOff, size, coat: hex(coat), seed, paths: MOVES.map((m, mi) => { const pts = m.pts.map(([X, z], i): [number, number] => { // Followers stand a little further up the bank; on the final crossing they spread out. const zz = z + dz * (mi === 2 && i > 0 ? 0.4 : 1); return [X + (streamX(zz) - streamX(z)) + dOff * (mi === 2 ? 0.5 : 1), zz]; }); return pathTrack(pts.map(([X, z], i) => [i / (pts.length - 1), X, z] as const)); }), }; } const HERD: Deer[] = [ makeDeer(0, 0, 0, 1, '#9c7a5c', 1), makeDeer(3.5, 1.6, 0.55, 0.88, '#94765a', 2), makeDeer(6.5, 3.1, 0.15, 0.66, '#b08462', 3), makeDeer(9, 2.3, 1.0, 0.62, '#aa8060', 4), ]; /** Where a deer is (world X, z) and which move it is on or last finished. */ function place(d: Deer, t: number): { X: number; z: number; move: number; moving: boolean } { const tt = t - d.lag; let move = 0; for (let i = 0; i < MOVES.length; i++) if (tt >= MOVES[i].t0) move = i; const m = MOVES[move]; const [X, z] = d.paths[move](along(tt, m)); return { X, z, move, moving: tt > m.t0 && tt < m.t1 }; } /** Screen-space direction of travel during a move: +1 right, -1 left. */ function moveDir(d: Deer, move: number, u: number): number { const a = d.paths[move](Math.max(0, u - 0.02)); const b = d.paths[move](Math.min(1, u + 0.02)); const xa = project(a[0], a[1])[0]; const xb = project(b[0], b[1])[0]; return xb >= xa ? 1 : -1; } function facing(d: Deer, t: number): number { const tt = t - d.lag; const p = place(d, t); const m = MOVES[p.move]; const u = along(tt, m); const dir = moveDir(d, p.move, Math.min(u, 0.98)); if (p.move === 0 || tt > m.t0 + 1.6) return dir; // Turn around at the start of a move if it heads the other way. const prev = moveDir(d, p.move - 1, 0.98); return prev + (dir - prev) * ease.sine(clamp01((tt - m.t0 + 0.2) / 1.6)); } // --------------------------------------------------------------------------- // Drawing // --------------------------------------------------------------------------- interface DeerPal { coat: RGB; dark: RGB; light: RGB; muzzle: RGB; nose: RGB; ear: RGB; hoof: RGB; } /** Distance from p to a tapering limb running from a to b; <= 0 means inside. */ function limb(lx: number, ly: number, ax: number, ay: number, bx: number, by: number, r0: number, r1: number): number { const vx = bx - ax; const vy = by - ay; const u = Math.max(0, Math.min(1, ((lx - ax) * vx + (ly - ay) * vy) / (vx * vx + vy * vy))); return Math.hypot(lx - ax - vx * u, ly - ay - vy * u) - (r0 + (r1 - r0) * u); } /** * The deer in local units of 10 cm: x forward, y down, origin mid-body 85 cm above the ground. * `phase` drives the walk, `stride` its size (0 standing), `drink` lowers the head to the water. */ function deerSampler(p: DeerPal, phase: number, stride: number, drink: number, flick: number, earTwitch: number) { // The neck rises forward from the shoulders; to drink it swings down and the shoulders dip. const baseY = -0.6 + drink * 1.2; const neckA = -0.9 + drink * 2.1; const neckL = 4 + drink * 2.2; const nx = 4.4 + Math.cos(neckA) * neckL; const ny = baseY + Math.sin(neckA) * neckL; const headA = 0.45 + drink * 1.0; const hcx = nx + Math.cos(headA) * 1.1; const hcy = ny + Math.sin(headA) * 1.1; const hdx = Math.cos(headA); const hdy = Math.sin(headA); const legs = [ { hipX: 3.6, ph: 0.25, far: true }, { hipX: -4, ph: 0, far: true }, { hipX: 4, ph: 0.75, far: false }, { hipX: -3.6, ph: 0.5, far: false }, ].map((l) => { const a = (phase + l.ph) * Math.PI * 2; return { ...l, fx: l.hipX + Math.sin(a) * 1.6 * stride, fy: 8.5 - Math.max(0, Math.cos(a)) * 1.1 * stride, kx: l.hipX + Math.sin(a) * 0.9 * stride + (l.hipX > 0 ? -0.3 : 0.4), }; }); const leg = (lx: number, ly: number, far: boolean): RGB | undefined => { for (const l of legs) { if (l.far !== far) continue; const ky = 4.6; if (limb(lx, ly, l.hipX, 1, l.kx, ky, 1.1, 0.55) <= 0 || limb(lx, ly, l.kx, ky, l.fx, l.fy, 0.55, 0.42) <= 0) { const c = ly > l.fy - 0.7 ? p.hoof : ly > 2.5 && lx < l.kx ? p.light : p.coat; return far ? mix(c, p.dark, 0.5) : c; } } return undefined; }; return (lx: number, ly: number): RGB | undefined => { if (lx < -8.4 || lx > 11.5 || ly < -9 || ly > 10) return undefined; // Head, ears and neck in front of the body. const rx = lx - hcx; const ry = ly - hcy; const nearHead = rx * rx + ry * ry < 16; const along = rx * hdx + ry * hdy; const across = -rx * hdy + ry * hdx; const taper = 1.25 - Math.max(0, along) * 0.18; if (nearHead && along > -1.6 && along < 2.4 && Math.abs(across) < taper) { if (along > 1.7) return p.nose; if (along > 0.9) return p.muzzle; if (along > -0.6 && along < -0.1 && across < -0.2 && across > -0.8) return p.nose; return across > 0.5 ? p.light : p.coat; } for (let side = 0; side < 2 && nearHead; side++) { const ea = headA - 2.1 - side * 0.45 - earTwitch * (side ? 0.4 : 0); const ex = hcx - hdx * 0.9 + Math.cos(ea) * 1.4; const ey = hcy - hdy * 0.9 + Math.sin(ea) * 1.4; if (limb(lx, ly, hcx - hdx * 0.9, hcy - hdy * 0.9, ex, ey, 0.55, 0.75) <= 0) return side ? mix(p.coat, p.dark, 0.4) : p.ear; } if (lx > 2 && limb(lx, ly, 4.0, baseY + 0.4, nx, ny, 2, 1.15) <= 0) return ly > ny + 1.2 && lx > 5.4 && drink < 0.5 ? p.light : p.coat; const near = ly > 0 ? leg(lx, ly, false) : undefined; if (near) return near; // Tail: a dark-tipped rope that flicks up to show the white rump. if (lx < -5.5 && limb(lx, ly, -6.3, -1.2, -7.1 - flick * 0.4, 0.8 - flick * 2.6, 0.55, 0.4) <= 0) return ly > -1.2 + (1 - flick) * 1.6 ? p.nose : p.light; const body = (lx / 6.4) ** 2 + ((ly + (lx < 0 ? 0.15 : 0)) / 2.8) ** 2; const chest = Math.hypot(lx - 4.2, ly - 0.3) < 2.5; if (body <= 1 || chest) { if (lx < -5 && ly < 0.6) return p.light; if (ly > 1.7) return p.light; return ly < -1.8 ? mix(p.coat, p.light, 0.25) : p.coat; } return ly > 0 ? leg(lx, ly, true) : undefined; }; } const stamp = new Stamp(); function drawDeer(s: Surface, f: Frame, d: Deer): void { const t = f.t; const p = place(d, t); const k = F / p.z; const sc = (k / 10) * d.size; const [x, y] = project(p.X, p.z, 0.85 * d.size); if (x < -40 || x > 360) return; const sun = Math.max(floorSunAt(f, p.X, p.z), 0); const L = (c: RGB) => sunlit(f, c, sun); const pal: DeerPal = { coat: L(d.coat), dark: L(mix(d.coat, hex('#3c2c22'), 0.55)), light: L(hex('#e8dece')), muzzle: L(hex('#5a4a3e')), nose: L(hex('#1e1814')), ear: L(hex('#c8a890')), hoof: L(hex('#2c2420')), }; // Walk cycle runs with distance covered, so legs never skate. const pa = place(d, t - 0.15); const pb = place(d, t + 0.15); const speed = Math.hypot(pb.X - pa.X, pb.z - pa.z) / 0.3; const stride = Math.min(1, speed / 0.9); const phase = t * 1.05 + d.seed * 0.37; const drink = DRINK(t - d.lag * 0.85 - d.seed * 0.6); const flick = Math.max(0, Math.sin(t * 0.9 + d.seed * 2) * 6 - 5); const twitch = hash(Math.floor(t * 0.7 + d.seed), d.seed, 9) > 0.85 ? 1 : 0; const v = { x, y, scale: sc, facing: facing(d, t) }; sampleShape(stamp, v, 11.5, deerSampler(pal, phase, stride, drink, flick, twitch)).draw(s, 0.72); drawRipples(s, f, p.X, p.z, sc, stride, d.seed); } /** Rings spreading on the water around a deer's legs while it wades or drinks. */ function drawRipples(s: Surface, f: Frame, X: number, z: number, sc: number, stride: number, seed: number): void { const e = Math.abs(X - streamX(z)) - streamHalf(z); if (e > 0.45) return; const [cx, cy] = project(X, z); for (let i = 0; i < 3; i++) { const age = (f.t * (0.6 + stride * 0.6) + i / 3 + seed * 0.13) % 1; const r = (2 + age * 12) * sc; const a = (1 - age) * 0.35 * (e < 0 ? 1 : 0.5); for (let j = 0; j < 40; j++) { const ang = (j / 40) * Math.PI * 2; const px = cx + Math.cos(ang) * r; const py = cy + Math.sin(ang) * r * 0.28; if (Math.abs(project(X, z)[0] - px) < 1 && py < cy) continue; const pxW = ((px - project(0, z)[0]) * z) / F; if (Math.abs(pxW - streamX(z)) > streamHalf(z)) continue; s.blend(px, py, [235, 240, 248], a * (0.5 + 0.5 * f.daylight)); } } } export function deerThings(f: Frame): Thing[] { if (f.t < TIMES.deer - 1) return []; return HERD.map((d) => { const p = place(d, f.t); return { z: p.z - 0.01, draw: (s: Surface, fr: Frame) => drawDeer(s, fr, d) }; }); }