Prompt specification
Build or customize the Three.js 3D scene "Sheltered Bay Sailing Game" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Sheltered Bay Sailing Game
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/sheltered-bay-sailing-game-x3679
Scene Description Prompt:
Make a voxel sailing game in an original sheltered bay, with explicit simplified wind rules and impassable islands. Three.js from cdnjs, standalone HTML.
Working Three.js Starter (sheltered-bay-sailing-game-x3679):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Lanternglass Cove — Voxel Sailing</title>
<style>
:root {
--ink: #102c39;
--muted: #55717a;
--paper: rgba(248, 247, 231, 0.92);
--paper-solid: #f8f7e7;
--teal: #168e94;
--teal-dark: #0b626c;
--amber: #ef9d45;
--green: #65b878;
--red: #df6554;
--line: rgba(16, 44, 57, 0.16);
--shadow: 0 12px 32px rgba(20, 55, 65, 0.17);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #acd7d9;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
#hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 5;
}
.panel {
background: var(--paper);
border: 1px solid rgba(255, 255, 255, 0.7);
box-shadow: var(--shadow);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
border-radius: 15px;
}
#brand {
position: absolute;
top: 18px;
left: 18px;
width: min(350px, calc(100vw - 36px));
padding: 15px 17px 14px;
}
.eyebrow {
color: var(--teal-dark);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.18em;
text-transform: uppercase;
}
#brand h1 {
margin: 3px 0 4px;
font-size: clamp(20px, 2.2vw, 29px);
line-height: 1;
letter-spacing: -0.035em;
}
#objectiveText {
margin: 9px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.brand-footer {
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
margin-top: 10px;
padding-top: 9px;
border-top: 1px solid var(--line);
font-size: 11px;
color: var(--muted);
}
#timer {
color: var(--ink);
font-weight: 800;
font-variant-numeric: tabular-nums;
}
.mini-button {
pointer-events: auto;
border: 1px solid var(--line);
background: rgba(255,255,255,.7);
color: var(--ink);
border-radius: 8px;
padding: 5px 8px;
cursor: pointer;
font-size: 10px;
font-weight: 800;
letter-spacing: .04em;
}
.mini-button:hover { background: white; }
#progress {
position: absolute;
top: 18px;
left: 50%;
transform: translateX(-50%);
display: flex;
align-items: center;
gap: 6px;
padding: 8px;
white-space: nowrap;
}
.route-chip {
display: flex;
align-items: center;
gap: 6px;
border-radius: 9px;
padding: 7px 9px;
color: #526d76;
background: rgba(255,255,255,.5);
border: 1px solid transparent;
font-size: 10px;
font-weight: 800;
letter-spacing: .025em;
transition: .25s ease;
}
.route-chip .dot {
width: 8px;
height: 8px;
border-radius: 2px;
background: var(--amber);
box-shadow: 0 0 0 3px rgba(239,157,69,.14);
}
.route-chip.collected {
color: var(--teal-dark);
background: rgba(101,184,120,.14);
border-color: rgba(101,184,120,.28);
}
.route-chip.collected .dot {
background: var(--green);
box-shadow: none;
}
.route-chip.goal {
color: #6f7880;
background: rgba(16,44,57,.05);
}
.route-chip.goal.ready {
color: #27673a;
background: rgba(101,184,120,.18);
animation: goalPulse 1.3s ease-in-out infinite;
}
.route-chip.goal.ready .dot {
background: var(--green);
box-shadow: 0 0 0 3px rgba(101,184,120,.18);
}
@keyframes goalPulse {
50% { box-shadow: 0 0 0 4px rgba(101,184,120,.08); }
}
#windPanel {
position: absolute;
top: 18px;
right: 18px;
width: 268px;
padding: 14px;
}
.wind-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
}
#windName {
font-size: 19px;
font-weight: 900;
letter-spacing: -.02em;
}
#windBlow {
margin-top: 2px;
color: var(--muted);
font-size: 11px;
}
.compass {
position: relative;
width: 82px;
height: 82px;
flex: 0 0 82px;
border-radius: 50%;
background:
radial-gradient(circle, rgba(255,255,255,.9) 0 47%, transparent 48%),
conic-gradient(from 0deg, rgba(22,142,148,.12), rgba(239,157,69,.1), rgba(22,142,148,.12));
border: 1px solid rgba(16,44,57,.16);
box-shadow: inset 0 0 0 5px rgba(255,255,255,.45);
}
.compass span {
position: absolute;
color: #6e8187;
font-size: 8px;
font-weight: 900;
}
.compass .n { top: 5px; left: 50%; transform: translateX(-50%); }
.compass .s { bottom: 5px; left: 50%; transform: translateX(-50%); }
.compass .e { right: 6px; top: 50%; transform: translateY(-50%); }
.compass .w { left: 5px; top: 50%; transform: translateY(-50%); }
#windNeedle {
position: absolute;
inset: 0;
transform: rotate(55deg);
transition: transform .25s ease;
}
.needle {
position: absolute;
left: 50%;
bottom: 50%;
width: 3px;
height: 31px;
transform: translateX(-50%);
border-radius: 3px;
background: var(--teal-dark);
}
.needle::before {
content: "";
position: absolute;
top: -6px;
left: 50%;
width: 0;
height: 0;
transform: translateX(-50%);
border-left: 6px solid transparent;
border-right: 6px solid transparent;
border-bottom: 10px solid var(--teal-dark);
}
.wind-bottom {
margin-top: 10px;
padding-top: 9px;
border-top: 1px solid var(--line);
display: flex;
justify-content: space-between;
gap: 10px;
color: var(--muted);
font-size: 10px;
}
#windTimer {
color: var(--ink);
font-weight: 850;
font-variant-numeric: tabular-nums;
}
#sailState {
margin-top: 8px;
border-radius: 9px;
padding: 8px 10px;
background: rgba(22,142,148,.09);
color: var(--teal-dark);
font-size: 10px;
font-weight: 900;
letter-spacing: .04em;
display: flex;
justify-content: space-between;
align-items: center;
}
.drive-track {
flex: 0 0 74px;
height: 5px;
overflow: hidden;
border-radius: 10px;
background: rgba(16,44,57,.12);
}
#driveFill {
height: 100%;
width: 0%;
background: var(--teal);
border-radius: inherit;
transition: width .1s linear;
}
#rules {
position: absolute;
left: 18px;
bottom: 18px;
width: 320px;
padding: 13px 15px;
font-size: 11px;
}
#rules h2 {
margin: 0 0 9px;
font-size: 11px;
letter-spacing: .12em;
text-transform: uppercase;
}
.rule-row {
display: grid;
grid-template-columns: 82px 1fr;
gap: 8px;
padding: 3px 0;
color: var(--muted);
line-height: 1.35;
}
.rule-row strong {
color: var(--ink);
font-size: 10px;
}
.wind-rule {
margin-top: 8px;
padding-top: 8px;
border-top: 1px solid var(--line);
color: var(--muted);
line-height: 1.45;
}
.wind-rule b { color: var(--teal-dark); }
#mapPanel {
position: absolute;
right: 18px;
bottom: 18px;
padding: 10px;
width: 188px;
}
#mapTitle {
display: flex;
justify-content: space-between;
margin: 0 2px 7px;
font-size: 9px;
font-weight: 900;
letter-spacing: .12em;
text-transform: uppercase;
color: var(--muted);
}
#minimap {
display: block;
width: 168px;
height: 168px;
border-radius: 10px;
background: #257f88;
box-shadow: inset 0 0 0 1px rgba(255,255,255,.18);
}
#mapLegend {
margin-top: 7px;
display: flex;
justify-content: space-between;
color: var(--muted);
font-size: 8px;
font-weight: 750;
}
#controlsHint {
position: absolute;
left: 50%;
bottom: 23px;
transform: translateX(-50%);
display: flex;
gap: 6px;
align-items: center;
padding: 8px 10px;
border-radius: 11px;
color: var(--muted);
background: rgba(248,247,231,.78);
border: 1px solid rgba(255,255,255,.7);
box-shadow: 0 8px 24px rgba(20,55,65,.1);
backdrop-filter: blur(10px);
font-size: 9px;
font-weight: 800;
letter-spacing: .035em;
}
kbd {
display: inline-grid;
min-width: 22px;
height: 22px;
place-items: center;
padding: 0 5px;
border-radius: 5px;
color: var(--ink);
background: white;
border: 1px solid rgba(16,44,57,.16);
box-shadow: 0 2px 0 rgba(16,44,57,.12);
font: inherit;
font-size: 9px;
}
#toast {
position: absolute;
left: 50%;
bottom: 84px;
transform: translate(-50%, 12px);
opacity: 0;
padding: 10px 15px;
border-radius: 10px;
color: white;
background: rgba(16,44,57,.9);
box-shadow: var(--shadow);
font-size: 11px;
font-weight: 800;
transition: .2s ease;
white-space: nowrap;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
.overlay {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
padding: 22px;
background: rgba(18, 48, 57, .32);
backdrop-filter: blur(5px);
-webkit-backdrop-filter: blur(5px);
}
.overlay.hidden { display: none; }
.modal {
width: min(590px, 100%);
padding: 28px;
border-radius: 22px;
background: var(--paper-solid);
border: 1px solid rgba(255,255,255,.9);
box-shadow: 0 25px 70px rgba(7,35,44,.28);
}
.modal h2 {
margin: 7px 0 10px;
font-size: clamp(29px, 6vw, 48px);
line-height: .98;
letter-spacing: -.055em;
}
.modal p {
color: var(--muted);
font-size: 13px;
line-height: 1.6;
}
.launch-rules {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
margin: 20px 0;
}
.launch-rule {
padding: 12px;
border-radius: 12px;
background: rgba(22,142,148,.08);
border: 1px solid rgba(22,142,148,.1);
color: var(--muted);
font-size: 11px;
line-height: 1.4;
}
.launch-rule strong {
display: block;
margin-bottom: 4px;
color: var(--ink);
font-size: 12px;
}
.primary-button {
width: 100%;
padding: 14px 18px;
border: 0;
border-radius: 12px;
color: white;
background: linear-gradient(135deg, var(--teal), var(--teal-dark));
box-shadow: 0 8px 20px rgba(11,98,108,.25);
cursor: pointer;
font-weight: 900;
letter-spacing: .04em;
}
.primary-button:hover { filter: brightness(1.08); }
.modal-foot {
margin-top: 12px;
text-align: center;
color: #819197;
font-size: 9px;
}
#resultStats {
display: flex;
gap: 8px;
margin: 18px 0;
}
.result-stat {
flex: 1;
padding: 12px;
border-radius: 11px;
background: rgba(16,44,57,.05);
color: var(--muted);
font-size: 10px;
}
.result-stat strong {
display: block;
margin-top: 3px;
color: var(--ink);
font-size: 18px;
font-variant-numeric: tabular-nums;
}
#touchControls {
position: absolute;
inset: auto 12px 12px;
display: none;
justify-content: space-between;
pointer-events: none;
}
.touch-group {
display: flex;
gap: 7px;
align-items: flex-end;
}
.touch-button {
pointer-events: auto;
width: 56px;
height: 56px;
border-radius: 14px;
border: 1px solid rgba(255,255,255,.65);
background: rgba(248,247,231,.82);
color: var(--ink);
box-shadow: 0 6px 18px rgba(20,55,65,.16);
font-size: 20px;
font-weight: 900;
touch-action: none;
user-select: none;
}
.touch-button.active {
color: white;
background: var(--teal-dark);
transform: translateY(2px);
}
.touch-button.trim {
font-size: 10px;
line-height: 1.15;
}
@media (max-width: 850px) {
#progress {
top: 112px;
}
#rules {
display: none;
}
#controlsHint {
display: none;
}
#windPanel {
width: 235px;
}
#mapPanel {
width: 150px;
}
#minimap {
width: 130px;
height: 130px;
}
}
@media (max-width: 600px) {
#brand {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
padding: 12px 14px;
}
#brand h1 {
font-size: 21px;
}
#objectiveText {
margin-top: 6px;
}
#windPanel {
top: 121px;
right: 10px;
width: 198px;
padding: 10px;
}
.compass {
width: 65px;
height: 65px;
flex-basis: 65px;
}
.needle { height: 24px; }
#windName { font-size: 16px; }
#progress {
top: 120px;
left: 10px;
transform: none;
flex-direction: column;
align-items: flex-start;
gap: 3px;
padding: 6px;
}
.route-chip {
padding: 4px 7px;
font-size: 9px;
}
#mapPanel {
right: 10px;
bottom: 91px;
width: 124px;
padding: 7px;
}
#minimap {
width: 108px;
height: 108px;
}
#mapLegend { display: none; }
#toast {
bottom: 91px;
max-width: 90vw;
white-space: normal;
text-align: center;
}
.modal {
padding: 22px;
}
.launch-rules {
grid-template-columns: 1fr;
}
}
@media (pointer: coarse) {
#touchControls { display: flex; }
#controlsHint { display: none; }
#mapPanel { bottom: 88px; }
}
</style>
</head>
<body>
<canvas id="scene"></canvas>
<div id="hud">
<section id="brand" class="panel">
<div class="eyebrow">A voxel sailing expedition</div>
<h1>Lanternglass Cove</h1>
<p id="objectiveText">Light all three channel beacons, then return to Lantern Dock.</p>
<div class="brand-footer">
<span>Elapsed <strong id="timer">00:00.0</strong></span>
<span>
<button id="pauseButton" class="mini-button">PAUSE</button>
<button id="resetButton" class="mini-button">RESET</button>
</span>
</div>
</section>
<section id="progress" class="panel">
<div id="route0" class="route-chip"><span class="dot"></span>Gannet Light</div>
<div id="route1" class="route-chip"><span class="dot"></span>Amber Kite</div>
<div id="route2" class="route-chip"><span class="dot"></span>Bell Crown</div>
<div id="routeGoal" class="route-chip goal"><span class="dot"></span>Lantern Dock</div>
</section>
<section id="windPanel" class="panel">
<div class="wind-heading">
<div>
<div class="eyebrow">Wind source</div>
<div id="windName">NE · 055°</div>
<div id="windBlow">Blowing toward SW · 235°</div>
</div>
<div class="compass" aria-label="Wind direction compass">
<span class="n">N</span>
<span class="e">E</span>
<span class="s">S</span>
<span class="w">W</span>
<div id="windNeedle"><div class="needle"></div></div>
</div>
</div>
<div class="wind-bottom">
<span>Next shift</span>
<span id="windTimer">32s</span>
</div>
<div id="sailState">
<span id="sailStatus">READY TO SAIL</span>
<span class="drive-track"><span id="driveFill"></span></span>
</div>
</section>
<section id="rules" class="panel">
<h2>Simple sailing rules</h2>
<div class="rule-row"><strong>W / ↑</strong><span>Sheet in the sail and drive forward</span></div>
<div class="rule-row"><strong>A D / ← →</strong><span>Turn the rudder left or right</span></div>
<div class="rule-row"><strong>S / ↓</strong><span>Reef the sail, brake, or reverse slowly</span></div>
<div class="wind-rule">
The compass arrow points to the <b>source</b> of the wind.<br>
<b>0–40°</b> off the source: no-go, the sail luffs.<br>
<b>41–130°</b>: strongest drive.<br>
<b>131–180°</b>: reduced running drive.<br>
Wind rotates 30° at each timed shift.
</div>
</section>
<section id="mapPanel" class="panel">
<div id="mapTitle"><span>Bay chart</span><span>N ↑</span></div>
<canvas id="minimap" width="336" height="336"></canvas>
<div id="mapLegend"><span>● Light</span><span>◆ Dock</span><span>▲ Boat</span></div>
</section>
<div id="controlsHint">
<kbd>W</kbd><kbd>A</kbd><kbd>S</kbd><kbd>D</kbd>
<span>SAIL • RUDDER • REEF</span>
<span>·</span>
<kbd>Wheel</kbd><span>ZOOM</span>
</div>
<div id="toast"></div>
<div id="touchControls">
<div class="touch-group">
<button class="touch-button" data-key="KeyA" aria-label="Turn left">←</button>
<button class="touch-button" data-key="KeyD" aria-label="Turn right">→</button>
</div>
<div class="touch-group">
<button class="touch-button trim" data-key="KeyS">REEF</button>
<button class="touch-button trim" data-key="KeyW">SAIL</button>
</div>
</div>
</div>
<div id="startOverlay" class="overlay">
<div class="modal">
<div class="eyebrow">Welcome to Lanternglass Cove</div>
<h2>Read the wind.<br>Thread the islands.</h2>
<p>
This sheltered voxel bay has one simple rule: never point directly into the wind.
Sail on a reaching course, collect all three channel beacons, and bring your boat home
to Lantern Dock.
</p>
<div class="launch-rules">
<div class="launch-rule"><strong>W / ↑ — Sail</strong>Sheet in for forward drive.</div>
<div class="launch-rule"><strong>A D / ← → — Steer</strong>Use the rudder to turn.</div>
<div class="launch-rule"><strong>S / ↓ — Reef</strong>Slow, brake, or reverse.</div>
</div>
<button id="launchButton" class="primary-button">LAUNCH BOAT</button>
<div class="modal-foot">The solid rock islands cannot be crossed. Press P to pause.</div>
</div>
</div>
<div id="pauseOverlay" class="overlay hidden">
<div class="modal">
<div class="eyebrow">The cove is waiting</div>
<h2>Paused.</h2>
<p>Your boat and the wind are frozen exactly where you left them.</p>
<button id="resumeButton" class="primary-button">RESUME SAILING</button>
<div class="modal-foot">Press P or Escape to resume.</div>
</div>
</div>
<div id="endOverlay" class="overlay hidden">
<div class="modal">
<div class="eyebrow">Lantern Dock reached</div>
<h2>Home by lantern light.</h2>
<p>All three channel beacons are burning and your voyage around Lanternglass Cove is complete.</p>
<div id="resultStats">
<div class="result-stat">Voyage time<strong id="resultTime">00:00.0</strong></div>
<div class="result-stat">Best time<strong id="bestTime">—</strong></div>
</div>
<button id="againButton" class="primary-button">SAIL ANOTHER LAP</button>
</div>
</div>
<script type="module">
import * as THREE from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js';
const $ = id => document.getElementById(id);
const canvas = $('scene');
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: 'high-performance'
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xaed9dc);
scene.fog = new THREE.Fog(0xaed9dc, 48, 115);
const camera = new THREE.PerspectiveCamera(
45,
window.innerWidth / window.innerHeight,
0.1,
180
);
const hemi = new THREE.HemisphereLight(0xdff7f0, 0x466269, 2.25);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff0cb, 3.15);
sun.position.set(-26, 48, 24);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -52;
sun.shadow.camera.right = 52;
sun.shadow.camera.top = 52;
sun.shadow.camera.bottom = -52;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 120;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x8cdde2, 0.75);
fill.position.set(30, 18, -30);
scene.add(fill);
const GRID = 36;
const CELL = 2;
const HALF = GRID * CELL / 2;
const MAX_SPEED = 6.8;
const RAD = Math.PI / 180;
const occupancy = new Set();
const islandCells = new Set();
const dockBlocked = new Set();
const cellKey = (i, j) => `${i},${j}`;
const hash = (i, j, seed = 0) => {
const n = Math.sin(i * 127.1 + j * 311.7 + seed * 74.7) * 43758.5453123;
return n - Math.floor(n);
};
const worldX = i => (i - (GRID - 1) / 2) * CELL;
const worldZ = j => (j - (GRID - 1) / 2) * CELL;
function addIsland(cx, cz, rx, rz, seed) {
for (let j = cz - rz; j <= cz + rz; j++) {
for (let i = cx - rx; i <= cx + rx; i++) {
if (i < 1 || j < 1 || i >= GRID - 1 || j >= GRID - 1) continue;
const dx = (i - cx) / rx;
const dz = (j - cz) / rz;
const q = dx * dx + dz * dz;
if (q > 1) continue;
const notch = q > 0.56 && hash(i, j, seed) > 0.60;
if (notch) continue;
occupancy.add(cellKey(i, j));
islandCells.add(cellKey(i, j));
}
}
}
for (let j = 0; j < GRID; j++) {
for (let i = 0; i < GRID; i++) {
if (i === 0 || j === 0 || i === GRID - 1 || j === GRID - 1) {
occupancy.add(cellKey(i, j));
}
}
}
// Rook's Comb, Morrow Stack, Kelp Fang, and Belltooth form the protected channels.
addIsland(9, 14, 4, 5, 2);
addIsland(25, 11, 3, 4, 5);
addIsland(23, 23, 3, 4, 8);
addIsland(10, 25, 4, 3, 12);
// The harbor dock is also solid.
for (let i = 20; i <= 22; i++) {
for (let j = 28; j <= 35; j++) {
dockBlocked.add(cellKey(i, j));
}
}
function pointIsBlocked(x, z) {
if (x < -34.6 || x > 34.6 || z < -34.6 || z > 34.6) return true;
const i = Math.floor((x + HALF) / CELL);
const j = Math.floor((z + HALF) / CELL);
return occupancy.has(cellKey(i, j)) || dockBlocked.has(cellKey(i, j));
}
function boatBlocked(x, z) {
const radius = 0.78;
const offsets = [
[0, 0],
[radius, 0], [-radius, 0], [0, radius], [0, -radius],
[radius * .72, radius * .72],
[-radius * .72, radius * .72],
[radius * .72, -radius * .72],
[-radius * .72, -radius * .72]
];
return offsets.some(([dx, dz]) => pointIsBlocked(x + dx, z + dz));
}
function boxMesh(w, h, d, material, x = 0, y = 0, z = 0, parent = scene) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), material);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// ---------------------------------------------------------------------
// Voxel water
// ---------------------------------------------------------------------
const deepWater = boxMesh(
HALF * 2 + 1, 0.5, HALF * 2 + 1,
new THREE.MeshStandardMaterial({ color: 0x176f7b, roughness: 0.82 }),
0, -0.78, 0
);
deepWater.castShadow = false;
const waterMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.72,
metalness: 0.04
});
const waterTiles = new THREE.InstancedMesh(
new THREE.BoxGeometry(1.94, 0.24, 1.94),
waterMaterial,
GRID * GRID
);
waterTiles.receiveShadow = true;
scene.add(waterTiles);
const matrix = new THREE.Matrix4();
const quaternion = new THREE.Quaternion();
const position = new THREE.Vector3();
const scale = new THREE.Vector3();
const color = new THREE.Color();
let tileIndex = 0;
for (let j = 0; j < GRID; j++) {
for (let i = 0; i < GRID; i++) {
position.set(worldX(i), -0.40, worldZ(j));
quaternion.identity();
scale.set(1, 1, 1);
matrix.compose(position, quaternion, scale);
waterTiles.setMatrixAt(tileIndex, matrix);
const hue = 0.51 + (hash(i, j, 1) - .5) * .025;
const light = .38 + hash(i, j, 2) * .055;
color.setHSL(hue, .48, light);
waterTiles.setColorAt(tileIndex, color);
tileIndex++;
}
}
waterTiles.instanceMatrix.needsUpdate = true;
waterTiles.instanceColor.needsUpdate = true;
const glintData = [];
for (let attempts = 0; glintData.length < 68 && attempts < 900; attempts++) {
const x = (Math.random() - .5) * 67;
const z = (Math.random() - .5) * 67;
if (!pointIsBlocked(x, z)) {
glintData.push({
x, z,
phase: Math.random() * Math.PI * 2,
speed: .45 + Math.random() * .7,
length: .35 + Math.random() * .8
});
}
}
const glints = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, .025, .12),
new THREE.MeshBasicMaterial({
color: 0xc8fbf0,
transparent: true,
opacity: .48,
depthWrite: false
}),
glintData.length
);
glints.frustumCulled = false;
scene.add(glints);
// ---------------------------------------------------------------------
// Islands and cliffs
// ---------------------------------------------------------------------
const interiorLand = [];
const borderLand = [];
const sandCells = new Set();
for (const key of occupancy) {
const [i, j] = key.split(',').map(Number);
if (i === 0 || j === 0 || i === GRID - 1 || j === GRID - 1) {
borderLand.push([i, j]);
} else {
interiorLand.push([i, j]);
for (let dz = -1; dz <= 1; dz++) {
for (let dx = -1; dx <= 1; dx++) {
if (dx === 0 && dz === 0) continue;
const ni = i + dx, nj = j + dz;
const nk = cellKey(ni, nj);
if (!occupancy.has(nk)) sandCells.add(nk);
}
}
}
}
const rockMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: .95,
flatShading: true
});
const rocks = new THREE.InstancedMesh(
new THREE.BoxGeometry(1.88, 1, 1.88),
rockMaterial,
occupancy.size
);
rocks.castShadow = true;
rocks.receiveShadow = true;
scene.add(rocks);
const capMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
flatShading: true
});
const caps = new THREE.InstancedMesh(
new THREE.BoxGeometry(1.92, .18, 1.92),
capMaterial,
interiorLand.length
);
caps.castShadow = true;
caps.receiveShadow = true;
scene.add(caps);
const sandMaterial = new THREE.MeshStandardMaterial({
color: 0xd9c98d,
roughness: 1
});
const sands = new THREE.InstancedMesh(
new THREE.BoxGeometry(1.90, .24, 1.90),
sandMaterial,
sandCells.size
);
sands.receiveShadow = true;
scene.add(sands);
let rockIndex = 0;
for (const [i, j] of borderLand) {
const south = j === GRID - 1;
const h = south ? 1.05 : 2.6 + hash(i, j, 31) * 1.4;
position.set(worldX(i), -.31 + h / 2, worldZ(j));
scale.set(1, h, 1);
matrix.compose(position, quaternion, scale);
rocks.setMatrixAt(rockIndex, matrix);
color.setHSL(.54, .08, .40 + hash(i, j, 32) * .08);
rocks.setColorAt(rockIndex, color);
rockIndex++;
}
for (const [i, j] of interiorLand) {
const h = 1.3 + hash(i, j, 33) * 2.25;
position.set(worldX(i), -.31 + h / 2, worldZ(j));
scale.set(1, h, 1);
matrix.compose(position, quaternion, scale);
rocks.setMatrixAt(rockIndex, matrix);
color.setHSL(.10, .13, .43 + hash(i, j, 34) * .07);
rocks.setColorAt(rockIndex, color);
rockIndex++;
}
rocks.instanceMatrix.needsUpdate = true;
rocks.instanceColor.needsUpdate = true;
interiorLand.forEach(([i, j], index) => {
const h = 1.3 + hash(i, j, 33) * 2.25;
position.set(worldX(i), h - .20, worldZ(j));
scale.set(1, 1, 1);
matrix.compose(position, quaternion, scale);
caps.setMatrixAt(index, matrix);
color.setHSL(.25 + hash(i, j, 35) * .05, .34, .35 + hash(i, j, 36) * .07);
caps.setColorAt(index, color);
});
caps.instanceMatrix.needsUpdate = true;
caps.instanceColor.needsUpdate = true;
let sandIndex = 0;
for (const key of sandCells) {
const [i, j] = key.split(',').map(Number);
if (i < 0 || j < 0 || i >= GRID || j >= GRID) continue;
position.set(worldX(i), -.08, worldZ(j));
scale.set(1, 1, 1);
matrix.compose(position, quaternion, scale);
sands.setMatrixAt(sandIndex, matrix);
sandIndex++;
}
sands.count = sandIndex;
sands.instanceMatrix.needsUpdate = true;
const trunkMaterial = new THREE.MeshStandardMaterial({ color: 0x6e513b, roughness: 1 });
const leafMaterial = new THREE.MeshStandardMaterial({ color: 0x2f8066, roughness: 1, flatShading: true });
const leafLightMaterial = new THREE.MeshStandardMaterial({ color: 0x4d9b6e, roughness: 1, flatShading: true });
function fullySurrounded(i, j) {
return [
[i + 1, j], [i - 1, j], [i, j + 1], [i, j - 1]
].every(([a, b]) => occupancy.has(cellKey(a, b)));
}
for (const [i, j] of interiorLand) {
if (hash(i, j, 40) < .91 || !fullySurrounded(i, j)) continue;
const x = worldX(i), z = worldZ(j);
boxMesh(.28, 1.2, .28, trunkMaterial, x, 1.2, z);
boxMesh(1.3, .85, 1.3, leafMaterial, x, 2.05, z);
boxMesh(.9, .75, .9, leafLightMaterial, x, 2.8, z);
}
// A small block lighthouse on Morrow Stack.
const islandLighthouse = new THREE.Group();
islandLighthouse.position.set(worldX(25), 3.4, worldZ(11));
scene.add(islandLighthouse);
const towerMat = new THREE.MeshStandardMaterial({ color: 0xe8e3cd, roughness: .85 });
const redMat = new THREE.MeshStandardMaterial({ color: 0xc85c49, roughness: .8 });
boxMesh(1.15, .55, 1.15, towerMat, 0, .25, 0, islandLighthouse);
boxMesh(.9, 1.25, .9, towerMat, 0, 1.1, 0, islandLighthouse);
boxMesh(1.02, .35, 1.02, redMat, 0, 1.75, 0, islandLighthouse);
boxMesh(.8, .8, .8, towerMat, 0, 2.3, 0, islandLighthouse);
boxMesh(.65, .5, .65, new THREE.MeshStandardMaterial({
color: 0xffd46d,
emissive: 0xffa62e,
emissiveIntensity: 2.2
}), 0, 2.95, 0, islandLighthouse);
// ---------------------------------------------------------------------
// Lantern Dock
// ---------------------------------------------------------------------
const dockMat = new THREE.MeshStandardMaterial({ color: 0x8a6242, roughness: .95 });
const dockLightMat = new THREE.MeshStandardMaterial({ color: 0xb1845b, roughness: .95 });
boxMesh(4.3, .35, 10, dockMat, 5, .18, 31.2);
for (let z = 27; z <= 35; z += 1.7) {
boxMesh(4.15, .07, .15, dockLightMat, 5, .39, z);
}
for (const x of [3.2, 6.8]) {
for (const z of [27.5, 31, 34.5]) {
boxMesh(.24, 2.1, .24, trunkMaterial, x, .35, z);
}
}
const wallMat = new THREE.MeshStandardMaterial({ color: 0xe6d9b7, roughness: .9 });
const roofMat = new THREE.MeshStandardMaterial({ color: 0x547a7d, roughness: .9 });
const windowMat = new THREE.MeshStandardMaterial({
color: 0xffc866,
emissive: 0xff9b36,
emissiveIntensity: 1.4
});
boxMesh(5.4, 2.5, 4, wallMat, 11.5, 1.6, 33.2);
boxMesh(5.8, .55, 4.4, roofMat, 11.5, 3.1, 33.2);
boxMesh(3.8, .55, 3.1, roofMat, 11.5, 3.65, 33.2);
boxMesh(.65, .85, .08, windowMat, 10.4, 1.65, 31.16);
boxMesh(.65, .85, .08, windowMat, 12.6, 1.65, 31.16);
boxMesh(1.65, 4.1, 1.65, wallMat, .5, 2.35, 32);
boxMesh(1.8, .45, 1.8, redMat, .5, 3.45, 32);
boxMesh(1.15, .7, 1.15, windowMat, .5, 4.85, 32);
boxMesh(1.5, .25, 1.5, roofMat, .5, 5.3, 32);
// ---------------------------------------------------------------------
// Channel beacons
// ---------------------------------------------------------------------
const markerData = [
{ id: 0, name: 'Gannet Light', x: -25, z: -23, color: 0xffb85a },
{ id: 1, name: 'Amber Kite', x: 24, z: -3, color: 0xff8f54 },
{ id: 2, name: 'Bell Crown', x: -7, z: 24, color: 0xf5d26b }
];
const markers = [];
const ringMaterial = new THREE.MeshBasicMaterial({
color: 0xffd378,
transparent: true,
opacity: .72
});
markerData.forEach((data, index) => {
const group = new THREE.Group();
group.position.set(data.x, 0, data.z);
scene.add(group);
const float = new THREE.Group();
group.add(float);
boxMesh(.9, .38, .9, new THREE.MeshStandardMaterial({
color: 0x284e59,
roughness: .7
}), 0, .02, 0, float);
boxMesh(.18, 1.8, .18, new THREE.MeshStandardMaterial({
color: 0xece1bc,
roughness: .8
}), 0, 1.05, 0, float);
const lampMat = new THREE.MeshStandardMaterial({
color: data.color,
emissive: data.color,
emissiveIntensity: 2.7
});
boxMesh(.58, .58, .58, lampMat, 0, 2.05, 0, float);
boxMesh(.7, .16, .7, ringMaterial, 0, 1.73, 0, float);
const halo = new THREE.Group();
halo.position.y = -.05;
group.add(halo);
for (let k = 0; k < 8; k++) {
const a = k / 8 * Math.PI * 2;
const cube = boxMesh(
.25, .12, .25,
new THREE.MeshBasicMaterial({
color: data.color,
transparent: true,
opacity: .8
}),
Math.cos(a) * 1.25,
0,
Math.sin(a) * 1.25,
halo
);
cube.castShadow = false;
}
const light = new THREE.PointLight(data.color, 2.4, 8, 2);
light.position.y = 2.1;
group.add(light);
markers.push({ ...data, group, float, halo, lampMat, collected: false, phase: index * 1.7 });
});
// Return beacon at Lantern Dock.
const goal = new THREE.Group();
goal.position.set(5, 0, 24);
scene.add(goal);
const goalRing = new THREE.Group();
goal.add(goalRing);
for (let k = 0; k < 12; k++) {
const a = k / 12 * Math.PI * 2;
const cube = boxMesh(
.28, .16, .28,
new THREE.MeshBasicMaterial({ color: 0xefaa4e, transparent: true, opacity: .85 }),
Math.cos(a) * 1.35,
.05,
Math.sin(a) * 1.35,
goalRing
);
cube.castShadow = false;
}
const goalCoreMaterial = new THREE.MeshStandardMaterial({
color: 0xefaa4e,
emissive: 0xefaa4e,
emissiveIntensity: 1.8
});
boxMesh(.65, .85, .65, goalCoreMaterial, 0, .55, 0, goal);
const goalLight = new THREE.PointLight(0xefaa4e, 1.5, 7, 2);
goalLight.position.y = 1;
goal.add(goalLight);
// ---------------------------------------------------------------------
// Voxel sailboat
// ---------------------------------------------------------------------
const boat = new THREE.Group();
scene.add(boat);
const boatModel = new THREE.Group();
boat.add(boatModel);
const hullMat = new THREE.MeshStandardMaterial({ color: 0x8a4f38, roughness: .8 });
const hullDarkMat = new THREE.MeshStandardMaterial({ color: 0x4d3430, roughness: .85 });
const deckMat = new THREE.MeshStandardMaterial({ color: 0xe9d7a9, roughness: .85 });
const trimMat = new THREE.MeshStandardMaterial({ color: 0x2d7880, roughness: .7 });
const mastMat = new THREE.MeshStandardMaterial({ color: 0x805a3d, roughness: .8 });
boxMesh(1.45, .28, 3.45, hullDarkMat, 0, -.28, .05, boatModel);
boxMesh(1.75, .55, 3.1, hullMat, 0, .08, 0, boatModel);
boxMesh(1.5, .25, 2.8, deckMat, 0, .47, .05, boatModel);
boxMesh(.88, .48, .88, hullMat, 0, .22, -1.82, boatModel);
boxMesh(.64, .22, .52, trimMat, 0, .69, -1.98, boatModel);
boxMesh(1.2, .68, 1.2, deckMat, 0, .88, .62, boatModel);
boxMesh(.86, .25, .9, trimMat, 0, 1.32, .62, boatModel);
boxMesh(.52, .27, .03, new THREE.MeshStandardMaterial({
color: 0x96dbe0,
emissive: 0x2d9ca5,
emissiveIntensity: .4
}), 0, 1.02, .01, boatModel);
boxMesh(.16, 3.65, .16, mastMat, 0, 2.25, -.3, boatModel);
boxMesh(.54, .12, .05, new THREE.MeshStandardMaterial({ color: 0xe56f4f }), .24, 4.02, -.3, boatModel);
const sailPivot = new THREE.Group();
sailPivot.position.set(0, 0, -.3);
boatModel.add(sailPivot);
const sailBlocks = [];
for (let row = 0; row < 10; row++) {
const maxCol = 7 - Math.floor(row / 2);
for (let col = 0; col <= maxCol; col++) {
sailBlocks.push({
y: .92 + row * .27,
z: col * .205,
stripe: row === 5 || (row === 6 && col < 4)
});
}
}
const sailMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: .9,
side: THREE.DoubleSide,
flatShading: true
});
const sailMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(.18, .235, .19),
sailMaterial,
sailBlocks.length
);
sailMesh.castShadow = true;
sailBlocks.forEach((block, index) => {
position.set(0, block.y, block.z);
scale.set(1, 1, 1);
matrix.compose(position, quaternion, scale);
sailMesh.setMatrixAt(index, matrix);
color.set(block.stripe ? 0xe66f50 : 0xf3ecd3);
sailMesh.setColorAt(index, color);
});
sailMesh.instanceMatrix.needsUpdate = true;
sailMesh.instanceColor.needsUpdate = true;
sailPivot.add(sailMesh);
boxMesh(.12, .12, 1.55, mastMat, 0, .82, .72, sailPivot);
boxMesh(.12, .12, 1.55, mastMat, 0, 3.27, .72, sailPivot);
// ---------------------------------------------------------------------
// Clouds and decorative sky
// ---------------------------------------------------------------------
const cloudMaterial = new THREE.MeshStandardMaterial({
color: 0xf7f1df,
roughness: 1,
flatShading: true
});
const clouds = [];
function makeCloud(x, y, z, scale) {
const group = new THREE.Group();
group.position.set(x, y, z);
group.scale.setScalar(scale);
scene.add(group);
boxMesh(3.2, .9, 1.5, cloudMaterial, 0, 0, 0, group).castShadow = false;
boxMesh(1.7, 1.15, 1.7, cloudMaterial, -.9, .4, 0, group).castShadow = false;
boxMesh(1.8, .8, 1.4, cloudMaterial, 1.1, .18, 0, group).castShadow = false;
clouds.push({ group, speed: .18 + Math.random() * .17 });
}
makeCloud(-25, 14, -30, 1.2);
makeCloud(20, 16, -27, 1.0);
makeCloud(-4, 18, -40, 1.4);
makeCloud(33, 13, -8, .85);
makeCloud(-35, 15, 8, .95);
// ---------------------------------------------------------------------
// Wake and particles
// ---------------------------------------------------------------------
const wakePool = [];
for (let i = 0; i < 26; i++) {
const mat = new THREE.MeshBasicMaterial({
color: 0xe7fff4,
transparent: true,
opacity: 0,
depthWrite: false
});
const mesh = new THREE.Mesh(new THREE.BoxGeometry(.32, .04, .16), mat);
mesh.visible = false;
scene.add(mesh);
wakePool.push({ mesh, life: 0, vx: 0, vz: 0 });
}
let wakeTimer = 0;
const particles = [];
const particleGeometry = new THREE.BoxGeometry(.16, .16, .16);
function burst(x, z, hexColor, amount = 18) {
for (let i = 0; i < amount; i++) {
const material = new THREE.MeshBasicMaterial({
color: hexColor,
transparent: true,
opacity: 1
});
const mesh = new THREE.Mesh(particleGeometry, material);
mesh.position.set(x, 1.1, z);
scene.add(mesh);
const angle = Math.random() * Math.PI * 2;
const speed = 1.3 + Math.random() * 3.2;
particles.push({
mesh,
life: .9 + Math.random() * .45,
vx: Math.cos(angle) * speed,
vy: 2 + Math.random() * 3.4,
vz: Math.sin(angle) * speed
});
}
}
// ---------------------------------------------------------------------
// Game state and input
// ---------------------------------------------------------------------
const state = {
started: false,
paused: false,
won: false,
elapsed: 0,
speed: 0,
heading: 0,
drive: 0,
relAngle: 55,
wind: 55,
windTarget: 55,
windPhase: 0,
windTimer: 32,
zoom: 38,
pos: new THREE.Vector3(0, 0, 14),
collisionCooldown: 0
};
const windSequence = [55, 85, 115, 85, 55, 25];
const keys = new Set();
const movementKeys = new Set([
'KeyW', 'KeyA', 'KeyS', 'KeyD',
'ArrowUp', 'ArrowLeft', 'ArrowDown', 'ArrowRight'
]);
let audioContext = null;
function initAudio() {
if (!audioContext) {
try {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
} catch (_) {}
}
if (audioContext?.state === 'suspended') audioContext.resume();
}
function tone(frequency, duration = .12, type = 'sine', volume = .045) {
if (!audioContext) return;
const osc = audioContext.createOscillator();
const gain = audioContext.createGain();
osc.type = type;
osc.frequency.value = frequency;
gain.gain.setValueAtTime(volume, audioContext.currentTime);
gain.gain.exponentialRampToValueAtTime(.001, audioContext.currentTime + duration);
osc.connect(gain).connect(audioContext.destination);
osc.start();
osc.stop(audioContext.currentTime + duration);
}
function wrapDegrees(angle) {
while (angle > 180) angle -= 360;
while (angle < -180) angle += 360;
return angle;
}
function cardinal(angle) {
const names = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'];
return names[Math.round(((angle % 360) + 360) % 360 / 45) % 8];
}
function formatTime(seconds) {
const minutes = Math.floor(seconds / 60);
const secs = seconds - minutes * 60;
return `${String(minutes).padStart(2, '0')}:${secs.toFixed(1).padStart(4, '0')}`;
}
let toastUntil = 0;
function showToast(text, duration = 2.1) {
$('toast').textContent = text;
$('toast').classList.add('show');
toastUntil = performance.now() + duration * 1000;
}
function resetGame() {
state.started = false;
state.paused = false;
state.won = false;
state.elapsed = 0;
state.speed = 0;
state.heading = 0;
state.drive = 0;
state.relAngle = 55;
state.wind = 55;
state.windTarget = 55;
state.windPhase = 0;
state.windTimer = 32;
state.pos.set(0, 0, 14);
state.collisionCooldown = 0;
keys.clear();
markers.forEach(marker => {
marker.collected = false;
marker.group.visible = true;
});
wakePool.forEach(wake => {
wake.life = 0;
wake.mesh.visible = false;
});
particles.forEach(particle => {
scene.remove(particle.mesh);
particle.mesh.material.dispose();
});
particles.length = 0;
$('startOverlay').classList.remove('hidden');
$('pauseOverlay').classList.add('hidden');
$('endOverlay').classList.add('hidden');
updateHUD();
}
function launchGame() {
resetGame();
initAudio();
state.started = true;
$('startOverlay').classList.add('hidden');
tone(392, .15, 'triangle', .035);
setTimeout(() => tone(523, .18, 'triangle', .03), 100);
showToast('Steer toward a beacon. Avoid pointing into the wind.', 3.2);
}
function setPaused(paused) {
if (!state.started || state.won) return;
state.paused = paused;
$('pauseOverlay').classList.toggle('hidden', !paused);
if (!paused) initAudio();
}
function winGame() {
state.won = true;
state.speed = 0;
burst(5, 24, 0xffc65d, 42);
tone(523, .18, 'triangle', .04);
setTimeout(() => tone(659, .2, 'triangle', .035), 120);
setTimeout(() => tone(784, .3, 'triangle', .03), 250);
let best = null;
try {
best = Number(localStorage.getItem('lanternglass-best')) || null;
if (!best || state.elapsed < best) {
best = state.elapsed;
localStorage.setItem('lanternglass-best', String(best));
}
} catch (_) {}
$('resultTime').textContent = formatTime(state.elapsed);
$('bestTime').textContent = best ? formatTime(best) : formatTime(state.elapsed);
$('endOverlay').classList.remove('hidden');
}
$('launchButton').addEventListener('click', launchGame);
$('againButton').addEventListener('click', launchGame);
$('resumeButton').addEventListener('click', () => setPaused(false));
$('pauseButton').addEventListener('click', () => setPaused(!state.paused));
$('resetButton').addEventListener('click', resetGame);
window.addEventListener('keydown', event => {
if (movementKeys.has(event.code)) {
event.preventDefault();
keys.add(event.code);
}
if (event.repeat) return;
if (event.code === 'Enter' || event.code === 'Space') {
if (!state.started) {
event.preventDefault();
launchGame();
}
}
if (event.code === 'KeyR') resetGame();
if (event.code === 'KeyP' || event.code === 'Escape') {
setPaused(!state.paused);
}
});
window.addEventListener('keyup', event => {
keys.delete(event.code);
});
window.addEventListener('blur', () => {
keys.clear();
if (state.started && !state.won) setPaused(true);
});
document.querySelectorAll('.touch-button').forEach(button => {
const release = () => {
keys.delete(button.dataset.key);
button.classList.remove('active');
};
button.addEventListener('pointerdown', event => {
event.preventDefault();
button.setPointerCapture(event.pointerId);
keys.add(button.dataset.key);
button.classList.add('active');
});
button.addEventListener('pointerup', release);
button.addEventListener('pointercancel', release);
button.addEventListener('lostpointercapture', release);
});
canvas.addEventListener('wheel', event => {
event.preventDefault();
state.zoom = THREE.MathUtils.clamp(state.zoom + event.deltaY * .018, 21, 55);
}, { passive: false });
// ---------------------------------------------------------------------
// Physics and game rules
// ---------------------------------------------------------------------
function updateGame(dt) {
state.elapsed += dt;
state.collisionCooldown = Math.max(0, state.collisionCooldown - dt);
// Predictable 30-degree wind shifts, with smooth interpolation.
state.windTimer -= dt;
if (state.windTimer <= 0) {
state.windPhase = (state.windPhase + 1) % windSequence.length;
state.windTarget = windSequence[state.windPhase];
state.windTimer = 32;
showToast(`The wind has shifted to ${cardinal(state.windTarget)}.`, 2.1);
tone(310, .12, 'sine', .025);
}
state.wind += wrapDegrees(state.windTarget - state.wind) * Math.min(1, dt * .65);
state.wind = ((state.wind % 360) + 360) % 360;
const trim = keys.has('KeyW') || keys.has('ArrowUp');
const reef = keys.has('KeyS') || keys.has('ArrowDown');
const rudder =
(keys.has('KeyD') || keys.has('ArrowRight') ? 1 : 0) -
(keys.has('KeyA') || keys.has('ArrowLeft') ? 1 : 0);
const headingDegrees = ((state.heading * RAD) % 360 + 360) % 360;
const relative = Math.abs(wrapDegrees(state.wind - headingDegrees));
state.relAngle = relative;
// Rudder authority rises with boat speed, but a little remains at rest.
const speedRatio = Math.min(Math.abs(state.speed) / MAX_SPEED, 1);
const turnRate = 1.08 * (.30 + speedRatio * .92);
state.heading = wrapDegrees(state.heading * RAD + rudder * turnRate * dt) / RAD;
let drive = 0;
if (trim) {
if (relative <= 40) {
drive = 0; // No-go zone: the sail luffs.
} else if (relative <= 130) {
drive = 4.65; // Reaching: strongest drive.
} else {
drive = 2.75; // Running: reduced drive.
}
} else {
drive = .12; // A little passive steerage while coasting.
}
if (reef) drive = -2.55;
let drag = .42 + Math.abs(state.speed) * .075;
if (trim) drag = .24 + Math.abs(state.speed) * .045;
if (reef) drag = 1.45 + Math.abs(state.speed) * .14;
state.speed += (drive - drag) * dt;
state.speed = THREE.MathUtils.clamp(state.speed, -1.65, MAX_SPEED);
state.drive = drive;
const heading = state.heading * RAD;
const forwardX = Math.sin(heading);
const forwardZ = -Math.cos(heading);
const oldX = state.pos.x;
const oldZ = state.pos.z;
const nextX = oldX + forwardX * state.speed * dt;
const nextZ = oldZ + forwardZ * state.speed * dt;
if (!boatBlocked(nextX, nextZ)) {
state.pos.x = nextX;
state.pos.z = nextZ;
} else {
let collided = false;
if (!boatBlocked(nextX, oldZ)) {
state.pos.x = nextX;
collided = true;
}
if (!boatBlocked(state.pos.x, nextZ)) {
state.pos.z = nextZ;
collided = true;
}
if (collided) {
state.speed *= .42;
if (state.collisionCooldown <= 0) {
showToast('Solid rock! Islands cannot be crossed.', 1.8);
state.collisionCooldown = 1.3;
tone(115, .14, 'square', .025);
}
} else {
state.speed *= -.16;
if (state.collisionCooldown <= 0) {
showToast('Solid rock! Islands cannot be crossed.', 1.8);
state.collisionCooldown = 1.3;
}
}
}
// Beacon collection.
for (const marker of markers) {
if (marker.collected) continue;
const dx = state.pos.x - marker.x;
const dz = state.pos.z - marker.z;
if (dx * dx + dz * dz < 2.25 * 2.25) {
marker.collected = true;
marker.group.visible = false;
burst(marker.x, marker.z, marker.color, 25);
tone(659, .13, 'triangle', .04);
setTimeout(() => tone(880, .18, 'triangle', .03), 100);
const count = markers.filter(m => m.collected).length;
if (count < markers.length) {
showToast(`${marker.name} lit — ${count} of 3 beacons burning.`, 2.3);
} else {
showToast('All beacons lit! Return to Lantern Dock.', 3.3);
}
}
}
// Return to the harbor only after all three lights are collected.
if (markers.every(marker => marker.collected)) {
const dx = state.pos.x - 5;
const dz = state.pos.z - 24;
if (dx * dx + dz * dz < 2.6 * 2.6) winGame();
}
// Wake.
wakeTimer -= dt;
if (Math.abs(state.speed) > .65 && wakeTimer <= 0) {
wakeTimer = .10;
const wake = wakePool.find(item => item.life <= 0);
if (wake) {
const side = (Math.random() - .5) * .75;
wake.mesh.position.set(
state.pos.x - forwardX * 1.55 - forwardZ * side,
-.17,
state.pos.z - forwardZ * 1.55 + forwardX * side
);
wake.mesh.scale.set(.7 + Math.random() * .8, 1, .7 + Math.random() * .8);
wake.vx = -forwardX * Math.abs(state.speed) * .22 + (Math.random() - .5) * .3;
wake.vz = -forwardZ * Math.abs(state.speed) * .22 + (Math.random() - .5) * .3;
wake.life = .9;
wake.mesh.visible = true;
}
}
}
function updateWake(dt) {
for (const wake of wakePool) {
if (wake.life <= 0) continue;
wake.life -= dt;
wake.mesh.position.x += wake.vx * dt;
wake.mesh.position.z += wake.vz * dt;
wake.mesh.position.y = -.16 + Math.sin(wake.life * 8) * .025;
wake.mesh.material.opacity = Math.max(0, wake.life / .9) * .58;
wake.mesh.scale.multiplyScalar(1 + dt * .18);
if (wake.life <= 0) wake.mesh.visible = false;
}
for (let i = particles.length - 1; i >= 0; i--) {
const particle = particles[i];
particle.life -= dt;
particle.vy -= 7.5 * dt;
particle.mesh.position.x += particle.vx * dt;
particle.mesh.position.y += particle.vy * dt;
particle.mesh.position.z += particle.vz * dt;
particle.mesh.rotation.x += dt * 4;
particle.mesh.rotation.y += dt * 5;
particle.mesh.material.opacity = Math.max(0, particle.life);
if (particle.life <= 0) {
scene.remove(particle.mesh);
particle.mesh.material.dispose();
particles.splice(i, 1);
}
}
}
// ---------------------------------------------------------------------
// Visual updates
// ---------------------------------------------------------------------
function updateVisuals(time, dt) {
const heading = state.heading * RAD;
boat.position.set(state.pos.x, 0, state.pos.z);
boat.rotation.y = -heading;
const bob = Math.sin(time * 2.1) * .045;
const roll = Math.sin(time * 1.55) * .025 + state.speed * -.004;
const pitch = Math.sin(time * 1.3 + 1) * .012;
boatModel.position.y = bob;
boatModel.rotation.z = roll;
boatModel.rotation.x = pitch;
const signedRelative = wrapDegrees(state.wind - (state.heading * RAD));
let sailTarget = THREE.MathUtils.clamp(signedRelative * .48, -1.15, 1.15);
if (keys.has('KeyS') || keys.has('ArrowDown')) sailTarget *= 1.45;
sailPivot.rotation.y += (sailTarget - sailPivot.rotation.y) * Math.min(1, dt * 3.2);
glintData.forEach((glint, index) => {
const x = glint.x + Math.sin(time * glint.speed + glint.phase) * .45;
const z = glint.z + Math.cos(time * glint.speed * .7 + glint.phase) * .28;
const y = -.245 + Math.sin(time * 1.7 + glint.phase) * .035;
position.set(x, y, z);
scale.set(glint.length, 1, 1);
matrix.compose(position, quaternion, scale);
glints.setMatrixAt(index, matrix);
});
glints.instanceMatrix.needsUpdate = true;
markers.forEach(marker => {
if (marker.collected) return;
marker.float.position.y = Math.sin(time * 2 + marker.phase) * .13;
marker.float.rotation.y = Math.sin(time * .55 + marker.phase) * .08;
marker.halo.rotation.y = time * .55;
marker.lampMat.emissiveIntensity = 2.35 + Math.sin(time * 3 + marker.phase) * .45;
});
goalRing.rotation.y = time * .35;
goal.position.y = Math.sin(time * 1.8) * .05;
const allLit = markers.every(marker => marker.collected);
const goalColor = allLit ? 0x65c878 : 0xefaa4e;
goalCoreMaterial.color.setHex(goalColor);
goalCoreMaterial.emissive.setHex(goalColor);
goalLight.color.setHex(goalColor);
goalLight.intensity = allLit ? 2.6 : 1.25;
for (const cloud of clouds) {
cloud.group.position.x += cloud.speed * dt;
if (cloud.group.position.x > 48) cloud.group.position.x = -48;
}
}
// Custom follow camera and zoom, implemented without camera addons.
const cameraTarget = new THREE.Vector3(0, 0, 14);
const desiredTarget = new THREE.Vector3();
const desiredCamera = new THREE.Vector3();
const cameraOffset = new THREE.Vector3();
function updateCamera(dt) {
const heading = state.heading * RAD;
desiredTarget.set(
state.pos.x + Math.sin(heading) * 1.4,
.3,
state.pos.z - Math.cos(heading) * 1.4
);
cameraTarget.lerp(desiredTarget, 1 - Math.exp(-dt * 3.4));
cameraOffset.set(18, 25, 21);
if (window.innerWidth < window.innerHeight) cameraOffset.set(13, 28, 18);
cameraOffset.normalize().multiplyScalar(state.zoom);
desiredCamera.copy(cameraTarget).add(cameraOffset);
camera.position.lerp(desiredCamera, 1 - Math.exp(-dt * 3.2));
camera.lookAt(cameraTarget);
}
// ---------------------------------------------------------------------
// HUD and minimap
// ---------------------------------------------------------------------
const mapCanvas = $('minimap');
const mapContext = mapCanvas.getContext('2d');
function drawMinimap() {
const ctx = mapContext;
const w = mapCanvas.width;
const h = mapCanvas.height;
ctx.clearRect(0, 0, w, h);
const gradient = ctx.createLinearGradient(0, 0, w, h);
gradient.addColorStop(0, '#2e8e93');
gradient.addColorStop(1, '#1f6c79');
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, w, h);
const scale = w / (GRID * CELL);
const mapX = x => (x + HALF) * scale;
const mapZ = z => (z + HALF) * scale;
for (const [i, j] of borderLand) {
ctx.fillStyle = '#596d69';
ctx.fillRect(mapX(worldX(i)) - 1, mapZ(worldZ(j)) - 1, CELL * scale + 2, CELL * scale + 2);
}
for (const [i, j] of interiorLand) {
ctx.fillStyle = '#66866c';
ctx.fillRect(mapX(worldX(i)) - 1, mapZ(worldZ(j)) - 1, CELL * scale + 2, CELL * scale + 2);
}
ctx.fillStyle = '#8f694b';
ctx.fillRect(mapX(3), mapZ(27), 4 * scale, 8 * scale);
markers.forEach(marker => {
if (marker.collected) return;
ctx.fillStyle = '#ffd06b';
ctx.shadowColor = '#ffe7a0';
ctx.shadowBlur = 10;
ctx.beginPath();
ctx.arc(mapX(marker.x), mapZ(marker.z), 6.2, 0, Math.PI * 2);
ctx.fill();
ctx.shadowBlur = 0;
});
const allLit = markers.every(marker => marker.collected);
ctx.save();
ctx.translate(mapX(5), mapZ(24));
ctx.rotate(Math.PI / 4);
ctx.fillStyle = allLit ? '#75e08b' : '#e9a351';
ctx.fillRect(-6, -6, 12, 12);
ctx.restore();
// Boat triangle; heading 0 points north.
ctx.save();
ctx.translate(mapX(state.pos.x), mapZ(state.pos.z));
ctx.rotate(state.heading * RAD);
ctx.fillStyle = '#fff7dd';
ctx.strokeStyle = '#173e48';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(0, -9);
ctx.lineTo(6, 7);
ctx.lineTo(0, 4);
ctx.lineTo(-6, 7);
ctx.closePath();
ctx.fill();
ctx.stroke();
ctx.restore();
// Tiny wind-source indicator.
ctx.save();
ctx.translate(w - 28, 28);
ctx.rotate(state.wind * RAD);
ctx.strokeStyle = '#e8fff8';
ctx.fillStyle = '#e8fff8';
ctx.lineWidth = 3;
ctx.beginPath();
ctx.moveTo(0, 9);
ctx.lineTo(0, -9);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0, -12);
ctx.lineTo(-5, -5);
ctx.lineTo(5, -5);
ctx.closePath();
ctx.fill();
ctx.restore();
}
function updateHUD() {
const wind = Math.round(state.wind);
const blow = (wind + 180) % 360;
$('windName').textContent = `${cardinal(wind)} · ${String(wind).padStart(3, '0')}°`;
$('windBlow').textContent = `Blowing toward ${cardinal(blow)} · ${String(blow).padStart(3, '0')}°`;
$('windNeedle').style.transform = `rotate(${wind}deg)`;
$('windTimer').textContent = `${Math.max(0, Math.ceil(state.windTimer))}s`;
$('timer').textContent = formatTime(state.elapsed);
markers.forEach(marker => {
$(`route${marker.id}`).classList.toggle('collected', marker.collected);
});
const allLit = markers.every(marker => marker.collected);
$('routeGoal').classList.toggle('ready', allLit);
$('routeGoal').lastElementChild.textContent = allLit ? 'Lantern Dock — RETURN' : 'Lantern Dock — LOCKED';
let status = 'COASTING';
if (keys.has('KeyS') || keys.has('ArrowDown')) status = 'REEFED / BRAKING';
else if ((keys.has('KeyW') || keys.has('ArrowUp')) && state.relAngle <= 40) status = 'LUFFING / NO-GO';
else if (state.relAngle <= 130) status = 'REACHING / FULL DRIVE';
else status = 'RUNNING / EASED DRIVE';
$('sailStatus').textContent = status;
const driveRatio = THREE.MathUtils.clamp(Math.max(0, state.drive) / 4.65, 0, 1);
$('driveFill').style.width = `${driveRatio * 100}%`;
$('driveFill').style.background = state.relAngle <= 40 && state.drive > 0 ? '#df6554' : '#168e94';
const count = markers.filter(marker => marker.collected).length;
$('objectiveText').textContent = allLit
? 'All channel beacons are lit. Return south to Lantern Dock.'
: `Light all three channel beacons, then return to Lantern Dock. ${count}/3 lit.`;
drawMinimap();
}
let hudAccumulator = 0;
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), .05);
const time = clock.elapsedTime;
if (state.started && !state.paused && !state.won) {
updateGame(dt);
}
updateVisuals(time, dt);
updateWake(dt);
updateCamera(dt);
hudAccumulator += dt;
if (hudAccumulator >= .1) {
hudAccumulator = 0;
updateHUD();
}
if (performance.now() > toastUntil) {
$('toast').classList.remove('show');
}
renderer.render(scene, camera);
}
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
resetGame();
updateCamera(1);
animate();
</script>
</body>
</html>
```