Back to the app
The code
Night Courier by GPT-6.2 Sol
Exactly as the AI wrote it. Shown as text, so nothing runs on this page.
index.html
works · auto-checked (Checked by an automated pass. Not hand-played yet.)<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<meta name="theme-color" content="#251e30">
<title>Night Courier</title>
<style>
:root {
color-scheme: dark;
--ink: #fff2df;
--muted: #dbc4be;
--gold: #ffcb85;
--gold-deep: #f2a966;
--glass: rgba(34, 25, 41, .83);
--edge: rgba(255, 225, 203, .19);
--outer: clamp(14px, 2.5vw, 38px);
font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { width: 100%; height: 100%; margin: 0; overflow: hidden; }
body { background: #554058; color: var(--ink); overscroll-behavior: none; }
button { font: inherit; color: inherit; -webkit-tap-highlight-color: transparent; }
button:focus-visible { outline: 2px solid #fff6d4; outline-offset: 4px; }
.stage { position: fixed; inset: 0; overflow: hidden; isolation: isolate; user-select: none; }
.scene, .shade, .flight-surface { position: absolute; inset: 0; width: 100%; height: 100%; }
.scene { display: block; }
.shade { pointer-events: none; background:
linear-gradient(180deg, rgba(34, 20, 39, .49), transparent 24%, transparent 67%, rgba(23, 17, 31, .68)),
radial-gradient(ellipse at 51% 42%, transparent 48%, rgba(32, 18, 31, .30) 100%); }
.flight-surface { touch-action: none; }
.hud { position: absolute; z-index: 4; left: var(--outer); right: var(--outer); top: max(var(--outer), env(safe-area-inset-top)); display: flex; align-items: flex-start; justify-content: space-between; gap: 15px; pointer-events: none; }
.brand { display: flex; align-items: center; gap: 12px; align-self: center; text-shadow: 0 2px 20px #28152e; }
.brand-mark { width: 50px; height: 50px; display: grid; place-items: center; flex: none; border-radius: 15px; color: #ffddaa; background: #3b2a3e; border: 1px solid rgba(255, 215, 172, .42); box-shadow: 0 7px 23px #27172c38, inset 0 1px rgba(255,255,255,.08); }
.brand-mark svg { width: 32px; height: 32px; }
.brand h1 { font: 600 clamp(25px, 3.1vw, 45px)/.98 Georgia, "Times New Roman", serif; letter-spacing: -.065em; margin: 0; }
.brand p { font-size: 10px; color: #ffdab8; font-weight: 750; text-transform: uppercase; letter-spacing: .24em; margin: 7px 0 0 2px; }
.stats { display: flex; padding: 5px; gap: 0; min-height: 61px; border-radius: 17px; background: var(--glass); border: 1px solid var(--edge); box-shadow: 0 8px 30px #21132129, inset 0 1px #ffffff0a; backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px); }
.stat { position: relative; display: flex; gap: 10px; align-items: center; min-width: 101px; padding: 7px 15px; }
.stat + .stat { border-left: 1px solid #ffe4c91c; }
.stat:last-child { min-width: 119px; }
.stat svg { width: 18px; height: 18px; color: var(--gold); flex: none; }
.stat div { display: grid; gap: 2px; }
.stat span { color: var(--muted); font-size: 9px; font-weight: 760; text-transform: uppercase; letter-spacing: .13em; white-space: nowrap; }
.stat strong { font-size: 21px; font-weight: 720; line-height: 1.08; font-variant-numeric: tabular-nums; letter-spacing: -.035em; }
.stat.urgent strong, .stat.urgent svg { color: #ff9a80; }
.clock-line { position: absolute; left: 16px; right: 16px; bottom: 2px; height: 2px; border-radius: 4px; overflow: hidden; background: #fff2d51c; }
.clock-line i { display: block; width: 100%; height: 100%; background: var(--gold); transform-origin: left; }
.urgent i { background: #ff9a80; }
.dock { position: absolute; z-index: 5; inset: auto var(--outer) max(var(--outer), env(safe-area-inset-bottom)) var(--outer); display: grid; grid-template-columns: 1fr auto 1fr; align-items: end; gap: 18px; pointer-events: none; }
.mission { justify-self: start; width: min(306px, 100%); padding: 16px; border-radius: 18px; background: var(--glass); border: 1px solid var(--edge); box-shadow: 0 12px 36px #17101c36, inset 0 1px #ffffff0b; backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px); }
.mission.is-ready { pointer-events: auto; }
.mission.is-flying { padding: 12px 14px; }
.mission h2 { font: 600 24px/1.04 Georgia, serif; letter-spacing: -.035em; margin: 0; }
.mission p { font-size: 12px; line-height: 1.5; color: var(--muted); margin: 7px 0 12px; }
.action { display: inline-flex; justify-content: center; align-items: center; gap: 10px; padding: 0 15px; min-height: 45px; border-radius: 10px; border: 1px solid #ffe3af; background: linear-gradient(160deg, #ffdd9f, #ffc17e 80%); color: #392232; font-size: 13px; font-weight: 820; cursor: pointer; box-shadow: inset 0 1px #ffffd877, 0 4px 12px #21131942; }
.action:active, .camera-button:active, .sound:active { transform: translateY(1px); }
.action svg { width: 17px; height: 17px; }
.mission-label { display: block; color: var(--gold); font-size: 9px; text-transform: uppercase; letter-spacing: .15em; font-weight: 800; margin-bottom: 5px; }
.route-info { display: flex; align-items: center; gap: 10px; }
.route-symbol { width: 37px; height: 37px; border-radius: 10px; background: #ffb86b16; color: var(--gold); border: 1px solid #ffca8b3b; display: grid; place-items: center; flex: none; }
.route-symbol svg { width: 21px; height: 21px; }
.route-text { min-width: 0; flex: 1; }
.route-text strong { display: block; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; font-size: 14px; line-height: 1.18; }
.distance { color: var(--muted); font-size: 12px; white-space: nowrap; font-variant-numeric: tabular-nums; }
.hint { display: flex; align-items: center; gap: 6px; margin-top: 11px; color: #ead4c8; font-size: 11px; font-weight: 560; }
.hint svg { width: 14px; height: 14px; color: var(--gold); flex: none; }
.mission.is-flying .hint { margin-top: 9px; }
.cameras { justify-self: center; display: flex; gap: 4px; padding: 5px; align-items: center; border-radius: 15px; border: 1px solid var(--edge); background: var(--glass); box-shadow: 0 8px 26px #19111e36, inset 0 1px #ffffff0c; backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px); pointer-events: auto; }
.camera-button { display: flex; align-items: center; justify-content: center; gap: 8px; border: 1px solid transparent; border-radius: 10px; padding: 0 14px; min-height: 44px; background: transparent; font-size: 12px; font-weight: 700; color: #fae7db; cursor: pointer; white-space: nowrap; }
.camera-button svg { width: 19px; height: 19px; flex: none; }
.camera-button[aria-pressed="true"] { color: #382332; background: linear-gradient(155deg, #ffe0a7, #ffc185); border-color: #ffdfa5; box-shadow: inset 0 1px #fff6cb9e, 0 3px 9px #1b111c50; }
.sound { width: 51px; height: 51px; display: grid; place-items: center; justify-self: end; border: 1px solid var(--edge); color: #ffe7cf; background: var(--glass); border-radius: 15px; box-shadow: 0 8px 26px #19111e30; cursor: pointer; pointer-events: auto; backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px); }
.sound svg { width: 21px; height: 21px; }
.sound[aria-pressed="true"] { color: var(--gold); }
.sound[aria-pressed="false"] .sound-on, .sound[aria-pressed="true"] .sound-off { display: none; }
.beacon { position: absolute; z-index: 2; left: 0; top: 0; display: flex; align-items: center; justify-content: center; gap: 7px; padding: 6px 10px; min-height: 27px; border-radius: 8px; border: 1px solid #ffe2a66e; background: #352436dc; color: #ffedca; box-shadow: 0 3px 17px #39233a65; font-size: 10px; letter-spacing: .09em; text-transform: uppercase; font-weight: 820; white-space: nowrap; pointer-events: none; }
.beacon .arrow { width: 12px; height: 12px; color: #ffd38f; flex: none; }
.beacon .diamond { width: 6px; height: 6px; transform: rotate(45deg); border-radius: 1px; background: #ffd687; box-shadow: 0 0 9px #ffc17b; }
.beacon:not(.offscreen) .arrow, .beacon.offscreen .diamond { display: none; }
.toast { position: absolute; top: max(104px, calc(var(--outer) + 72px)); left: 50%; z-index: 5; display: flex; align-items: center; gap: 9px; padding: 9px 14px; transform: translate(-50%, -7px); border: 1px solid #ffe0ae55; border-radius: 12px; background: #3a293de8; color: #fff0d6; box-shadow: 0 5px 23px #20152545; font-size: 12px; font-weight: 690; white-space: nowrap; opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast svg { width: 18px; height: 18px; color: var(--gold); }
.toast strong { color: var(--gold); }
.touch-stick { position: absolute; z-index: 3; width: 63px; height: 63px; border: 1px solid #ffe7bc7a; border-radius: 50%; background: #36243f46; left: 0; top: 0; pointer-events: none; }
.touch-stick i { position: absolute; inset: 20px; border-radius: 50%; background: #ffdb9cc7; border: 1px solid #fff3c8; box-shadow: 0 0 16px #f9b57575; }
.cockpit { position: absolute; z-index: 1; width: 100%; height: 40%; bottom: 0; left: 0; pointer-events: none; filter: drop-shadow(0 -6px 18px #35243742); }
.result-shade { position: absolute; inset: 0; z-index: 8; display: grid; place-items: center; background: #241a2c69; padding: 16px; }
.result { width: min(100%, 414px); padding: 28px; text-align: center; border-radius: 24px; border: 1px solid #ffe1b94f; background: #342639f2; box-shadow: 0 28px 100px #1b102878, inset 0 1px #fff5e21d; backdrop-filter: blur(17px); -webkit-backdrop-filter: blur(17px); }
.result-icon { width: 56px; height: 56px; display: grid; place-items: center; margin: 0 auto 15px; color: var(--gold); border: 1px solid #ffd09157; border-radius: 17px; background: #ffc48516; }
.result-icon svg { width: 32px; height: 32px; }
.result h2 { font: 600 clamp(30px, 5vw, 39px)/1.02 Georgia, serif; margin: 0; letter-spacing: -.05em; }
.result p { font-size: 14px; color: #e8d2c7; line-height: 1.45; margin: 12px 0 17px; }
.result-score { display: flex; gap: 10px; align-items: center; justify-content: center; margin-bottom: 19px; color: #e7c7bc; font-size: 12px; text-transform: uppercase; font-weight: 780; letter-spacing: .12em; }
.result-score strong { font-size: 24px; color: var(--gold); letter-spacing: -.025em; font-variant-numeric: tabular-nums; }
.result .action { min-width: 160px; }
svg.icon { fill: none; stroke: currentColor; stroke-width: 1.65; stroke-linecap: round; stroke-linejoin: round; }
@media (max-width: 780px) {
.dock { gap: 10px; }
.mission { padding: 13px; width: min(260px, 100%); }
.camera-button { padding: 0 9px; gap: 5px; }
}
@media (max-width: 620px) and (orientation: portrait) {
.hud { flex-direction: column; gap: 12px; }
.brand { align-self: flex-start; gap: 9px; }
.brand-mark { width: 39px; height: 39px; border-radius: 11px; }
.brand-mark svg { width: 26px; height: 26px; }
.brand h1 { font-size: 29px; }
.brand p { font-size: 8px; margin-top: 4px; }
.stats { align-self: stretch; min-height: 51px; padding: 4px 2px; border-radius: 13px; }
.stat, .stat:last-child { min-width: 0; flex: 1; justify-content: center; padding: 3px 6px 5px; gap: 7px; }
.stat svg { width: 16px; height: 16px; }
.stat strong { font-size: 19px; }
.stat span { font-size: 8px; letter-spacing: .10em; }
.dock { grid-template-columns: 1fr auto; gap: 9px; }
.mission { grid-column: 1 / -1; width: min(275px, 100%); }
.mission h2 { font-size: 21px; }
.mission p { margin: 5px 0 8px; }
.mission.is-flying { width: min(243px, 100%); padding: 9px 11px; }
.mission.is-flying .hint { margin-top: 6px; font-size: 10px; }
.cameras { justify-self: stretch; }
.camera-button { padding: 0 5px; flex: 1; min-height: 42px; font-size: 11px; }
.camera-button svg { width: 16px; height: 16px; }
.sound { width: 47px; height: 52px; }
.toast { top: max(129px, calc(env(safe-area-inset-top) + 114px)); }
.cockpit { height: 32%; }
}
@media (max-width: 355px) {
.camera-button { gap: 4px; }
.camera-button svg { width: 13px; }
.dock { gap: 7px; }
.sound { width: 42px; }
}
@media (max-height: 540px) and (orientation: landscape) {
:root { --outer: 11px; }
.brand h1 { font-size: 27px; }
.brand-mark { width: 39px; height: 39px; border-radius: 11px; }
.brand-mark svg { width: 26px; height: 26px; }
.brand p { margin-top: 4px; }
.stats { min-height: 48px; }
.stat { padding: 4px 10px; }
.stat strong { font-size: 17px; }
.mission { padding: 9px 11px; }
.mission h2 { font-size: 19px; }
.mission p { margin: 4px 0 6px; font-size: 11px; }
.hint { margin-top: 6px; }
.toast { top: 64px; }
.result { padding: 16px; }
.result-icon { width: 40px; height: 40px; margin-bottom: 9px; }
.result p { margin: 8px 0; }
.result-score { margin-bottom: 9px; }
}
</style>
</head>
<body>
<main class="stage" id="stage" aria-label="Night Courier flying game">
<svg width="0" height="0" aria-hidden="true" style="position:absolute">
<defs>
<symbol id="i-brand" viewBox="0 0 32 32">
<path fill="currentColor" stroke="none" d="M7.3 16.3c3.6.3 6 2 8.7 3.7 2.8-1.7 5.1-3.4 8.7-3.7 1.5-.1 3.6.4 5.2 1.2-2.1 2.9-5.9 4-9.7 3.7.3 1.6-.2 2.7-1.5 3.1l-2.7.7-2.7-.7c-1.3-.4-1.8-1.5-1.5-3.1-3.8.3-7.6-.8-9.7-3.7 1.6-.8 3.7-1.3 5.2-1.2Z"/>
<path d="M11 14.9v-5l3.3 2.4a7.7 7.7 0 0 1 3.4 0L21 9.9v5M14.7 17h2.6M16 4v3M7.8 7.2l1.5 1.6M24.2 7.2l-1.5 1.6"/>
</symbol>
<symbol id="i-star" viewBox="0 0 24 24"><path d="M12 2.8 14.9 9l6.8 1-4.9 4.8 1.2 6.8-6-3.2-6 3.2 1.2-6.8L2.3 10l6.8-1z"/></symbol>
<symbol id="i-fish" viewBox="0 0 24 24"><path d="M15.8 12c-2.8 4.9-7.7 5-12.4 0 4.7-5 9.6-4.9 12.4 0ZM15.8 12l4.8-4v8l-4.8-4ZM7.8 11.9h.15"/></symbol>
<symbol id="i-crate" viewBox="0 0 24 24"><path d="m3.2 7.5 8.8-4.4 8.8 4.4v9L12 21l-8.8-4.5zM3.2 7.5l8.8 4.6 8.8-4.6M12 12.1V21M7.5 5.4l8.8 4.5"/></symbol>
<symbol id="i-moon" viewBox="0 0 24 24"><path d="M20.5 15.2A8.8 8.8 0 0 1 8.8 3.6 8.9 8.9 0 1 0 20.5 15.2ZM16.3 4.8v4M14.3 6.8h4"/></symbol>
<symbol id="i-chase" viewBox="0 0 24 24"><path d="M12 3.3 14 10l7.5 4.2v3L14 15l-.5 4 2 1.1v1H8.5v-1l2-1.1-.5-4-7.5 2.2v-3L10 10z"/></symbol>
<symbol id="i-cockpit" viewBox="0 0 24 24"><path d="M3.1 14a8.9 8.9 0 0 1 17.8 0v4.3H3.1zM3.2 14h17.6M9 14l1-4m5 4-1-4M10.3 16.3h3.4"/></symbol>
<symbol id="i-overhead" viewBox="0 0 24 24"><path d="m12 3 9.6 5-9.6 5-9.6-5zM2.4 12l9.6 5 9.6-5M2.4 16l9.6 5 9.6-5"/></symbol>
<symbol id="i-sound" viewBox="0 0 24 24"><path d="M3 9h4l5-4v14l-5-4H3z"/></symbol>
<symbol id="i-arrow" viewBox="0 0 24 24"><path d="M4.5 12h14.6m-5.7-5.7 5.7 5.7-5.7 5.7"/></symbol>
<symbol id="i-pointer" viewBox="0 0 24 24"><path d="m6 3 12.1 10h-5.7l2.5 5.7-2.8 1.2-2.5-5.8-3.6 4.1z"/></symbol>
<symbol id="i-touch" viewBox="0 0 24 24"><path d="M8.6 13V5a1.6 1.6 0 0 1 3.2 0v5.6l4.3.6a3.1 3.1 0 0 1 2.6 3.5l-.5 3.6a2.8 2.8 0 0 1-2.8 2.4h-3.3a3.6 3.6 0 0 1-2.8-1.3l-3.6-4.3a1.5 1.5 0 0 1 2.2-2z"/></symbol>
<symbol id="i-waypoint" viewBox="0 0 24 24"><circle cx="12" cy="12" r="7.5"/><path d="M12 2v4M12 18v4M2 12h4m12 0h4M10 12l2-2 2 2-2 2z"/></symbol>
<symbol id="i-parachute" viewBox="0 0 24 24"><path d="M3.2 10a8.8 8.8 0 0 1 17.6 0H3.2ZM8 10c0-4 1.6-7.1 4-8.7 2.4 1.6 4 4.7 4 8.7M3.5 10l6.3 6.1m10.7-6.1-6.3 6.1M9.5 16h5v5h-5z"/></symbol>
<symbol id="i-replay" viewBox="0 0 24 24"><path d="M5 9a8 8 0 1 1-.3 5.8M5 4v5h5"/></symbol>
</defs>
</svg>
<canvas id="world" class="scene" aria-hidden="true"></canvas>
<canvas id="fallback" class="scene" aria-hidden="true" hidden></canvas>
<svg id="cockpit" class="cockpit" viewBox="0 0 1000 255" preserveAspectRatio="xMidYMax slice" aria-hidden="true" hidden>
<defs>
<linearGradient id="dash-paint" x2="0" y2="1" gradientUnits="objectBoundingBox">
<stop stop-color="#725054"/><stop offset=".47" stop-color="#412e3c"/><stop offset="1" stop-color="#241e32"/>
</linearGradient>
<linearGradient id="dash-rail" x2="0" y2="1" gradientUnits="objectBoundingBox">
<stop stop-color="#ffdb9b"/><stop offset=".42" stop-color="#d78957"/><stop offset="1" stop-color="#8b554e"/>
</linearGradient>
<radialGradient id="gauge-face"><stop stop-color="#554451"/><stop offset="1" stop-color="#242132"/></radialGradient>
</defs>
<path d="M-40 194Q100 160 194 85Q302 3 500 12Q698 3 806 85Q900 160 1040 194V275H-40Z" fill="#261c30"/>
<path d="M-40 204Q100 172 202 96Q306 17 500 25Q694 17 798 96Q900 172 1040 204V275H-40Z" fill="url(#dash-paint)" stroke="url(#dash-rail)" stroke-width="6"/>
<path d="M-20 221Q104 201 218 115Q315 42 500 47Q685 42 782 115Q896 201 1020 221" fill="none" stroke="#f1b77f" stroke-width="1.2" opacity=".49"/>
<g fill="#b98268" stroke="#f4c08d" stroke-width="1"><circle cx="280" cy="83" r="3.5"/><circle cx="720" cy="83" r="3.5"/><circle cx="203" cy="138" r="3"/><circle cx="797" cy="138" r="3"/><circle cx="119" cy="196" r="3"/><circle cx="881" cy="196" r="3"/></g>
<g id="instruments"></g>
<path d="M500 252v-36m-88-18q88 43 176 0" stroke="#271e2e" stroke-width="25" fill="none" stroke-linecap="round"/>
<path d="M500 252v-36m-88-18q88 43 176 0" stroke="#875b51" stroke-width="14" fill="none" stroke-linecap="round"/>
<path d="M500 244v-24m-84-23q84 35 168 0" stroke="#bc8f69" stroke-width="2" fill="none" opacity=".68"/>
<g fill="#f4a259" stroke="#ab583d" stroke-width="3">
<path d="M374 261c6-31 10-49 20-68 5-10 13-15 23-12 11 3 12 16 8 25-4 11-11 20-11 29l-5 26Z"/>
<path d="M626 261c-6-31-10-49-20-68-5-10-13-15-23-12-11 3-12 16-8 25 4 11 11 20 11 29l5 26Z"/>
</g>
<path d="m396 193 8 3m0-9 9 4m191 2-8 3m0-9-9 4" stroke="#ffe0a5" stroke-width="2.2" stroke-linecap="round"/>
</svg>
<div class="shade"></div>
<div id="flightSurface" class="flight-surface" aria-hidden="true"></div>
<div id="stick" class="touch-stick" hidden><i></i></div>
<header class="hud">
<div class="brand">
<div class="brand-mark"><svg class="icon" viewBox="0 0 32 32" aria-hidden="true"><use href="#i-brand"/></svg></div>
<div><h1>Night Courier</h1><p>Lake Alder</p></div>
</div>
<div class="stats" aria-label="Flight status">
<div class="stat"><svg class="icon" aria-hidden="true"><use href="#i-star"/></svg><div><span>Score</span><strong id="score">0</strong></div></div>
<div class="stat"><svg class="icon" aria-hidden="true"><use href="#i-crate"/></svg><div><span>Crates left</span><strong id="crates">5</strong></div></div>
<div id="clock" class="stat"><svg class="icon" aria-hidden="true"><use href="#i-moon"/></svg><div><span>Nightfall</span><strong id="time">1:15</strong></div><div class="clock-line"><i id="clockLine"></i></div></div>
</div>
</header>
<div class="beacon" id="beacon" aria-hidden="true" hidden><span class="diamond"></span><svg class="icon arrow"><use href="#i-arrow"/></svg><span id="beaconText">Willow Dock</span></div>
<div class="toast" id="toast" role="status" aria-live="polite"><svg class="icon" aria-hidden="true"><use id="toastIcon" href="#i-parachute"/></svg><span id="toastText"></span><strong id="toastPoints"></strong></div>
<footer class="dock">
<section id="mission" class="mission is-ready" aria-label="Delivery route">
<div id="readyCard"><h2>Fish for the village</h2><p>Fly through the glowing rings to drop off fish before dark.</p><button id="start" class="action" type="button">Start delivery <svg class="icon" aria-hidden="true"><use href="#i-arrow"/></svg></button></div>
<div id="routeCard" class="route-info" hidden><div class="route-symbol"><svg class="icon" aria-hidden="true"><use href="#i-waypoint"/></svg></div><div class="route-text"><span id="routeLabel" class="mission-label">Next delivery</span><strong id="routeName">Willow Dock</strong></div><span id="distance" class="distance"></span></div>
<div class="hint"><svg class="icon" aria-hidden="true"><use id="hintIcon" href="#i-pointer"/></svg><span id="hintText">Move your mouse to steer</span></div>
</section>
<div class="cameras" role="group" aria-label="Camera">
<button class="camera-button" type="button" data-camera="0" aria-pressed="true" aria-label="Behind the plane"><svg class="icon" aria-hidden="true"><use href="#i-chase"/></svg>Behind</button>
<button class="camera-button" type="button" data-camera="1" aria-pressed="false" aria-label="Inside the cockpit"><svg class="icon" aria-hidden="true"><use href="#i-cockpit"/></svg>Cockpit</button>
<button class="camera-button" type="button" data-camera="2" aria-pressed="false" aria-label="Overhead view"><svg class="icon" aria-hidden="true"><use href="#i-overhead"/></svg>Overhead</button>
</div>
<button id="sound" class="sound" type="button" aria-pressed="false" aria-label="Sound" title="Toggle sound"><svg class="icon" aria-hidden="true"><use href="#i-sound"/><path class="sound-off" d="m16.4 10 4.2 4.2m0-4.2-4.2 4.2"/><path class="sound-on" d="M15.4 9a4 4 0 0 1 0 6m2.6-8.5a7.4 7.4 0 0 1 0 11"/></svg></button>
</footer>
<div id="resultShade" class="result-shade" hidden>
<section class="result" role="dialog" aria-modal="true" aria-labelledby="resultTitle" aria-describedby="resultText">
<div class="result-icon"><svg class="icon" aria-hidden="true"><use id="resultIcon" href="#i-fish"/></svg></div>
<h2 id="resultTitle">All fish delivered!</h2>
<p id="resultText"></p>
<div class="result-score">Score <strong id="finalScore">500</strong></div>
<button id="again" class="action" type="button"><svg class="icon" aria-hidden="true"><use href="#i-replay"/></svg> Play again</button>
</section>
</div>
</main>
<script id="screen-v" type="x-shader/x-vertex">
attribute vec2 aCorner;
varying vec2 vScreen;
void main() { vScreen = aCorner; gl_Position = vec4(aCorner, 0., 1.); }
</script>
<script id="sky-f" type="x-shader/x-fragment">
precision highp float;
varying vec2 vScreen;
uniform vec3 uRight, uUp, uFwd, uSun;
uniform vec2 uLens;
uniform float uTime, uDusk;
float hash21(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
float noise01(vec2 p) {
vec2 i=floor(p), f=fract(p);
f=f*f*(3.-2.*f);
return mix(mix(hash21(i), hash21(i+vec2(1,0)), f.x), mix(hash21(i+vec2(0,1)), hash21(i+vec2(1,1)), f.x), f.y);
}
float puff(vec2 p, vec2 size) { return 1.-length(p/size); }
float cloud(vec2 az, vec2 center, vec2 size, float key) {
float dx=mod(az.x-center.x+3.14159-uTime*.0016,6.28318)-3.14159;
vec2 p=vec2(dx, az.y-center.y)/size;
float edge=puff(p,vec2(.98,.37));
edge=max(edge,puff(p-vec2(-.38,.12),vec2(.58,.51)));
edge=max(edge,puff(p-vec2(.12,.29),vec2(.51,.57)));
edge=max(edge,puff(p-vec2(.55,.09),vec2(.45,.42)));
float grit=noise01(p*4.4+key)*.31+noise01(p*10.1+key)*.10;
return smoothstep(-.115,.14,edge+grit-.23);
}
vec3 paintCloud(vec3 col, vec2 az, vec2 center, vec2 size, float key) {
float mask=cloud(az,center,size,key);
float height=(az.y-center.y)/size.y;
vec3 fringe=mix(vec3(1.,.68,.54),vec3(.53,.34,.51),uDusk);
vec3 cap=mix(vec3(.70,.48,.62),vec3(.27,.24,.44),uDusk);
return mix(col, mix(fringe,cap,smoothstep(-.26,.77,height)), mask*.68);
}
void main() {
vec3 ray=normalize(uFwd + uRight*vScreen.x*uLens.x + uUp*vScreen.y*uLens.y);
float h=ray.y;
float sd=max(0.,dot(ray,uSun));
vec3 horizon=mix(vec3(.99,.59,.43),vec3(.50,.29,.43),uDusk);
vec3 zenith=mix(vec3(.35,.32,.55),vec3(.115,.11,.29),uDusk);
vec3 c=mix(horizon,zenith,smoothstep(-.04,.67,h));
c=mix(c,vec3(.30,.24,.40),smoothstep(-.22,-.76,h)*.59);
c+=vec3(.16,.101,.035)*pow(sd,8.)*(1.-uDusk*.72);
float halo=exp(min(0.,(sd-1.)*169.));
c+=halo*vec3(.15,.103,.041)*(1.-uDusk*.84);
float solar=smoothstep(.99855,.99886,sd);
c=mix(c,mix(vec3(1.,.928,.715),vec3(1.,.64,.48),uDusk),solar*(1.-smoothstep(.79,1.,uDusk)));
vec2 az=vec2(atan(ray.x,ray.z),h);
c=paintCloud(c,az,vec2(-.61,.209),vec2(.225,.039),2.);
c=paintCloud(c,az,vec2(-.35,.358),vec2(.297,.054),4.);
c=paintCloud(c,az,vec2(.33,.266),vec2(.245,.047),9.);
c=paintCloud(c,az,vec2(.78,.443),vec2(.282,.058),17.);
c=paintCloud(c,az,vec2(1.39,.17),vec2(.26,.037),24.);
c=paintCloud(c,az,vec2(2.08,.34),vec2(.31,.052),7.);
c=paintCloud(c,az,vec2(-2.83,.25),vec2(.36,.055),3.);
c=paintCloud(c,az,vec2(-1.71,.32),vec2(.29,.05),34.);
vec2 starPos=az*vec2(64.,61.);
float seed=hash21(floor(starPos));
vec2 star=fract(starPos)-vec2(.36+seed*.18,.45);
float sparkle=(1.-smoothstep(.025,.123,length(star)))*step(.943,seed);
c+=sparkle*vec3(.70,.61,.54)*smoothstep(.42,.95,uDusk)*smoothstep(.09,.25,h);
c+=(hash21(gl_FragCoord.xy)-.5)*.020;
gl_FragColor=vec4(c,1.);
}
</script>
<script id="mesh-v" type="x-shader/x-vertex">
precision highp float;
attribute vec3 aP, aN, aC;
attribute float aG;
uniform vec3 uEye, uRight, uUp, uFwd;
uniform vec2 uLens, uDepth;
uniform mat4 uModel;
uniform float uTime, uReflect;
varying vec3 vPosition, vNormal, vColor;
varying float vGlow, vOriginalY;
void main() {
vec3 p=(uModel*vec4(aP,1.)).xyz;
vOriginalY=p.y;
if(uReflect>.5) {
p.x+=sin(p.z*.52+p.y*2.15+uTime*1.45)*(.08+min(p.y,.9)*.2);
p.y=-p.y;
}
vPosition=p;
vNormal=(uModel*vec4(aN,0.)).xyz;
vColor=aC;
vGlow=aG;
vec3 d=p-uEye;
float z=dot(d,uFwd);
gl_Position=vec4(dot(d,uRight)/uLens.x,dot(d,uUp)/uLens.y,z*uDepth.x+uDepth.y,z);
}
</script>
<script id="mesh-f" type="x-shader/x-fragment">
precision highp float;
uniform vec3 uEye;
uniform float uDusk, uReflect;
varying vec3 vPosition, vNormal, vColor;
varying float vGlow, vOriginalY;
void main() {
if(uReflect>.5 && vOriginalY<.018) discard;
vec3 n=normalize(vNormal);
float light=max(0.,dot(n,vec3(-.57,.744,-.35)));
float bounce=max(0.,dot(n,vec3(.41,.32,.85)));
vec3 sun=mix(vec3(1.07,.93,.85),vec3(.63,.64,.89),uDusk);
vec3 base=vColor*(.72 + light*.40 + bounce*.10)*sun;
base+=vGlow*(.18+uDusk*.33)*vColor;
float haze=1.-exp(-length(vPosition-uEye)*.0061);
vec3 fog=mix(vec3(.71,.47,.55),vec3(.25,.23,.43),uDusk);
vec3 c=mix(base,fog,haze*.71*(1.-vGlow*.22));
if(uReflect>.5) c=mix(c*vec3(.78,.77,.89),vec3(.43,.29,.46),.18);
gl_FragColor=vec4(c,1.);
}
</script>
<script id="water-v" type="x-shader/x-vertex">
attribute vec3 aP;
uniform vec3 uEye, uRight, uUp, uFwd;
uniform vec2 uLens, uDepth;
varying vec3 vPosition;
void main() {
vPosition=aP;
vec3 d=aP-uEye;
float z=dot(d,uFwd);
gl_Position=vec4(dot(d,uRight)/uLens.x,dot(d,uUp)/uLens.y,z*uDepth.x+uDepth.y,z);
}
</script>
<script id="water-f" type="x-shader/x-fragment">
precision highp float;
uniform vec3 uEye, uSun;
uniform float uTime, uDusk;
varying vec3 vPosition;
float hash21(vec2 p) { return fract(sin(dot(p,vec2(127.1,311.7)))*43758.5453); }
float noise01(vec2 p) {
vec2 i=floor(p), f=fract(p);
f=f*f*(3.-2.*f);
return mix(mix(hash21(i),hash21(i+vec2(1,0)),f.x),mix(hash21(i+vec2(0,1)),hash21(i+vec2(1,1)),f.x),f.y);
}
void main() {
vec3 d=vPosition-uEye;
float dist=length(d);
float mist=1.-exp(-dist*.010);
vec3 nearCol=mix(vec3(.205,.271,.371),vec3(.088,.117,.234),uDusk);
vec3 farCol=mix(vec3(.67,.43,.48),vec3(.34,.24,.40),uDusk);
vec3 col=mix(nearCol,farCol,mist*.98);
vec2 xz=vPosition.xz;
float broad=noise01(xz*vec2(.13,.42)+vec2(uTime*.17,-uTime*.36));
float flecks=noise01(xz*vec2(.57,2.45)+vec2(-uTime*.24,uTime*.9));
float wave=sin(xz.y*3.18+xz.x*.79+broad*5.1+uTime*2.1)*.5+.5;
float bars=smoothstep(.55,.99,wave)*smoothstep(.32,.81,broad*.58+flecks*.42);
float distanceFade=1.-smoothstep(85.,250.,dist);
col+=(broad-.47)*vec3(.078,.068,.078)*distanceFade;
col+=bars*vec3(.23,.18,.17)*distanceFade;
vec2 toSun=normalize(uSun.xz);
vec2 view=normalize(d.xz+vec2(.0001));
float side=view.x*toSun.y-view.y*toSun.x+(broad-.5)*(.058+.16*(1.-mist));
float width=.057+.065*(1.-mist);
float glimmer=exp(-side*side/(width*width))*smoothstep(0.,.4,dot(view,toSun));
vec3 gold=mix(vec3(1.,.77,.54),vec3(.81,.40,.36),uDusk);
col=mix(col,gold,glimmer*(.16+.57*bars+.10*broad)*(1.-uDusk*.70));
col+=(hash21(gl_FragCoord.xy)-.5)*.018;
gl_FragColor=vec4(col,.79-.12*mist+.06*glimmer);
}
</script>
<script id="ring-v" type="x-shader/x-vertex">
attribute vec2 aCorner;
uniform vec3 uEye, uRight, uUp, uFwd;
uniform vec2 uLens, uDepth;
uniform mat4 uModel;
varying vec2 vLocal;
void main() {
vLocal=aCorner;
vec3 p=(uModel*vec4(aCorner,0.,1.)).xyz-uEye;
float z=dot(p,uFwd);
gl_Position=vec4(dot(p,uRight)/uLens.x,dot(p,uUp)/uLens.y,z*uDepth.x+uDepth.y,z);
}
</script>
<script id="ring-f" type="x-shader/x-fragment">
precision mediump float;
varying vec2 vLocal;
uniform float uTime, uOpacity;
void main() {
float r=length(vLocal);
float angle=atan(vLocal.y,vLocal.x);
float d=abs(r-.775);
float core=1.-smoothstep(.007,.027,d);
float collar=1.-smoothstep(.004,.012,abs(r-.830));
float marks=step(.79,cos(angle*8.))*(1.-smoothstep(.014,.036,abs(r-.835)));
float pulse=.90+.10*sin(uTime*3.);
float haze=exp(-d*32.)*.49+exp(-d*12.)*.18;
float sparkle=pow(.5+.5*cos(angle*3.-uTime*2.9),9.)*core;
vec3 warm=vec3(1.,.55,.235);
vec3 light=vec3(1.,.934,.688);
vec3 c=mix(warm,light,clamp(core*.88+sparkle*.24+marks*.41,0.,1.));
float alpha=clamp((haze+core*.92+collar*.19+marks*.44)*pulse*uOpacity,0.,1.);
if(alpha<.012 || r>.996) discard;
gl_FragColor=vec4(c,alpha);
}
</script>
<script>
(() => {
"use strict";
const $ = id => document.getElementById(id);
const ui = {
stage: $("stage"), canvas: $("world"), fallback: $("fallback"), surface: $("flightSurface"),
cockpit: $("cockpit"), stick: $("stick"), start: $("start"), again: $("again"), sound: $("sound"),
score: $("score"), crates: $("crates"), time: $("time"), clock: $("clock"), clockLine: $("clockLine"),
mission: $("mission"), ready: $("readyCard"), routeCard: $("routeCard"), routeLabel: $("routeLabel"),
routeName: $("routeName"), distance: $("distance"), hintText: $("hintText"), hintIcon: $("hintIcon"),
beacon: $("beacon"), beaconText: $("beaconText"), toast: $("toast"), toastText: $("toastText"),
toastPoints: $("toastPoints"), toastIcon: $("toastIcon"), result: $("resultShade"),
resultTitle: $("resultTitle"), resultText: $("resultText"), resultIcon: $("resultIcon"), finalScore: $("finalScore"),
cameraButtons: Array.from(document.querySelectorAll("[data-camera]"))
};
const PI = Math.PI;
const TAU = PI * 2;
const clamp = (v, a, b) => Math.max(a, Math.min(b, v));
const sat = v => clamp(v, 0, 1);
const mix = (a, b, t) => a + (b - a) * t;
const smooth = (a, b, v) => { const t = sat((v - a) / (b - a)); return t * t * (3 - 2 * t); };
const add = (a, b) => [a[0] + b[0], a[1] + b[1], a[2] + b[2]];
const sub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
const mul = (a, t) => [a[0] * t, a[1] * t, a[2] * t];
const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
const cross = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
const norm = a => Math.hypot(a[0], a[1], a[2]);
const unit = a => mul(a, 1 / (norm(a) || 1));
const along = (a, b, t) => [mix(a[0], b[0], t), mix(a[1], b[1], t), mix(a[2], b[2], t)];
const angleDelta = (a, b) => Math.atan2(Math.sin(a - b), Math.cos(a - b));
const yawTo = d => Math.atan2(d[0], d[2]);
const pitchTo = d => Math.atan2(d[1], Math.hypot(d[0], d[2]));
const hex = s => {
const v = Number.parseInt(s.replace("#", ""), 16);
return [(v >> 16) / 255, ((v >> 8) & 255) / 255, (v & 255) / 255];
};
const rgb = c => "rgb(" + c.map(v => Math.round(sat(v) * 255)).join(",") + ")";
const shade = (c, t) => mul(c, t);
let seed = 491802;
function random() {
seed = (Math.imul(1664525, seed) + 1013904223) >>> 0;
return seed / 4294967296;
}
const up = [0, 1, 0];
const identity = new Float32Array([1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1]);
function matrix(right, rise, forward, p) {
return new Float32Array([right[0],right[1],right[2],0, rise[0],rise[1],rise[2],0, forward[0],forward[1],forward[2],0, p[0],p[1],p[2],1]);
}
function frame(p, yaw = 0, pitch = 0, bank = 0, scale = 1) {
const f = [Math.sin(yaw) * Math.cos(pitch), Math.sin(pitch), Math.cos(yaw) * Math.cos(pitch)];
const r = [Math.cos(yaw), 0, -Math.sin(yaw)];
const u = cross(f, r);
return matrix(mul(add(mul(r, Math.cos(bank)), mul(u, Math.sin(bank))), scale),
mul(sub(mul(u, Math.cos(bank)), mul(r, Math.sin(bank))), scale), mul(f, scale), p);
}
const basis = (m, i) => [m[i], m[i + 1], m[i + 2]];
const transform = (m, p) => [
m[0]*p[0]+m[4]*p[1]+m[8]*p[2]+m[12],
m[1]*p[0]+m[5]*p[1]+m[9]*p[2]+m[13],
m[2]*p[0]+m[6]*p[1]+m[10]*p[2]+m[14]
];
class Mesh {
constructor() { this.data = []; this.packed = null; }
tri(a, b, c, paint, glow = 0) {
const n = cross(sub(b, a), sub(c, a));
const l = norm(n);
if (l < .000001) return;
const nx = n[0] / l, ny = n[1] / l, nz = n[2] / l;
for (const p of [a, b, c]) this.data.push(p[0],p[1],p[2],nx,ny,nz,paint[0],paint[1],paint[2],glow);
}
quad(a, b, c, d, paint, glow = 0) {
this.tri(a, b, c, paint, glow);
this.tri(a, c, d, paint, glow);
}
top(a, b, c, paint, glow = 0) {
if (cross(sub(b, a), sub(c, a))[1] < 0) this.tri(a, c, b, paint, glow);
else this.tri(a, b, c, paint, glow);
}
box(x, y, z, w, h, d, paint, yaw = 0, glow = 0) {
const co = Math.cos(yaw), si = Math.sin(yaw);
const corner = (a, b, c) => [x + co*a + si*c, y + b, z - si*a + co*c];
const a=corner(-w/2,-h/2,-d/2), b=corner(w/2,-h/2,-d/2), c=corner(w/2,-h/2,d/2), d1=corner(-w/2,-h/2,d/2);
const e=corner(-w/2,h/2,-d/2), f=corner(w/2,h/2,-d/2), g=corner(w/2,h/2,d/2), h1=corner(-w/2,h/2,d/2);
this.quad(a,e,f,b,paint,glow);
this.quad(c,g,h1,d1,paint,glow);
this.quad(d1,h1,e,a,shade(paint,.965),glow);
this.quad(b,f,g,c,shade(paint,.965),glow);
this.quad(e,h1,g,f,shade(paint,1.045),glow);
this.quad(a,b,c,d1,shade(paint,.84),glow);
}
frustum(x, y, z, h, bottom, top, paint, sides = 10, turn = 0, rz = 1, glow = 0) {
const a = [], b = [];
for (let i = 0; i < sides; i++) {
const t = i * TAU / sides + turn;
a.push([x + Math.cos(t)*bottom, y, z + Math.sin(t)*bottom*rz]);
b.push([x + Math.cos(t)*top, y+h, z + Math.sin(t)*top*rz]);
}
for (let i = 0; i < sides; i++) {
const j = (i+1)%sides;
const c = shade(paint, 1 + .035 * Math.sin(i*4.13));
this.quad(a[i],b[i],b[j],a[j],c,glow);
this.tri([x,y,z],a[i],a[j],shade(c,.9),glow);
if (top > .001) this.tri([x,y+h,z],b[j],b[i],c,glow);
}
}
ball(x, y, z, rx, ry, rz, paint, sides = 10, rows = 6, glow = 0) {
const layers = [];
for (let k = 0; k <= rows; k++) {
const el = -PI/2 + PI*k/rows;
const layer = [];
for (let i = 0; i < sides; i++) {
const t=i*TAU/sides;
layer.push([x+rx*Math.cos(t)*Math.cos(el),y+ry*Math.sin(el),z+rz*Math.sin(t)*Math.cos(el)]);
}
layers.push(layer);
}
for (let k = 0; k < rows; k++) for (let i = 0; i < sides; i++) {
const j=(i+1)%sides;
this.quad(layers[k][i],layers[k+1][i],layers[k+1][j],layers[k][j],shade(paint,1+.023*Math.sin(i*3.2+k*1.74)),glow);
}
}
tube(a, b, r0, r1, paint, sides = 7, glow = 0) {
const axis=unit(sub(b,a));
const x=unit(cross(axis, Math.abs(axis[1])>.93 ? [1,0,0] : up));
const z=cross(x,axis);
const base=[], tip=[];
for (let i=0;i<sides;i++) {
const t=i*TAU/sides;
const n=add(mul(x,Math.cos(t)),mul(z,Math.sin(t)));
base.push(add(a,mul(n,r0)));
tip.push(add(b,mul(n,r1)));
}
for(let i=0;i<sides;i++) {
const j=(i+1)%sides;
this.quad(base[i],tip[i],tip[j],base[j],paint,glow);
this.tri(a,base[i],base[j],paint,glow);
if(r1>.001) this.tri(b,tip[j],tip[i],paint,glow);
}
}
disk(x, y, z, rx, rz, paint, sides = 13, glow = 0) {
for (let i=0;i<sides;i++) {
const a=i*TAU/sides, b=(i+1)*TAU/sides;
this.tri([x,y,z],[x+rx*Math.cos(b),y,z+rz*Math.sin(b)],[x+rx*Math.cos(a),y,z+rz*Math.sin(a)],paint,glow);
}
}
join(mesh, m) {
const v=mesh.vertices();
for(let i=0;i<v.length;i+=30) {
const point=j=>transform(m,[v[j],v[j+1],v[j+2]]);
this.tri(point(i),point(i+10),point(i+20),[v[i+6],v[i+7],v[i+8]],v[i+9]);
}
}
vertices() {
if (!this.packed) { this.packed=new Float32Array(this.data); this.data=[]; }
return this.packed;
}
}
const scenery = new Mesh();
const pineDark=hex("#394854"), pineLight=hex("#575e59"), trunk=hex("#715450");
const plaster=hex("#ffe0af"), timber=hex("#603e46"), stone=hex("#9a7372");
const ground = .90;
function path(mesh, points, width, y, paint) {
const sides = points.map((p,i) => {
const a=points[Math.max(0,i-1)], b=points[Math.min(points.length-1,i+1)];
const length=Math.hypot(b[0]-a[0],b[1]-a[1]) || 1;
const n=[-(b[1]-a[1])*width/(2*length),(b[0]-a[0])*width/(2*length)];
return [[p[0]+n[0],y,p[1]+n[1]],[p[0]-n[0],y,p[1]-n[1]]];
});
for(let i=1;i<sides.length;i++) {
const [a,b]=sides[i-1], [c,d]=sides[i];
mesh.top(a,b,d,paint);
mesh.top(a,d,c,paint);
}
}
function island(outline, paint) {
let points=outline;
for(let step=0;step<2;step++) {
points=points.flatMap((p,i)=>{
const q=points[(i+1)%points.length];
return [[mix(p[0],q[0],.22),mix(p[1],q[1],.22)],[mix(p[0],q[0],.78),mix(p[1],q[1],.78)]];
});
}
const center=points.reduce((a,p)=>[a[0]+p[0]/points.length,a[1]+p[1]/points.length],[0,0]);
const edge=(i,y,scale) => [mix(center[0],points[i][0],scale), y, mix(center[1],points[i][1],scale)];
for(let i=0;i<points.length;i++) {
const j=(i+1)%points.length;
const a=edge(i,ground,1), b=edge(j,ground,1);
const c=edge(j,.26,1.017), d=edge(i,.26,1.017);
const c0=edge(j,-.27,1.029), d0=edge(i,-.27,1.029);
scenery.top([center[0],ground,center[1]],a,b,shade(paint,.96+random()*.08));
scenery.quad(d,c,b,a,shade(stone,.82+random()*.24));
scenery.quad(d0,c0,c,d,hex("#76616b"));
scenery.top(a,b,edge(j,ground+.012,.96),hex("#be9780"));
scenery.top(a,edge(j,ground+.012,.96),edge(i,ground+.012,.96),hex("#be9780"));
if(i%3!==1) {
const wetA=edge(i,.028,1.05), wetB=edge(j,.027,1.05);
scenery.top(wetA,wetB,edge(j,.029,1.039),hex("#a38791"));
scenery.top(wetA,edge(j,.029,1.039),edge(i,.029,1.039),hex("#a38791"));
}
}
}
function ridge(cx, cz, width, depth, yaw, base, peak, phase, tint) {
const cols=54, rows=7, grid=[];
const co=Math.cos(yaw), si=Math.sin(yaw);
for(let j=0;j<=rows;j++) {
const row=[];
const v=j/rows;
for(let i=0;i<=cols;i++) {
const t=i/cols;
const x=(t-.5)*width;
const envelope=Math.pow(Math.sin(PI*t),.50);
const lobes=.47+.27*Math.sin(t*19.5+phase)+.16*Math.sin(t*39.9-1.7*phase)+.085*Math.sin(t*83.1+phase*.4);
const crest=.53+.10*Math.sin(t*12+phase);
const rise=Math.max(0,1-Math.abs(v-crest)/(v>crest ? 1-crest : crest));
const y=-1.45+envelope*(base+peak*lobes)*Math.pow(rise,.71)*(1+.09*Math.sin(i*9.11+j*4.8));
const z=(v-.5)*depth+3.2*Math.sin(t*22+phase);
row.push([cx+x*co+z*si,y,cz-x*si+z*co]);
}
grid.push(row);
}
for(let j=0;j<rows;j++) for(let i=0;i<cols;i++) {
const paint=shade(tint,.86+(Math.sin(i*4.27+j*6.1)*.5+.5)*.23);
scenery.top(grid[j][i],grid[j+1][i],grid[j+1][i+1],paint);
scenery.top(grid[j][i],grid[j+1][i+1],grid[j][i+1],shade(paint,.98));
}
}
ridge(-62,237,432,128, .02,13,61,1.6,hex("#8a6f88"));
ridge(117,195,297,88,-.11,10,48,2.2,hex("#765b76"));
ridge(-146,134,224,79,.46,8,35,3.8,hex("#665572"));
ridge(162,109,256,69,-.76,8,35,7,hex("#625367"));
ridge(-137,-9,208,56,1.43,7,31,4.4,hex("#635365"));
ridge(160,-67,204,77,-1.28,8,40,2.5,hex("#645169"));
ridge(0,-190,383,88, PI,8,51,8.6,hex("#68536e"));
island([[11,-4],[15,-12],[29,-14],[46,-12],[62,-6],[69,7],[70,23],[64,39],[52,49],[35,52],[21,46],[12,34],[11,22],[13,12],[10,3]],hex("#8d776b"));
island([[-49,-2],[-42,-11],[-30,-11],[-20,-5],[-17,4],[-20,14],[-28,22],[-41,21],[-51,12]],hex("#786c66"));
island([[-48,47],[-41,39],[-29,40],[-22,43],[-20,51],[-24,62],[-35,67],[-47,63],[-51,54]],hex("#787365"));
island([[-8,19],[-4,15],[3,16],[5,21],[1,27],[-7,26],[-10,22]],hex("#7d766c"));
island([[68,-19],[75,-23],[84,-16],[84,-7],[77,-4],[68,-9]],hex("#837364"));
const lane = hex("#b49888");
path(scenery,[[17,-8],[17,-1],[16,9],[17,20],[18,31],[26,43],[36,45]],2.7,.924,lane);
path(scenery,[[19,2],[30,2],[42,1],[54,3],[63,8]],2.35,.925,shade(lane,.93));
path(scenery,[[17,17],[27,17],[39,18],[51,20],[62,23]],3.0,.928,lane);
path(scenery,[[20,33],[32,34],[45,34],[56,38]],2.5,.926,lane);
path(scenery,[[36,-6],[35,7],[36,17],[33,29],[32,42]],2.4,.929,lane);
path(scenery,[[-45,-1],[-38,4],[-32,5],[-25,5],[-22,2]],1.8,.924,lane);
path(scenery,[[-44,56],[-36,53],[-26,50],[-21,50]],1.8,.924,lane);
function frontPanel(mesh,x,y,z,w,h,paint,yaw=0,glow=0) {
const c=Math.cos(yaw)*w/2, s=-Math.sin(yaw)*w/2;
mesh.quad([x-c,y-h/2,z-s],[x-c,y+h/2,z-s],[x+c,y+h/2,z+s],[x+c,y-h/2,z+s],paint,glow);
}
function windowPane(mesh,x,y,z,w,h,yaw,lit,trim) {
const c=Math.cos(yaw), s=Math.sin(yaw);
const panel=(dx,dy,forward,ww,hh,paint,glow=0)=>frontPanel(mesh,x+c*dx-s*forward,y+dy,z-s*dx-c*forward,ww,hh,paint,yaw,glow);
panel(0,0,0,w+.12,h+.13,trim);
panel(0,0,.015,w,h,hex("#513547"));
panel(-w*.19,.017,.025,w*.30,h*.73,lit ? hex("#ffc77c") : hex("#806781"),lit ? .72 : .03);
panel(w*.19,.017,.026,w*.30,h*.73,lit ? hex("#fff0b6") : hex("#9b7b8a"),lit ? .85 : .03);
panel(0,h*.03,.039,w*.78,.055,timber);
panel(0,-h*.55,.047,w+.22,.080,trim);
}
function cottage(spec) {
const x=spec.x, z=spec.z, w=spec.w, d=spec.d, h=spec.h, yaw=spec.yaw || 0;
const body=hex(spec.body), roof=hex(spec.roof), m=new Mesh();
const gable=spec.peak || 1.35;
m.box(0,.16,0,w+.12,.32,d+.14,shade(stone,.78));
m.box(0,h/2+.04,0,w,h,d,body);
m.box(0,.36,0,w+.034,.105,d+.038,shade(body,.83));
const levels=h>3.75 ? 2 : 1;
const columns=w>5.6 ? 3 : 2;
for(let j=0;j<levels;j++) for(let i=0;i<columns;i++) {
const px=(i-(columns-1)/2)*(w/(columns+.52));
if(j===0 && columns===3 && i===1) continue;
windowPane(m,px,h*(levels===1 ? .61 : .37+j*.35),-d/2-.01,.59+.045*w,.75+(j*.073),0,random()>.23,plaster);
}
for(let side of [-1,1]) for(let i=0;i<2;i++) {
const py=levels===1 ? h*.63 : h*.69;
windowPane(m,side*(w/2+.009),py,(i-.5)*d*.46,.72,.84,-side*PI/2,random()>.37,plaster);
if(levels===2) windowPane(m,side*(w/2+.01),h*.33,(i-.5)*d*.46,.68,.74,-side*PI/2,random()>.36,shade(plaster,.94));
}
frontPanel(m,0,.94,-d/2-.022,.91,1.76,timber);
frontPanel(m,0,.95,-d/2-.037,.72,1.55,shade(roof,.91));
frontPanel(m,.22,.91,-d/2-.052,.08,.08,plaster);
m.box(0,.04,-d/2-.22,1.25,.09,.62,stone);
m.box(0,h+.045,0,w+.21,.14,d+.21,shade(roof,.76));
if(spec.flat) {
m.box(0,h+.153,0,w+.31,.154,d+.31,roof);
const rail=shade(roof,1.18);
m.box(-w/2,h+.34,0,.17,.22,d+.34,rail);
m.box(w/2,h+.34,0,.17,.22,d+.34,rail);
m.box(0,h+.34,d/2,w,.22,.17,rail);
m.box(-w*.36,h+.30,-d/2,w*.28,.14,.16,rail);
m.box(w*.36,h+.30,-d/2,w*.28,.14,.16,rail);
m.box(w*.31,h+.43,d*.30,.51,.41,.50,shade(body,.91));
} else {
const a=w/2+.24, b=d/2+.23, e=h+.03, tip=e+gable;
m.tri([-w/2,h,-d/2],[0,tip,-d/2],[w/2,h,-d/2],body);
m.tri([w/2,h,d/2],[0,tip,d/2],[-w/2,h,d/2],body);
for(const side of [-1,1]) {
const point=(v,zPos,offset=0)=>[side*a*(1-v),mix(e,tip,v)+offset,zPos];
const rf=shade(roof,side===1 ? .91 : 1.025);
const a0=point(0,-b), b0=point(1,-b), c0=point(1,b), d0=point(0,b);
m.top(a0,b0,c0,rf); m.top(a0,c0,d0,rf);
for(let i=1;i<=5;i++) {
const t=i/6;
const a1=point(t,-b,.009), b1=point(t+.022,-b,.009), c1=point(t+.022,b,.009), d1=point(t,b,.009);
m.top(a1,b1,c1,shade(rf,.87)); m.top(a1,c1,d1,shade(rf,.87));
}
m.tube(point(0,-b),point(1,-b),.052,.052,shade(roof,1.25),5);
m.tube(point(0,b),point(1,b),.049,.049,shade(roof,1.12),5);
m.tube(point(0,-b),point(0,b),.055,.055,timber,5);
}
m.tube([0,tip+.029,-b],[0,tip+.029,b],.069,.069,shade(roof,1.16),6);
m.box(w*.24,h+gable*.61,d*.18,.50,1.06,.50,shade(body,.74));
m.box(w*.24,h+gable*.61+.56,d*.18,.64,.16,.61,shade(roof,.72));
if(w>4.7) windowPane(m,0,h+gable*.34,-d/2-.016,.47,.49,0,true,shade(plaster,.91));
}
if(spec.market) {
const tints=[hex("#bf6557"),plaster];
for(let i=0;i<8;i++) {
const a=-w*.48+i*w*.12,b=a+w*.12;
const y=h*.51;
m.quad([a,y-.27,-d/2-.81],[a,y+.10,-d/2-.01],[b,y+.10,-d/2-.01],[b,y-.27,-d/2-.81],tints[i%2]);
frontPanel(m,(a+b)/2,y-.34,-d/2-.815,w*.12,.13,tints[i%2]);
}
}
if(spec.flower) {
m.box(w*.28,h*.37,-d/2-.18,1.12,.23,.30,hex("#84514a"));
for(let i=0;i<5;i++) m.ball(w*.28-.44+i*.22,h*.37+.15,-d/2-.19,.115,.15,.11,
i%2 ? hex("#efb06f") : hex("#d67770"),6,3);
}
scenery.join(m,frame([x,ground,z],yaw));
return ground+h+.230;
}
const houses=[
{x:20,z:-6,w:5.1,d:4.3,h:3.0,body:"#e5aa86",roof:"#a34d4c",yaw:.10,flower:true},
{x:27,z:-6,w:5.7,d:4.7,h:3.62,body:"#efcd9e",roof:"#bc6250",yaw:-.05,market:true},
{x:42,z:-6,w:5.8,d:4.6,h:3.65,body:"#cb918d",roof:"#874b57",yaw:.15},
{x:50,z:-4,w:5.9,d:5.4,h:3.0,body:"#e6ab88",roof:"#a9594f",yaw:-.20},
{x:58,z:-.4,w:4.7,d:4.3,h:3.9,body:"#e7c8a5",roof:"#804958",yaw:.10},
{x:12.7,z:4.3,w:3.8,d:3.6,h:2.45,body:"#e8bc8d",roof:"#9c5c57",yaw:.14},
{x:20.2,z:7.2,w:4.7,d:4.5,h:3.3,body:"#eeb49a",roof:"#ad5759",yaw:-.12,market:true},
{x:30.8,z:5.9,w:4.25,d:4,h:3.45,body:"#cfa2a0",roof:"#7f5366",yaw:.13},
{x:40.9,z:8.2,w:6.6,d:6,h:4.5,body:"#ebc39a",roof:"#b3795b",yaw:.11,flower:true,peak:1.65},
{x:50.3,z:9,w:5,d:4.9,h:3.74,body:"#ca8a83",roof:"#914959",yaw:-.12},
{x:61,z:13,w:5,d:5.3,h:3.62,body:"#d6ad9b",roof:"#805b6a",yaw:.25},
{x:21.5,z:23,w:5.1,d:5,h:3.51,body:"#eecd99",roof:"#b76a50",yaw:.10},
{x:29,z:24,w:5.8,d:5.7,h:4.72,body:"#eab291",roof:"#a95755",yaw:-.04,market:true,peak:1.73},
{x:46.7,z:24.8,w:5.7,d:4.8,h:3.6,body:"#d99c91",roof:"#875061",yaw:.16},
{x:55.5,z:27,w:5.4,d:5.3,h:4.1,body:"#edc5a1",roof:"#b46c54",yaw:-.19,flower:true},
{x:62.3,z:33,w:4.3,d:5,h:3.1,body:"#bfa1a5",roof:"#744b65",yaw:.20},
{x:23.8,z:36.8,w:4.7,d:4.4,h:3.1,body:"#d49e87",roof:"#93535d",yaw:-.15},
{x:32.3,z:38.8,w:5.4,d:4.7,h:4.0,body:"#f2d4ac",roof:"#a55c53",yaw:.12},
{x:50.8,z:39.7,w:5.6,d:4.8,h:3.7,body:"#dab08e",roof:"#a96b58",yaw:-.1},
{x:37.7,z:46.1,w:4.7,d:4.2,h:2.84,body:"#caa4a1",roof:"#72536b",yaw:.21},
{x:-38.6,z:-2,w:4.5,d:4.1,h:3.1,body:"#e4ad91",roof:"#ad5957",yaw:.2},
{x:-35.5,z:8,w:5.3,d:4.6,h:3.6,body:"#dfbd9f",roof:"#80516a",yaw:-.11},
{x:-24.9,z:11.7,w:4.5,d:4.1,h:2.76,body:"#d0ab9c",roof:"#995765",yaw:.25},
{x:-33.5,z:48.5,w:5.6,d:4.5,h:3.57,body:"#e8c2a4",roof:"#a65d54",yaw:-.24},
{x:-39.8,z:57.6,w:4.25,d:4.5,h:2.78,body:"#c8a1a2",roof:"#855470",yaw:.27},
{x:-28.0,z:56.1,w:4.9,d:4.6,h:3.2,body:"#dbb391",roof:"#a35755",yaw:.16}
];
for(const h of houses) cottage(h);
const copperY=cottage({x:26.5,z:11.5,w:6.25,d:5.9,h:4.6,body:"#f3bb91",roof:"#b86851",flat:true,flower:true});
const bellY=cottage({x:42.6,z:40.6,w:6.1,d:5.5,h:4.35,body:"#e9b292",roof:"#956366",flat:true});
const lanternY=cottage({x:-25.5,z:1.0,w:5.2,d:5.1,h:3.8,body:"#ebbb8f",roof:"#a35e52",flat:true});
function pine(x,z,h,paint=pineDark,y=ground) {
const w=h*(.20+random()*.067);
scenery.frustum(x,y,z,h*.52,w*.15,w*.085,trunk,6);
for(let i=0;i<3;i++) {
const size=w*(1-i*.229);
scenery.frustum(x+(i-1)*w*.08,y+h*(.22+i*.216),z-i*w*.054,h*(.47-i*.053),size,.025,
shade(paint,.91+i*.103+random()*.12),7,i*.36+random()*.75);
}
}
function broadleaf(x,z,h,paint) {
scenery.tube([x,ground,z],[x+.14,ground+h*.67,z],h*.087,h*.022,trunk,6);
for(let i=0;i<5;i++) {
const a=i*TAU/5;
scenery.ball(x+Math.cos(a)*h*.20,ground+h*(.63+random()*.19),z+Math.sin(a)*h*.18,
h*.27,h*.28,h*.26,shade(paint,.92+random()*.19),7,4);
}
}
const amberTree=hex("#b88661"), redTree=hex("#a16b68");
[[13,11,3.6],[14,27,4.5],[16,35,3.5],[22,45,4.6],[27,44,3.7],[30,47,6],[36,48.9,5.5],[43,47,4.2],
[50,44,5.5],[54,45,4.8],[58,40,5.3],[60,42,6],[64,38,4.8],[65,29,5.5],[66,20,4.6],[67,14,4.7],
[64,6,3.9],[61,-3,4.3],[54,-7,3.8],[46,-10,3.4],[31,-10.9,2.9],[18,-10.2,3.1],
[-46,5,5.5],[-45,0,5.0],[-44,11,4.0],[-42,15,6.3],[-39,17,4.2],[-35,17.5,5.0],[-32,19,5.2],
[-28,16,4.2],[-25,17,4.5],[-22,13,3.8],[-21,9,3.15],[-23,-4.5,3.6],[-27,-7,4.45],
[-31,-7,4.2],[-37,-7.5,3.2],[-41,-6,4.8],[-43,-3,4.4],
[-46,47,5.1],[-43,44,4.2],[-39,43,4.7],[-34,42,4.2],[-29,43,4.4],[-24,46,4.4],[-22.5,55,4.0],
[-27,60,5.7],[-31,63,5.5],[-35,63,4.1],[-39,63,4.9],[-45,60,5.7],[-47,56,5.4],[-46,52,3.9],
[-6.4,20.4,3.7],[-3,19.2,2.9],[.6,20.9,3.5],[-3,23,4.8],[0,23.3,2.9],
[74,-17,4.0],[78,-15,5.0],[78,-11,3.8],[73,-11,3.2]
].forEach(([x,z,h],i)=>pine(x,z,h,i%5===0?pineLight:pineDark));
[[23,1,3.7],[23,17,3.9],[38,16,4.1],[49,15.8,3.4],[54,12,3.9],[55,32,4.3],[29,31,3.8],[18,36,3.4],
[-34,3,4.7],[-40,6,4.6],[-28,5,3.1],[-30,52,3.6],[-41,55,4.1],[-5,23,3.0]].forEach(([x,z,h],i)=>broadleaf(x,z,h,i%3 ? amberTree : redTree));
for (let i=0;i<80;i++) {
const x=-124+i*3.2;
const z=110+11*Math.sin(i*.149)+4.5*Math.sin(i*.58);
pine(x,z,2.2+random()*4.4,hex("#5c5368"),.05+Math.max(0,5.8*Math.sin(i*.089+1.3)));
}
function lamp(x,z,y=ground) {
scenery.frustum(x,y,z,.15,.185,.12,hex("#594553"),6);
scenery.frustum(x,y+.10,z,1.58,.053,.036,hex("#4b3b4a"),6);
scenery.box(x,y+1.73,z,.22,.33,.22,hex("#ffd493"),0,.88);
scenery.frustum(x,y+1.89,z,.19,.172,.016,hex("#584350"),6,PI/6);
scenery.frustum(x,y+1.54,z,.05,.16,.16,hex("#674a50"),6);
scenery.disk(x,y+.035,z,.32,.26,shade(lane,.92),9);
}
[[17,-6],[16,3],[17,12],[17,24],[20,33],[23,10],[29,17],[35,3],[36,17],[40,20],[49,18],[57,21],
[35,35],[50,35],[61,18],[-30,5],[-38,5],[-22,3],[-29,50],[-40,54]].forEach(p=>lamp(...p));
for(let i=0;i<110;i++) {
const t=random();
const z=-8+t*51;
const x=18+Math.max(0,z-33)*.65+(random()-.5)*2.5;
const size=.07+random()*.24;
scenery.disk(x,.942,z,size,size*.59,shade(lane,.83+random()*.29),5);
}
function bellTower(x,z) {
const y=ground;
const shell=hex("#d1a198"), trim=hex("#edd2ad"), roof=hex("#98585d");
scenery.box(x,y+3.35,z,3.6,6.7,3.6,shell,.03);
scenery.box(x,y+.36,z,3.78,.72,3.78,shade(stone,.85));
for(let a of [1.10,4.28,6.25,8.15]) scenery.box(x,y+a,z,3.78,.20,3.78,trim);
scenery.box(x,y+7.27,z,3.44,1.75,3.44,shade(shell,.93));
windowPane(scenery,x,y+7.20,z-1.731,.89,1.25,0,true,trim);
windowPane(scenery,x+1.731,y+7.20,z,.89,1.25,-PI/2,true,trim);
scenery.tube([x,y+5.09,z-1.79],[x,y+5.09,z-1.86],.83,.83,hex("#684951"),18);
scenery.tube([x,y+5.09,z-1.86],[x,y+5.09,z-1.89],.68,.67,hex("#ffdc9e"),18,.26);
scenery.tube([x,y+5.09,z-1.91],[x-.27,y+5.34,z-1.91],.041,.029,timber,6);
scenery.tube([x,y+5.09,z-1.915],[x+.35,y+5.26,z-1.915],.035,.022,timber,6);
scenery.frustum(x,y+8.25,z,.31,2.65,2.37,shade(roof,.85),4,PI/4);
scenery.frustum(x,y+8.55,z,2.68,2.41,.078,roof,4,PI/4);
scenery.frustum(x,y+11.22,z,.55,.072,.045,plaster,6);
scenery.ball(x,y+11.83,z,.135,.14,.135,hex("#ffc576"),7,4,.31);
}
bellTower(41.2,31.4);
function lighthouse(x,z) {
const y=ground;
const ivory=hex("#e8c7a8"), red=hex("#a65355");
scenery.frustum(x,y,z,.54,2.25,1.94,shade(stone,1.04),12);
for(let i=0;i<4;i++) {
scenery.frustum(x,y+.54+i*1.72,z,1.72,1.59-i*.146,1.59-(i+1)*.146,i%2?red:ivory,12);
}
scenery.frustum(x,y+7.38,z,.26,1.15,1.71,shade(red,.8),12);
scenery.frustum(x,y+7.64,z,.17,1.71,1.71,ivory,12);
scenery.frustum(x,y+7.80,z,1.17,.99,.99,hex("#ffcf85"),12,0,1,.87);
for(let i=0;i<8;i++){
const a=i*TAU/8;
scenery.frustum(x+Math.cos(a)*.98,y+7.75,z+Math.sin(a)*.98,1.21,.065,.055,shade(red,.66),6);
}
scenery.frustum(x,y+9,z,1.18,1.38,.08,red,12);
scenery.frustum(x,y+10.17,z,.40,.071,.052,timber,6);
windowPane(scenery,x,y+1.41,z-1.54,.61,1.15,0,true,ivory);
scenery.box(x,y+.10,z-1.80,1.3,.16,.56,stone);
}
lighthouse(-32.8,-2.6);
function dock(cx,cz,length,width) {
const y=1.22, wood=hex("#a6755b"), bolt=hex("#76504b");
scenery.box(cx,y-.14,cz,length,.22,width,shade(wood,.77));
const count=Math.round(length/.49);
for(let i=0;i<count;i++) {
const x=cx-length/2+(i+.5)*length/count;
scenery.box(x,y-.025,cz,length/count-.024,.10,width,shade(wood,.82+random()*.41));
}
for(let i=0;i<=Math.floor(length/2.1);i++) for(let side of [-1,1]) {
const x=cx-length/2+.32+i*2.08;
const z=cz+side*(width/2-.095);
scenery.frustum(x,-.25,z,1.94,.106,.118,bolt,6);
scenery.frustum(x,1.64,z,.11,.128,.105,hex("#d4a482"),6);
}
return y+.026;
}
const dockY=dock(11.1,-8.3,15.7,3.20);
dock(5.1,-8.25,4.66,5.25);
dock(10.5,20.15,8.4,2.46);
const juniperY=dock(-17.8,49.5,12.7,3.29);
dock(-21.7,6.9,7.8,2.2);
lamp(4.1,-10.27,dockY); lamp(8.75,-9.61,dockY); lamp(-13.8,50.85,juniperY);
const boxWood=hex("#bb855a"), boxEdge=hex("#714c43");
function packingCrate(mesh,x,y,z,s,mark=true) {
mesh.box(x,y,z,s,s*.90,s,boxWood);
for(let i=0;i<3;i++) {
const dy=(i-1)*s*.24;
mesh.box(x,y+dy,z-s*.509,s*.92,s*.212,.028*s,shade(boxWood,.86+i*.074));
mesh.box(x+s*.51,y+dy,z,.030*s,s*.21,s*.91,shade(boxWood,.91+i*.07));
}
for(let k of [-1,1]) {
mesh.box(x+k*s*.413,y,z-s*.533,s*.115,s*.91,s*.065,boxEdge);
mesh.box(x+k*s*.413,y+s*.467,z,s*.113,s*.046,s,boxEdge);
}
if(mark) {
const zz=z-s*.57, white=hex("#fce1a7");
mesh.quad([x-.24*s,y,zz],[x-.096*s,y+.115*s,zz],[x+.14*s,y,zz],[x-.096*s,y-.115*s,zz],white,.12);
mesh.tri([x+.09*s,y,zz],[x+.24*s,y+.101*s,zz],[x+.24*s,y-.101*s,zz],white,.12);
}
}
[[4,-9.54,dockY,.55],[5,-10.1,dockY,.39],[11.1,-7.00,dockY,.52],[-19.1,48.23,juniperY,.62],
[11.1,20.69,dockY,.58],[23.5,16.45,ground,.41],[38.8,17.4,ground,.44],[-28.7,5.75,ground,.46]].forEach(([x,z,y,s])=>packingCrate(scenery,x,y+s*.45,z,s,false));
function boat(x,z,yaw,paint,sail=false,size=1) {
const m=new Mesh(), deck=hex("#af816d");
const p=[[-.27,-2.03],[-.64,-1.5],[-.75,.53],[-.49,1.45],[0,2.13],[.49,1.45],[.75,.53],[.64,-1.5],[.27,-2.03]];
for(let i=0;i<p.length;i++){
const a=p[i], b=p[(i+1)%p.length];
const highA=[a[0],.36+Math.abs(a[1])*.036,a[1]], highB=[b[0],.36+Math.abs(b[1])*.036,b[1]];
const lowA=[a[0]*.68,-.12,a[1]*.88], lowB=[b[0]*.68,-.12,b[1]*.88];
m.quad(lowA,highA,highB,lowB,paint);
m.tube(highA,highB,.044,.044,plaster,5);
m.top([0,.17,0],[a[0]*.92,.17,a[1]*.94],[b[0]*.92,.17,b[1]*.94],shade(deck,.89));
}
for(const q of [-1.06,-.05,.79]) m.box(0,.329,q,1.21,.07,.245,deck);
if(sail) {
m.frustum(0,.22,.29,3.69,.057,.033,hex("#76504e"),7);
const a=[0,3.74,.28], b=[0,.85,.25], c=[-.16,.83,-1.76], d=[-.23,2.14,-.76];
m.tri(a,b,d,hex("#f7dcad")); m.tri(b,c,d,hex("#e9a779")); m.tri(c,a,d,hex("#ffca9f"));
m.tri([.03,2.79,.41],[0,.96,.42],[.13,.93,1.58],hex("#f3d5a9"));
m.tri([0,3.85,.30],[0,3.65,-.46],[0,3.57,.30],hex("#d56b58"));
m.tube([0,.84,.35],[-.16,.84,-1.78],.038,.030,hex("#5e4350"),6);
} else {
m.tube([-.16,.34,.4],[-1.26,.19,-.62],.035,.048,timber,6);
m.tube([.10,.34,.4],[1.29,.19,-.68],.035,.048,timber,6);
}
scenery.join(m,frame([x,.04,z],yaw,0,0,size));
}
boat(-15.5,-18,.53,hex("#9b645e"),true,1.35);
boat(4.6,-14,1.54,hex("#a95f58"),false,.92);
boat(9.2,4.7,-.25,hex("#677b84"),false,1.02);
boat(5.2,18.9,.32,hex("#b66a54"),false,.93);
boat(-15,39.2,-.66,hex("#98707c"),true,1.04);
boat(32,-16.8,-.81,hex("#6d8285"),false,1.12);
boat(-17.1,8.0,1.38,hex("#bb795f"),false,.92);
boat(53.8,58,1.70,hex("#a26868"),true,1.21);
function cat(coat, cream, scarf, pilot=false) {
const m=new Mesh();
const stripe=shade(coat,.69), pink=hex("#de9985"), ink=hex("#493044");
m.ball(0,.37,-.08,.31,.36,.30,coat,10,6);
m.ball(0,.38,.12,.22,.28,.16,cream,9,5);
for(let k of [-1,1]) {
m.ball(k*.22,.19,-.08,.16,.18,.24,shade(coat,.96),8,5);
m.ball(k*.147,.085,.22,.122,.086,.18,cream,8,4);
const a=[k*.40,1.06,.02], b=[k*.105,1.16,.02], c=[k*.285,1.412,.035], back=[k*.26,1.16,-.125];
m.tri(a,b,c,coat); m.tri(a,c,back,stripe); m.tri(b,back,c,coat);
m.tri([k*.327,1.125,.044],[k*.162,1.168,.044],[k*.276,1.34,.050],pink);
}
m.ball(0,.933,.047,.392,.345,.353,coat,13,8);
m.ball(-.122,.846,.326,.148,.112,.107,cream,9,5);
m.ball(.122,.846,.326,.148,.112,.107,cream,9,5);
m.ball(0,.914,.411,.062,.042,.034,hex("#754251"),8,5);
for(let k of [-1,1]) {
m.ball(k*.171,1.001,.345,.046,.062,.027,ink,8,5);
m.ball(k*.169+.012,1.022,.367,.015,.016,.012,plaster,6,4,.18);
for(let i=0;i<2;i++) m.tube([k*.21,.85+i*.044,.374],[k*(.43+i*.013),.83+i*.086,.387],.008,.003,shade(cream,.92),4);
m.tri([k*.247,1.125,-.180],[k*.344,1.061,-.133],[k*.29,1.03,-.180],stripe);
m.tri([k*.269,1.018,-.232],[k*.373,.970,-.128],[k*.316,.94,-.163],stripe);
}
m.tri([-.098,1.22,-.123],[.006,1.264,.002],[.082,1.216,-.13],stripe);
m.tube([.20,.16,-.235],[.44,.25,-.39],.086,.072,coat,8);
m.tube([.44,.25,-.39],[.48,.46,-.48],.073,.053,coat,8);
m.tube([.48,.46,-.48],[.417,.60,-.44],.054,.037,stripe,8);
m.ball(.417,.60,-.44,.037,.042,.04,stripe,7,4);
m.frustum(0,.63,-.018,.095,.247,.231,scarf,13,0,.93);
if(pilot) {
for(let i=0;i<16;i++) {
const a=i*TAU/16,b=(i+1)*TAU/16;
m.quad([.391*Math.cos(a),.998,.047+.341*Math.sin(a)],[.381*Math.cos(a),1.073,.047+.331*Math.sin(a)],
[.381*Math.cos(b),1.073,.047+.331*Math.sin(b)],[.391*Math.cos(b),.998,.047+.341*Math.sin(b)],hex("#68434b"));
}
for(let k of [-1,1]) {
m.tube([k*.155,1.054,.348],[k*.155,1.054,.393],.106,.102,hex("#563747"),10);
m.tube([k*.155,1.054,.392],[k*.155,1.054,.414],.082,.081,hex("#e8c598"),10);
m.tube([k*.155,1.054,.414],[k*.155,1.054,.419],.065,.061,hex("#7e9fa4"),10,.10);
}
}
return m;
}
const receiverModels=[
cat(hex("#d7a078"),hex("#ffdeb2"),hex("#7d9b97")),
cat(hex("#8f899c"),hex("#edc8b6"),hex("#dc9c62")),
cat(hex("#d88c57"),hex("#ffe1ad"),hex("#9a5b70")),
cat(hex("#a99791"),hex("#f9ddbf"),hex("#c4766b")),
cat(hex("#c2a5a3"),hex("#ffe1bb"),hex("#6d8e91"))
];
const craft=new Mesh(), nose=new Mesh(), propeller=new Mesh();
const vermilion=hex("#d46a4a"), deepRed=hex("#b64843"), ivory=hex("#f4d397");
const metal=hex("#75525a"), dark=hex("#473746");
function fuselage() {
const sections=[
[-3.03,.26,.14,.149],[-2.65,.21,.24,.23],[-1.92,.14,.385,.35],[-1.05,.07,.56,.47],
[-.39,.03,.68,.60],[.55,0,.727,.606],[1.42,.015,.68,.56],[1.84,.034,.60,.52],[2.04,.04,.49,.439]
];
const n=16;
const profile=(s,i)=>[Math.cos(i*TAU/n)*s[2],s[1]+Math.sin(i*TAU/n)*s[3],s[0]];
for(let k=1;k<sections.length;k++) for(let i=0;i<n;i++) {
const j=(i+1)%n;
let paint=shade(vermilion,.88+.17*Math.max(0,Math.sin((i+.5)*TAU/n)));
if(k===7) paint=hex("#bb5444");
if(i===0 || i===7) paint=k<6 ? ivory : shade(vermilion,.94);
craft.quad(profile(sections[k-1],i),profile(sections[k-1],j),profile(sections[k],j),profile(sections[k],i),paint);
}
craft.ball(0,.23,-3.00,.17,.165,.15,deepRed,10,5);
nose.tube([0,.04,1.87],[0,.04,2.22],.565,.469,hex("#d18c58"),16);
nose.tube([0,.04,2.21],[0,.04,2.26],.468,.452,ivory,16);
nose.tube([0,.04,2.254],[0,.04,2.28],.402,.397,dark,16);
for(let i=0;i<10;i++) {
const a=i*TAU/10;
nose.ball(Math.cos(a)*.43,.04+Math.sin(a)*.43,2.27,.021,.021,.020,hex("#ffdfa2"),6,3,.12);
}
nose.ball(0,.04,2.335,.182,.173,.20,hex("#e4aa6b"),12,6);
craft.join(nose,identity);
}
fuselage();
function wing(side,tail=false) {
const a=tail ? [[.14,-2.92],[.93,-2.98],[1.18,-2.84],[1.22,-2.46],[.92,-2.35],[.16,-2.24]]
: [[.52,-.78],[3.07,-.64],[3.55,-.46],[3.67,-.19],[3.60,.37],[3.29,.62],[.51,1.10]];
const yAt=x=>tail ? .30+x*.075 : -.105+x*.049;
const top=a.map(([x,z])=>[side*x,yAt(x),z]);
const lower=top.map(([x,y,z])=>[x,y-(tail?.092:.156),z]);
for(let i=1;i<top.length-1;i++) {
craft.top(top[0],top[i],top[i+1],tail?vermilion:ivory);
craft.tri(lower[0],lower[i+1],lower[i],shade(tail?vermilion:ivory,.83));
}
for(let i=0;i<top.length;i++) {
const j=(i+1)%top.length;
craft.quad(top[i],lower[i],lower[j],top[j],tail?deepRed:shade(vermilion,.91));
}
if(!tail){
const q=(x,z)=>[side*x,yAt(x)+.012,z];
const paint=hex("#bb554b");
craft.top(q(2.76,-.55),q(3.02,-.54),q(3.02,.63),paint);
craft.top(q(2.76,-.55),q(3.02,.63),q(2.76,.65),paint);
craft.top(q(3.14,-.55),q(3.44,-.48),q(3.49,.41),vermilion);
craft.top(q(3.14,-.55),q(3.49,.41),q(3.14,.62),vermilion);
craft.tube(q(.79,-.64),q(2.64,-.54),.017,.017,hex("#c99070"),5);
craft.disk(side*1.86,yAt(1.86)+.029,.12,.327,.303,hex("#b76b59"),14);
craft.disk(side*1.86,yAt(1.86)+.034,.12,.237,.218,hex("#ffdc9e"),14);
craft.top(q(1.68,.13),q(1.88,-.03),q(2.02,.13),paint);
craft.top(q(1.68,.13),q(2.02,.13),q(1.88,.25),paint);
}
}
wing(1); wing(-1); wing(1,true); wing(-1,true);
const finA=[0,.31,-3.01], finB=[0,1.33,-2.77], finC=[0,1.33,-2.47], finD=[0,.38,-1.87];
function finSide(offset) {
const p=q=>[offset,q[1],q[2]];
craft.quad(p(finA),p(finB),p(finC),p(finD),deepRed);
craft.quad([offset,.83,-2.88],[offset,.96,-2.83],[offset,.957,-2.22],[offset,.83,-2.14],ivory);
}
finSide(-.07); finSide(.07);
craft.tube(finB,finC,.066,.047,vermilion,6);
for(const side of [-1,1]) {
craft.tube([side*.37,-.35,.87],[side*.85,-.80,.68],.052,.043,metal,7);
craft.tube([side*.29,-.41,-.24],[side*.85,-.81,.67],.035,.041,metal,7);
craft.tube([side*.735,-.86,.67],[side*1.00,-.86,.67],.274,.274,dark,12);
craft.tube([side*.998,-.86,.67],[side*1.023,-.86,.67],.142,.13,ivory,11);
craft.tube([side*.54,-.31,1.69],[side*.58,-.34,1.11],.067,.077,shade(metal,.87),8);
}
craft.tube([-.097,-.01,-2.62],[.098,-.01,-2.62],.134,.134,dark,10);
craft.ball(0,.525,-.39,.515,.208,.606,shade(deepRed,.87),13,6);
craft.ball(0,.606,-.41,.437,.166,.510,dark,13,6);
craft.ball(0,.73,-.73,.326,.173,.185,hex("#a7765c"),11,5);
const pilot=cat(hex("#ed9653"),hex("#ffdb9b"),hex("#bd4f55"),true);
craft.join(pilot,frame([0,.58,-.47],-.29,0,0,.89));
craft.quad([-.22,1.18,-.66],[-.13,1.15,-.83],[-.77,1.23,-1.30],[-.94,1.36,-1.26],hex("#c15159"));
craft.quad([-.94,1.36,-1.26],[-.77,1.23,-1.30],[-1.24,1.08,-1.84],[-1.45,1.21,-1.94],hex("#da7569"));
craft.quad([-.92,1.35,-1.29],[-.88,1.33,-1.34],[-.72,1.24,-1.36],[-.76,1.23,-1.31],ivory);
const glass=hex("#a9b8b2");
craft.quad([-.42,.65,.32],[-.34,1.043,.21],[.34,1.043,.21],[.42,.65,.32],glass,.15);
craft.tube([-.42,.65,.32],[-.34,1.048,.21],.023,.023,ivory,5);
craft.tube([.42,.65,.32],[.34,1.048,.21],.023,.023,ivory,5);
craft.tube([-.34,1.048,.21],[.34,1.048,.21],.022,.022,ivory,5);
function blade(sign) {
const bladePoint=(x,y)=>[x*sign,y*sign,0];
const p=bladePoint(-.07,.13), q=bladePoint(-.154,.91), r=bladePoint(-.087,1.41), s=bladePoint(.052,1.50), t=bladePoint(.128,.99), u=bladePoint(.068,.12);
propeller.tri(p,q,u,hex("#5f424a")); propeller.tri(q,r,s,hex("#805456"));
propeller.tri(q,s,t,hex("#71484f")); propeller.tri(q,t,u,hex("#69424a"));
propeller.quad(bladePoint(-.108,1.22),r,s,bladePoint(.080,1.24),ivory,.11);
}
blade(1); blade(-1);
propeller.ball(0,0,.016,.142,.142,.077,hex("#f1b66e"),11,5);
const parcel=new Mesh(), chute=new Mesh(), sparkMesh=new Mesh();
packingCrate(parcel,0,0,0,.96,true);
const gore=[hex("#f4bd7f"),hex("#d96356"),hex("#ffdb9e"),hex("#d86d61")];
const skirt=(r,y,i,n)=>[Math.cos(i*TAU/n)*r,y,Math.sin(i*TAU/n)*r];
for(let i=0;i<16;i++){
const n=16, col=gore[i%gore.length];
const a=skirt(1.40,1.70,i,n), b=skirt(1.40,1.70,i+1,n);
const c=skirt(1.17,2.24,i,n), d=skirt(1.17,2.24,i+1,n);
const e=skirt(.71,2.55,i,n), f=skirt(.71,2.55,i+1,n);
chute.quad(a,c,d,b,col,.11);
chute.quad(c,e,f,d,shade(col,1.04),.12);
chute.tri(e,[0,2.72,0],f,shade(col,1.08),.14);
if(i%2===0) chute.tube(a,skirt(.31,.36,i,n),.016,.012,hex("#fff1c3"),4,.10);
}
sparkMesh.ball(0,0,0,.086,.13,.086,hex("#ffde91"),5,3,1);
const birdWings=[];
for(let i=0;i<7;i++) {
const m=new Mesh(), flap=Math.sin(i*TAU/7)*.31;
for(const k of [-1,1]) {
const a=[0,0,.093],b=[k*.46,flap,-.042],c=[k*.30,flap*.70,-.167],d=[0,0,-.099];
m.quad(d,c,b,a,hex("#f3beab"));
}
birdWings.push(m);
}
const START=[-6.5,9.8,-53];
const RING_RADIUS=3.62;
const NIGHT=75;
const route=[
{name:"Willow Dock",ring:[.7,13.3,-24],cat:[5.63,dockY,-8.02],land:[6.78,dockY,-8.96],catYaw:-.32},
{name:"Copper Roof",ring:[19.8,16.0,4.5],cat:[26.1,copperY,11.1],land:[27.4,copperY,11.34],catYaw:-.85},
{name:"Bell Tower",ring:[41,14.6,32.5],cat:[42.35,bellY,39.95],land:[43.48,bellY,40.45],catYaw:-.75},
{name:"Juniper Pier",ring:[-7.8,12.3,49],cat:[-17.17,juniperY,50.04],land:[-15.9,juniperY,49.05],catYaw:.83},
{name:"Lantern Point",ring:[-27.4,13.8,10],cat:[-25.27,lanternY,.34],land:[-26.3,lanternY,1.17],catYaw:.92}
];
route.forEach((stop,i)=>{
const v=sub(stop.ring,i ? route[i-1].ring : START);
stop.normal=unit(v);
stop.yaw=yawTo(v);
stop.pitch=pitchTo(v);
stop.ringFrame=frame(stop.ring,stop.yaw,stop.pitch,0,RING_RADIUS/.775);
});
let viewW=1, viewH=1, aspect=1, pixelRatio=1, cameraMode=0;
let sun=[0,.15,1], dusk=0, sceneTime=0;
let camera=null;
function cameraFor(plane, mode, dt) {
const portrait=aspect<.85;
const yaw=plane.yaw, pitch=plane.pitch;
const direction=[Math.sin(yaw),0,Math.cos(yaw)];
const right=[Math.cos(yaw),0,-Math.sin(yaw)];
let eye, aim, lens, roll=0;
if (mode===1) {
const model=frame(plane.p,yaw,pitch,plane.bank*.55);
eye=transform(model,[0,1.24,.045]);
aim=add(eye,add(mul(basis(model,8),22),[0,-.60,0]));
lens=portrait ? .76 : .60;
roll=plane.bank*.48;
} else if (mode===2) {
eye=add(plane.p,add(mul(direction,-8.5),[0,portrait?41:35,0]));
aim=add(plane.p,add(mul(direction,9.0),[0,-4,0]));
lens=portrait ? .73 : .57;
} else {
const last=drops[drops.length-1];
const reel=last && !last.landed ? Math.sin(sat(last.age/last.duration)*PI)*1.9 : 0;
eye=add(plane.p,add(add(mul(direction,-(portrait?14.3:12.75)-reel),mul(right,portrait?-.70:-1.46)),[0,4.32+reel*.45,0]));
aim=add(plane.p,add(mul(direction,16),[0,.55-reel*.48,0]));
lens=portrait ? .69 : .535;
}
if (camera && camera.mode===mode) {
const rate=mode===1 ? 24 : mode===2 ? 7 : 8;
const t=1-Math.exp(-dt*rate);
eye=along(camera.eye,eye,t);
aim=along(camera.aim,aim,t);
}
const fwd=unit(sub(aim,eye)), r=unit(cross(up,fwd)), u=cross(fwd,r);
camera={eye,aim,fwd,right:add(mul(r,Math.cos(roll)),mul(u,Math.sin(roll))),
up:sub(mul(u,Math.cos(roll)),mul(r,Math.sin(roll))), lensX:lens*aspect,lensY:lens,mode};
return camera;
}
function project(point, cam=camera) {
const d=sub(point,cam.eye), depth=dot(d,cam.fwd);
const k=Math.max(.08,Math.abs(depth));
return {x:viewW*(.5+dot(d,cam.right)/(2*k*cam.lensX)),y:viewH*(.5-dot(d,cam.up)/(2*k*cam.lensY)),depth};
}
const uniformNames=["uEye","uRight","uUp","uFwd","uSun","uLens","uDepth","uTime","uDusk","uModel","uReflect","uOpacity"];
class Graphics {
constructor(canvas) {
const gl=canvas.getContext("webgl",{alpha:false,antialias:true,depth:true,powerPreference:"high-performance"});
if(!gl) throw new Error("WebGL unavailable");
this.gl=gl;
this.canvas=canvas;
this.buffers=new WeakMap();
this.sky=this.program("screen-v","sky-f",{aCorner:0});
this.matte=this.program("mesh-v","mesh-f",{aP:0,aN:1,aC:2,aG:3});
this.water=this.program("water-v","water-f",{aP:0});
this.ringShader=this.program("ring-v","ring-f",{aCorner:0});
this.quad=this.buffer(new Float32Array([-1,-1,1,-1,-1,1,1,1]));
this.surface=this.buffer(new Float32Array([-880,0,-880,880,0,-880,-880,0,880,880,0,880]));
gl.clearColor(.36,.25,.38,1);
gl.blendFunc(gl.SRC_ALPHA,gl.ONE_MINUS_SRC_ALPHA);
}
buffer(data) {
const gl=this.gl, b=gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER,b);
gl.bufferData(gl.ARRAY_BUFFER,data,gl.STATIC_DRAW);
return b;
}
program(vert,frag,attrs) {
const gl=this.gl, p=gl.createProgram();
for(const [id,type] of [[vert,gl.VERTEX_SHADER],[frag,gl.FRAGMENT_SHADER]]) {
const s=gl.createShader(type);
gl.shaderSource(s,$(id).textContent);
gl.compileShader(s);
if(!gl.getShaderParameter(s,gl.COMPILE_STATUS)) throw new Error(gl.getShaderInfoLog(s));
gl.attachShader(p,s);
gl.deleteShader(s);
}
Object.entries(attrs).forEach(([name,loc])=>gl.bindAttribLocation(p,loc,name));
gl.linkProgram(p);
if(!gl.getProgramParameter(p,gl.LINK_STATUS)) throw new Error(gl.getProgramInfoLog(p));
const u={};
for(const name of uniformNames) u[name]=gl.getUniformLocation(p,name);
return {p,u};
}
uniforms(program) {
if(this.current===program) return program.u;
const gl=this.gl, {u}=program, c=this.cam;
gl.useProgram(program.p);
gl.uniform3fv(u.uEye,c.eye); gl.uniform3fv(u.uRight,c.right); gl.uniform3fv(u.uUp,c.up);
gl.uniform3fv(u.uFwd,c.fwd); gl.uniform3fv(u.uSun,sun);
gl.uniform2f(u.uLens,c.lensX,c.lensY);
gl.uniform2f(u.uDepth,(980+.12)/(980-.12),-2*980*.12/(980-.12));
gl.uniform1f(u.uTime,sceneTime); gl.uniform1f(u.uDusk,dusk);
this.current=program;
return u;
}
single(buffer,n) {
const gl=this.gl;
gl.bindBuffer(gl.ARRAY_BUFFER,buffer);
gl.enableVertexAttribArray(0);
gl.disableVertexAttribArray(1);
gl.disableVertexAttribArray(2);
gl.disableVertexAttribArray(3);
gl.vertexAttribPointer(0,n,gl.FLOAT,false,0,0);
gl.drawArrays(gl.TRIANGLE_STRIP,0,4);
}
start(cam) {
const gl=this.gl;
this.cam=cam; this.current=null;
gl.viewport(0,0,this.canvas.width,this.canvas.height);
gl.depthMask(true); gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER_BIT);
gl.disable(gl.DEPTH_TEST); gl.disable(gl.BLEND); gl.depthMask(false);
this.uniforms(this.sky);
this.single(this.quad,2);
gl.depthMask(true); gl.enable(gl.DEPTH_TEST);
this.draw(scenery,identity,true);
gl.enable(gl.BLEND);
this.uniforms(this.water);
this.single(this.surface,3);
gl.disable(gl.BLEND);
this.draw(scenery);
}
draw(mesh,model=identity,reflection=false) {
const gl=this.gl, u=this.uniforms(this.matte);
let cached=this.buffers.get(mesh);
if(!cached) {
const data=mesh.vertices();
cached={buffer:this.buffer(data),count:data.length/10};
this.buffers.set(mesh,cached);
}
gl.uniformMatrix4fv(u.uModel,false,model);
gl.uniform1f(u.uReflect,reflection ? 1 : 0);
gl.bindBuffer(gl.ARRAY_BUFFER,cached.buffer);
for(let i=0;i<4;i++) {
gl.enableVertexAttribArray(i);
gl.vertexAttribPointer(i,i===3 ? 1 : 3,gl.FLOAT,false,40,i*12);
}
gl.drawArrays(gl.TRIANGLES,0,cached.count);
}
ring(stop,opacity,overhead) {
const gl=this.gl, u=this.uniforms(this.ringShader);
let m=stop.ringFrame;
if(overhead) m=frame(stop.ring,stop.yaw,-.97,0,RING_RADIUS/.775);
gl.uniformMatrix4fv(u.uModel,false,m);
gl.uniform1f(u.uOpacity,opacity);
gl.enable(gl.BLEND); gl.depthMask(false);
this.single(this.quad,2);
gl.depthMask(true); gl.disable(gl.BLEND);
}
end() {}
}
class Painter {
constructor(canvas) { this.canvas=canvas; this.ctx=canvas.getContext("2d",{alpha:false}); this.faces=[]; this.rings=[]; }
start(cam) {
this.cam=cam; this.faces.length=0; this.rings.length=0;
const ctx=this.ctx;
ctx.setTransform(pixelRatio,0,0,pixelRatio,0,0);
const az=unit([cam.fwd[0],0,cam.fwd[2]]);
const horizon=clamp(project(add(cam.eye,mul(az,680)),cam).y,0,viewH);
const sky=ctx.createLinearGradient(0,0,0,Math.max(1,horizon));
sky.addColorStop(0,rgb(along(hex("#766185"),hex("#302944"),dusk)));
sky.addColorStop(.7,rgb(along(hex("#d39591"),hex("#70526b"),dusk)));
sky.addColorStop(1,rgb(along(hex("#f6a180"),hex("#a86773"),dusk)));
ctx.fillStyle=sky; ctx.fillRect(0,0,viewW,viewH);
const p=project(add(cam.eye,mul(sun,600)),cam);
if(p.depth>0 && p.x>-viewW && p.x<viewW*2) {
const r=viewH*.05;
const bloom=ctx.createRadialGradient(p.x,p.y,0,p.x,p.y,r*4.2);
bloom.addColorStop(0,"#ffd19a9a"); bloom.addColorStop(1,"#ffd19a00");
ctx.fillStyle=bloom; ctx.fillRect(p.x-r*4.2,p.y-r*4.2,r*8.4,r*8.4);
ctx.fillStyle=rgb(along(hex("#ffe8ac"),hex("#f19a82"),dusk));
ctx.beginPath(); ctx.arc(p.x,p.y,r,0,TAU); ctx.fill();
}
for(const [yaw,h,w] of [[-.64,.19,.19],[-.30,.37,.25],[.31,.26,.25],[.8,.44,.28],[1.4,.2,.25],[2.2,.34,.31],[-2.33,.29,.27]]) {
const a=yaw+sceneTime*.0015;
const q=project(add(cam.eye,[Math.sin(a)*440,h*440,Math.cos(a)*440]),cam);
if(q.depth<120 || q.x<-viewW || q.x>viewW*2) continue;
ctx.fillStyle=rgb(along(hex("#e6ab9e"),hex("#8d667d"),dusk));
ctx.globalAlpha=.41;
ctx.beginPath();
const rx=viewH*w*.52, ry=viewH*.017;
ctx.ellipse(q.x,q.y,rx,ry,0,0,TAU);
ctx.ellipse(q.x-rx*.25,q.y-ry*.67,rx*.57,ry*1.34,0,0,TAU);
ctx.ellipse(q.x+rx*.29,q.y-ry*.25,rx*.53,ry*.94,0,0,TAU);
ctx.fill(); ctx.globalAlpha=1;
}
const water=ctx.createLinearGradient(0,horizon,0,Math.max(horizon+1,viewH));
water.addColorStop(0,rgb(along(hex("#ac7585"),hex("#67485f"),dusk)));
water.addColorStop(1,rgb(along(hex("#43516b"),hex("#252941"),dusk)));
ctx.fillStyle=water; ctx.fillRect(0,horizon,viewW,viewH-horizon);
for(let i=0;i<112;i++) {
const f=(i%43)/43;
const y=mix(horizon+1,viewH,f*f);
const x=((i*147.841+sceneTime*(2+i%4))%viewW+viewW)%viewW;
ctx.globalAlpha=(.14+.34*f)*(1-dusk*.45);
ctx.fillStyle=p.depth>0 && Math.abs(x-p.x)<viewW*(.038+f*.08) ? "#ffd399" : "#cba3aa";
ctx.fillRect(x,y,2+(i%10)*f*4,(.5+f*.8));
}
ctx.globalAlpha=1;
this.draw(scenery);
}
draw(mesh, m=identity) {
const v=mesh.vertices(), cam=this.cam, e=cam.eye, r=cam.right, u=cam.up, f=cam.fwd;
const scaleX=viewW/(2*cam.lensX), scaleY=viewH/(2*cam.lensY);
for(let i=0;i<v.length;i+=30) {
const projected=[];
let ok=true;
for(let j=i;j<i+30;j+=10) {
const x=v[j],y=v[j+1],z=v[j+2];
const dx=m[0]*x+m[4]*y+m[8]*z+m[12]-e[0];
const dy=m[1]*x+m[5]*y+m[9]*z+m[13]-e[1];
const dz=m[2]*x+m[6]*y+m[10]*z+m[14]-e[2];
const d=dx*f[0]+dy*f[1]+dz*f[2];
if(d<.22) { ok=false; break; }
projected.push(viewW/2+(dx*r[0]+dy*r[1]+dz*r[2])*scaleX/d,
viewH/2-(dx*u[0]+dy*u[1]+dz*u[2])*scaleY/d,d);
}
if(!ok) continue;
const [x1,y1,d1,x2,y2,d2,x3,y3,d3]=projected;
if(Math.min(x1,x2,x3)>viewW || Math.max(x1,x2,x3)<0 || Math.min(y1,y2,y3)>viewH || Math.max(y1,y2,y3)<0) continue;
const area=(x2-x1)*(y3-y1)-(x3-x1)*(y2-y1);
if(Math.abs(area)<.41) continue;
const nz=v[i+3],ny=v[i+4],nx=v[i+5];
const light=Math.max(0,-nz*.57+ny*.744-nx*.35);
const depth=(d1+d2+d3)/3;
const fog=(1-Math.exp(-depth*.0061))*.72;
const lum=(.76+light*.39)*(1-dusk*.34)+v[i+9]*(.20+dusk*.21);
const color=rgb([
mix(v[i+6]*lum,mix(.71,.25,dusk),fog),mix(v[i+7]*lum,mix(.47,.23,dusk),fog),mix(v[i+8]*lum,mix(.55,.43,dusk),fog)
]);
this.faces.push([depth,x1,y1,x2,y2,x3,y3,color]);
}
}
ring(stop,opacity,overhead) { this.rings.push([stop,opacity,overhead]); }
end() {
const ctx=this.ctx;
this.faces.sort((a,b)=>b[0]-a[0]);
for(const f of this.faces) {
ctx.fillStyle=f[7]; ctx.strokeStyle=f[7]; ctx.lineWidth=.45;
ctx.beginPath(); ctx.moveTo(f[1],f[2]); ctx.lineTo(f[3],f[4]); ctx.lineTo(f[5],f[6]); ctx.closePath(); ctx.fill(); ctx.stroke();
}
for(const [stop,opacity,overhead] of this.rings) {
const m=overhead ? frame(stop.ring,stop.yaw,-.97,0,RING_RADIUS/.775) : stop.ringFrame;
if(project(stop.ring).depth<.7) continue;
ctx.save();
ctx.beginPath();
for(let i=0;i<=64;i++) {
const a=i*TAU/64;
const p=project(transform(m,[.775*Math.cos(a),.775*Math.sin(a),0]));
if(i===0) ctx.moveTo(p.x,p.y); else ctx.lineTo(p.x,p.y);
}
ctx.globalAlpha=opacity*.58; ctx.strokeStyle="#ffac69"; ctx.lineWidth=10; ctx.stroke();
ctx.globalAlpha=opacity; ctx.strokeStyle="#ffe6b2"; ctx.shadowColor="#ffb771"; ctx.shadowBlur=13; ctx.lineWidth=3; ctx.stroke();
ctx.restore();
}
}
}
let renderer;
function usePainter() {
ui.canvas.hidden=true; ui.fallback.hidden=false;
renderer=new Painter(ui.fallback);
resize();
}
try { renderer=new Graphics(ui.canvas); } catch (_) { usePainter(); }
ui.canvas.addEventListener("webglcontextlost",e=>{ e.preventDefault(); usePainter(); });
function resize() {
viewW=Math.max(1,ui.stage.clientWidth);
viewH=Math.max(1,ui.stage.clientHeight);
aspect=viewW/viewH;
const limit=renderer instanceof Painter ? 1.35 : 1.85;
pixelRatio=Math.min(window.devicePixelRatio||1,limit,Math.sqrt(2450000/(viewW*viewH)));
const canvas=renderer.canvas;
canvas.width=Math.round(viewW*pixelRatio);
canvas.height=Math.round(viewH*pixelRatio);
}
resize();
window.addEventListener("resize",resize);
const instrumentNs="http://www.w3.org/2000/svg";
function svgNode(name,attrs) {
const el=document.createElementNS(instrumentNs,name);
for(const [key,value] of Object.entries(attrs)) el.setAttribute(key,value);
return el;
}
const instruments=$("instruments");
function gauge(x,y,label,r) {
const group=svgNode("g",{transform:"translate("+x+" "+y+")"});
group.append(svgNode("circle",{r:r+3,fill:"#332632",stroke:"#ba845f","stroke-width":2}));
group.append(svgNode("circle",{r,fill:"url(#gauge-face)",stroke:"#e2b484","stroke-width":1.2}));
for(let i=0;i<=10;i++) {
const a=-2.26+i*.452, d=i%5===0?6:3;
group.append(svgNode("line",{x1:Math.sin(a)*(r-6-d),y1:-Math.cos(a)*(r-6-d),x2:Math.sin(a)*(r-6),y2:-Math.cos(a)*(r-6),
stroke:i>8?"#ed956e":"#e9c8a5","stroke-width":i%5===0?1.55:.84}));
}
const caption=svgNode("text",{"text-anchor":"middle",y:r*.53,"font-size":8,fill:"#f0caa5","font-family":"system-ui","letter-spacing":".12em"});
caption.textContent=label;
const needle=svgNode("path",{d:"M-1.4 5 0 "+(-r+9)+" 1.4 5Z",fill:"#ffb568"});
group.append(caption,needle,svgNode("circle",{r:2.5,fill:"#edbc83"}));
instruments.append(group);
return needle;
}
const altNeedle=gauge(408,118,"ALT",33), hdgNeedle=gauge(500,106,"HDG",39), speedNeedle=gauge(592,118,"SPD",33);
const smallScreen=matchMedia("(pointer: coarse)");
let touchControls=smallScreen.matches;
const input={x:0,y:0,mouseX:0,mouseY:0,hasMouse:false,pointer:null,startX:0,startY:0};
const stickDot=ui.stick.firstElementChild;
let flight, drops, targetIndex, delivered, elapsed, phase, bannerUntil=0;
let visibleSeconds=-1, worldBegan=0, lastFrame=0, lastPaint=0;
let demo=location.hash==="#demo", demoClock=0;
let audio=null, engine=null, engineVolume=null, soundOn=false;
const demoTimes=[0,2.82,5.77,8.64,12.02,15.49,18.82,20];
const demoPoints=[START,...route.map(stop=>stop.ring),[-19,13,-21],[-8,14,-31]];
function demoPosition(t) {
let i=0;
while(i<demoTimes.length-2 && t>demoTimes[i+1]) i++;
const a=demoPoints[Math.max(0,i-1)], b=demoPoints[i], c=demoPoints[i+1], d=demoPoints[Math.min(i+2,demoPoints.length-1)];
const span=demoTimes[i+1]-demoTimes[i], v=sat((t-demoTimes[i])/span);
const ta=demoTimes[i+1]-demoTimes[Math.max(0,i-1)], tb=demoTimes[Math.min(i+2,demoTimes.length-1)]-demoTimes[i];
const t0=mul(sub(c,a),span/ta), t1=mul(sub(d,b),span/tb);
const v2=v*v, v3=v2*v;
return add(add(mul(b,2*v3-3*v2+1),mul(t0,v3-2*v2+v)),add(mul(c,-2*v3+3*v2),mul(t1,v3-v2)));
}
function setCamera(mode) {
if(cameraMode!==mode) camera=null;
cameraMode=mode;
ui.cameraButtons.forEach((btn,i)=>btn.setAttribute("aria-pressed",String(i===mode)));
ui.cockpit.hidden=mode!==1;
}
function updateHint() {
ui.hintText.textContent=demo ? "Touch or click to take the controls" : touchControls ? "Drag one finger to steer" : "Move your mouse to steer";
ui.hintIcon.setAttribute("href",touchControls ? "#i-touch" : "#i-pointer");
}
function missionCard() {
const ready=phase==="ready";
ui.mission.classList.toggle("is-ready",ready);
ui.mission.classList.toggle("is-flying",!ready);
ui.ready.hidden=!ready;
ui.routeCard.hidden=ready;
ui.routeLabel.textContent=demo ? "Demo flight" : targetIndex<route.length ? "Next delivery" : "Final delivery";
ui.routeName.textContent=targetIndex<route.length ? route[targetIndex].name : "Crate on its way";
updateHint();
}
function numbers() {
const remaining=demo ? Math.max(0,NIGHT-demoClock*3.62) : Math.max(0,NIGHT-elapsed);
const second=Math.ceil(remaining);
if(second!==visibleSeconds) {
visibleSeconds=second;
ui.time.textContent=Math.floor(second/60)+":"+String(second%60).padStart(2,"0");
ui.clock.classList.toggle("urgent",second<=15);
}
ui.clockLine.style.transform="scaleX("+(remaining/NIGHT)+")";
ui.score.textContent=String(delivered*100);
ui.crates.textContent=String(route.length-targetIndex);
}
function reset(nextPhase,resetView) {
const aim=sub(route[0].ring,START);
flight={p:START.slice(),yaw:yawTo(aim),pitch:.025,bank:0,speed:0};
drops=[];
targetIndex=0; delivered=0; elapsed=0;
phase=nextPhase;
demoClock=0;
bannerUntil=0; visibleSeconds=-1;
input.x=0; input.y=0; input.hasMouse=false; input.pointer=null;
ui.stick.hidden=true; ui.result.hidden=true; ui.toast.classList.remove("show");
camera=null;
if(resetView) setCamera(0);
missionCard();
numbers();
}
function startFlight() {
if(phase!=="ready") return;
phase="flight";
missionCard();
tone([392,523,659],.13,.057);
}
function announce(name,points,icon) {
ui.toastText.textContent=name;
ui.toastPoints.textContent=points;
ui.toastIcon.setAttribute("href","#"+icon);
ui.toast.classList.add("show");
bannerUntil=sceneTime+2.05;
}
function endFlight(win) {
phase="result";
flight.speed=0;
ui.resultTitle.textContent=win ? "All fish delivered!" : "Nightfall";
ui.resultText.textContent=win
? "All five cats have their fish. Thanks for flying over Lake Alder."
: "You delivered "+delivered+" of "+route.length+" crates before dark.";
ui.resultIcon.setAttribute("href",win ? "#i-fish" : "#i-moon");
ui.finalScore.textContent=String(delivered*100);
ui.result.hidden=false;
ui.toast.classList.remove("show");
ui.stick.hidden=true;
input.pointer=null;
if(!demo) ui.again.focus({preventScroll:true});
tone(win ? [523,659,784,1047] : [440,392,330],.19,.072);
}
function releaseCrate(point) {
const stop=route[targetIndex];
const from=point.slice();
from[1]-=.44;
drops.push({stop,index:targetIndex,from,age:0,duration:2.36+(targetIndex%2)*.12,landed:false});
targetIndex++;
missionCard();
announce("Crate away!","", "i-parachute");
tone([660,880],.10,.05);
}
function updateCrates(dt) {
for(const drop of drops) {
drop.age+=dt;
if(!drop.landed && drop.age>=drop.duration) {
drop.landed=true;
delivered++;
announce(drop.stop.name,"+100","i-fish");
tone([587,740,880],.105,.068);
}
}
}
function checkRing(prev,next) {
const stop=route[targetIndex];
if(!stop) return;
const a=sub(prev,stop.ring), b=sub(next,stop.ring);
const d0=dot(a,stop.normal), d1=dot(b,stop.normal);
if(d0*d1<=0 && Math.abs(d0-d1)>.00001) {
const hit=along(prev,next,d0/(d0-d1));
if(norm(sub(hit,stop.ring)) < RING_RADIUS+.16) { releaseCrate(hit); return; }
}
if(norm(b)<.82) releaseCrate(next);
}
function manualAxes() {
if(!input.hasMouse || !camera || touchControls) return [input.x,input.y];
const x=clamp((input.mouseX-viewW*.50)/(viewW*.44),-1,1);
const y=clamp((viewH*.435-input.mouseY)/(viewH*.365),-1,1);
let pull=1;
if(targetIndex<route.length) {
const projected=project(route[targetIndex].ring);
if(projected.depth>0) {
const meters=RING_RADIUS*viewH/(2*camera.lensY*projected.depth);
pull=mix(.065,1,smooth(meters*.64,meters+44,Math.hypot(projected.x-input.mouseX,projected.y-input.mouseY)));
}
}
return [x*pull,y*pull];
}
function fly(dt) {
const before=flight.p;
const a=manualAxes();
const stop=route[targetIndex];
let turn=0;
let climb=0;
if(stop) {
const d=sub(stop.ring,before), range=Math.hypot(d[0],d[2]);
const guide=range<.65 ? flight.yaw : yawTo(d);
const leeway=mix(.40,.94,smooth(3.5,19.0,range));
turn=angleDelta(guide+a[0]*leeway,flight.yaw);
const wantY=stop.ring[1]+a[1]*9.3*mix(.36,1,smooth(2,18,range));
climb=clamp(Math.atan2(wantY-before[1],Math.max(6.5,range*.70)),-.48,.51);
} else {
turn=a[0]*.26;
climb=a[1]*.15;
}
const yawRate=clamp(turn*2.7,-1.62,1.62);
flight.yaw+=yawRate*dt;
flight.pitch=mix(flight.pitch,climb,1-Math.exp(-dt*5.3));
flight.bank=mix(flight.bank,clamp(-yawRate*.43-a[0]*.14,-.65,.65),1-Math.exp(-dt*6.4));
flight.speed=mix(flight.speed,10.65+Math.abs(a[1])*.3,1-Math.exp(-dt*4.5));
const advance=flight.speed*dt;
const f=[Math.sin(flight.yaw)*Math.cos(flight.pitch),Math.sin(flight.pitch),Math.cos(flight.yaw)*Math.cos(flight.pitch)];
const next=add(before,mul(f,advance));
next[1]=clamp(next[1],5.9,31);
flight.p=next;
checkRing(before,next);
updateCrates(dt);
elapsed=Math.min(NIGHT,elapsed+dt);
if(delivered===route.length) endFlight(true);
else if(elapsed>=NIGHT) endFlight(false);
}
function flyDemo(dt) {
demoClock+=dt;
if(demoClock>=20) reset("flight",true);
const t=demoClock, p=demoPosition(t), ahead=demoPosition(t+.047), back=demoPosition(Math.max(0,t-.047));
const head=sub(ahead,back);
const heading=yawTo(head);
flight.bank=clamp(-angleDelta(heading,flight.yaw)/Math.max(dt,.013)*.29,-.57,.57);
flight.yaw=heading;
flight.pitch=pitchTo(head);
flight.p=p;
flight.speed=norm(head)/.094;
const cam=t<5.01 ? 0 : t<9.86 ? 1 : t<14.12 ? 2 : 0;
if(cam!==cameraMode) setCamera(cam);
if(targetIndex<route.length && t>=demoTimes[targetIndex+1]) releaseCrate(route[targetIndex].ring);
updateCrates(dt);
if(delivered===route.length && phase!=="result") endFlight(true);
}
function ringBeacon() {
const stop=route[targetIndex];
ui.beacon.hidden=!stop || phase==="result";
if(!stop || phase==="result") return;
const now=project(stop.ring);
const isPortrait=viewW<621 && viewH>viewW;
const yTop=isPortrait ? 143 : (viewH<540 ? 67 : 101);
const yBottom=viewH-(isPortrait ? 126 : 84);
const xPad=isPortrait ? 67 : 78;
const safeBottom=Math.max(yTop+12,yBottom);
const radius=RING_RADIUS*viewH/(2*camera.lensY*Math.max(3,now.depth));
const labelY=now.y-Math.min(viewH*.19,radius)-22;
let x=now.x, y=labelY;
let off=now.depth<=.25 || x<xPad || x>viewW-xPad || now.y<yTop-4 || now.y>safeBottom;
if(off) {
let dx=now.x-viewW*.5, dy=now.y-viewH*.5;
if(now.depth<.25) { dx=dx===0 ? viewW : dx*1.18; dy=Math.abs(dy)*.30; }
const scale=Math.min((viewW*.5-xPad)/Math.max(1,Math.abs(dx)),(safeBottom-yTop)*.43/Math.max(1,Math.abs(dy)));
x=viewW*.5+dx*scale;
y=(yTop+safeBottom)*.5+dy*scale;
ui.beacon.querySelector(".arrow").style.transform="rotate("+Math.atan2(dy,dx)+"rad)";
}
ui.beacon.classList.toggle("offscreen",off);
ui.beaconText.textContent=stop.name;
ui.beacon.style.transform="translate("+clamp(x,xPad,viewW-xPad).toFixed(1)+"px,"+clamp(y,yTop,safeBottom).toFixed(1)+"px) translate(-50%,-50%)";
ui.distance.textContent=Math.ceil(norm(sub(stop.ring,flight.p)))+" m";
}
function drawDrops() {
for(const drop of drops) {
const stop=drop.stop;
const t=sat(drop.age/drop.duration);
const glide=smooth(0,1,t);
const tail=1-glide;
const landing=add(stop.land,[0,.438,0]);
const p=along(drop.from,landing,glide);
p[1]=mix(drop.from[1],landing[1],Math.pow(t,1.07));
p[0]+=.70*Math.sin(t*PI*1.8)*Math.sin(t*PI);
const swing=Math.sin(t*9)*tail;
renderer.draw(parcel,frame(p,.21+swing*.18,0,swing*.16));
if(!drop.landed) {
const spread=.12+.88*smooth(.018,.20,t);
renderer.draw(chute,frame(p,.18+swing*.13,0,swing*.16,spread));
}
const hop=drop.age-drop.duration;
if(hop>0 && hop<.86) for(let i=0;i<7;i++) {
const a=i*TAU/7+hop*.78;
const s=.38+hop*1.76;
const q=[stop.land[0]+Math.cos(a)*s,stop.land[1]+.48+Math.sin(hop/.90*PI)*(.85+(i%3)*.24),stop.land[2]+Math.sin(a)*s];
renderer.draw(sparkMesh,frame(q,a,0,hop*.7,1-hop/.89));
}
}
}
function drawReceivers() {
route.forEach((stop,i)=>{
const drop=drops[i];
const jump=drop && drop.landed ? Math.max(0,Math.sin(sat((drop.age-drop.duration)/.68)*PI))*.73 : 0;
renderer.draw(receiverModels[i],frame(add(stop.cat,[0,jump,0]),stop.catYaw,0,0,1.04));
});
}
function drawBirds() {
for(let i=0;i<5;i++) {
const t=.14*sceneTime+i*.13;
const x=-23+13*Math.sin(t)+i*1.48;
const z=32+15*Math.cos(t*.81)-i*1.08;
const y=19.5+2.1*Math.sin(t*2+i*.45);
const k=Math.floor((sceneTime*15+i*1.12)%birdWings.length);
renderer.draw(birdWings[k],frame([x,y,z],.64-t*.71,0,Math.cos(t)*.18,.93+i*.04));
}
}
function drawAircraft() {
const idle=phase==="ready" ? Math.sin(sceneTime*1.38)*.16 : 0;
const p=add(flight.p,[0,idle,0]);
const prop=sceneTime*(phase==="ready" ? 19 : 39);
const m=frame(p,flight.yaw,flight.pitch,flight.bank+Math.sin(sceneTime*1.17)*.016);
if(cameraMode===1) renderer.draw(nose,m);
else renderer.draw(craft,m);
const r=basis(m,0), u=basis(m,4), f=basis(m,8);
const cs=Math.cos(prop), sn=Math.sin(prop);
renderer.draw(propeller,matrix(add(mul(r,cs),mul(u,sn)),sub(mul(u,cs),mul(r,sn)),f,transform(m,[0,.04,2.39])));
}
function tone(notes,duration,volume) {
if(!soundOn || !audio || audio.state!=="running") return;
const t=audio.currentTime;
notes.forEach((note,i)=>{
const oscillator=audio.createOscillator(), gain=audio.createGain();
const at=t+i*duration*.84;
oscillator.type="sine";
oscillator.frequency.value=note;
gain.gain.setValueAtTime(.0001,at);
gain.gain.exponentialRampToValueAtTime(volume,at+.018);
gain.gain.exponentialRampToValueAtTime(.0001,at+duration+.14);
oscillator.connect(gain); gain.connect(audio.destination);
oscillator.start(at); oscillator.stop(at+duration+.16);
});
}
ui.sound.addEventListener("click",()=>{
try {
if(!audio) {
const AudioClass=window.AudioContext||window.webkitAudioContext;
if(!AudioClass) return;
audio=new AudioClass();
engine=audio.createOscillator(); engineVolume=audio.createGain();
const filter=audio.createBiquadFilter();
filter.type="lowpass"; filter.frequency.value=124; filter.Q.value=.35;
engine.type="triangle"; engine.frequency.value=39;
engineVolume.gain.value=0;
engine.connect(filter); filter.connect(engineVolume); engineVolume.connect(audio.destination);
engine.start();
}
soundOn=!soundOn;
if(soundOn) audio.resume().catch(()=>{});
ui.sound.setAttribute("aria-pressed",String(soundOn));
if(soundOn) tone([440,587],.13,.057);
} catch (_) {}
});
ui.start.addEventListener("click",startFlight);
ui.again.addEventListener("click",()=>{
demo=false;
reset("ready",true);
startFlight();
ui.surface.setPointerCapture && ui.cameraButtons[0].focus({preventScroll:true});
});
ui.cameraButtons.forEach((b,i)=>b.addEventListener("click",()=>setCamera(i)));
window.addEventListener("pointerdown",e=>{
if(!demo) return;
const starts=e.target.closest && e.target.closest("#again, #start");
demo=false;
reset("ready",true);
if(starts) startFlight();
},true);
function releasePointer(e) {
if(input.pointer!==e.pointerId) return;
input.pointer=null;
input.x=0; input.y=0;
ui.stick.hidden=true;
}
ui.surface.addEventListener("pointerdown",e=>{
if(phase==="result" || (e.pointerType==="mouse" && e.button!==0)) return;
if(e.pointerType!=="mouse" && input.pointer!==null) return;
if(phase==="ready") startFlight();
touchControls=e.pointerType!=="mouse";
updateHint();
if(touchControls) {
input.hasMouse=false;
input.pointer=e.pointerId;
input.startX=e.clientX; input.startY=e.clientY;
input.x=0; input.y=0;
ui.stick.hidden=false;
ui.stick.style.transform="translate("+(e.clientX-31)+"px,"+(e.clientY-31)+"px)";
stickDot.style.transform="";
try { ui.surface.setPointerCapture(e.pointerId); } catch (_) {}
} else {
input.hasMouse=true; input.mouseX=e.clientX; input.mouseY=e.clientY;
}
});
ui.surface.addEventListener("pointermove",e=>{
if(demo || phase!=="flight") return;
if(e.pointerType==="mouse") {
if(touchControls) { touchControls=false; updateHint(); }
input.hasMouse=true; input.mouseX=e.clientX; input.mouseY=e.clientY;
} else if(e.pointerId===input.pointer) {
const scale=clamp(viewW*.245,72,134);
input.x=clamp((e.clientX-input.startX)/scale,-1,1);
input.y=clamp((input.startY-e.clientY)/scale,-1,1);
stickDot.style.transform="translate("+(input.x*18)+"px,"+(-input.y*18)+"px)";
}
});
ui.surface.addEventListener("pointerleave",e=>{ if(e.pointerType==="mouse") input.hasMouse=false; });
ui.surface.addEventListener("pointerup",releasePointer);
ui.surface.addEventListener("pointercancel",releasePointer);
ui.surface.addEventListener("lostpointercapture",releasePointer);
window.addEventListener("blur",()=>{
ui.stick.hidden=true; input.pointer=null; input.x=0; input.y=0; input.hasMouse=false;
});
let fragmentDemo=demo;
window.addEventListener("hashchange",()=>{
const requested=location.hash==="#demo";
if(requested===fragmentDemo) return;
fragmentDemo=requested;
demo=requested;
reset(demo ? "flight" : "ready",true);
});
reset(demo ? "flight" : "ready",true);
function tick(stamp) {
if(!worldBegan) worldBegan=stamp;
sceneTime=(stamp-worldBegan)/1000;
const dt=lastFrame ? Math.min(.24,(stamp-lastFrame)/1000) : .016;
lastFrame=stamp;
if(demo) flyDemo(dt);
else if(phase==="flight") {
const substeps=Math.max(1,Math.ceil(dt/.037));
for(let i=0;i<substeps && phase==="flight";i++) fly(dt/substeps);
}
if(bannerUntil && sceneTime>bannerUntil) { ui.toast.classList.remove("show"); bannerUntil=0; }
const night=demo ? .57*smooth(1,18,demoClock) : smooth(0,NIGHT,elapsed);
dusk=night;
const sunYaw=mix(.132,-.088,sat((aspect-.59)/.88));
sun=unit([Math.sin(sunYaw),.146-dusk*.172,Math.cos(sunYaw)]);
cameraFor(flight,cameraMode,dt);
numbers();
ringBeacon();
if(cameraMode===1) {
altNeedle.setAttribute("transform","rotate("+mix(-115,116,sat(flight.p[1]/27))+")");
hdgNeedle.setAttribute("transform","rotate("+(flight.yaw*180/PI)+")");
speedNeedle.setAttribute("transform","rotate("+mix(-125,120,sat(flight.speed/17))+")");
}
if(engineVolume && audio.state==="running") {
engineVolume.gain.setTargetAtTime(soundOn ? phase==="flight" ? .055 : phase==="ready" ? .026 : 0 : 0,audio.currentTime,.13);
engine.frequency.setTargetAtTime(36+flight.speed*1.25,audio.currentTime,.12);
}
if(!(renderer instanceof Painter) || !lastPaint || stamp-lastPaint>=31) {
lastPaint=stamp;
renderer.start(camera);
drawReceivers();
drawDrops();
drawBirds();
drawAircraft();
if(phase!=="result") {
if(route[targetIndex+1]) renderer.ring(route[targetIndex+1],.31,cameraMode===2);
if(route[targetIndex]) renderer.ring(route[targetIndex],1,cameraMode===2);
}
renderer.end();
}
requestAnimationFrame(tick);
}
requestAnimationFrame(tick);
})();
</script>
</body>
</html>
