diff --git a/public/audio/alpine-afternoon.mp3 b/public/audio/alpine-afternoon.mp3 new file mode 100644 index 0000000..e5c668b Binary files /dev/null and b/public/audio/alpine-afternoon.mp3 differ diff --git a/public/audio/fx-thunder-01.mp3 b/public/audio/fx-thunder-01.mp3 new file mode 100644 index 0000000..a49e2dc Binary files /dev/null and b/public/audio/fx-thunder-01.mp3 differ diff --git a/public/audio/fx-thunder-02.mp3 b/public/audio/fx-thunder-02.mp3 new file mode 100644 index 0000000..526be65 Binary files /dev/null and b/public/audio/fx-thunder-02.mp3 differ diff --git a/public/audio/fx-thunder-03.mp3 b/public/audio/fx-thunder-03.mp3 new file mode 100644 index 0000000..c686065 Binary files /dev/null and b/public/audio/fx-thunder-03.mp3 differ diff --git a/public/audio/fx-thunder-04.mp3 b/public/audio/fx-thunder-04.mp3 new file mode 100644 index 0000000..5b95485 Binary files /dev/null and b/public/audio/fx-thunder-04.mp3 differ diff --git a/src/meditations/alpine-afternoon/angler.ts b/src/meditations/alpine-afternoon/angler.ts new file mode 100644 index 0000000..be47751 --- /dev/null +++ b/src/meditations/alpine-afternoon/angler.ts @@ -0,0 +1,635 @@ +import { add, hex, mix, type RGB } from '../../engine/color'; +import { drawSprite, type SpriteArt } from '../../engine/sprite'; +import { Stamp } from '../../engine/stamp'; +import { ease, pathTrack, progress } from '../../engine/Timeline'; +import type { Surface } from '../../engine/Surface'; +import type { Ring } from './lake'; +import { drawBucket, drawRod, drawRodOnGround } from './props'; +import { lit, TIMES, type Frame } from './timeline'; +import { ANGLER, CAMP, RETRIEVE, TENT } from './world'; + +// --------------------------------------------------------------------------- +// Sprites, all facing right (mirrored to face left), 13 rows tall with the boots on the last row. +// Arms are drawn separately so the hands can hold a rod, a fish, a match or a pail. +// --------------------------------------------------------------------------- + +const UPPER: SpriteArt = ['..HHH..', '.hhhhhh', '..sses.', '..bsss.', '..bbb..', '.cCccc.', '.cCccc.', '.cCccc.', '.cCccc.']; +const UPPER_GAZE: SpriteArt = ['.HHH...', '..hhhh.', '..ssse.', '..bssss', '..bbb..', '.cCccc.', '.cCccc.', '.cCccc.', '.cCccc.']; + +const LEGS_STAND: SpriteArt = ['..pppp.', '..p..p.', '..p..p.', '..kk.kk']; +const LEGS_STRIDE: SpriteArt = ['..pppp.', '.pp..pp', '.p....p', 'kk....k']; +const LEGS_PASS: SpriteArt = ['..ppp..', '..ppp..', '...pp..', '...kkk.']; + +const STAND: SpriteArt = [...UPPER, ...LEGS_STAND]; + +/** Down on one knee. */ +const KNEEL: SpriteArt = [ + '........', + '........', + '........', + '...HHH..', + '..hhhhhh', + '...sses.', + '...bsss.', + '..cbbb..', + '.cCccc..', + '.cCccc..', + '..ppppp.', + '..P..pp.', + 'kPP...kk', +]; + +/** Sitting on the log, knees forward. */ +const SIT: SpriteArt = ['........', ...UPPER.slice(0, 8).map((r) => r + '.'), '.Pppppp.', '.....pp.', '.....pp.', '.....kkk']; +const SIT_GAZE: SpriteArt = ['........', ...UPPER_GAZE.slice(0, 8).map((r) => r + '.'), '.Pppppp.', '.....pp.', '.....pp.', '.....kkk']; + +const BASE: Record = { + H: hex('#a08a5c'), + h: hex('#7c6846'), + s: hex('#e2a482'), + e: hex('#2a1c18'), + b: hex('#6a4a32'), + c: hex('#c8402e'), + C: hex('#842624'), + p: hex('#5e5c4a'), + P: hex('#44423a'), + k: hex('#3a2a22'), +}; + +const FISH_ART: SpriteArt = ['.bbbbb..', 'tsrrrrse', '.wwwww..']; +const SMALL_FISH_ART: SpriteArt = ['.bb.', 'tsse', '.ww.']; +const FISH_BASE: Record = { + b: hex('#6a7048'), + s: hex('#c8d0d4'), + r: hex('#e07a8a'), + w: hex('#eef0f0'), + t: hex('#8a8e70'), + e: hex('#3a3a32'), +}; +const COOKED = hex('#c88a48'); +const COOKED_DARK = hex('#8a5428'); +const STICK = hex('#6a4a30'); +const LINE = hex('#eef2f4'); +const SUN_WARM = hex('#ffc890'); +const DOOR_DARK = hex('#20120a'); +const MATCH = hex('#fff0a0'); + +/** Rotate a sprite a quarter turn, so a fish hangs head-down from the line. */ +function hang(art: SpriteArt): SpriteArt { + const w = art[0].length; + return Array.from({ length: w }, (_, c) => art.map((row) => row[w - 1 - c]).join('')); +} +const FISH_HANG = hang(FISH_ART); +const SMALL_HANG = hang(SMALL_FISH_ART); + +// --------------------------------------------------------------------------- +// Choreography +// --------------------------------------------------------------------------- + +interface Cast { + t0: number; + dur: number; + land: readonly [number, number]; + bite?: number; + kind?: 'small' | 'keeper'; +} + +/** Unhurried casts as the afternoon wears on: a small fish he lets go, then a keeper. */ +const CASTS: readonly Cast[] = [ + { t0: 8, dur: 13, land: [150, 114] }, + { t0: 21, dur: 13, land: [130, 120] }, + { t0: 34, dur: 13, land: [168, 108] }, + { t0: 47, dur: 11, land: [142, 112], bite: 52.5, kind: 'small' }, + { t0: 71, dur: 13, land: [158, 116] }, + { t0: 84, dur: 13, land: [126, 110] }, + { t0: 97, dur: 13, land: [172, 118] }, + { t0: 110, dur: 17.5, land: [146, 110], bite: 114, kind: 'keeper' }, +]; + +/** The trout's runs while he plays it in. */ +const FIGHT = pathTrack([ + [114, 146, 111], + [116.5, 162, 116], + [119, 138, 123], + [121.5, 154, 128], + [124, 118, 130], + [125.5, 104, 132], +]); +const FIGHT_SPLASHES = [114, 117.2, 120.5, 123.6]; + +/** Key moments after the fishing, in seconds. */ +export const BEATS = { + release: [58, 67], + holdUp: [127.5, 130.5], + bucket: [130.5, 133.5], + pickUp: [133.5, 136], + walk: [136, 156], + setDown: [156, 158], + kneel: [158, 163.5], + warm: [163.5, 168], + reach: [168, 170], + cook: [170, 190], + eat: [190, 202], + gaze: [202, 211], + rise: [211, 213], + toTent: [213, 219.5], + enter: [219.5, 222], +} as const; + +const RELEASE_AT = 64; +const BITES = [191, 194.5, 198, 201]; + +const WALK = pathTrack([ + [BEATS.walk[0], ANGLER.x, ANGLER.feetY], + [140, 98, 145], + [145, 124, 155], + [150, 172, 159], + [BEATS.walk[1], CAMP.kneelX, CAMP.feetY], +]); + +/** Where the pail sits on the rock before he fills it, and where he lays the rod down. */ +const PAIL_ROCK = { x: 76, y: 137 }; +const ROD_ON_ROCK = [60, 136, 74, 131] as const; +const ROD_AT_CAMP = [247, 158, 266, 154] as const; + +/** Is he past the tent door yet? */ +export const doorClosed = (t: number): number => progress(t, BEATS.enter[1], BEATS.enter[1] + 1.5); + +const inside = (t: number, [a, b]: readonly [number, number]) => t >= a && t < b; + +// --------------------------------------------------------------------------- +// Rings on the water for the lake to draw +// --------------------------------------------------------------------------- + +function castLurePos(c: Cast, t: number): [number, number] { + const u = t - c.t0; + const reelEnd = c.dur - 1.7; + if (c.kind === 'keeper' && c.bite !== undefined && t >= c.bite) return FIGHT(t); + const k = Math.max(0, Math.min(1, (u - 3.3) / (reelEnd - 3.3))); + return [c.land[0] + (RETRIEVE[0] - c.land[0]) * k, c.land[1] + (RETRIEVE[1] - c.land[1]) * k]; +} + +export function anglerRings(t: number): Ring[] { + const out: Ring[] = []; + const add = (at: number, x: number, y: number, size: number, life: number, splash = false) => { + const age = t - at; + if (age >= 0 && age < life) out.push({ x, y, age, size, life, splash }); + }; + for (const c of CASTS) { + add(c.t0 + 2.5, c.land[0], c.land[1], 1, 3, true); + if (c.bite !== undefined) { + const [bx, by] = castLurePos(c, c.bite); + add(c.bite, bx, by, c.kind === 'keeper' ? 1.5 : 0.8, 3, true); + } + } + const small = CASTS[3]; + for (const at of [54, 55.4]) { + const [x, y] = castLurePos(small, at); + add(at, x, y, 0.6, 2, true); + } + for (const at of FIGHT_SPLASHES.slice(1)) { + const [x, y] = FIGHT(at); + add(at, x, y, 1.3, 3, true); + } + add(125.8, RETRIEVE[0], RETRIEVE[1], 1, 2.5, true); + add(RELEASE_AT, 99, 134, 0.9, 3.5, true); + return out; +} + +// --------------------------------------------------------------------------- +// Drawing +// --------------------------------------------------------------------------- + +interface Ctx { + s: Surface; + f: Frame; + pal: Record; + sx: number; + top: number; + w: number; + flip: boolean; +} + +/** Screen position of a sprite cell. */ +const at = (c: Ctx, col: number, row: number): [number, number] => [c.flip ? c.sx + (c.w - 1 - col) : c.sx + col, c.top + row]; + +function drawArm(c: Ctx, from: readonly [number, number], to: readonly [number, number]): void { + const [x0, y0] = from; + const [x1, y1] = to; + const n = Math.max(Math.abs(x1 - x0), Math.abs(y1 - y0), 1); + for (let i = 1; i <= n; i++) { + const x = Math.round(x0 + ((x1 - x0) * i) / n); + const y = Math.round(y0 + ((y1 - y0) * i) / n); + c.s.set(x, y, i === n ? c.pal.s : c.pal.c); + } +} + +function drawLine(s: Surface, f: Frame, from: readonly [number, number], to: readonly [number, number], sag: number): void { + const col = mix(lit(f, LINE), LINE, 0.3); + const len = Math.hypot(to[0] - from[0], to[1] - from[1]); + const n = Math.ceil(len * 1.3) + 1; + const glint = f.warm > 0.2 ? (f.t * 0.45) % 1 : -1; + const base = 0.32 + 0.2 * f.warm; + for (let i = 0; i <= n; i++) { + const u = i / n; + const x = from[0] + (to[0] - from[0]) * u; + const y = from[1] + (to[1] - from[1]) * u + sag * 4 * u * (1 - u); + const g = glint >= 0 && Math.abs(u - glint) < 0.04 ? 0.5 * f.warm : 0; + s.blend(x, y, col, base + g); + } +} + +function fishPalette(f: Frame, cooked = 0): Record { + const pal: Record = {}; + for (const [k, c] of Object.entries(FISH_BASE)) { + const done = k === 'b' || k === 't' ? COOKED_DARK : COOKED; + pal[k] = add(lit(f, mix(c, done, cooked)), SUN_WARM, f.warm * 0.15); + } + return pal; +} + +/** A glint of sun along a fish's flank. */ +function fishGlint(s: Surface, f: Frame, x: number, y: number): void { + if (f.warm <= 0.1) return; + const g = Math.floor(f.t * 5) % 9; + if (g < 6) s.blend(x + g, y, [255, 255, 255], 0.75 * f.warm); +} + +function castState(cast: Cast, t: number) { + const u = t - cast.t0; + const reelEnd = cast.dur - 1.7; + const angleAt = (v: number) => + v < 0.9 + ? 55 + 60 * ease.sine(v / 0.9) + : v < 1.3 + ? 115 - 90 * ease.in((v - 0.9) / 0.4) + : v < 3.3 + ? 25 + 5 * ease.sine((v - 1.3) / 2) + : v < reelEnd + ? 30 + : v < reelEnd + 1 + ? 30 + 25 * ease.sine(v - reelEnd) + : 55; + let angle = angleAt(u); + let bend = 0; + let mode: 'dangle' | 'air' | 'water' | 'fight' = 'dangle'; + let target: [number, number] = [0, 0]; + let crank = false; + let fish: 'small' | 'big' | undefined; + const hooked = cast.bite !== undefined && t >= cast.bite; + if (u >= 1.15 && u < 2.5) mode = 'air'; + else if (u >= 2.5 && u < reelEnd) { + mode = 'water'; + target = castLurePos(cast, t); + crank = u > 3.3; + } else if (u >= reelEnd && u < reelEnd + 1) mode = 'air'; + if (hooked && u < reelEnd) { + mode = 'fight'; + crank = true; + if (cast.kind === 'keeper') { + angle = 52 + Math.sin(t * 1.6) * 9; + bend = 0.55 + 0.2 * Math.sin(t * 3.1); + crank = Math.sin(t * 1.6) < 0.2; + } else { + angle = 36 + Math.sin(t * 9) * 3; + bend = 0.3; + target = [target[0] + Math.sin(t * 7) * 1.5, target[1]]; + } + } + if (hooked && u >= reelEnd) fish = cast.kind === 'keeper' ? 'big' : 'small'; + return { u, reelEnd, angle, angleAt, bend, mode, target, crank, fish }; +} + +export function drawAngler(s: Surface, f: Frame): void { + const t = f.t; + if (t >= BEATS.enter[1]) return; + + const pal: Record = {}; + let presence = 1; + if (inside(t, BEATS.enter)) presence = 1 - ease.sine(progress(t, BEATS.enter[0], BEATS.enter[1], ease.linear)); + for (const [k, c] of Object.entries(BASE)) pal[k] = mix(add(lit(f, c), SUN_WARM, f.warm * 0.12), DOOR_DARK, 1 - presence); + + // Things lying around that aren't in his hands. + if (t < BEATS.pickUp[0]) drawBucket(s, f, PAIL_ROCK.x, PAIL_ROCK.y); + if (inside(t, [BEATS.release[0] + 0.5, BEATS.release[1] - 0.5]) || inside(t, [BEATS.holdUp[1], BEATS.pickUp[0] + 0.5])) { + drawRodOnGround(s, f, ...ROD_ON_ROCK); + } + + if (t < BEATS.walk[0]) drawFishing(s, f, pal); + else if (t < BEATS.setDown[1]) drawWalkAndSetDown(s, f, pal); + else drawAtCamp(s, f, pal, presence); +} + +function drawFishing(s: Surface, f: Frame, pal: Record): void { + const t = f.t; + const x = ANGLER.x; + const feet = ANGLER.feetY; + + // Kneeling at the water's edge to let the small fish go. + if (inside(t, BEATS.release)) { + const kneeling = progress(t, BEATS.release[0], BEATS.release[0] + 1.2) * (1 - progress(t, BEATS.release[1] - 1, BEATS.release[1])); + if (kneeling < 0.5) { + drawStanding(s, f, pal, x, feet, false, (c) => { + drawRodPose(c, 55, 0, 'dangle', [0, 0], t < RELEASE_AT ? 'small' : undefined); + }); + return; + } + const c = ctx(s, f, pal, KNEEL, x, feet, false); + const hand: [number, number] = [97, 134]; + drawArm(c, at(c, 4, 8), hand); + if (t < RELEASE_AT) { + // Holding it gently in the shallows, facing upstream. + const sway = Math.round(Math.sin(t * 3) * 0.6); + drawSprite(s, SMALL_FISH_ART, hand[0] + sway, hand[1] - 1, fishPalette(f), false, 0.75); + } else { + const k = progress(t, RELEASE_AT, RELEASE_AT + 1.3, ease.out); + if (k < 1) drawSprite(s, SMALL_FISH_ART, hand[0] + 2 + k * 10, hand[1] - 1 - k * 4, fishPalette(f), false, 0.7 * (1 - k)); + } + return; + } + + // Admiring the trout, then putting it in the pail. + if (inside(t, BEATS.holdUp)) { + drawStanding(s, f, pal, x, feet, false, (c) => { + const back = at(c, 1, 8); + drawRod(s, f, back[0], back[1], 1, 108, 0, 15); + const hand: [number, number] = [c.sx + 7, c.top + 6]; + drawArm(c, at(c, 4, 5), hand); + const lift = Math.round(Math.sin(progress(t, BEATS.holdUp[0], BEATS.holdUp[1]) * Math.PI) * 1.5); + drawSprite(s, FISH_ART, hand[0] - 1, hand[1] - 2 - lift, fishPalette(f)); + fishGlint(s, f, hand[0], hand[1] - 1 - lift); + }); + return; + } + if (inside(t, BEATS.bucket)) { + const c = ctx(s, f, pal, KNEEL, x - 2, feet, true); + const k = progress(t, BEATS.bucket[0], BEATS.bucket[1] - 0.8); + const hand: [number, number] = [Math.round(84 - k * 4), Math.round(131 + k * 2)]; + drawArm(c, at(c, 4, 8), hand); + if (k < 1) drawSprite(s, FISH_ART, hand[0] - 7, hand[1] - 1, fishPalette(f), true); + return; + } + if (inside(t, BEATS.pickUp)) { + drawStanding(s, f, pal, x, feet, false, (c) => carryPose(c, progress(t, BEATS.pickUp[0], BEATS.pickUp[1]))); + return; + } + + // Casting and reeling. + const cast = CASTS.find((cc) => t >= cc.t0 && t < cc.t0 + cc.dur); + drawStanding(s, f, pal, x, feet, false, (c) => { + if (!cast) { + drawRodPose(c, 55, 0, 'dangle', [0, 0]); + return; + } + const st = castState(cast, t); + if (st.mode === 'air' && st.u < 2.5) { + // The lure flies out in an arc from where the rod tip was as it was let go. + const hand = rodHand(c); + const from = rodTipAt(hand, st.angleAt(1.15)); + const k = (st.u - 1.15) / 1.35; + const lx = from[0] + (cast.land[0] - from[0]) * k; + const ly = from[1] + (cast.land[1] - from[1]) * k - 22 * Math.sin(Math.PI * k); + drawRodPose(c, st.angle, st.bend, 'air', [lx, ly]); + } else if (st.mode === 'air') { + // Lifting the lure (or the fish) out at the end of the retrieve. + const hand = rodHand(c); + const tip = rodTipAt(hand, st.angle); + const k = ease.sine(st.u - st.reelEnd); + const lx = RETRIEVE[0] + (tip[0] - RETRIEVE[0]) * k; + const ly = RETRIEVE[1] + (tip[1] + 4 - RETRIEVE[1]) * k; + drawRodPose(c, st.angle, st.bend, 'air', [lx, ly], st.fish); + } else drawRodPose(c, st.angle, st.bend, st.mode, st.target, st.fish, st.crank); + }); +} + +const rodHand = (c: Ctx): [number, number] => [c.sx + 7, c.top + 6]; + +function rodTipAt(hand: readonly [number, number], angle: number, len = 15): [number, number] { + const a = (angle * Math.PI) / 180; + return [hand[0] + Math.cos(a) * len, hand[1] - Math.sin(a) * len]; +} + +/** Rod in his front hand, his other hand on the reel, and the line out to the lure or fish. */ +function drawRodPose( + c: Ctx, + angle: number, + bend: number, + mode: 'dangle' | 'air' | 'water' | 'fight', + target: readonly [number, number], + fish?: 'small' | 'big', + crank = false, +): void { + const { s, f } = c; + const hand = rodHand(c); + drawArm(c, at(c, 4, 5), hand); + const tip = drawRod(s, f, hand[0], hand[1], 1, angle, bend); + // Reel hand, turning the crank. + const turn = crank ? Math.floor(f.t * 7) % 2 : 0; + s.set(hand[0] - 2 + turn, hand[1] + 1 + turn, c.pal.s); + s.set(hand[0] - 3, hand[1] + 1, c.pal.c); + + if (mode === 'dangle') { + const hangLen = fish ? 3 : 4; + const end: [number, number] = [tip[0] + Math.sin(f.t * 1.3) * 0.5, tip[1] + hangLen]; + drawLine(s, f, tip, end, 0); + if (fish) drawHangingFish(s, f, end, fish); + else s.blend(end[0], end[1], [240, 240, 240], 0.8); + } else if (mode === 'air') { + drawLine(s, f, tip, target, 1); + if (fish) drawHangingFish(s, f, target, fish); + else s.blend(target[0], target[1], [240, 240, 240], 0.9); + } else { + const len = Math.hypot(target[0] - tip[0], target[1] - tip[1]); + drawLine(s, f, tip, target, mode === 'fight' ? 0 : 1.5 + len * 0.04); + // A little V of wake where the line meets the water. + const wake = mix(lit(f, LINE), LINE, 0.2); + s.blend(target[0] + 1, target[1], wake, 0.4); + s.blend(target[0] + 2, target[1] - 1, wake, 0.25); + s.blend(target[0] + 2, target[1] + 1, wake, 0.25); + } +} + +function drawHangingFish(s: Surface, f: Frame, at: readonly [number, number], fish: 'small' | 'big'): void { + const wiggle = Math.round(Math.sin(f.t * 8)); + const art = fish === 'big' ? FISH_HANG : SMALL_HANG; + drawSprite(s, art, Math.round(at[0]) - 1 + (fish === 'big' ? wiggle : 0), Math.round(at[1]), fishPalette(f), wiggle > 0 && fish === 'small'); +} + +const STAMP = new Stamp(); + +/** Draw his body with a soft dark outline so he reads against the grass and rock. */ +function ctx(s: Surface, f: Frame, pal: Record, art: SpriteArt, x: number, feet: number, flip: boolean, alpha = 1): Ctx { + const sx = Math.round(x) - 3; + const top = Math.round(feet) - (art.length - 1); + const w = art[0].length; + STAMP.begin(sx, top, w, art.length); + for (let r = 0; r < art.length; r++) { + for (let c = 0; c < w; c++) { + const col = pal[art[r][c]]; + if (col) STAMP.put(flip ? sx + w - 1 - c : sx + c, top + r, col); + } + } + STAMP.draw(s, 0.72, alpha); + return { s, f, pal, sx, top, w, flip }; +} + +function drawStanding( + s: Surface, + f: Frame, + pal: Record, + x: number, + feet: number, + flip: boolean, + then: (c: Ctx) => void, + legs: SpriteArt = LEGS_STAND, + alpha = 1, +): void { + const c = ctx(s, f, pal, legs === LEGS_STAND ? STAND : [...UPPER, ...legs], x, feet, flip, alpha); + then(c); +} + +/** Pail in his back hand, rod over his shoulder. `lift` 0–1 raises the pail from the rock. */ +function carryPose(c: Ctx, lift = 1): void { + const { s, f } = c; + const back = at(c, 1, 8); + drawBucket(s, f, back[0] - 2, Math.round(back[1] + 7 - lift * 1), true); + s.set(back[0], back[1], c.pal.s); + const front: [number, number] = at(c, 5, 7); + drawArm(c, at(c, 4, 5), front); + drawRod(s, f, front[0], front[1], c.flip ? -1 : 1, 128, 0, 14); +} + +function drawWalkAndSetDown(s: Surface, f: Frame, pal: Record): void { + const t = f.t; + if (t < BEATS.walk[1]) { + const [x, feet] = WALK(t); + const dist = x - ANGLER.x; + const phase = Math.floor(dist / 2.5) % 4; + const legs = phase === 0 || phase === 2 ? LEGS_STRIDE : LEGS_PASS; + drawStanding(s, f, pal, x, feet, false, (c) => carryPose(c), legs); + return; + } + // Setting the pail down by the log and laying the rod in the grass. + const k = progress(t, BEATS.setDown[0], BEATS.setDown[0] + 1); + if (k < 1) { + drawStanding(s, f, pal, CAMP.kneelX, CAMP.feetY, false, (c) => carryPose(c)); + return; + } + const c = ctx(s, f, pal, KNEEL, CAMP.kneelX, CAMP.feetY, false); + drawArm(c, at(c, 4, 8), [CAMP.bucket.x + 1, CAMP.bucket.y - 5]); +} + +function drawAtCamp(s: Surface, f: Frame, pal: Record, presence: number): void { + const t = f.t; + const { seatX, kneelX, feetY } = CAMP; + + // Kneeling at the pit to light the fire. + if (inside(t, BEATS.kneel)) { + const c = ctx(s, f, pal, KNEEL, kneelX, feetY, true); + const strike = t - TIMES.match; + const hand: [number, number] = strike > -0.6 && strike < 0 ? [228, 148] : [227, 151]; + drawArm(c, at(c, 4, 8), hand); + if (strike >= 0 && strike < 1.6) { + const k = 1 - strike / 1.6; + s.set(hand[0] - 1, hand[1] - 1, mix(MATCH, [255, 150, 60], 1 - k)); + for (let dy = -6; dy <= 6; dy++) { + for (let dx = -6; dx <= 6; dx++) { + const d = Math.hypot(dx, dy) / 6; + if (d < 1) s.light(hand[0] + dx, hand[1] + dy, [255, 190, 110], 0.35 * k * (1 - d) * (1 - d)); + } + } + } + return; + } + + // Sitting on the log by the fire. + if (t < BEATS.rise[0]) { + const gaze = inside(t, BEATS.gaze); + const c = ctx(s, f, pal, gaze ? SIT_GAZE : SIT, seatX, CAMP.log.y + 2, true); + const shoulder = at(c, 4, 6); + if (inside(t, BEATS.warm)) { + drawArm(c, shoulder, [231, 147]); + } else if (inside(t, BEATS.reach)) { + const k = Math.sin(progress(t, BEATS.reach[0], BEATS.reach[1]) * Math.PI); + drawArm(c, shoulder, [Math.round(236 + k * 5), Math.round(147 + k * 4)]); + } else if (inside(t, BEATS.cook)) { + const hand: [number, number] = [231, 147]; + drawArm(c, shoulder, hand); + drawStickFish(s, f, hand, [222, 143], progress(t, BEATS.cook[0], BEATS.cook[1] - 2), Math.floor(t / 5) % 2 === 1, 4); + } else if (inside(t, BEATS.eat)) { + let bite = 0; + let eaten = 0; + for (const b of BITES) { + if (t >= b + 1.4) eaten++; + else if (t >= b) bite = Math.sin(((t - b) / 1.4) * Math.PI); + } + const hand: [number, number] = [Math.round(231 + bite * 3), Math.round(148 - bite * 4)]; + drawArm(c, shoulder, hand); + const left = 4 - eaten; + if (left > 0) drawStickFish(s, f, hand, [hand[0] - 3, hand[1] - 1], 1, false, left); + } else { + // Hands resting on his knees. + drawArm(c, shoulder, at(c, 6, 9)); + } + return; + } + + // Up from the log and over to the tent as the drizzle starts. + if (inside(t, BEATS.rise)) { + drawStanding(s, f, pal, seatX, feetY, false, (c) => { + s.set(...at(c, 1, 8), c.pal.s); + }); + return; + } + const k = ease.sine(progress(t, BEATS.toTent[0], BEATS.toTent[1], ease.linear)); + const x = seatX + (TENT.doorX - seatX) * k; + const feet = feetY + (TENT.doorFeet - feetY) * k; + let legs = LEGS_STAND; + if (t < BEATS.toTent[1]) { + const phase = Math.floor((x - seatX) / 2.5) % 4; + legs = phase === 0 || phase === 2 ? LEGS_STRIDE : LEGS_PASS; + } + drawStanding( + s, + f, + pal, + x, + feet, + false, + (c) => { + s.blend(...at(c, 1, 8), c.pal.s, Math.min(1, presence * 1.5)); + }, + legs, + Math.min(1, presence * 1.5), + ); +} + +/** A stick with a piece of trout on the end, browning over the fire. */ +function drawStickFish( + s: Surface, + f: Frame, + hand: readonly [number, number], + end: readonly [number, number], + cooked: number, + turned: boolean, + len: number, +): void { + const stick = lit(f, STICK); + const n = Math.max(Math.abs(end[0] - hand[0]), Math.abs(end[1] - hand[1]), 1); + for (let i = 1; i <= n; i++) s.set(Math.round(hand[0] + ((end[0] - hand[0]) * i) / n), Math.round(hand[1] + ((end[1] - hand[1]) * i) / n), stick); + const pal = fishPalette(f, cooked); + const top = turned ? pal.w : pal.b; + const mid = turned ? pal.s : pal.r; + for (let i = 0; i < len; i++) { + s.set(end[0] - 1 - i, end[1], top); + s.set(end[0] - 1 - i, end[1] + 1, mid); + } +} + +/** The rod lying in the grass at camp, once he's set it down. */ +export function drawCampRod(s: Surface, f: Frame): void { + if (f.t >= BEATS.setDown[0] + 1) drawRodOnGround(s, f, ...ROD_AT_CAMP); +} + +/** Whether the pail sits by the log yet. */ +export const pailAtCamp = (t: number): boolean => t >= BEATS.setDown[0] + 1; diff --git a/src/meditations/alpine-afternoon/camp.ts b/src/meditations/alpine-afternoon/camp.ts new file mode 100644 index 0000000..d910297 --- /dev/null +++ b/src/meditations/alpine-afternoon/camp.ts @@ -0,0 +1,289 @@ +import { add, hex, mix, type RGB } from '../../engine/color'; +import { progress } from '../../engine/Timeline'; +import { hash, noise1 } from '../../engine/rng'; +import type { Surface } from '../../engine/Surface'; +import { doorClosed, drawCampRod, pailAtCamp } from './angler'; +import { drawBucket } from './props'; +import { lit, TIMES, type Frame } from './timeline'; +import { CAIRN, CAMP, H, TENT } from './world'; + +const FLY = hex('#e07a36'); +const FLY_SHADE = hex('#b0582a'); +const FLY_SEAM = hex('#8a4020'); +const DOOR = hex('#c86a30'); +const INTERIOR = hex('#22140c'); +const GUY = hex('#c8c4b8'); +const GLOW = hex('#ffc070'); +const SHADOW = hex('#2a2a22'); +const STONE = hex('#8e8a86'); +const STONE_LIGHT = hex('#b4aea6'); +const STONE_DARK = hex('#5a5654'); +const ASH = hex('#3a3430'); +const WOOD = hex('#6a4a30'); +const CHAR = hex('#2a1e18'); +const LOG = hex('#6e4c34'); +const LOG_TOP = hex('#94704c'); +const LOG_DARK = hex('#3e2a1e'); +const LOG_END = hex('#d0a870'); +const LOG_RING = hex('#8a6440'); +const FIRE_CORE = hex('#fff0a0'); +const FIRE_MID = hex('#ffae40'); +const FIRE_EDGE = hex('#e8582a'); +const EMBER = hex('#ff7a30'); +const SMOKE = hex('#a8a8b0'); +const FIRELIGHT = hex('#ff9a48'); +const SUN_WARM = hex('#ffc890'); + +export function drawCamp(s: Surface, f: Frame): void { + drawCairn(s, f); + drawTent(s, f); + drawFirePit(s, f); + drawLog(s, f); + if (pailAtCamp(f.t)) drawBucket(s, f, CAMP.bucket.x, CAMP.bucket.y); + drawCampRod(s, f); +} + +/** A little cairn of stacked stones above the shore, left by someone who came before. */ +function drawCairn(s: Surface, f: Frame): void { + const light = add(lit(f, STONE_LIGHT), SUN_WARM, f.warm * 0.2); + const mid = lit(f, STONE); + const dark = lit(f, STONE_DARK); + const stones: readonly (readonly [number, number])[] = [ + [5, 2], + [4, 2], + [3, 2], + [2, 1], + ]; + let y = CAIRN.base; + let i = 0; + for (const [w, h] of stones) { + const x0 = CAIRN.x - Math.floor(w / 2) + (i === 1 ? 1 : 0); + for (let dy = 0; dy < h; dy++) { + for (let dx = 0; dx < w; dx++) s.set(x0 + dx, y - 1 - dy, dy === h - 1 ? (dx === 0 ? light : mid) : dx === w - 1 ? dark : mid); + } + y -= h; + i++; + } + for (let dx = -2; dx <= 3; dx++) s.blend(CAIRN.x + dx, CAIRN.base, lit(f, SHADOW), 0.5); +} + +const ridgeY = (x: number) => TENT.apexY + ((x - TENT.apexX) / (TENT.backX - TENT.apexX)) * (TENT.backY - TENT.apexY); +const frontHalf = (y: number) => ((y - TENT.apexY) / (TENT.base - TENT.apexY)) * TENT.half; +const panelRight = (y: number) => TENT.backX + ((y - TENT.backY) / (TENT.base - TENT.backY)) * (TENT.backBase - TENT.backX); + +/** Where the lantern hangs inside, and the man sitting beside it writing in his journal. */ +const LAMP = { x: 270, y: 140 }; +const SIL = { x: 276, y: 142 }; + +function drawTent(s: Surface, f: Frame): void { + const t = f.t; + const glowK = f.lantern; + const tone = (c: RGB, k: number) => add(lit(f, c), SUN_WARM, f.warm * k); + const fly = tone(FLY, 0.15); + const flyShade = tone(FLY_SHADE, 0.02); + const seam = lit(f, FLY_SEAM); + const door = tone(DOOR, 0.1); + const interior = lit(f, INTERIOR); + const closed = doorClosed(t); + const lamp = (x: number, y: number, c: RGB) => { + if (glowK <= 0) return c; + const d = Math.hypot(x - LAMP.x, (y - LAMP.y) * 1.3); + return add(c, GLOW, glowK * (0.5 - Math.min(0.38, d * 0.03))); + }; + // Shadow on the grass beneath the tent. + for (let x = TENT.apexX - TENT.half - 1; x <= TENT.backBase + 2; x++) s.blend(x, TENT.base, lit(f, SHADOW), 0.6); + + // Side panel, running back from the door end. + for (let y = TENT.apexY; y < TENT.base; y++) { + const xl = TENT.apexX + frontHalf(y); + const xr = panelRight(y); + for (let x = Math.ceil(xl); x <= xr; x++) { + if (y < ridgeY(x)) continue; + let c = Math.abs(y - Math.round(ridgeY(x))) < 1 ? fly : flyShade; + if ((y - TENT.apexY) % 5 === 4) c = seam; + if (glowK > 0) { + c = lamp(x, y, c); + // His silhouette against the lit wall, head bowed over a journal. + const bow = Math.sin(t * 0.4) > 0.6 ? 1 : 0; + const head = Math.hypot(x - SIL.x, y - (SIL.y - 2 + bow)) < 1.6; + const body = y >= SIL.y && Math.abs(x - SIL.x - (y - SIL.y) * 0.15) < 1.5 + (y - SIL.y) * 0.35; + if (head || body) c = mix(c, interior, 0.7 * glowK); + } + s.set(x, y, c); + } + } + // Front, with the door tied back (dark inside) until he zips it shut. + for (let y = TENT.apexY; y < TENT.base; y++) { + const hw = frontHalf(y); + for (let x = Math.round(TENT.apexX - hw); x <= TENT.apexX + hw; x++) { + const dhw = ((y - TENT.apexY - 3) / (TENT.base - TENT.apexY - 3)) * 4.5; + const inDoor = y >= TENT.apexY + 3 && Math.abs(x - TENT.apexX) <= dhw; + let c = Math.abs(x - TENT.apexX) >= hw - 0.8 ? flyShade : fly; + if (inDoor) { + const shut = y - TENT.apexY - 3 < (TENT.base - TENT.apexY - 3) * closed; + c = shut ? (x === TENT.apexX ? seam : door) : interior; + } + s.set(x, y, lamp(x, y, c)); + } + } + // Ridge tip and guy lines. + const guy = lit(f, GUY); + line(s, TENT.apexX, TENT.apexY - 1, TENT.apexX - 12, TENT.base + 1, guy, 0.4); + line(s, TENT.backX, TENT.backY - 1, TENT.backBase + 5, TENT.base + 1, guy, 0.4); + s.set(TENT.apexX, TENT.apexY - 1, lit(f, STONE_DARK)); +} + +function line(s: Surface, x0: number, y0: number, x1: number, y1: number, c: RGB, a: number): void { + const n = Math.max(Math.abs(x1 - x0), Math.abs(y1 - y0)); + for (let i = 0; i <= n; i++) s.blend(Math.round(x0 + ((x1 - x0) * i) / n), Math.round(y0 + ((y1 - y0) * i) / n), c, a); +} + +const PIT = { rx: 7, ry: 2.5 }; + +function drawFirePit(s: Surface, f: Frame): void { + const t = f.t; + const { fireX: cx, fireY: cy } = CAMP; + const ash = lit(f, ASH); + const wood = lit(f, WOOD); + const charred = lit(f, CHAR); + const lightStone = add(lit(f, STONE_LIGHT), SUN_WARM, f.warm * 0.15); + const stone = lit(f, STONE); + const darkStone = lit(f, STONE_DARK); + const burnt = progress(t, TIMES.match + 2, TIMES.match + 20); + + for (let y = cy - 2; y <= cy + 2; y++) { + for (let x = cx - PIT.rx + 1; x <= cx + PIT.rx - 1; x++) { + if (((x - cx) / (PIT.rx - 1)) ** 2 + ((y - cy) / (PIT.ry - 0.5)) ** 2 > 1) continue; + s.set(x, y, ash); + } + } + // Kindling laid in a little teepee, blackening once it burns. + const sticks: readonly (readonly [number, number, number, number])[] = [ + [cx - 3, cy + 1, cx, cy - 3], + [cx + 3, cy + 1, cx, cy - 3], + [cx - 2, cy + 1, cx + 1, cy - 2], + ]; + for (const [x0, y0, x1, y1] of sticks) line(s, x0, y0, x1, y1, mix(wood, charred, burnt), 1); + + // Ring of stones. + for (let i = 0; i < 12; i++) { + const a = (i / 12) * Math.PI * 2; + const x = Math.round(cx + Math.cos(a) * PIT.rx); + const y = Math.round(cy + Math.sin(a) * PIT.ry); + const front = Math.sin(a) > 0; + s.set(x, y, front ? stone : darkStone); + s.set(x + 1, y, front ? darkStone : stone); + if (!front || hash(i, 0, 51) > 0.5) s.set(x, y - 1, lightStone); + } + + if (f.fire <= 0.01) return; + // Embers glowing in the ash. + for (let i = 0; i < 14; i++) { + const x = cx - 4 + Math.floor(hash(i, 0, 52) * 9); + const y = cy - 1 + Math.floor(hash(i, 1, 52) * 3); + const tw = 0.5 + 0.5 * Math.sin(t * (2 + hash(i, 2, 52) * 3) + i); + s.blend(x, y, EMBER, Math.min(1, f.fire * 1.4) * (0.4 + 0.6 * tw)); + } + // Flames. + const flame = Math.min(1, f.fire); + for (let dx = -3; dx <= 3; dx++) { + const hgt = flame * (8 - Math.abs(dx) * 1.7) * (0.65 + 0.6 * noise1(t * 6 + dx * 1.7, 53)); + for (let k = 0; k < hgt; k++) { + const u = k / Math.max(1, hgt) + (1 - flame) * 0.45; + const c = u < 0.35 ? FIRE_CORE : u < 0.68 ? FIRE_MID : FIRE_EDGE; + const x = cx + dx + Math.round(Math.sin(t * 5 + k * 0.6 + dx) * u * 0.8); + const a = u > 0.8 ? 0.7 : 1; + s.blend(x, cy - 1 - k, c, a); + } + } +} + +function drawLog(s: Surface, f: Frame): void { + const { x0, x1, y } = CAMP.log; + const top = add(lit(f, LOG_TOP), SUN_WARM, f.warm * 0.15); + const log = lit(f, LOG); + const dark = lit(f, LOG_DARK); + for (let x = x0; x <= x1; x++) { + s.set(x, y, hash(x, 0, 54) > 0.85 ? log : top); + s.set(x, y + 1, hash(x, 1, 54) > 0.8 ? dark : log); + s.set(x, y + 2, dark); + } + const end = add(lit(f, LOG_END), SUN_WARM, f.warm * 0.15); + s.set(x0 - 1, y, end); + s.set(x0 - 1, y + 1, lit(f, LOG_RING)); + s.set(x0 - 1, y + 2, end); + for (let x = x0 - 1; x <= x1 + 1; x++) s.blend(x, y + 3, lit(f, SHADOW), 0.55); +} + +const SMOKE_N = 34; +const SMOKE_LIFE = 15; + +/** Wood smoke drifting up and away toward the peak, heavier and paler as the rain douses the fire. */ +export function drawSmoke(s: Surface, f: Frame): void { + const t = f.t; + const start = TIMES.match + 1.5; + if (t < start) return; + const amb: RGB = [Math.max(0.42, f.amb[0]), Math.max(0.44, f.amb[1]), Math.max(0.5, f.amb[2])]; + const base = [SMOKE[0] * amb[0], SMOKE[1] * amb[1], SMOKE[2] * amb[2]] as const; + const steam = progress(t, 222, 250); + for (let i = 0; i < SMOKE_N; i++) { + const offset = (i * SMOKE_LIFE) / SMOKE_N; + const age = (((t - offset) % SMOKE_LIFE) + SMOKE_LIFE) % SMOKE_LIFE; + const birth = t - age; + if (birth < start) continue; + const u = age / SMOKE_LIFE; + const ramp = progress(birth, start, start + 14); + const cx = CAMP.fireX + age * 1.1 + Math.sin(age * 0.5 + i * 1.7) * age * 0.25; + const cy = CAMP.fireY - 6 - age * 2.6; + const r = 0.8 + u * 4.2; + const alpha = (0.55 + steam * 0.25) * ramp * Math.pow(1 - u, 1.3) * Math.min(1, age / 0.8); + const c = mix(base, EMBER, Math.max(0, 0.3 - u * 2) * Math.min(1, f.fire)); + for (let y = Math.floor(cy - r); y <= cy + r; y++) { + for (let x = Math.floor(cx - r); x <= cx + r; x++) { + const d = Math.hypot(x - cx, y - cy) / r; + if (d > 1) continue; + s.dither(x, y, c, alpha * Math.sqrt(1 - d)); + } + } + } + // Sparks lifting off the fire. + if (f.fire < 0.4) return; + const period = 2.6; + for (let i = 0; i < 6; i++) { + const cycleT = t + i * 0.71; + const cycle = Math.floor(cycleT / period); + if (hash(cycle, i, 55) < 0.45) continue; + const age = cycleT % period; + if (age > 1.6) continue; + const x = CAMP.fireX + (hash(cycle, i, 56) - 0.5) * 5 + age * 2 + Math.sin(age * 7 + i) * 1.2; + const y = CAMP.fireY - 6 - age * 11; + s.blend(x, y, EMBER, (1 - age / 1.6) * 0.95); + } +} + +/** Warm firelight on the camp, and later the lantern's glow on the grass around the tent. */ +export function drawFirelight(s: Surface, f: Frame): void { + const dark = 1 - f.warm * 0.65; + if (f.fire > 0.01) { + const R = 36 + 3 * Math.sin(f.t * 7); + const cx = CAMP.fireX; + const cy = CAMP.fireY - 4; + for (let y = Math.max(110, Math.floor(cy - R)); y < Math.min(H, cy + R); y++) { + for (let x = Math.floor(cx - R * 1.3); x <= cx + R * 1.3; x++) { + const d = Math.hypot((x - cx) / 1.3, y - cy) / R; + if (d >= 1) continue; + s.light(x, y, FIRELIGHT, f.fire * 0.3 * dark * (1 - d) * (1 - d)); + } + } + } + if (f.lantern > 0.01) { + const R = 22; + for (let y = TENT.base - 2; y < TENT.base + 14; y++) { + for (let x = TENT.apexX - 26; x <= TENT.backBase + 10; x++) { + const d = Math.hypot((x - LAMP.x) / 1.6, (y - TENT.base) * 1.2) / R; + if (d >= 1) continue; + s.light(x, y, GLOW, f.lantern * 0.14 * (1 - d) * (1 - d)); + } + } + } +} diff --git a/src/meditations/alpine-afternoon/index.ts b/src/meditations/alpine-afternoon/index.ts new file mode 100644 index 0000000..25171c4 --- /dev/null +++ b/src/meditations/alpine-afternoon/index.ts @@ -0,0 +1,83 @@ +import { Rng } from '../../engine/rng'; +import type { Cue, Meditation, Scene } from '../types'; +import { drawAngler } from './angler'; +import { drawCamp, drawFirelight, drawSmoke } from './camp'; +import { drawLake } from './lake'; +import { drawLand } from './land'; +import { STRIKES } from './lightning'; +import { drawMountains } from './mountains'; +import { drawGodrays, drawSky, drawVeil } from './sky'; +import { drawBolts, drawRain } from './storm'; +import { DURATION, frameAt } from './timeline'; +import { drawBackTrees, drawFrontTrees, drawLeaves } from './trees'; +import { drawElk, drawOsprey, drawPika, drawRises } from './wildlife'; + +const scene: Scene = { + render(s, t) { + const f = frameAt(t); + drawSky(s, f); + drawBolts(s, f); + drawMountains(s, f); + drawVeil(s, f); + drawOsprey(s, f); + drawElk(s, f); + drawLake(s, f); + drawRises(s, f); + drawLand(s, f); + drawPika(s, f); + drawBackTrees(s, f); + drawCamp(s, f); + drawAngler(s, f); + drawSmoke(s, f); + drawFirelight(s, f); + drawGodrays(s, f); + drawFrontTrees(s, f); + drawLeaves(s, f); + drawRain(s, f); + s.darken(f.fade); + }, +}; + +const THUNDER_FILES = 4; +const THUNDER_LEN = 5; + +/** + * Thunder for each flash, arriving after the light by the strike's distance: closer strikes are + * louder and sooner. Files rotate so neighboring rolls never sound the same, and pan follows the flash. + */ +const THUNDER: Cue[] = (() => { + const r = new Rng(4242); + const endsAt = new Array(THUNDER_FILES).fill(-Infinity); + let last = -1; + return STRIKES.map((st) => { + const t = st.t + st.dist; + let free = endsAt.flatMap((e, n) => (e <= t && n !== last ? [n] : [])); + if (!free.length) free = endsAt.flatMap((_, n) => (n !== last ? [n] : [])); + const n = free[r.int(0, free.length - 1)]; + endsAt[n] = t + THUNDER_LEN; + last = n; + return { + t, + src: `audio/fx-thunder-0${n + 1}.mp3`, + volume: Math.min(0.75, 0.2 + 0.6 * st.power), + pan: Math.max(-0.7, Math.min(0.7, ((st.x - 160) / 160) * 0.7)), + }; + }); +})(); + +export const alpineAfternoon: Meditation = { + id: 'alpine-afternoon', + title: 'Alpine Afternoon', + subtitle: 'Fishing, firelight and a passing storm at tree line', + description: + 'A high lake beneath a great peak, ringed by golden aspens. Watch a lone angler cast into the afternoon light, release a small trout and land a fine one, then cook it over a fire as the sunset glows on the mountain. When the rain comes he settles into his tent, and a gentle thunderstorm rolls over the peak and passes on into a clear, starlit night.', + music: 'audio/alpine-afternoon.mp3', + ambience: [ + { src: 'audio/fx-rain.mp3', volume: 0.3, from: 208, fadeIn: 14 }, + { src: 'audio/fx-rain.mp3', volume: 0.4, from: 232, fadeIn: 18, until: 286, fadeOut: 20 }, + ], + cues: THUNDER, + duration: DURATION, + previewTime: 196, + createScene: () => scene, +}; diff --git a/src/meditations/alpine-afternoon/lake.ts b/src/meditations/alpine-afternoon/lake.ts new file mode 100644 index 0000000..587055c --- /dev/null +++ b/src/meditations/alpine-afternoon/lake.ts @@ -0,0 +1,121 @@ +import { ditherStep, hex, mix, scale } from '../../engine/color'; +import { hash, noise2 } from '../../engine/rng'; +import type { Surface } from '../../engine/Surface'; +import { anglerRings } from './angler'; +import { lit, type Frame } from './timeline'; +import { riseRings } from './wildlife'; +import { H, HORIZON, LAKE_BOTTOM, SHORE, W } from './world'; + +/** A spreading ring on the water: from a lure, a fish, or a raindrop. `size` scales how far it spreads. */ +export interface Ring { + x: number; + y: number; + age: number; + size: number; + /** Seconds the ring lasts. */ + life: number; + /** A burst of droplets at the start (a splash rather than a gentle rise). */ + splash?: boolean; +} + +const WATER = hex('#24606c'); +const SHALLOW = hex('#6a8c70'); +const RING = hex('#e8f0f4'); +const DROP = hex('#ffffff'); +const MIST = hex('#c8cede'); + +const isWater = (x: number, y: number) => x >= 0 && x < W && y >= HORIZON && y < SHORE[x]; + +/** The lake mirrors whatever was drawn above the horizon, with ripples, glitter and passing rings. */ +export function drawLake(s: Surface, f: Frame): void { + const t = f.t; + const tint = lit(f, WATER); + const shallow = lit(f, SHALLOW); + const wobAmp = 0.4 + f.rain * 1.6; + for (let y = HORIZON; y < LAKE_BOTTOM; y++) { + const depth = y - HORIZON; + const sy = HORIZON - 1 - depth; + for (let x = 0; x < W; x++) { + if (y >= SHORE[x]) continue; + const wob = Math.round(Math.sin(y * 0.9 + t * 1.1 + x * 0.04) * (wobAmp + depth / 30)); + const src = s.get(x + wob, sy); + let c = mix(scale(src, 0.82), tint, 0.24 + depth * 0.006 + f.rain * 0.2); + // Cat's-paws of breeze ruffling the surface. + const ripple = noise2(x * 0.06 - t * 0.05, y * 0.75, 61); + if (ripple > 0.73) c = scale(c, 1.12); + else if (ripple < 0.22) c = scale(c, 0.9); + if (y >= SHORE[x] - 3) c = mix(c, shallow, 0.3); + s.set(x, y, c); + + // Broken glitter path beneath the sun. + const sunW = 3 + depth * 0.14; + if (f.sunVis > 0 && Math.abs(x - f.sunX) < sunW && noise2(x * 0.18, y * 1.7 + t * 0.6, 62) > 0.6) { + s.blend(x, y, f.sunColor, 0.65 * f.sunVis * (f.warm + 0.2) * (1 - Math.abs(x - f.sunX) / sunW)); + } + } + } + + const rings = [...anglerRings(t), ...riseRings(t), ...rainRings(f)]; + for (const r of rings) drawRing(s, f, r); + drawMist(s, f); +} + +function drawRing(s: Surface, f: Frame, r: Ring): void { + const u = r.age / r.life; + if (u < 0 || u >= 1) return; + const col = mix(lit(f, RING), RING, 0.25); + const rad = r.size * (0.6 + Math.sqrt(u) * 3.4); + const a = (1 - u) * (1 - u) * 0.75; + const n = Math.max(8, Math.ceil(rad * 7)); + for (let i = 0; i < n; i++) { + const ang = (i / n) * Math.PI * 2; + const x = Math.round(r.x + Math.cos(ang) * rad); + const y = Math.round(r.y + Math.sin(ang) * rad * 0.32); + if (isWater(x, y)) s.blend(x, y, col, a); + } + if (r.splash && r.age < 0.45) { + const k = r.age / 0.45; + for (let i = 0; i < 5; i++) { + const dx = (hash(i, Math.round(r.x * 7), 63) - 0.5) * 4 * r.size; + const up = (1.5 + hash(i, Math.round(r.y * 7), 64) * 3) * r.size; + s.blend(r.x + dx * k, r.y - up * 4 * k * (1 - k), DROP, (1 - k) * 0.85); + } + } +} + +const RAIN_RINGS = 140; +const RAIN_RING_LIFE = 0.7; + +function rainRings(f: Frame): Ring[] { + const n = Math.round(RAIN_RINGS * f.rain); + const out: Ring[] = []; + for (let i = 0; i < n; i++) { + const period = 0.9 + hash(i, 0, 65) * 0.6; + const p = f.t / period + hash(i, 1, 65); + const cycle = Math.floor(p); + const age = (p - cycle) * period; + if (age > RAIN_RING_LIFE) continue; + const x = Math.floor(hash(i, cycle, 66) * W); + const y = Math.floor(HORIZON + 1 + hash(i, cycle, 67) * (LAKE_BOTTOM - HORIZON - 1)); + if (!isWater(x, y)) continue; + const near = (y - HORIZON) / (H - HORIZON); + out.push({ x, y, age, life: RAIN_RING_LIFE, size: 0.35 + near * 0.6 }); + } + return out; +} + +/** Evening mist drifting low over the water. */ +function drawMist(s: Surface, f: Frame): void { + if (f.mist <= 0.01) return; + const col = mix(lit(f, MIST), f.horizonColor, 0.4); + for (let y = HORIZON - 6; y < HORIZON + 16; y++) { + const band = 1 - Math.abs(y - (HORIZON + 3)) / 13; + if (band <= 0) continue; + for (let x = 0; x < W; x++) { + if (y >= SHORE[x]) continue; + const n = noise2((x - f.t * 2.2) / 36, y / 3.5, 68) * noise2((x - f.t * 1.2) / 14, y / 2, 69); + const a = f.mist * band * Math.max(0, n - 0.12) * 1.1; + s.blend(x, y, col, ditherStep(a, x, y, 5)); + } + } +} diff --git a/src/meditations/alpine-afternoon/land.ts b/src/meditations/alpine-afternoon/land.ts new file mode 100644 index 0000000..17c7aab --- /dev/null +++ b/src/meditations/alpine-afternoon/land.ts @@ -0,0 +1,103 @@ +import { add, bayer, hex, mix, scale, type RGB } from '../../engine/color'; +import { hash, noise1, noise2 } from '../../engine/rng'; +import type { Surface } from '../../engine/Surface'; +import { lit, type Frame } from './timeline'; +import { BOULDERS, cloudShadow, H, ROCK_POINT, SHORE, W } from './world'; + +const GOLD = hex('#b8a050'); +const OLIVE = hex('#7e8644'); +const RUST = hex('#a85a36'); +const DARK = hex('#5a5c34'); +const TIP = hex('#e0c878'); +const WET_EDGE = hex('#3c3e34'); +const PEBBLE = hex('#8a8478'); +const GRANITE = hex('#a8a2a0'); +const GRANITE_MID = hex('#86808a'); +const GRANITE_DARK = hex('#5c5864'); +const LICHEN = hex('#c89a3a'); +const SUN_WARM = hex('#ffc890'); +const WET = hex('#2a3040'); + +const rockBottom = (x: number) => + ROCK_POINT.bottom + Math.round(noise1(x / 5, 81) * 4) - Math.round(6 * Math.max(0, 1 - (x - ROCK_POINT.x0) / 6)); + +const inRockPoint = (x: number, y: number) => x >= ROCK_POINT.x0 && x <= ROCK_POINT.x1 && y < rockBottom(x); + +export function drawLand(s: Surface, f: Frame): void { + const t = f.t; + const sun = (c: RGB, k: number) => add(lit(f, c), SUN_WARM, f.warm * k); + const gold = sun(GOLD, 0.2); + const olive = sun(OLIVE, 0.14); + const rust = sun(RUST, 0.18); + const dark = sun(DARK, 0.08); + const tip = sun(TIP, 0.25); + const wetEdge = lit(f, WET_EDGE); + const pebble = sun(PEBBLE, 0.12); + const granite = sun(GRANITE, 0.22); + const graniteMid = sun(GRANITE_MID, 0.1); + const graniteDark = lit(f, GRANITE_DARK); + const lichen = sun(LICHEN, 0.15); + const wetK = f.rain * 0.35; + const shadowK = 0.26 * f.shadows; + + for (let x = 0; x < W; x++) { + const top = SHORE[x]; + for (let y = top; y < H; y++) { + let c: RGB; + if (inRockPoint(x, y)) { + const d = y - top; + const crack = noise1((x + y * 0.5) / 3, 82) > 0.8 || noise1((x - y * 0.8) / 4, 83) > 0.84; + const shade = noise2(x / 8, y / 4, 84); + c = d < 2 ? granite : shade > 0.6 ? graniteDark : shade > 0.38 ? graniteMid : granite; + if (crack && d > 1) c = graniteDark; + if (hash(x, y, 85) > 0.965) c = lichen; + } else if (y - top < 1) { + c = hash(x, 0, 86) > 0.5 ? wetEdge : pebble; + } else { + const v = noise2(x / 22, y / 7, 71) + (y - 150) * 0.003 + (bayer(x, y) - 0.5) * 0.06; + const w = noise2(x / 7, y / 4, 72); + c = v > 0.64 ? rust : v < 0.4 ? olive : gold; + if (w > 0.76) c = dark; + // Tufts of grass catching the light, more of them close by. + if (hash(x, y, 73) > 0.985 - Math.max(0, y - 150) * 0.0008) c = tip; + if (y - top < 2 && hash(x, y, 74) > 0.6) c = pebble; + } + // Darker foreground, for depth. + const near = Math.max(0, (y - 158) / 22); + if (near > 0) c = scale(c, 1 - near * 0.2); + if (shadowK > 0 && cloudShadow(x, y * 3.2 - 200, t, f.cover) > 0.5) c = scale(c, 1 - shadowK); + if (wetK > 0) c = mix(c, mul2(c, WET), wetK); + s.set(x, y, c); + } + } + drawBoulders(s, f); +} + +/** Darken toward a wet color: multiply, but keep a little of the original. */ +function mul2(c: RGB, k: RGB): RGB { + return [(c[0] * (k[0] + 120)) / 300, (c[1] * (k[1] + 120)) / 300, (c[2] * (k[2] + 120)) / 300]; +} + +function drawBoulders(s: Surface, f: Frame): void { + const light = add(lit(f, GRANITE), SUN_WARM, f.warm * 0.22); + const mid = add(lit(f, GRANITE_MID), SUN_WARM, f.warm * 0.08); + const dark = lit(f, GRANITE_DARK); + const lichen = lit(f, LICHEN); + const shadow = lit(f, hex('#3a3a30')); + for (let i = 0; i < BOULDERS.length; i++) { + const [cx, cy, rx, ry] = BOULDERS[i]; + // Contact shadow on the side away from the sun. + for (let x = Math.floor(cx - rx * 0.4); x <= cx + rx + 2; x++) s.blend(x, Math.round(cy + ry * 0.5), shadow, 0.5 * (0.4 + f.warm * 0.6)); + for (let y = Math.floor(cy - ry); y <= cy + ry * 0.5; y++) { + for (let x = Math.floor(cx - rx); x <= cx + rx; x++) { + const u = (x + 0.5 - cx) / rx; + const v = (y + 0.5 - cy) / ry; + if (u * u + v * v > 1 + (noise2(x / 2, y / 2, 90 + i) - 0.5) * 0.4) continue; + const k = -u * 0.5 - v * 0.75 + (bayer(x, y) - 0.5) * 0.4; + let c = k > 0.35 ? light : k > -0.25 ? mid : dark; + if (hash(x, y, 91 + i) > 0.94) c = lichen; + s.set(x, y, c); + } + } + } +} diff --git a/src/meditations/alpine-afternoon/lightning.ts b/src/meditations/alpine-afternoon/lightning.ts new file mode 100644 index 0000000..57aaa7e --- /dev/null +++ b/src/meditations/alpine-afternoon/lightning.ts @@ -0,0 +1,56 @@ +/** + * The thunderstorm's lightning, fixed in time so the flashes and the thunder cues always agree. + * `dist` is the delay (s) before its thunder arrives; `bolt` strikes draw a visible branched bolt, + * the rest are glows hidden inside the cloud. + */ +export interface Strike { + t: number; + x: number; + dist: number; + power: number; + bolt: boolean; + seed: number; +} + +export const STRIKES: readonly Strike[] = [ + { t: 229, x: 262, dist: 4.2, power: 0.35, bolt: false, seed: 1 }, + { t: 236, x: 232, dist: 3.4, power: 0.5, bolt: true, seed: 2 }, + { t: 242.5, x: 150, dist: 2.8, power: 0.65, bolt: false, seed: 3 }, + { t: 248, x: 128, dist: 2.3, power: 0.85, bolt: true, seed: 4 }, + { t: 253.5, x: 84, dist: 2, power: 0.9, bolt: true, seed: 5 }, + { t: 258.5, x: 236, dist: 2.1, power: 0.8, bolt: false, seed: 6 }, + { t: 264, x: 146, dist: 1.8, power: 1, bolt: true, seed: 7 }, + { t: 270, x: 246, dist: 2.7, power: 0.7, bolt: true, seed: 8 }, + { t: 276, x: 276, dist: 3.4, power: 0.5, bolt: false, seed: 9 }, + { t: 281.5, x: 298, dist: 4.2, power: 0.32, bolt: true, seed: 10 }, + { t: 287, x: 312, dist: 5, power: 0.2, bolt: false, seed: 11 }, +]; + +/** A lightning flash flickers: a bright stroke, a dimmer one, a strong restrike, then a fading glow. */ +const PULSES: readonly (readonly [number, number])[] = [ + [0, 1], + [0.13, 0.5], + [0.27, 0.85], +]; + +export const FLASH_LEN = 1.4; + +export function strikeEnvelope(dt: number): number { + if (dt < 0 || dt > FLASH_LEN) return 0; + let k = 0.2 * Math.exp(-dt * 3); + for (const [p0, a] of PULSES) { + const u = dt - p0; + if (u >= 0) k = Math.max(k, a * Math.exp(-u * 14)); + } + return k; +} + +/** Brightest flash at time t and where it is. */ +export function flashAt(t: number): { k: number; x: number; strike?: Strike } { + let best = { k: 0, x: 160 } as { k: number; x: number; strike?: Strike }; + for (const s of STRIKES) { + const k = strikeEnvelope(t - s.t) * s.power; + if (k > best.k) best = { k, x: s.x, strike: s }; + } + return best; +} diff --git a/src/meditations/alpine-afternoon/mountains.ts b/src/meditations/alpine-afternoon/mountains.ts new file mode 100644 index 0000000..055361e --- /dev/null +++ b/src/meditations/alpine-afternoon/mountains.ts @@ -0,0 +1,106 @@ +import { add, hex, mix, scale, type RGB } from '../../engine/color'; +import { hash, noise1, noise2 } from '../../engine/rng'; +import type { Surface } from '../../engine/Surface'; +import { lit, type Frame } from './timeline'; +import { BENCH, cloudShadow, HORIZON, RIDGE, W } from './world'; + +const ROCK = hex('#8e8794'); +const ROCK_SHADE = hex('#5e596c'); +const ROCK_DEEP = hex('#474354'); +const SNOW = hex('#fbfbff'); +const SNOW_SHADE = hex('#b2bad8'); +const TALUS = hex('#8c8474'); +const TALUS_SHADE = hex('#665f56'); +const MEADOW_GOLD = hex('#b09a4c'); +const MEADOW_RUST = hex('#a2603c'); +const MEADOW_OLIVE = hex('#7c8244'); +const KRUMMHOLZ = hex('#2a4232'); +const ASPEN_GOLD = hex('#e8b030'); +const STREAM = hex('#e8f0ff'); +const SHORELINE = hex('#3a3c34'); +const SUN_WARM = hex('#ffc890'); +const ALPEN = hex('#ff8c9c'); +const ALPEN_ROCK = hex('#e07a6c'); +const MOON_SNOW = hex('#c8d4ff'); +const FLASH = hex('#c4ccff'); + +/** The stream that tumbles down from the snowfield into the lake. */ +const streamX = (y: number) => 206 + Math.round(Math.sin(y * 0.31) * 1.2 + (y - 60) * 0.18); +const STREAM_TOP = 60; + +export function drawMountains(s: Surface, f: Frame): void { + const t = f.t; + const fog = 0.2; + const toneOf = (c: RGB, sunK: number) => mix(add(lit(f, c), SUN_WARM, f.warm * sunK), f.horizonColor, fog); + const rock = toneOf(ROCK, 0.16); + const rockShade = toneOf(ROCK_SHADE, 0); + const rockDeep = toneOf(ROCK_DEEP, 0); + let snow = toneOf(SNOW, 0.12); + let snowShade = toneOf(SNOW_SHADE, 0); + snow = mix(snow, MOON_SNOW, f.clearing * 0.4); + snowShade = mix(snowShade, MOON_SNOW, f.clearing * 0.2); + const alpenSnow = mix(snow, ALPEN, f.alpenglow * 0.62); + const alpenSnowShade = mix(snowShade, ALPEN, f.alpenglow * 0.35); + const alpenRock = mix(rock, ALPEN_ROCK, f.alpenglow * 0.5); + const talus = toneOf(TALUS, 0.14); + const talusShade = toneOf(TALUS_SHADE, 0.05); + const gold = toneOf(MEADOW_GOLD, 0.18); + const rust = toneOf(MEADOW_RUST, 0.16); + const olive = toneOf(MEADOW_OLIVE, 0.12); + const krumm = toneOf(KRUMMHOLZ, 0.05); + const aspen = toneOf(ASPEN_GOLD, 0.2); + const stream = mix(lit(f, STREAM), f.horizonColor, 0.15); + const shoreline = mix(lit(f, SHORELINE), f.horizonColor, 0.1); + const shadowK = 0.28 * f.shadows; + + for (let x = 0; x < W; x++) { + const top = RIDGE[x]; + const bench = BENCH[x]; + const facesSun = (RIDGE[Math.max(0, x - 2)] ?? top) >= (RIDGE[Math.min(W - 1, x + 2)] ?? top); + const capDepth = top < 80 ? 2 + Math.floor(noise1(x / 6, 31) * 5) + Math.floor((80 - top) * 0.22) : 1; + const snowLine = 40 + noise1(x / 9, 35) * 16 + (top > 45 ? 8 : 0); + const flashK = f.flash > 0 ? f.flash * 0.3 * Math.exp(-Math.abs(x - f.flashX) / 90) : 0; + for (let y = top; y < HORIZON; y++) { + const d = y - top; + let c: RGB; + let isSnow = false; + if (y < bench) { + // Snow-filled couloirs running diagonally down the faces, and ribs of shadowed rock. + const gully = noise1((x + (facesSun ? -d : d) * 0.9) / 2.4, 33); + const rib = noise2((x + (facesSun ? d : -d) * 0.7) / 4.5, y / 40, 36); + const field = noise2(x / 6, y / 4, 34); + isSnow = + d < capDepth || + (y < snowLine && field > 0.42) || + (gully > 0.74 - d * 0.003 && y < snowLine + 16 && hash(x, y, 37) > 0.2); + const lit2 = facesSun ? rib < 0.62 : rib > 0.72; + if (isSnow) { + const hi = y < f.alpenLine; + c = lit2 ? (hi ? alpenSnow : snow) : hi ? alpenSnowShade : snowShade; + } else { + c = lit2 ? (y < f.alpenLine ? alpenRock : rock) : rib > 0.5 && !facesSun ? rockDeep : rockShade; + if (hash(x, y, 38) > 0.93) c = rockDeep; + } + } else { + // Talus fans below the cliffs, then autumn tundra down to the water. + const u = (y - bench) / Math.max(1, HORIZON - bench); + const talusK = noise2(x / 7, y / 3, 39) + (1 - u) * 0.6; + if (talusK > 0.85) c = hash(x, y, 40) > 0.5 ? talus : talusShade; + else { + const v = noise2(x / 12, y / 3, 41); + c = v > 0.62 ? rust : v < 0.38 ? olive : gold; + } + // Stunted spruce near the water, and a few golden aspen groves on the left. + if (u > 0.55 && hash(x >> 1, y, 42) > 0.93 - noise1(x / 20, 43) * 0.1) c = krumm; + if (x < 110 && u > 0.5 && noise2(x / 6, y / 2, 44) > 0.68) c = aspen; + } + if (y >= STREAM_TOP && Math.abs(x - streamX(y)) < 1 && y < HORIZON - 1) { + c = hash(y, Math.floor(t * 6), 45) > 0.4 ? stream : mix(stream, c, 0.4); + } + if (y === HORIZON - 1) c = shoreline; + if (shadowK > 0 && cloudShadow(x, y * 0.7, t, f.cover) > 0.5) c = scale(c, 1 - shadowK * (isSnow ? 0.7 : 1)); + if (flashK > 0) c = add(c, FLASH, flashK * (isSnow ? 1.2 : 0.6)); + s.set(x, y, c); + } + } +} diff --git a/src/meditations/alpine-afternoon/props.ts b/src/meditations/alpine-afternoon/props.ts new file mode 100644 index 0000000..d812fc2 --- /dev/null +++ b/src/meditations/alpine-afternoon/props.ts @@ -0,0 +1,67 @@ +import { add, hex, type RGB } from '../../engine/color'; +import type { Surface } from '../../engine/Surface'; +import { lit, type Frame } from './timeline'; + +const PAIL = hex('#9aa2aa'); +const PAIL_LIGHT = hex('#d0d6dc'); +const PAIL_DARK = hex('#646a72'); +const HANDLE = hex('#50545a'); +const ROD = hex('#2c2a30'); +const CORK = hex('#c8a070'); +const SUN_WARM = hex('#ffc890'); + +/** A small galvanized pail, 5 wide, standing on baseY. */ +export function drawBucket(s: Surface, f: Frame, x: number, baseY: number, handleUp = true): void { + x = Math.round(x); + baseY = Math.round(baseY); + const pail = add(lit(f, PAIL), SUN_WARM, f.warm * 0.15); + const light = add(lit(f, PAIL_LIGHT), SUN_WARM, f.warm * 0.2); + const dark = lit(f, PAIL_DARK); + const handle = lit(f, HANDLE); + for (let dy = 0; dy < 4; dy++) { + const y = baseY - 1 - dy; + const inset = dy === 0 ? 1 : 0; + for (let dx = inset; dx < 5 - inset; dx++) s.set(x + dx, y, dy === 3 ? light : dx === 1 ? light : dx === 4 ? dark : pail); + } + if (handleUp) { + s.set(x, baseY - 5, handle); + s.set(x + 1, baseY - 6, handle); + s.set(x + 2, baseY - 6, handle); + s.set(x + 3, baseY - 6, handle); + s.set(x + 4, baseY - 5, handle); + } +} + +/** + * A fishing rod from the hand at (hx, hy): `angle` is its elevation in degrees toward `dir`, and + * `bend` curls the tip down (as when a fish pulls). Returns the tip. + */ +export function drawRod(s: Surface, f: Frame, hx: number, hy: number, dir: number, angle: number, bend = 0, len = 15): [number, number] { + const rod: RGB = lit(f, ROD); + const cork: RGB = add(lit(f, CORK), SUN_WARM, f.warm * 0.15); + let x = hx; + let y = hy; + // Butt end tucked behind the hand. + const a0 = (angle * Math.PI) / 180; + s.set(Math.round(hx - Math.cos(a0) * dir * 2), Math.round(hy + Math.sin(a0) * 2), cork); + s.set(Math.round(hx - Math.cos(a0) * dir), Math.round(hy + Math.sin(a0)), cork); + for (let i = 0; i < len; i++) { + const u = i / len; + const a = ((angle - bend * 70 * u * u) * Math.PI) / 180; + x += Math.cos(a) * dir; + y -= Math.sin(a); + s.blend(Math.round(x), Math.round(y), rod, u > 0.75 ? 0.7 : 1); + } + return [x, y]; +} + +/** A rod laid down on the grass. */ +export function drawRodOnGround(s: Surface, f: Frame, x0: number, y0: number, x1: number, y1: number): void { + const rod = lit(f, ROD); + const cork = lit(f, CORK); + const n = Math.ceil(Math.hypot(x1 - x0, y1 - y0)); + for (let i = 0; i <= n; i++) { + const u = i / n; + s.blend(Math.round(x0 + (x1 - x0) * u), Math.round(y0 + (y1 - y0) * u), u < 0.15 ? cork : rod, u > 0.8 ? 0.7 : 1); + } +} diff --git a/src/meditations/alpine-afternoon/sky.ts b/src/meditations/alpine-afternoon/sky.ts new file mode 100644 index 0000000..052c802 --- /dev/null +++ b/src/meditations/alpine-afternoon/sky.ts @@ -0,0 +1,195 @@ +import { add, bayer, ditherStep, hex, mix, scale, type RGB } from '../../engine/color'; +import { progress } from '../../engine/Timeline'; +import { noise1, noise2, Rng } from '../../engine/rng'; +import type { Surface } from '../../engine/Surface'; +import { SKY_BANDS, SUN_R, type Frame } from './timeline'; +import { cloudDensity, H, HORIZON, RIDGE, W } from './world'; + +const FLASH = hex('#c4ccff'); +const MOON = hex('#f2f0e2'); +const MOON_SHADE = hex('#a8a89c'); +const MOON_RIM = hex('#9aa8d0'); + +interface Star { + x: number; + y: number; + mag: number; + twinkle: number; + phase: number; + tint: RGB; +} + +const STARS: Star[] = (() => { + const r = new Rng(77); + return Array.from({ length: 130 }, () => ({ + x: r.int(0, W - 1), + y: r.int(0, HORIZON - 20), + mag: Math.pow(r.next(), 2.2), + twinkle: r.range(0.4, 1.6), + phase: r.range(0, Math.PI * 2), + tint: r.next() < 0.2 ? hex('#cfdcff') : r.next() < 0.3 ? hex('#ffe8c8') : hex('#ffffff'), + })); +})(); + +const MOON_X = 92; +const MOON_Y = 24; + +/** Cloud density for every sky pixel this frame, so neighbors can be compared cheaply. */ +const DENS = new Float32Array(W * HORIZON); + +export function drawSky(s: Surface, f: Frame): void { + for (let y = 0; y < HORIZON; y++) { + for (let x = 0; x < W; x++) { + if (y >= RIDGE[x]) continue; // hidden behind the mountains + const v = (y / (HORIZON - 1)) * (SKY_BANDS - 1) + (bayer(x, y) - 0.5) * 0.95; + const band = Math.max(0, Math.min(SKY_BANDS - 1, Math.round(v))); + s.set(x, y, f.skyBands[band]); + } + } + if (f.clearing > 0) { + drawStars(s, f); + drawMoon(s, f); + } + drawSun(s, f); + drawClouds(s, f); +} + +function drawStars(s: Surface, f: Frame): void { + for (const st of STARS) { + const on = progress(f.clearing, 0.15 + (1 - st.mag) * 0.5, 0.45 + (1 - st.mag) * 0.5); + if (on <= 0) continue; + const tw = 0.65 + 0.35 * Math.sin(f.t * st.twinkle + st.phase); + const a = on * tw * (0.35 + 0.65 * st.mag); + s.blend(st.x, st.y, st.tint, a); + if (st.mag > 0.85) { + s.blend(st.x - 1, st.y, st.tint, a * 0.35); + s.blend(st.x + 1, st.y, st.tint, a * 0.35); + s.blend(st.x, st.y - 1, st.tint, a * 0.35); + s.blend(st.x, st.y + 1, st.tint, a * 0.35); + } + } +} + +function drawMoon(s: Surface, f: Frame): void { + const vis = progress(f.clearing, 0.2, 0.7); + const R = 5; + for (let y = MOON_Y - 18; y <= MOON_Y + 18; y++) { + for (let x = MOON_X - 18; x <= MOON_X + 18; x++) { + const d = Math.hypot(x - MOON_X, y - MOON_Y); + if (d <= R || d > 18) continue; + s.blend(x, y, MOON, ditherStep(vis * 0.18 * Math.pow(1 - d / 18, 2), x, y, 4)); + } + } + for (let y = -R; y <= R; y++) { + for (let x = -R; x <= R; x++) { + if (x * x + y * y > R * R + R * 0.6) continue; + // A waxing gibbous: shadow on the left limb. + const shade = (x - 2) * (x - 2) + y * y > 34 && x < 0; + const mare = (x === 1 && y === -1) || (x === -1 && y === 1) || (x === 2 && y === 2); + s.blend(MOON_X + x, MOON_Y + y, shade || mare ? MOON_SHADE : MOON, vis); + } + } +} + +function drawSun(s: Surface, f: Frame): void { + if (f.sunVis <= 0) return; + const cx = Math.round(f.sunX); + const cy = Math.round(f.sunY); + const glowR = 52; + const glow = mix(f.sunColor, hex('#ffb070'), 0.4); + for (let y = Math.max(0, cy - glowR); y < Math.min(HORIZON, cy + glowR); y++) { + for (let x = cx - glowR; x <= cx + glowR; x++) { + const d = Math.hypot(x - cx, (y - cy) * 1.2); + if (d > glowR) continue; + const a = Math.pow(1 - d / glowR, 2.2) * 0.6 * (0.4 + 0.6 * f.sunVis); + s.blend(x, y, glow, ditherStep(a, x, y)); + } + } + const rim = mix(f.sunColor, hex('#ffa060'), 0.45); + for (let y = -SUN_R; y <= SUN_R; y++) { + for (let x = -SUN_R; x <= SUN_R; x++) { + const d2 = x * x + y * y; + if (d2 > SUN_R * SUN_R + SUN_R * 0.8) continue; + s.set(cx + x, cy + y, d2 > (SUN_R - 1.5) * (SUN_R - 1.5) ? rim : f.sunColor); + } + } +} + +const dens = (x: number, y: number) => DENS[Math.max(0, Math.min(HORIZON - 1, y)) * W + Math.max(0, Math.min(W - 1, x))]; + +function drawClouds(s: Surface, f: Frame): void { + const t = f.t; + for (let y = 0; y < HORIZON; y++) { + for (let x = 0; x < W; x++) DENS[y * W + x] = y < RIDGE[x] + 4 ? cloudDensity(x, y, t, f.cover, f.tilt) : 0; + } + const sunNear = f.warm * (0.3 + 0.7 * f.sunVis); + const moonNear = progress(f.clearing, 0.2, 0.8); + for (let y = 0; y < HORIZON; y++) { + for (let x = 0; x < W; x++) { + if (y >= RIDGE[x]) continue; + const d = DENS[y * W + x]; + // Soft dithered fringe on the cloud edges. + if (d * 45 + 0.5 < bayer(x, y)) continue; + const toward = dens(x - 3, y - 2); + const below = dens(x, y + 3); + const lightK = Math.max(0, Math.min(1, 0.55 + (d - toward) * 7 - d * 1.4)); + const underK = Math.max(0, Math.min(1, (d - below) * 7)); + let c = mix(f.cloudShade, f.cloudLit, ditherStep(lightK, x, y, 3)); + c = mix(c, f.cloudUnder, ditherStep(underK * 0.75, x, y, 3)); + c = scale(c, 1 - Math.min(1, d * 1.6) * 0.22); + const edge = 1 - Math.min(1, d * 5); + if (sunNear > 0) { + const near = Math.max(0, 1 - Math.hypot(x - f.sunX, (y - f.sunY) * 1.2) / 70); + if (near > 0) c = add(c, f.sunColor, edge * near * near * sunNear * 0.75); + } + if (moonNear > 0) { + const near = Math.max(0, 1 - Math.hypot(x - MOON_X, y - MOON_Y) / 80); + c = add(c, MOON_RIM, edge * near * moonNear * 0.4); + } + if (f.flash > 0) c = add(c, FLASH, f.flash * Math.exp(-Math.abs(x - f.flashX) / 70) * (0.3 + Math.min(1, d * 2)) * 0.8); + s.set(x, y, c); + } + } +} + +/** + * Low storm cloud that wraps around the peak (drawn after the mountains), glowing from inside + * when lightning flashes. + */ +export function drawVeil(s: Surface, f: Frame): void { + if (f.veil <= 0) return; + const t = f.t; + for (let y = 4; y < HORIZON - 6; y++) { + const band = Math.min(1, (y - 4) / 12) * Math.min(1, (HORIZON - 6 - y) / 24); + for (let x = 0; x < W; x++) { + const n = 0.6 * noise2((x - t * 1.4) / 34, y / 9, 601) + 0.4 * noise2((x - t * 2.2) / 13, y / 4, 602); + const d = n - (1 - f.veil * band); + if (d * 30 + 0.5 < bayer(x, y)) continue; + let c = mix(f.cloudShade, f.cloudLit, ditherStep(Math.max(0, Math.min(1, 0.4 - d * 2 + n * 0.3)), x, y, 3)); + if (f.flash > 0) c = add(c, FLASH, f.flash * Math.exp(-Math.abs(x - f.flashX) / 60) * 0.7); + s.blend(x, y, c, 0.78); + } + } +} + +/** Slanting shafts of light from the sun, falling through the gaps in the clouds. */ +export function drawGodrays(s: Surface, f: Frame): void { + if (f.godrays <= 0.01 || f.sunVis <= 0) return; + const sx = f.sunX; + const sy = f.sunY; + const col = mix(f.sunColor, hex('#ffd8a0'), 0.4); + for (let y = Math.max(0, Math.floor(sy) + 4); y < H; y++) { + for (let x = 0; x < W; x++) { + const dx = x - sx; + const dy = y - sy; + const dist = Math.hypot(dx, dy); + const ang = Math.atan2(dy, dx); + const ray = noise1(ang * 9 + f.t * 0.035, 411) * noise1(ang * 4 - f.t * 0.02, 412); + if (ray < 0.22) continue; + const fall = Math.max(0, 1 - dist / 320) * Math.min(1, dist / 40) * (y > HORIZON ? 0.55 : 1); + const a = Math.min(0.12, (ray - 0.22) * 0.3) * fall * f.godrays; + if (a <= 0.004) continue; + s.light(x, y, col, a); + } + } +} diff --git a/src/meditations/alpine-afternoon/storm.ts b/src/meditations/alpine-afternoon/storm.ts new file mode 100644 index 0000000..4deac35 --- /dev/null +++ b/src/meditations/alpine-afternoon/storm.ts @@ -0,0 +1,122 @@ +import { hex, mix } from '../../engine/color'; +import { hash, Rng } from '../../engine/rng'; +import type { Surface } from '../../engine/Surface'; +import { STRIKES, strikeEnvelope, type Strike } from './lightning'; +import type { Frame } from './timeline'; +import { H, HORIZON, RIDGE, SHORE, W } from './world'; + +// --------------------------------------------------------------------------- +// Lightning bolts, drawn behind the mountains so they seem to strike beyond the peak. +// --------------------------------------------------------------------------- + +const BOLT = hex('#f6f8ff'); +const BOLT_GLOW = hex('#a8b4ff'); + +/** A jagged bolt from the cloud base down to the ridge, with a couple of forks. */ +function boltPath(st: Strike): [number, number][][] { + const r = new Rng(st.seed * 977); + const paths: [number, number][][] = []; + const grow = (x: number, y: number, len: number, drift: number) => { + const pts: [number, number][] = [[x, y]]; + let dir = drift; + for (let i = 0; i < len; i++) { + if (i % 3 === 0) dir = drift + r.range(-1.2, 1.2); + x += dir; + y += 1; + pts.push([x, y]); + if (y >= RIDGE[Math.max(0, Math.min(W - 1, Math.round(x)))] + 2) break; + } + return pts; + }; + const main = grow(st.x, 10 + r.range(0, 8), 100, r.range(-0.3, 0.3)); + paths.push(main); + for (let b = 0; b < 2; b++) { + const at = main[Math.floor(r.range(0.2, 0.6) * main.length)]; + paths.push(grow(at[0], at[1], Math.floor(r.range(6, 16)), r.next() < 0.5 ? -0.9 : 0.9)); + } + return paths; +} + +const BOLTS = new Map(STRIKES.filter((s) => s.bolt).map((s) => [s, boltPath(s)] as const)); + +export function drawBolts(s: Surface, f: Frame): void { + for (const [st, paths] of BOLTS) { + const dt = f.t - st.t; + if (dt < 0 || dt > 0.4) continue; + const k = strikeEnvelope(dt); + if (k < 0.3) continue; + paths.forEach((pts, i) => { + const a = (i === 0 ? 1 : 0.6) * Math.min(1, k * 1.3) * (0.4 + 0.6 * st.power); + for (const [x, y] of pts) { + s.blend(x - 1, y, BOLT_GLOW, a * 0.35); + s.blend(x + 1, y, BOLT_GLOW, a * 0.35); + s.blend(x, y, BOLT, a); + } + }); + } +} + +// --------------------------------------------------------------------------- +// Rain in three depth layers, with splashes on the ground (the lake draws its own rings). +// --------------------------------------------------------------------------- + +interface Drop { + x0: number; + y0: number; + rank: number; +} + +interface Layer { + drops: Drop[]; + speed: number; + len: number; + alpha: number; +} + +const SLANT = 0.12; +const RAIN = hex('#9aa6c0'); + +function layer(seed: number, n: number, speed: number, len: number, alpha: number): Layer { + const r = new Rng(seed); + return { drops: Array.from({ length: n }, (_, i) => ({ x0: r.range(-30, W + 10), y0: r.range(0, H + 40), rank: i / n })), speed, len, alpha }; +} + +const LAYERS: readonly Layer[] = [layer(31, 700, 120, 3, 0.16), layer(32, 300, 190, 5, 0.24), layer(33, 80, 330, 10, 0.3)]; + +export function drawRain(s: Surface, f: Frame): void { + if (f.rain <= 0) return; + const col = mix([RAIN[0] * f.amb[0], RAIN[1] * f.amb[1], RAIN[2] * f.amb[2]], RAIN, 0.35 + f.flash * 0.5); + for (const L of LAYERS) { + const wrap = H + L.len + 20; + for (const dr of L.drops) { + if (dr.rank > f.rain) continue; + const y = ((dr.y0 + L.speed * f.t) % wrap) - L.len - 10; + const x = dr.x0 + y * SLANT; + for (let n = 0; n < L.len; n++) s.blend(x + n * SLANT, y + n, col, L.alpha * (0.4 + 0.6 * (n / L.len))); + } + } + drawSplashes(s, f, col); +} + +const SPLASHES = 160; +const SPLASH_LIFE = 0.14; + +function drawSplashes(s: Surface, f: Frame, col: readonly [number, number, number]): void { + const n = Math.round(SPLASHES * f.rain); + for (let i = 0; i < n; i++) { + const period = 0.45 + hash(i, 0, 140) * 0.4; + const p = f.t / period + hash(i, 1, 140); + const cycle = Math.floor(p); + const age = (p - cycle) * period; + if (age > SPLASH_LIFE) continue; + const x = Math.floor(hash(i, cycle, 141) * W); + const y = Math.floor(HORIZON + 24 + hash(i, cycle, 142) * (H - HORIZON - 24)); + if (y < SHORE[x]) continue; + const k = 0.6 * (1 - age / SPLASH_LIFE); + s.blend(x, y, col, k); + if (y > 150) { + s.blend(x - 1, y - 1, col, k * 0.5); + s.blend(x + 1, y - 1, col, k * 0.5); + } + } +} diff --git a/src/meditations/alpine-afternoon/timeline.ts b/src/meditations/alpine-afternoon/timeline.ts new file mode 100644 index 0000000..9356a66 --- /dev/null +++ b/src/meditations/alpine-afternoon/timeline.ts @@ -0,0 +1,186 @@ +import { gradient, hex, mix, mul, scale, type RGB } from '../../engine/color'; +import { colorTrack, ease, progress, track } from '../../engine/Timeline'; +import { noise1 } from '../../engine/rng'; +import { flashAt } from './lightning'; +import { cloudDensity, RIDGE } from './world'; + +/** All timings for the Alpine Afternoon meditation, in seconds. */ +export const DURATION = 300; + +export const TIMES = { + fadeIn: [0, 6], + fadeOut: [291, 300], + /** He strikes the match; the fire takes a few seconds to catch. */ + match: 160.2, + drizzle: 207, + lanternOn: 223.5, + lanternOut: 257, +} as const; + +/** Light phases: afternoon → golden hour → sunset → afterglow → dusk → storm → (storm) → clearing night. */ +const P = [0, 110, 170, 192, 212, 236, 268, 292] as const; +const phase = (...cs: string[]) => colorTrack(P.map((t, i) => [t, hex(cs[i])] as const)); + +/** Sky gradient stops from zenith to horizon. */ +const SKY = [ + phase('#3a78c4', '#3a6cb8', '#33528c', '#2a3a76', '#1a2048', '#0d1017', '#0d1017', '#070b1a'), + phase('#5490d2', '#5686ca', '#56649c', '#4a4888', '#30305c', '#12151e', '#12151e', '#0b1228'), + phase('#78aadc', '#7ea0d0', '#987aa0', '#92608c', '#523a64', '#171b26', '#171b26', '#121c36'), + phase('#a2c4e2', '#b2b8cc', '#e09a7c', '#e8806a', '#8a4a5a', '#1c202b', '#1c202b', '#1a2642'), + phase('#d4e0e4', '#ecd2ae', '#ffc884', '#ffb060', '#b86a50', '#22262f', '#22262f', '#24304a'), +]; + +/** Light falling on everything, as a per-channel multiplier. */ +const AMBIENT = phase('#fff4e4', '#ffe8cc', '#ffcaa0', '#f0a48c', '#806e94', '#363b50', '#363b50', '#48527a'); + +const CLOUD_LIT = phase('#fffaf0', '#fff0d8', '#ffd8a8', '#ffb08a', '#8a6a8c', '#2c303d', '#2c303d', '#56607e'); +const CLOUD_SHADE = phase('#aab4cc', '#b0a8ba', '#a888a2', '#8a5e88', '#3c3452', '#191c25', '#191c25', '#1d2537'); +const CLOUD_UNDER = phase('#d6dbe4', '#f2caa8', '#ff9c72', '#ff7a6c', '#a8506c', '#21242d', '#21242d', '#252d42'); + +const cover = track([ + [0, 0.42], + [150, 0.48], + [185, 0.56], + [205, 0.72], + [222, 0.9], + [236, 1.02], + [266, 1.02], + [284, 0.6], + [300, 0.48], +]); +/** The storm builds on the right first, and at the end clears from the left. */ +const tilt = track([ + [180, 0], + [210, 0.3], + [236, 0.1], + [266, 0.1], + [282, 0.45], + [300, 0.35], +]); + +const sunX = track([[0, 74], [195, 46]], ease.linear); +const sunY = track([[0, 16], [120, 40], [180, 62], [195, 76]], ease.linear); +const warm = track([[0, 1], [150, 0.95], [182, 0.72], [192, 0]]); +const godrays = track([[0, 0.55], [40, 1], [140, 1], [178, 0.7], [192, 0]]); +const alpenglow = track([[176, 0], [190, 1], [205, 0.85], [216, 0]]); +/** Alpenglow lights only what is above this row: the shadow of the horizon climbs the peak. */ +const alpenLine = track([[176, 100], [190, 72], [216, 12]], ease.linear); +const night = track([[195, 0], [236, 1]]); +const mist = track([[180, 0], [205, 0.7], [230, 0.5], [265, 0.35], [292, 0.65]]); +const rain = track([ + [TIMES.drizzle, 0], + [214, 0.14], + [228, 0.3], + [244, 0.85], + [260, 1], + [272, 0.7], + [284, 0.25], + [296, 0.06], +]); +const clearing = track([[272, 0], [293, 1]]); +const veil = track([[200, 0], [232, 0.55], [266, 0.5], [284, 0.22], [298, 0.06]]); +const fire = track([[TIMES.match + 0.6, 0], [162.5, 0.14], [166, 0.45], [174, 1], [224, 1], [244, 0.5], [262, 0.22], [300, 0.1]]); +const lantern = track([[TIMES.lanternOn, 0], [TIMES.lanternOn + 1, 1], [TIMES.lanternOut, 1], [TIMES.lanternOut + 3, 0]]); + +export const SUN_R = 6; +const FLASH_AMB: RGB = [0.78, 0.82, 0.96]; + +export interface Frame { + t: number; + /** Ambient light multiplier, 0–1 per channel (already brightened by lightning). */ + amb: RGB; + /** Sky gradient sampled into discrete pixel-art bands, zenith → horizon. */ + skyBands: RGB[]; + horizonColor: RGB; + cloudLit: RGB; + cloudShade: RGB; + cloudUnder: RGB; + cover: number; + tilt: number; + sunX: number; + sunY: number; + sunColor: RGB; + /** Fraction of the sun still above the mountains. */ + sunVis: number; + /** Strength of warm direct sunlight, dimmed while a cloud crosses the sun. */ + warm: number; + godrays: number; + /** How strongly moving cloud shadows show on the land. */ + shadows: number; + alpenglow: number; + alpenLine: number; + night: number; + mist: number; + rain: number; + /** Clouds breaking up after the storm, letting out the stars and moon. */ + clearing: number; + /** Low storm cloud wrapped around the peak. */ + veil: number; + /** Campfire strength, already including flicker. */ + fire: number; + lantern: number; + /** Lightning brightness now, and where it is. */ + flash: number; + flashX: number; + /** Overall brightness for the fade in/out. */ + fade: number; +} + +export const SKY_BANDS = 22; + +export function frameAt(t: number): Frame { + const fl = flashAt(t); + const flash = fl.k; + const sky = SKY.map((c) => mix(c(t), [150, 158, 200], flash * 0.35)); + const skyBands = Array.from({ length: SKY_BANDS }, (_, i) => gradient(sky, i / (SKY_BANDS - 1))); + const sx = sunX(t); + const sy = sunY(t); + const ridge = RIDGE[Math.round(sx)]; + const sunVis = Math.max(0, Math.min(1, (ridge - (sy - SUN_R)) / (SUN_R * 2))); + const cv = cover(t); + const tl = tilt(t); + // How much cloud is in front of the sun right now (a few samples so it changes gently). + let occ = 0; + for (const [dx, dy] of [[0, 0], [-4, 0], [4, 0], [0, -4], [0, 4]]) occ += Math.max(0, Math.min(1, cloudDensity(sx + dx, sy + dy, t, cv, tl) * 6)); + occ /= 5; + const w = warm(t) * (1 - 0.6 * occ); + const amb0 = scale(AMBIENT(t), (1 - 0.1 * occ * warm(t)) / 255); + const amb = mix(amb0, FLASH_AMB, flash * 0.45); + const flicker = 0.84 + 0.1 * noise1(t * 4.3, 5) + 0.06 * noise1(t * 11, 6); + const sunRed = progress(t, 100, 190); + return { + t, + amb, + skyBands, + horizonColor: sky[4], + cloudLit: CLOUD_LIT(t), + cloudShade: CLOUD_SHADE(t), + cloudUnder: CLOUD_UNDER(t), + cover: cv, + tilt: tl, + sunX: sx, + sunY: sy, + sunColor: mul(hex('#fff6d0'), [1, 1 - sunRed * 0.32, 1 - sunRed * 0.62]), + sunVis, + warm: w, + godrays: godrays(t) * (0.5 + 0.5 * occ) * (0.4 + 0.6 * sunVis), + shadows: warm(t) * (1 - progress(t, 165, 190)), + alpenglow: alpenglow(t), + alpenLine: alpenLine(t), + night: night(t), + mist: mist(t), + rain: rain(t), + clearing: clearing(t), + veil: veil(t), + fire: fire(t) * flicker, + lantern: lantern(t) * (0.92 + 0.08 * noise1(t * 3.1, 7)), + flash, + flashX: fl.x, + fade: progress(t, TIMES.fadeIn[0], TIMES.fadeIn[1]) * (1 - progress(t, TIMES.fadeOut[0], TIMES.fadeOut[1])), + }; +} + +/** Apply the frame's ambient light to a base (daylight) color. */ +export function lit(f: Frame, base: RGB): RGB { + return mul(base, f.amb); +} diff --git a/src/meditations/alpine-afternoon/trees.ts b/src/meditations/alpine-afternoon/trees.ts new file mode 100644 index 0000000..8a3aa18 --- /dev/null +++ b/src/meditations/alpine-afternoon/trees.ts @@ -0,0 +1,178 @@ +import { add, hex, mix, scale, type RGB } from '../../engine/color'; +import { progress } from '../../engine/Timeline'; +import { hash, noise1, noise2 } from '../../engine/rng'; +import type { Surface } from '../../engine/Surface'; +import { lit, type Frame } from './timeline'; +import { H, HORIZON, SHORE, W } from './world'; + +const SPRUCE_DARK = hex('#1c3628'); +const SPRUCE_MID = hex('#2c4c36'); +const SPRUCE_LIGHT = hex('#466a40'); +const TRUNK = hex('#3a2a22'); +const BARK = hex('#ece6d6'); +const BARK_SHADE = hex('#aca494'); +const BARK_EYE = hex('#3a3028'); +const LEAF_LIGHT = hex('#ffe060'); +const LEAF = hex('#eeae2a'); +const LEAF_SHADE = hex('#b0701c'); +const LEAF_DEEP = hex('#7a4a18'); +const LEAF_RED = hex('#e0682a'); +const SUN_WARM = hex('#ffc890'); + +interface TreeOpts { + seed: number; + /** Darkening for near silhouettes. */ + dim?: number; + /** How quickly a spruce's branches widen toward its base. */ + spread?: number; +} + +const spruceHalf = (r: number, h: number, spread: number) => { + if (r < 0) return -1; + const tiers = Math.max(3, Math.round(h / 7)); + const th = h / tiers; + const local = (r % th) / th; + return Math.floor((r * spread + 1) * (0.4 + 0.6 * local)); +}; + +/** A narrow subalpine spruce, lit warm on its sunward (left) side. */ +function drawSpruce(s: Surface, f: Frame, cx: number, baseY: number, h: number, o: TreeOpts): void { + const dim = o.dim ?? 1; + const spread = o.spread ?? 0.26; + const tone = (c: RGB, k: number) => scale(add(lit(f, c), SUN_WARM, f.warm * k), dim); + const dark = tone(SPRUCE_DARK, 0); + const mid = tone(SPRUCE_MID, 0.05); + const light = tone(SPRUCE_LIGHT, 0.22); + const trunk = tone(TRUNK, 0); + const top = baseY - h - 3; + for (let r = 0; r < h; r++) { + const hw = spruceHalf(r, h, spread); + const sway = Math.round(Math.sin(f.t * 0.6 + o.seed) * (r < h * 0.2 ? 0.6 : 0) * (1 + f.rain)); + for (let dx = -hw; dx <= hw; dx++) { + const x = cx + dx + sway; + const y = top + r; + const edge = hw > 0 ? dx / hw : 0; + let c = edge < -0.45 && hash(x, y, o.seed) > 0.3 ? light : edge > 0.2 ? dark : mid; + if (hash(x, y, o.seed + 1) > 0.8) c = dark; + s.set(x, y, c); + } + } + for (let y = baseY - 3; y < baseY; y++) { + s.set(cx - 1, y, trunk); + s.set(cx, y, trunk); + } +} + +/** + * A quaking aspen: a pale trunk with dark "eyes", and a golden autumn crown whose leaves shimmer + * as they catch the breeze. + */ +function drawAspen(s: Surface, f: Frame, cx: number, baseY: number, h: number, o: TreeOpts): void { + const dim = o.dim ?? 1; + const t = f.t; + const tone = (c: RGB, k: number) => scale(add(lit(f, c), SUN_WARM, f.warm * k), dim); + const bark = tone(BARK, 0.15); + const barkShade = tone(BARK_SHADE, 0); + const eye = tone(BARK_EYE, 0); + const red = hash(o.seed, 0, 1) > 0.6; + const leafLight = tone(red ? mix(LEAF_LIGHT, LEAF_RED, 0.3) : LEAF_LIGHT, 0.2); + const leaf = tone(red ? mix(LEAF, LEAF_RED, 0.45) : LEAF, 0.15); + const leafShade = tone(LEAF_SHADE, 0.05); + const leafDeep = tone(LEAF_DEEP, 0); + const wide = h >= 34; + const lean = (hash(o.seed, 1, 2) - 0.5) * 4; + const trunkTop = baseY - Math.round(h * 0.9); + for (let y = trunkTop; y < baseY; y++) { + const x = cx + Math.round((lean * (baseY - y)) / h); + s.set(x, y, hash(x, y, o.seed + 3) > 0.9 ? eye : bark); + if (wide) s.set(x + 1, y, hash(x + 1, y, o.seed + 4) > 0.88 ? eye : barkShade); + } + + const ccx = cx + lean * 0.6; + const ccy = baseY - h * 0.62; + const rx = h * 0.22; + const ry = h * 0.4; + const breeze = 1 + f.rain * 2; + for (let y = Math.floor(ccy - ry - 1); y <= ccy + ry + 1; y++) { + for (let x = Math.floor(ccx - rx - 1); x <= ccx + rx + 1; x++) { + const u = (x + 0.5 - ccx) / rx; + const v = (y + 0.5 - ccy) / ry; + const d = u * u + v * v + (noise2(x / 2.6, y / 2.6, o.seed + 5) - 0.5) * 0.9; + if (d > 1) continue; + // Gaps toward the edge let the trunk and sky show through. + if (hash(x, y, o.seed + 6) < d * 0.45 - 0.05) continue; + const k = -u * 0.45 - v * 0.35 + (noise2(x / 3, y / 3, o.seed + 7) - 0.5) * 0.8; + let c = k > 0.3 ? leafLight : k > -0.15 ? leaf : k > -0.5 ? leafShade : leafDeep; + // Quaking: individual leaves flip and flash in the breeze. + const q = hash(x, y, o.seed + 8); + if (q > 0.72 && Math.sin(t * (4 + q * 5) * breeze + q * 60) > 0.55) c = c === leafDeep ? leafShade : leafLight; + s.set(x, y, c); + } + } +} + +/** Trees behind the man and the camp. */ +export function drawBackTrees(s: Surface, f: Frame): void { + drawAspen(s, f, 24, 134, 44, { seed: 11 }); + drawSpruce(s, f, 50, 130, 30, { seed: 12 }); + drawAspen(s, f, 40, 131, 32, { seed: 13 }); + drawAspen(s, f, 212, 128, 28, { seed: 14 }); + drawSpruce(s, f, 240, 126, 36, { seed: 15 }); + drawAspen(s, f, 228, 129, 42, { seed: 16 }); + drawAspen(s, f, 282, 126, 34, { seed: 17 }); + drawSpruce(s, f, 300, 129, 54, { seed: 18 }); +} + +/** Big near trees framing the edges of the view. */ +export function drawFrontTrees(s: Surface, f: Frame): void { + drawSpruce(s, f, 6, 186, 150, { seed: 21, dim: 0.5, spread: 0.15 }); + drawSpruce(s, f, 22, 184, 60, { seed: 22, dim: 0.62, spread: 0.24 }); + drawAspen(s, f, 302, 186, 86, { seed: 23, dim: 0.78 }); + drawSpruce(s, f, 316, 186, 140, { seed: 24, dim: 0.5, spread: 0.15 }); +} + +/** Crowns that shed leaves [x, y, spread]. */ +const SOURCES: readonly (readonly [number, number, number])[] = [ + [24, 106, 8], + [40, 112, 6], + [228, 102, 8], + [282, 104, 7], + [302, 132, 15], +]; + +const LEAVES = 16; + +/** Golden leaves let go, flutter down on the breeze, and settle on the water or the grass. */ +export function drawLeaves(s: Surface, f: Frame): void { + const t = f.t; + const rate = 1 - progress(t, 205, 228); + if (rate <= 0) return; + const gold = add(lit(f, LEAF), SUN_WARM, f.warm * 0.2); + const light = add(lit(f, LEAF_LIGHT), SUN_WARM, f.warm * 0.2); + for (let i = 0; i < LEAVES; i++) { + const period = 11 + hash(i, 0, 31) * 9; + const p = t / period + hash(i, 1, 31); + const cycle = Math.floor(p); + if (hash(i, cycle, 32) > rate * 0.85) continue; + const age = (p - cycle) * period; + const [sx, sy, sp] = SOURCES[Math.floor(hash(i, cycle, 33) * SOURCES.length)]; + const x0 = sx + (hash(i, cycle, 34) - 0.5) * sp * 2; + const y0 = sy + (hash(i, cycle, 35) - 0.5) * sp; + const fallFor = 5 + hash(i, cycle, 36) * 4; + const fall = Math.min(age, fallFor); + let x = x0 + fall * (3 + noise1(t * 0.2, 37) * 3) + Math.sin(fall * 2.2 + i) * 3; + let y = y0 + fall * (6 + hash(i, 2, 38) * 3); + // Once down, it rests (drifting slowly if it landed on the water) and fades. + const ground = Math.round(x) >= 0 && Math.round(x) < W ? SHORE[Math.round(x)] : H; + const onWater = y < ground && y >= HORIZON + 4; + if (age > fallFor) { + if (onWater) x += (age - fallFor) * 0.8; + y = Math.round(y); + } + const fadeOut = age > fallFor ? 1 - Math.min(1, (age - fallFor) / Math.min(3, period - fallFor)) : 1; + if (fadeOut <= 0) continue; + const flip = age < fallFor && Math.sin(fall * 6 + i * 2) > 0; + s.blend(x, y, flip ? light : gold, 0.9 * fadeOut); + if (!flip && age < fallFor) s.blend(x + 1, y, gold, 0.6 * fadeOut); + } +} diff --git a/src/meditations/alpine-afternoon/wildlife.ts b/src/meditations/alpine-afternoon/wildlife.ts new file mode 100644 index 0000000..94fbcb5 --- /dev/null +++ b/src/meditations/alpine-afternoon/wildlife.ts @@ -0,0 +1,173 @@ +import { add, hex, mix, type RGB } from '../../engine/color'; +import { drawSprite, type SpriteArt } from '../../engine/sprite'; +import { Stamp } from '../../engine/stamp'; +import { progress } from '../../engine/Timeline'; +import { hash } from '../../engine/rng'; +import type { Surface } from '../../engine/Surface'; +import type { Ring } from './lake'; +import { lit, type Frame } from './timeline'; +import { BOULDERS, HORIZON } from './world'; + +const SUN_WARM = hex('#ffc890'); + +const STAMP = new Stamp(); + +/** A sprite with a dark outline, so small animals stand out from the tundra. */ +function drawOutlined(s: Surface, art: SpriteArt, x: number, y: number, pal: Record, flip: boolean, alpha: number, outline: number): void { + const w = art[0].length; + STAMP.begin(x, y, w, art.length); + for (let r = 0; r < art.length; r++) { + for (let c = 0; c < w; c++) { + const col = pal[art[r][c]]; + if (col) STAMP.put(flip ? x + w - 1 - c : x + c, y + r, col); + } + } + STAMP.draw(s, outline, alpha); +} + +// --------------------------------------------------------------------------- +// Osprey: circles high over the lake through the afternoon, then glides away over the ridge. +// --------------------------------------------------------------------------- + +const OSPREY_GLIDE: SpriteArt = ['d.........d', '.dw.....wd.', '..dwwhwwd..', '.....w.....']; +const OSPREY_UP: SpriteArt = ['..d.....d..', '..dw...wd..', '...dwhwd...', '.....w.....']; +const OSPREY_DOWN: SpriteArt = ['...........', '...........', '.ddwwhwwdd.', 'd....w....d']; +const OSPREY_COLS: Record = { d: hex('#4a3a30'), w: hex('#f0ece4'), h: hex('#d8d0c4') }; + +const OSPREY_FROM = 4; +const OSPREY_LEAVE = 104; +const OSPREY_GONE = 124; + +export function drawOsprey(s: Surface, f: Frame): void { + const t = f.t; + if (t < OSPREY_FROM || t > OSPREY_GONE) return; + const w = (Math.PI * 2) / 26; + let x = 132 + 38 * Math.cos(t * w); + let y = 40 + 9 * Math.sin(t * w); + if (t > OSPREY_LEAVE) { + const k = (t - OSPREY_LEAVE) / (OSPREY_GONE - OSPREY_LEAVE); + x += k * k * 220; + y -= k * 18; + } + // Mostly soaring, with a few lazy wingbeats now and then. + const flapping = hash(Math.floor(t / 3), 0, 21) > 0.7; + const beat = Math.floor(t * 5) % 3; + const art = !flapping ? OSPREY_GLIDE : beat === 0 ? OSPREY_UP : beat === 1 ? OSPREY_GLIDE : OSPREY_DOWN; + const pal: Record = {}; + for (const [k, c] of Object.entries(OSPREY_COLS)) pal[k] = add(lit(f, c), SUN_WARM, f.warm * 0.15); + const alpha = progress(t, OSPREY_FROM, OSPREY_FROM + 3); + drawSprite(s, art, Math.round(x) - 5, Math.round(y) - 2, pal, false, alpha); +} + +// --------------------------------------------------------------------------- +// Elk: a small band grazing its way along the far shore at dusk. +// --------------------------------------------------------------------------- + +const BULL: SpriteArt = ['....a.a', '....aa.', '.....hh', 'rbbbbn.', 'rbbbb..', '.l..l..']; +const BULL_GRAZE: SpriteArt = ['.......', '.......', '.......', 'rbbbbna', 'rbbbbnh', '.l..lh.']; +const COW: SpriteArt = ['.......', '.......', '.....hh', 'rbbbbn.', 'rbbbb..', '.l..l..']; +const COW_GRAZE: SpriteArt = ['.......', '.......', '.......', 'rbbbbn.', 'rbbbbnh', '.l..lh.']; +const ELK_COLS: Record = { + a: hex('#e8dcc0'), + h: hex('#3e2618'), + n: hex('#33200f'), + b: hex('#6a4430'), + r: hex('#f0e2c4'), + l: hex('#2a1c14'), +}; + +const HERD = [ + { x0: 92, bull: true, seed: 1 }, + { x0: 82, bull: false, seed: 2 }, + { x0: 74, bull: false, seed: 3 }, + { x0: 66, bull: false, seed: 4 }, +]; +const ELK_FROM = 138; +const ELK_TO = 226; + +export function drawElk(s: Surface, f: Frame): void { + const t = f.t; + if (t < ELK_FROM || t > ELK_TO) return; + const alpha = progress(t, ELK_FROM, ELK_FROM + 8) * (1 - progress(t, ELK_TO - 10, ELK_TO)); + const pal: Record = {}; + for (const [k, c] of Object.entries(ELK_COLS)) pal[k] = mix(add(lit(f, c), SUN_WARM, f.warm * 0.15), f.horizonColor, 0.15); + for (const e of HERD) { + // Walk a few steps, then graze a while. + const local = t - ELK_FROM + e.seed * 2.3; + const cycle = Math.floor(local / 9); + const within = local % 9; + const walking = within < 3; + const x = e.x0 + cycle * 4 + (walking ? within * 1.3 : 4) - 4; + const graze = !walking && hash(cycle, e.seed, 22) > 0.2; + const art = e.bull ? (graze ? BULL_GRAZE : BULL) : graze ? COW_GRAZE : COW; + drawOutlined(s, art, Math.round(x), HORIZON - art.length, pal, false, alpha, 0.55); + } +} + +// --------------------------------------------------------------------------- +// Pika: pops up on its boulder to look around, then ducks back into the rocks. +// --------------------------------------------------------------------------- + +const PIKA: SpriteArt = ['.a.a.', '.bbbb', 'bbbeb', 'bbbbb']; +const PIKA_COLS: Record = { a: hex('#6a5444'), b: hex('#8a7462'), e: hex('#1a1210') }; +const PIKA_VISITS: readonly (readonly [number, number])[] = [ + [9, 38], + [52, 64], + [86, 118], + [150, 172], +]; + +export function drawPika(s: Surface, f: Frame): void { + const t = f.t; + const visit = PIKA_VISITS.find(([a, b]) => t >= a && t < b); + if (!visit) return; + const [a, b] = visit; + const up = Math.min(progress(t, a, a + 0.6), 1 - progress(t, b - 0.6, b)); + const [cx, cy, , ry] = BOULDERS[0]; + const top = Math.round(cy - ry) - 3; + const rows = Math.round(up * PIKA.length); + if (rows <= 0) return; + const pal: Record = {}; + for (const [k, c] of Object.entries(PIKA_COLS)) pal[k] = add(lit(f, c), SUN_WARM, f.warm * 0.15); + // Turns its head now and then. + const flip = hash(Math.floor(t / 4), 0, 23) > 0.5; + drawOutlined(s, PIKA.slice(0, rows), cx - 3, top + (PIKA.length - rows), pal, flip, 1, 0.5); +} + +// --------------------------------------------------------------------------- +// Trout rising: rings on the water, more of them in the evening. +// --------------------------------------------------------------------------- + +const RISES: readonly (readonly [number, number, number])[] = [ + [30, 152, 109], + [92, 176, 107], + [104, 118, 114], + [143, 120, 113], + [176, 150, 110], + [183, 186, 105], + [189, 134, 117], + [196, 162, 108], + [204, 112, 112], +]; + +export function riseRings(t: number): Ring[] { + const out: Ring[] = []; + for (const [at, x, y] of RISES) { + const age = t - at; + if (age >= 0 && age < 4.5) out.push({ x, y, age, size: 1, life: 4.5, splash: false }); + } + return out; +} + +/** The trout's back breaking the surface for a moment as it rises. */ +export function drawRises(s: Surface, f: Frame): void { + const back = lit(f, hex('#5a6448')); + for (const [at, x, y] of RISES) { + const age = f.t - at; + if (age < 0.05 || age > 0.6) continue; + const k = Math.sin((age / 0.6) * Math.PI); + s.blend(x - 1, y - Math.round(k), back, 0.9); + s.blend(x, y - Math.round(k * 1.4), back, 0.9); + s.blend(x + 1, y - Math.round(k), back, 0.9); + } +} diff --git a/src/meditations/alpine-afternoon/world.ts b/src/meditations/alpine-afternoon/world.ts new file mode 100644 index 0000000..de18422 --- /dev/null +++ b/src/meditations/alpine-afternoon/world.ts @@ -0,0 +1,139 @@ +import { noise1, noise2 } from '../../engine/rng'; + +/** Fixed layout of the scene on the 320x180 canvas. */ +export const W = 320; +export const H = 180; + +/** Far shore of the lake; the water mirrors everything above this row. */ +export const HORIZON = 100; + +export const smoothstep = (a: number, b: number, x: number): number => { + const u = Math.max(0, Math.min(1, (x - a) / (b - a))); + return u * u * (3 - 2 * u); +}; + +/** Interpolate [x, y] keys with smooth steps between them. */ +function keyed(keys: readonly (readonly [number, number])[], x: number): number { + if (x <= keys[0][0]) return keys[0][1]; + for (let i = 0; i < keys.length - 1; i++) { + const [x0, y0] = keys[i]; + const [x1, y1] = keys[i + 1]; + if (x < x1) return y0 + (y1 - y0) * smoothstep(x0, x1, x); + } + return keys[keys.length - 1][1]; +} + +/** + * Summits [x, top row, slope]: one great peak just right of center, a lower sub-summit to its right, + * and the left shoulder (x≈40) that the sun sets behind. + */ +const PEAKS: readonly (readonly [number, number, number])[] = [ + [-12, 70, 0.4], + [40, 61, 0.42], + [96, 52, 0.55], + [178, 16, 0.8], + [216, 41, 0.6], + [262, 51, 0.55], + [318, 62, 0.5], +]; + +/** The skyline of the mountains. */ +export const RIDGE: readonly number[] = Array.from({ length: W }, (_, x) => { + const y = Math.min(...PEAKS.map(([px, py, k]) => py + Math.abs(x - px) * k)); + return Math.round(Math.min(84, y + (noise1(x / 4, 13) - 0.5) * 3 + (noise1(x / 11, 14) - 0.5) * 3)); +}); + +/** Where bare rock gives way to the talus and tundra slopes that run down to the far shore. */ +export const BENCH: readonly number[] = Array.from({ length: W }, (_, x) => + Math.max(RIDGE[x] + 3, Math.round(85 + (noise1(x / 26, 15) - 0.5) * 10)), +); + +/** + * First row of land on the near shore for each column. A rock point juts into the lake on the left + * (where he fishes), the shore dips low in the middle, and a grassy bench rises on the right for camp. + */ +export const SHORE: readonly number[] = Array.from({ length: W }, (_, x) => { + const y = keyed( + [ + [0, 127], + [40, 129], + [58, 127], + [92, 127], + [104, 146], + [178, 148], + [204, 126], + [320, 124], + ], + x, + ); + return Math.round(y + (noise1(x / 6, 16) - 0.5) * 2); +}); + +export const LAKE_BOTTOM = Math.max(...SHORE); + +/** The granite outcrop he fishes from. */ +export const ROCK_POINT = { x0: 56, x1: 101, bottom: 142 }; + +/** Where he stands to fish, and where his lure comes back to at the end of each retrieve. */ +export const ANGLER = { x: 88, feetY: 136 }; +export const RETRIEVE: readonly [number, number] = [102, 132]; + +export const CAMP = { + fireX: 222, + fireY: 152, + log: { x0: 230, x1: 244, y: 150 }, + bucket: { x: 242, y: 156 }, + /** Where he sits on the log, and where he kneels to light the fire. */ + seatX: 237, + kneelX: 233, + feetY: 155, +}; + +/** A small A-frame tent, its door facing the fire. */ +export const TENT = { + apexX: 258, + apexY: 135, + base: 147, + half: 7, + backX: 279, + backY: 137, + backBase: 287, + /** Where he stands before ducking inside. */ + doorX: 255, + doorFeet: 149, +}; + +export const CAIRN = { x: 198, base: 138 }; + +/** Boulders on the near shore [cx, cy, rx, ry]; the first is the pika's rock. */ +export const BOULDERS: readonly (readonly [number, number, number, number])[] = [ + [140, 171, 10, 6], + [114, 152, 4, 2.5], + [176, 157, 5, 3], + [32, 147, 6, 3.5], + [304, 164, 7, 4], + [206, 169, 4, 3], + [190, 150, 3, 2], + [62, 162, 5, 3], + [262, 172, 5, 3], +]; + +/** + * Cloud density at a point: above 0 is cloud. `cover` 0–1 sets how much of the sky is overcast, and + * `tilt` thickens the clouds toward the right (where the storm builds) for positive values. + */ +export function cloudDensity(x: number, y: number, t: number, cover: number, tilt: number, seed = 0): number { + const u = x - t * 0.5; + const n = + 0.55 * noise2(u / 54, y / 15, 301 + seed) + 0.3 * noise2(u / 22, y / 7, 302 + seed) + 0.15 * noise2(u / 9, y / 3.5, 303 + seed); + const c = cover + (tilt * (x - 160)) / 160; + // Fair-weather clouds thin out low over the mountains. + const low = y > 58 ? ((y - 58) / 42) * 0.4 * (1 - c) : 0; + return n - (1 - c) - low; +} + +/** Moving cloud shadow on the ground or the mountain faces, 0 (sunlit) – 1 (in shade). */ +export function cloudShadow(x: number, y: number, t: number, cover: number): number { + const n = 0.7 * noise2((x - t * 1.1) / 40, y / 9, 501) + 0.3 * noise2((x - t * 1.1) / 15, y / 4, 502); + return smoothstep(0.62 - cover * 0.18, 0.66 - cover * 0.18, n); +} diff --git a/src/meditations/registry.ts b/src/meditations/registry.ts index e3f3863..ac5fc2c 100644 --- a/src/meditations/registry.ts +++ b/src/meditations/registry.ts @@ -1,4 +1,5 @@ import type { Meditation } from './types'; +import { alpineAfternoon } from './alpine-afternoon'; import { cyberpunkCity } from './cyberpunk-city'; import { desertSunrise } from './desert-sunrise'; import { mediterraneanCoast } from './mediterranean-coast'; @@ -9,7 +10,7 @@ import { theReef } from './the-reef'; import { tibetanMonastery } from './tibetan-monastery'; /** Every meditation listed on the main menu, in display order. Add new ones here. */ -export const meditations: readonly Meditation[] = [northernCabin, mediterraneanCoast, theReef, ringedPlanet, desertSunrise, tibetanMonastery, morningFlight, cyberpunkCity]; +export const meditations: readonly Meditation[] = [northernCabin, mediterraneanCoast, theReef, ringedPlanet, desertSunrise, tibetanMonastery, morningFlight, cyberpunkCity, alpineAfternoon]; export function findMeditation(id: string): Meditation | undefined { return meditations.find((m) => m.id === id);