pixeltation/src/meditations/northern-cabin/weather.ts

115 lines
3.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { hex, mix, mul, type RGB } from '../../engine/color';
import { progress } from '../../engine/Timeline';
import { hash, Rng } from '../../engine/rng';
import type { Surface } from '../../engine/Surface';
import { TIMES, type Frame } from './timeline';
import { CHIMNEY, H, W } from './world';
interface Flake {
x0: number;
y0: number;
speed: number;
sway: number;
freq: number;
phase: number;
big: boolean;
alpha: number;
/** 0–1 rank; flakes above the current density fade out. */
rank: number;
}
function makeFlakes(seed: number, n: number, near: boolean): Flake[] {
const r = new Rng(seed);
return Array.from({ length: n }, (_, i) => ({
x0: r.range(0, W),
y0: r.range(0, H),
speed: near ? r.range(11, 17) : r.range(4.5, 8),
sway: near ? r.range(3, 7) : r.range(1.5, 4),
freq: r.range(0.25, 0.7),
phase: r.range(0, Math.PI * 2),
big: near && r.next() < 0.45,
alpha: near ? r.range(0.75, 0.95) : r.range(0.4, 0.65),
rank: i / n,
}));
}
const FAR = makeFlakes(7, 120, false);
const NEAR = makeFlakes(8, 42, true);
const WIND = 2.2;
const FLAKE = hex('#ffffff');
const FLAKE_TINT = hex('#dfe6ff');
function drawFlakes(s: Surface, f: Frame, flakes: Flake[], color: RGB): void {
const wrapW = W + 8;
const wrapH = H + 8;
for (const fl of flakes) {
const fadeIn = Math.max(0, Math.min(1, (f.snow - fl.rank) * 12));
if (fadeIn <= 0) continue;
const y = ((fl.y0 + fl.speed * f.t) % wrapH) - 4;
const xRaw = fl.x0 + WIND * f.t + fl.sway * Math.sin(f.t * fl.freq + fl.phase);
const x = (((xRaw % wrapW) + wrapW) % wrapW) - 4;
const a = fl.alpha * fadeIn;
s.blend(x, y, color, a);
if (fl.big) {
s.blend(x + 1, y, color, a * 0.6);
s.blend(x, y + 1, color, a * 0.6);
s.blend(x + 1, y + 1, color, a * 0.3);
}
}
}
export function drawFarSnow(s: Surface, f: Frame): void {
drawFlakes(s, f, FAR, mix(mul(FLAKE, f.amb), FLAKE_TINT, 0.25));
}
export function drawNearSnow(s: Surface, f: Frame): void {
drawFlakes(s, f, NEAR, mix(mul(FLAKE, f.amb), FLAKE_TINT, 0.4));
}
const SMOKE_N = 40;
const SMOKE_LIFE = 16;
const SMOKE = hex('#a4a6b4');
const EMBER = hex('#ffb050');
export function drawSmoke(s: Surface, f: Frame): void {
const t = f.t;
if (t < TIMES.smokeStart) return;
// Smoke stays a little lighter than its surroundings so it still reads against the night sky.
const amb: RGB = [Math.max(0.46, f.amb[0]), Math.max(0.48, f.amb[1]), Math.max(0.56, f.amb[2])];
const base = mul(SMOKE, amb);
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 < TIMES.smokeStart) continue;
const u = age / SMOKE_LIFE;
const ramp = progress(birth, TIMES.smokeStart, TIMES.smokeStart + 22);
const cx = CHIMNEY.smokeX + age * 1.25 + Math.sin(age * 0.45 + i * 1.7) * age * 0.22;
const cy = CHIMNEY.smokeY - age * 3.1;
const r = 0.9 + u * 4.5;
const alpha = 0.8 * ramp * Math.pow(1 - u, 1.2) * Math.min(1, age / 0.8);
const c = mix(base, EMBER, Math.max(0, 0.35 - u * 2) * 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));
}
}
}
// An occasional spark drifting up out of the flue.
if (f.fire < 0.5) return;
const period = 4.5;
for (let i = 0; i < 4; i++) {
const cycleT = t + i * 1.13;
const cycle = Math.floor(cycleT / period);
if (hash(cycle, i, 91) < 0.55) continue;
const age = cycleT % period;
if (age > 1.8) continue;
const x = CHIMNEY.smokeX + (hash(cycle, i, 92) - 0.5) * 3 + age * 2 + Math.sin(age * 6) * 0.8;
const y = CHIMNEY.smokeY - age * 8;
s.blend(x, y, EMBER, (1 - age / 1.8) * 0.9);
}
}