pixeltation/src/meditations/tibetan-monastery/flags.ts

108 lines
4.2 KiB
TypeScript

import { hex, mix, scale, type RGB } from '../../engine/color';
import { hash } from '../../engine/rng';
import type { Surface } from '../../engine/Surface';
import { lit, sunlit, type Frame } from './timeline';
import { FLAG_LINES, POLE, type FlagLine } from './world';
/** Lungta flags always run blue, white, red, green, yellow: sky, air, fire, water, earth. */
const COLORS: readonly RGB[] = [hex('#2f5fbf'), hex('#eeeee8'), hex('#d03a30'), hex('#2f9a52'), hex('#f0c83a')];
const STRING = hex('#3a3236');
const POLE_WOOD = hex('#5a4030');
const POLE_DARK = hex('#3a281e');
const GOLD = hex('#e0b040');
/** How far flags stream out downwind (to the right): mostly hanging, lifting a little as the breeze builds. */
function liftOf(f: Frame): number {
return 0.15 + f.breeze * 0.5;
}
function linePoint(l: FlagLine, f: Frame, li: number, u: number): [number, number] {
const bow = 4 * u * (1 - u);
// Lines bow and sway slowly in the breeze.
const sway = f.breeze * (0.5 + 0.5 * Math.sin(f.t * (0.35 + li * 0.05) + li * 2.1)) * bow * 1.2;
return [l.ax + (l.bx - l.ax) * u, l.ay + (l.by - l.ay) * u + l.sag * bow + sway];
}
function drawLine(s: Surface, f: Frame, l: FlagLine, li: number): void {
const len = Math.hypot(l.bx - l.ax, l.by - l.ay) + l.sag;
const string = l.near ? scale(lit(f, STRING), 0.8) : lit(f, STRING);
const steps = Math.ceil(len * 1.4);
for (let i = 0; i <= steps; i++) {
const [x, y] = linePoint(l, f, li, i / steps);
s.blend(x, y, string, l.near ? 1 : 0.8);
}
const lift = liftOf(f);
const amp = 0.3 + f.breeze * 0.8;
const shade = l.near ? 0.78 : 1;
const n = Math.floor(len / l.gap);
for (let j = 1; j < n; j++) {
const [x, y] = linePoint(l, f, li, j / n);
const base = COLORS[(j + li) % 5];
const col = scale(sunlit(f, base, 0.35), shade);
const dark = scale(col, 0.72);
const print = mix(col, dark, 0.8);
// Flags fly shorter as they lift, their free edge rippling.
const rows = Math.max(2, Math.round(l.fh * (1 - 0.45 * lift)));
const phase = j * 1.37 + li * 0.7;
for (let r = 0; r < rows; r++) {
const k = (r + 1) / rows;
const wave = Math.sin(f.flutter + phase - r * 0.7);
const off = k * (lift * l.fh * 0.95 + amp * wave);
const fx = Math.round(x + off);
const fy = Math.round(y) + 1 + r;
const rowCol = wave < -0.35 ? dark : col;
for (let c = 0; c < l.fw; c++) {
// A frayed bottom edge where years of wind have worn the cotton.
if (r === rows - 1 && hash(j * 7 + c, li, 40) < 0.35) continue;
let pc = rowCol;
if (l.near && r > 1 && r < rows - 2 && c > 1 && c < l.fw - 2 && hash(j * 13 + c, r, 41 + li) < 0.4) pc = print;
s.set(fx + c, fy, pc);
}
}
}
}
/** The tall darchor pole on the west knob, with its long banner. */
function drawPole(s: Surface, f: Frame): void {
const wood = sunlit(f, POLE_WOOD, 0.3);
const dark = lit(f, POLE_DARK);
for (let y = POLE.top; y <= POLE.base; y++) {
s.set(POLE.x, y, dark);
s.set(POLE.x + 1, y, wood);
}
const gold = sunlit(f, GOLD, 0.5);
s.set(POLE.x, POLE.top - 1, gold);
s.set(POLE.x + 1, POLE.top - 1, gold);
s.set(POLE.x, POLE.top - 2, gold);
s.set(POLE.x, POLE.top - 3, gold);
// The banner is lashed along the pole and streams out to the right, drooping when the air is still.
const lift = liftOf(f);
const len = 6 + Math.round(lift * 4);
const top = POLE.top + 3;
const height = 25;
for (let c = 0; c < len; c++) {
const k = (c + 1) / len;
const droop = (1 - lift) * c * 1.3;
const wave = Math.sin(f.flutter * 0.9 - c * 0.6) * (0.3 + f.breeze * 0.8) * k;
const shade = Math.cos(f.flutter * 0.9 - c * 0.6) < -0.4;
for (let r = 0; r < height - Math.round(c * 0.4); r++) {
const col = sunlit(f, COLORS[Math.floor(r / 5) % 5], 0.35);
s.set(POLE.x + 2 + c, Math.round(top + r + droop + wave), shade ? scale(col, 0.75) : col);
}
}
}
export function drawBackFlags(s: Surface, f: Frame): void {
drawPole(s, f);
FLAG_LINES.forEach((l, i) => {
if (!l.near) drawLine(s, f, l, i);
});
}
export function drawNearFlags(s: Surface, f: Frame): void {
FLAG_LINES.forEach((l, i) => {
if (l.near) drawLine(s, f, l, i);
});
}