67 lines
2.7 KiB
TypeScript
67 lines
2.7 KiB
TypeScript
import { hex, mix, type RGB } from '../../engine/color';
|
|
import { hash } from '../../engine/rng';
|
|
import type { Surface } from '../../engine/Surface';
|
|
import { lit, sunlit, TIMES, type Frame } from './timeline';
|
|
import { HORIZON } from './world';
|
|
|
|
type Part = 'hull' | 'keel' | 'mast' | 'main' | 'jib';
|
|
|
|
/** The sloop, bow to the right, as [dx, dy, part] around a pivot at the waterline (dy < 0 is up). */
|
|
const PIXELS: [number, number, Part][] = (() => {
|
|
const out: [number, number, Part][] = [];
|
|
for (let dx = -5; dx <= 4; dx++) out.push([dx, 0, 'keel']);
|
|
for (let dx = -6; dx <= 6; dx++) out.push([dx, -1, 'hull']);
|
|
for (let dy = -13; dy <= -2; dy++) out.push([-1, dy, 'mast']);
|
|
// Mainsail behind the mast, widening toward the boom.
|
|
for (let dy = -12; dy <= -3; dy++) {
|
|
const w = Math.round(((dy + 13) / 10) * 5);
|
|
for (let dx = -2; dx >= -1 - w; dx--) out.push([dx, dy, 'main']);
|
|
}
|
|
// Jib ahead of the mast, down to the bow.
|
|
for (let dy = -11; dy <= -3; dy++) {
|
|
const w = Math.round(((dy + 12) / 9) * 5);
|
|
for (let dx = 0; dx < w; dx++) out.push([dx, dy, 'jib']);
|
|
}
|
|
return out;
|
|
})();
|
|
|
|
const ROCK_PERIOD = 4.3;
|
|
|
|
export function drawSailboat(s: Surface, f: Frame): void {
|
|
const t = f.t;
|
|
const [t0, t1] = TIMES.boat;
|
|
if (t < t0 || t > t1) return;
|
|
const x0 = -16 + ((t - t0) / (t1 - t0)) * 352;
|
|
const y0 = HORIZON + 7;
|
|
// Rocking on the swell: lean the mast and sails side to side about the waterline.
|
|
const angle = 0.12 * Math.sin((2 * Math.PI * t) / ROCK_PERIOD) + 0.035 * Math.sin((2 * Math.PI * t) / 2.1 + 1);
|
|
const lean = Math.tan(angle);
|
|
const bob = Math.sin((2 * Math.PI * t) / ROCK_PERIOD + 1.2) > 0.75 ? 1 : 0;
|
|
|
|
const haze = 0.22;
|
|
const colors: Record<Part, RGB> = {
|
|
hull: mix(sunlit(f, hex('#eeeae0'), 0.3), f.horizonColor, haze),
|
|
keel: mix(lit(f, hex('#2e3a4c')), f.horizonColor, haze),
|
|
mast: mix(lit(f, hex('#4a4a50')), f.horizonColor, haze),
|
|
main: mix(sunlit(f, hex('#f8f4ea'), 0.35), f.horizonColor, haze * 0.6),
|
|
jib: mix(sunlit(f, hex('#e2ded4'), 0.2), f.horizonColor, haze * 0.6),
|
|
};
|
|
|
|
for (const [dx, dy, part] of PIXELS) {
|
|
const x = Math.round(x0 + dx - dy * lean);
|
|
const y = y0 + dy + bob;
|
|
s.set(x, y, colors[part]);
|
|
// A faint, wavering reflection.
|
|
const ry = y0 - dy + 1 + bob;
|
|
if (dy > -9) {
|
|
const wob = Math.round(Math.sin(ry * 1.3 + t * 3) * 0.6);
|
|
s.blend(x + wob, ry, colors[part], part === 'main' || part === 'jib' ? 0.22 : 0.3);
|
|
}
|
|
}
|
|
// A little wake trailing behind.
|
|
const foam = mix(lit(f, hex('#f4f8f6')), f.horizonColor, haze);
|
|
for (let k = 7; k < 14; k++) {
|
|
if (hash(k, Math.floor(t * 3), 51) < 0.55 - k * 0.03) s.blend(Math.round(x0) - k, y0 + bob, foam, 0.7 - k * 0.04);
|
|
}
|
|
}
|