Prompt specification
Build or customize the Three.js 3D scene "Warehouse Key Delivery Platform" 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: Warehouse Key Delivery Platform
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/warehouse-key-delivery-platform-x3459
Scene Description Prompt:
Make a playable voxel warehouse key-delivery puzzle with ramps, crates, collision boundaries, and a solvable route. Use Three.js from cdnjs in standalone HTML.
Working Three.js Starter (warehouse-key-delivery-platform-x3459):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Shift // Key Delivery</title>
<style>
:root {
color-scheme: dark;
--bg: #08131e;
--panel: rgba(12, 25, 37, 0.88);
--line: rgba(152, 204, 230, 0.18);
--cyan: #66e3ff;
--green: #72f0aa;
--gold: #ffd166;
--red: #ff6978;
--text: #eaf7ff;
--muted: #91aabd;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
color: var(--text);
}
button { font: inherit; }
#game {
position: fixed;
inset: 0;
}
#game canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
#hud {
position: fixed;
inset: 0;
pointer-events: none;
padding: 20px;
}
.panel {
background: var(--panel);
border: 1px solid var(--line);
box-shadow: 0 16px 42px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
}
.brand {
position: absolute;
top: 20px;
left: 20px;
width: 238px;
padding: 15px 17px;
border-radius: 14px;
}
.eyebrow {
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.brand h1 {
margin: 5px 0 3px;
font-size: 21px;
line-height: 1;
letter-spacing: -0.03em;
}
.brand p {
margin: 8px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
#objective {
position: absolute;
top: 20px;
left: 50%;
width: min(540px, calc(100vw - 580px));
min-width: 320px;
padding: 14px 18px;
transform: translateX(-50%);
border-radius: 14px;
}
.objective-row {
display: flex;
align-items: center;
gap: 12px;
}
#objective-number {
display: grid;
width: 34px;
height: 34px;
flex: 0 0 auto;
place-items: center;
border: 1px solid rgba(102, 227, 255, 0.45);
border-radius: 9px;
background: rgba(102, 227, 255, 0.08);
color: var(--cyan);
font-size: 12px;
font-weight: 900;
}
#objective-text {
font-size: 14px;
font-weight: 750;
line-height: 1.3;
}
#objective-tip {
margin: 8px 0 0 46px;
color: var(--muted);
font-size: 11px;
line-height: 1.4;
}
.steps {
display: flex;
gap: 5px;
margin-top: 12px;
}
.step {
height: 4px;
flex: 1;
overflow: hidden;
border-radius: 99px;
background: rgba(255, 255, 255, 0.09);
}
.step::after {
display: block;
width: 100%;
height: 100%;
border-radius: inherit;
background: var(--cyan);
content: "";
transform: translateX(-105%);
transition: transform 0.35s ease;
}
.step.done::after { transform: translateX(0); }
.step.current::after { transform: translateX(0); background: var(--gold); }
.route-help {
position: absolute;
bottom: 20px;
left: 20px;
width: 238px;
padding: 15px 17px;
border-radius: 14px;
font-size: 12px;
}
.route-help strong {
display: block;
margin-bottom: 9px;
color: var(--gold);
font-size: 10px;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.route-help ol {
margin: 0;
padding-left: 19px;
color: var(--muted);
line-height: 1.55;
}
.route-help li + li { margin-top: 4px; }
.controls {
display: grid;
grid-template-columns: auto 1fr;
gap: 5px 10px;
margin-top: 13px;
padding-top: 12px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 10px;
}
kbd {
min-width: 28px;
padding: 2px 6px;
border: 1px solid rgba(255, 255, 255, 0.17);
border-bottom-width: 2px;
border-radius: 5px;
background: rgba(255, 255, 255, 0.06);
color: var(--text);
font: 700 10px ui-monospace, SFMono-Regular, Menlo, monospace;
text-align: center;
}
#timer {
position: absolute;
top: 20px;
right: 20px;
min-width: 100px;
padding: 12px 15px;
border-radius: 13px;
text-align: right;
}
#timer-label {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.18em;
}
#timer-value {
margin-top: 2px;
font: 800 20px ui-monospace, SFMono-Regular, Menlo, monospace;
}
#map-panel {
position: absolute;
right: 20px;
bottom: 20px;
width: 252px;
padding: 10px;
border-radius: 14px;
}
#map-title {
display: flex;
justify-content: space-between;
margin: 0 2px 7px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
#map {
display: block;
width: 100%;
height: auto;
border-radius: 8px;
background: #07111a;
}
#prompt {
position: absolute;
bottom: 84px;
left: 50%;
padding: 11px 17px;
transform: translate(-50%, 12px);
border: 1px solid rgba(255, 209, 102, 0.5);
border-radius: 10px;
background: rgba(20, 28, 32, 0.92);
color: var(--gold);
font-size: 13px;
font-weight: 800;
opacity: 0;
transition: opacity 0.18s, transform 0.18s;
}
#prompt.show {
transform: translate(-50%, 0);
opacity: 1;
}
#toast {
position: absolute;
top: 116px;
left: 50%;
max-width: min(520px, calc(100vw - 40px));
padding: 11px 17px;
transform: translate(-50%, -8px);
border: 1px solid rgba(102, 227, 255, 0.35);
border-radius: 10px;
background: rgba(12, 29, 40, 0.94);
color: var(--text);
font-size: 12px;
font-weight: 700;
opacity: 0;
transition: opacity 0.2s, transform 0.2s;
}
#toast.show {
transform: translate(-50%, 0);
opacity: 1;
}
#success {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: rgba(3, 10, 16, 0.72);
opacity: 0;
pointer-events: none;
transition: opacity 0.3s;
}
#success.show {
opacity: 1;
pointer-events: auto;
}
.success-card {
width: min(430px, calc(100vw - 32px));
padding: 30px;
border: 1px solid rgba(114, 240, 170, 0.35);
border-radius: 20px;
background: rgba(11, 26, 35, 0.96);
box-shadow: 0 30px 90px rgba(0, 0, 0, 0.48);
text-align: center;
}
.success-icon {
display: grid;
width: 66px;
height: 66px;
margin: 0 auto 17px;
place-items: center;
border-radius: 18px;
background: rgba(114, 240, 170, 0.13);
color: var(--green);
font-size: 33px;
}
.success-card h2 {
margin: 5px 0 8px;
font-size: 29px;
}
.success-card p {
margin: 0 0 22px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
#final-time {
display: block;
margin-bottom: 20px;
color: var(--gold);
font: 800 23px ui-monospace, SFMono-Regular, Menlo, monospace;
}
.primary-button {
width: 100%;
padding: 12px 18px;
border: 0;
border-radius: 10px;
background: var(--green);
color: #092119;
cursor: pointer;
font-weight: 900;
}
.primary-button:hover { filter: brightness(1.08); }
#loading {
position: fixed;
inset: 0;
z-index: 50;
display: grid;
place-items: center;
background: var(--bg);
color: var(--cyan);
font-size: 12px;
font-weight: 900;
letter-spacing: 0.18em;
}
#touch-controls {
position: fixed;
z-index: 10;
right: 14px;
bottom: 18px;
left: 14px;
display: none;
justify-content: space-between;
align-items: end;
pointer-events: none;
}
.dpad {
display: grid;
grid-template-columns: repeat(3, 48px);
grid-template-rows: repeat(2, 48px);
gap: 5px;
}
.touch-button {
border: 1px solid rgba(255, 255, 255, 0.18);
border-radius: 12px;
background: rgba(12, 25, 37, 0.8);
color: white;
pointer-events: auto;
touch-action: none;
user-select: none;
font-weight: 900;
}
.touch-button:active { background: rgba(102, 227, 255, 0.25); }
.touch-actions {
display: flex;
gap: 8px;
align-items: end;
}
.touch-actions .touch-button {
width: 60px;
height: 60px;
}
@media (pointer: coarse) {
#touch-controls { display: flex; }
#map-panel { display: none; }
.route-help { display: none; }
}
@media (max-width: 850px) {
.brand { width: 190px; }
.brand p { display: none; }
#objective {
left: auto;
right: 20px;
width: calc(100vw - 250px);
min-width: 0;
transform: none;
}
#timer { top: 112px; right: 20px; }
.route-help { display: none; }
#map-panel { width: 190px; }
}
@media (max-width: 600px) {
#hud { padding: 10px; }
.brand { top: 10px; left: 10px; width: 150px; padding: 11px; }
.brand h1 { font-size: 16px; }
#objective {
top: 87px;
right: 10px;
left: 10px;
width: auto;
padding: 11px 13px;
}
#objective-tip { display: none; }
#timer { top: 10px; right: 10px; min-width: 75px; padding: 9px; }
#timer-value { font-size: 16px; }
#map-panel { display: none; }
#prompt { bottom: 150px; }
}
</style>
</head>
<body>
<div id="game"></div>
<div id="hud">
<section class="brand panel">
<div class="eyebrow">Warehouse puzzle</div>
<h1>SHIFT // KEY</h1>
<p>Restore the cargo bridge, retrieve the key, and complete the delivery.</p>
</section>
<section id="objective" class="panel">
<div class="objective-row">
<div id="objective-number">01</div>
<div id="objective-text">Push the crate onto the floor switch</div>
</div>
<div id="objective-tip">Stand to the crate's right and hold A to push it left.</div>
<div class="steps">
<div class="step current" id="step-0"></div>
<div class="step" id="step-1"></div>
<div class="step" id="step-2"></div>
</div>
</section>
<section class="route-help panel">
<strong>Solvable route</strong>
<ol>
<li>Push the brown crate onto the green switch.</li>
<li>Cross the blue bridge and climb the west ramp.</li>
<li>Collect the gold key, then use the east dock ramp.</li>
</ol>
<div class="controls">
<span><kbd>WASD</kbd></span><span>Move / push</span>
<span><kbd>E</kbd></span><span>Collect / deliver</span>
<span><kbd>Space</kbd></span><span>Jump</span>
<span><kbd>R</kbd></span><span>Reset puzzle</span>
<span><kbd>Drag</kbd></span><span>Orbit camera</span>
</div>
</section>
<section id="timer" class="panel">
<div id="timer-label">Elapsed</div>
<div id="timer-value">00:00</div>
</section>
<section id="map-panel" class="panel">
<div id="map-title">
<span>Warehouse map</span>
<span>N ↑</span>
</div>
<canvas id="map" width="240" height="170"></canvas>
</section>
<div id="prompt"></div>
<div id="toast"></div>
</div>
<div id="touch-controls">
<div class="dpad">
<button class="touch-button" data-code="KeyW" style="grid-column:2">↑</button>
<button class="touch-button" data-code="KeyA" style="grid-column:1;grid-row:2">←</button>
<button class="touch-button" data-code="KeyS" style="grid-column:2;grid-row:2">↓</button>
<button class="touch-button" data-code="KeyD" style="grid-column:3;grid-row:2">→</button>
</div>
<div class="touch-actions">
<button class="touch-button" id="touch-jump">JUMP</button>
<button class="touch-button" id="touch-use">E</button>
</div>
</div>
<div id="success">
<div class="success-card">
<div class="success-icon">✓</div>
<div class="eyebrow">Delivery confirmed</div>
<h2>Shift complete</h2>
<p>The key was recovered and the warehouse cargo gate is now secured.</p>
<span id="final-time">00:00</span>
<button class="primary-button" id="play-again">Run it again</button>
</div>
</div>
<div id="loading">LOADING WAREHOUSE…</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 clamp = THREE.MathUtils.clamp;
// -----------------------------------------------------------------------
// Renderer and scene
// -----------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x08131e);
scene.fog = new THREE.Fog(0x08131e, 38, 72);
const camera = new THREE.OrthographicCamera(-20, 20, 12, -12, 0.1, 120);
camera.position.set(0, 24, 26);
camera.lookAt(0, 0, 0);
const renderer = new THREE.WebGLRenderer({ antialias: true, powerPreference: "high-performance" });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
$("game").appendChild(renderer.domElement);
scene.add(new THREE.HemisphereLight(0xbfe8ff, 0x24313a, 1.5));
const sun = new THREE.DirectionalLight(0xfff1d1, 3.1);
sun.position.set(8, 20, 11);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -21;
sun.shadow.camera.right = 21;
sun.shadow.camera.top = 18;
sun.shadow.camera.bottom = -18;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 55;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x49c8ff, 1.15);
rimLight.position.set(-12, 9, -10);
scene.add(rimLight);
// -----------------------------------------------------------------------
// Materials and geometry helpers
// -----------------------------------------------------------------------
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0.04,
flatShading: true,
...options
});
const M = {
floor: material(0x293744),
floorEdge: material(0x1d2933),
wall: material(0x243542),
wallTrim: material(0x4e6575),
dark: material(0x17232d),
black: material(0x0d151c),
steel: material(0x718794, { metalness: 0.35, roughness: 0.48 }),
cyan: material(0x54d9f6, { emissive: 0x0c6e85, emissiveIntensity: 0.85 }),
cyanDark: material(0x24566b),
green: material(0x60e7a1, { emissive: 0x155f3a, emissiveIntensity: 0.7 }),
gold: material(0xffc857, { emissive: 0x9a5700, emissiveIntensity: 0.55, metalness: 0.25 }),
orange: material(0xff8a3d),
red: material(0xff586a, { emissive: 0x781522, emissiveIntensity: 0.65 }),
wood: material(0xa96f3f),
woodLight: material(0xd29a5c),
woodDark: material(0x65412c),
ramp: material(0xb98239),
yellow: material(0xe3b44b),
white: material(0xe9f8ff)
};
function box(w, h, d, mat, x = 0, y = 0, z = 0, parent = scene) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), mat);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeRamp(xLow, xHigh, z0, z1, yLow, yHigh, mat) {
const a = [xLow, yLow, z0];
const b = [xHigh, yHigh, z0];
const c = [xHigh, yHigh, z1];
const d = [xLow, yLow, z1];
const e = [xLow, -0.28, z0];
const f = [xHigh, -0.28, z0];
const g = [xHigh, -0.28, z1];
const h = [xLow, -0.28, z1];
const vertices = [
...a, ...b, ...c,
...a, ...c, ...d,
...e, ...f, ...b,
...e, ...b, ...a,
...h, ...d, ...c,
...h, ...c, ...g,
...e, ...a, ...d,
...e, ...d, ...h,
...f, ...g, ...c,
...f, ...c, ...b,
...e, ...h, ...g,
...e, ...g, ...f
];
const geometry = new THREE.BufferGeometry();
geometry.setAttribute("position", new THREE.Float32BufferAttribute(vertices, 3));
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(geometry, mat);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
function makeLabel(text, color = "#bcecff", width = 3.4) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "rgba(7, 18, 27, 0.88)";
ctx.strokeStyle = color;
ctx.lineWidth = 5;
ctx.beginPath();
ctx.roundRect(8, 13, 496, 101, 18);
ctx.fill();
ctx.stroke();
ctx.fillStyle = color;
ctx.font = "800 37px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 256, 65);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
}));
sprite.scale.set(width, width * 0.25, 1);
sprite.renderOrder = 20;
return sprite;
}
function createCrate(size = 1.34, dynamic = false) {
const group = new THREE.Group();
box(size, size, size, M.wood, 0, size / 2, 0, group);
const edge = size * 0.075;
const inset = size * 0.5 - edge * 0.45;
for (const x of [-inset, inset]) {
for (const z of [-inset, inset]) {
box(edge, size * 1.02, edge, M.woodDark, x, size / 2, z, group);
}
}
for (const z of [-size * 0.5 - 0.025, size * 0.5 + 0.025]) {
const braceA = box(size * 1.02, size * 0.12, 0.075, M.woodLight, 0, size / 2, z, group);
braceA.rotation.z = Math.PI / 4;
const braceB = box(size * 1.02, size * 0.12, 0.075, M.woodLight, 0, size / 2, z, group);
braceB.rotation.z = -Math.PI / 4;
}
box(size * 0.42, 0.13, 0.09, M.woodLight, 0, size * 0.5, size * 0.5 + 0.06, group);
box(size * 0.13, size * 0.35, 0.09, M.woodLight, 0, size * 0.5, size * 0.5 + 0.065, group);
if (dynamic) {
box(0.18, 0.08, 0.08, M.cyan, 0, size * 0.52, size * 0.5 + 0.12, group);
}
return group;
}
function createShelf(cx, cz, width = 3, baseY = 0) {
const group = new THREE.Group();
group.position.set(cx, baseY, cz);
const depth = 1.05;
const height = 2.85;
for (const x of [-width / 2 + 0.09, width / 2 - 0.09]) {
box(0.18, height, depth, M.steel, x, height / 2, 0, group);
}
for (const y of [0.22, 1.45, 2.72]) {
box(width, 0.14, depth, M.steel, 0, y, 0, group);
}
const cargoColors = [M.wood, M.woodLight, M.cyanDark];
for (let i = 0; i < 3; i++) {
const x = (i - 1) * (width / 3.25);
box(width / 4.2, 0.78, 0.75, cargoColors[i], x, 0.68, 0, group);
box(width / 4.2, 0.63, 0.75, cargoColors[(i + 1) % 3], x, 1.89, 0, group);
}
scene.add(group);
addCollider(cx, cz, width, depth, baseY, baseY + height);
}
// -----------------------------------------------------------------------
// Collision data
// -----------------------------------------------------------------------
const staticColliders = [];
function addCollider(x, z, w, d, yMin = 0, yMax = 3) {
staticColliders.push({ x, z, w, d, yMin, yMax });
}
function rectOverlap(x, z, halfX, halfZ, rect) {
return (
Math.abs(x - rect.x) < halfX + rect.w * 0.5 &&
Math.abs(z - rect.z) < halfZ + rect.d * 0.5
);
}
function verticalOverlap(feet, height, rect) {
return feet + 0.04 < rect.yMax && feet + height > rect.yMin;
}
// -----------------------------------------------------------------------
// Warehouse floor, chasm, walls, platforms, ramps
// -----------------------------------------------------------------------
// Main floor slabs leave a gap from x=-5..-1 and z=2..6.
box(9, 0.5, 20, M.floor, -9.5, -0.25, 0);
box(15, 0.5, 20, M.floor, 6.5, -0.25, 0);
box(4, 0.5, 12, M.floor, -3, -0.25, -4);
box(4, 0.5, 4, M.floor, -3, -0.25, 8);
box(4.25, 0.25, 4.25, M.black, -3, -2.25, 4);
// Floor grid seams.
const gridMat = material(0x40515d, { transparent: true, opacity: 0.36 });
function gridRect(x0, x1, z0, z1) {
for (let x = x0; x <= x1; x += 2) {
box(0.025, 0.012, z1 - z0, gridMat, x, 0.008, (z0 + z1) / 2);
}
for (let z = z0; z <= z1; z += 2) {
box(x1 - x0, 0.012, 0.025, gridMat, (x0 + x1) / 2, 0.009, z);
}
}
gridRect(-14, -5, -10, 10);
gridRect(-1, 14, -10, 10);
gridRect(-5, -1, -10, 2);
gridRect(-5, -1, 6, 10);
// Boundary walls.
box(28.4, 4.5, 0.45, M.wall, 0, 2.25, -10.1);
box(0.45, 2.5, 20.4, M.wall, -14.1, 1.25, 0);
box(0.45, 2.5, 20.4, M.wall, 14.1, 1.25, 0);
box(28.4, 1.0, 0.45, M.wall, 0, 0.5, 10.1);
box(28.4, 0.16, 0.52, M.wallTrim, 0, 4.55, -10.1);
box(0.52, 0.16, 20.4, M.wallTrim, -14.1, 2.55, 0);
box(0.52, 0.16, 20.4, M.wallTrim, 14.1, 2.55, 0);
box(28.4, 0.12, 0.52, M.yellow, 0, 1.05, 10.1);
// Visible boundary/collision limits.
addCollider(-14, 0, 0.45, 20.5, 0, 5);
addCollider(14, 0, 0.45, 20.5, 0, 5);
addCollider(0, -10, 28.5, 0.45, 0, 5);
addCollider(0, 10, 28.5, 0.45, 0, 5);
// Hazard rails around the chasm.
for (const z of [2, 6]) {
box(4.3, 0.16, 0.13, M.yellow, -3, 0.12, z);
for (let x = -4.6; x <= -1; x += 0.55) {
const stripe = box(0.26, 0.17, 0.14, M.dark, x, 0.13, z);
stripe.rotation.y = 0.45;
}
}
box(0.13, 0.18, 4, M.yellow, -1, 0.13, 4);
box(0.13, 0.18, 4, M.yellow, -5, 0.13, 4);
// West mezzanine and its ramp.
box(5, 3, 16, M.floorEdge, -11.5, 1.5, 0);
box(5.05, 0.12, 16.05, M.wallTrim, -11.5, 3.04, 0);
makeRamp(-5, -9, 1, 5, 0, 3, M.ramp);
// Ramp edge markers.
for (const z of [1, 5]) {
for (let x = -5.5; x > -9; x -= 0.8) {
const marker = box(0.44, 0.035, 0.11, M.yellow, x, 0.02, z);
marker.rotation.y = 0.52;
}
}
// Delivery dock and ramp.
box(3, 1.5, 4, M.floorEdge, 11.5, 0.75, -6);
box(3.05, 0.1, 4.05, M.wallTrim, 11.5, 1.54, -6);
makeRamp(7, 10, -7, -5, 0, 1.5, M.ramp);
for (const z of [-7, -5]) {
for (let x = 7.5; x < 10; x += 0.75) {
const marker = box(0.4, 0.035, 0.1, M.red, x, 0.02, z);
marker.rotation.y = -0.5;
}
}
// Shelves and fixed crate stacks, all positioned away from the key route.
createShelf(9, 1.2, 3.2);
createShelf(-0.5, -6.5, 3.0);
createShelf(-12, -6, 2.8, 3);
createShelf(11.7, 7.6, 2.8);
function addCrateStack(x, z, size = 1.4, baseY = 0) {
const crate = createCrate(size);
crate.position.set(x, baseY, z);
scene.add(crate);
addCollider(x, z, size, size, baseY, baseY + size);
}
addCrateStack(3, -1.5, 1.45);
addCrateStack(11.8, 5.7, 1.5);
addCrateStack(-12.1, -3.8, 1.45, 3);
addCrateStack(-10.2, -0.4, 1.35, 3);
// Floor switch.
const plateGroup = new THREE.Group();
plateGroup.position.set(1, 0, 4);
scene.add(plateGroup);
box(1.55, 0.12, 1.55, M.dark, 0, 0.06, 0, plateGroup);
const plateTop = box(1.18, 0.12, 1.18, M.green, 0, 0.13, 0, plateGroup);
for (const x of [-0.67, 0.67]) {
box(0.08, 0.09, 1.5, M.cyan, x, 0.19, 0, plateGroup);
}
for (const z of [-0.67, 0.67]) {
box(1.5, 0.09, 0.08, M.cyan, 0, 0.19, z, plateGroup);
}
// Deployable bridge.
const bridge = new THREE.Group();
bridge.visible = false;
bridge.position.y = -1.5;
scene.add(bridge);
for (let i = 0; i < 9; i++) {
box(0.43, 0.22, 2.0, i % 2 ? M.cyanDark : M.steel, -4.78 + i * 0.47, -0.11, 4, bridge);
}
for (const z of [3.02, 4.98]) {
box(4.15, 0.12, 0.12, M.cyan, -3, 0.05, z, bridge);
}
// -----------------------------------------------------------------------
// Key and delivery station
// -----------------------------------------------------------------------
function createKey(scale = 1) {
const group = new THREE.Group();
box(0.16, 0.75, 0.16, M.gold, 0, -0.22, 0, group);
box(0.55, 0.16, 0.16, M.gold, 0, 0.27, 0, group);
box(0.16, 0.37, 0.16, M.gold, -0.25, 0.25, 0, group);
box(0.16, 0.37, 0.16, M.gold, 0.25, 0.25, 0, group);
box(0.16, 0.16, 0.16, M.gold, -0.25, 0.42, 0, group);
box(0.16, 0.16, 0.16, M.gold, 0.25, 0.42, 0, group);
box(0.25, 0.16, 0.16, M.gold, 0.13, -0.48, 0, group);
group.scale.setScalar(scale);
return group;
}
const key = createKey(1);
key.position.set(-11, 3.72, 5);
scene.add(key);
const keyLabel = makeLabel("KEY CACHE", "#ffd166", 3.2);
keyLabel.position.set(-11, 5.15, 5);
scene.add(keyLabel);
const plateLabel = makeLabel("FLOOR SWITCH", "#72f0aa", 3.6);
plateLabel.position.set(1, 1.25, 4);
scene.add(plateLabel);
const bridgeLabel = makeLabel("POWER BRIDGE", "#66e3ff", 3.5);
bridgeLabel.position.set(-3, 1.2, 4);
bridgeLabel.visible = false;
scene.add(bridgeLabel);
const station = new THREE.Group();
station.position.set(11.5, 1.5, -6);
scene.add(station);
box(1.6, 0.25, 1.3, M.dark, 0, 0.125, 0, station);
box(0.22, 2.35, 0.22, M.steel, -0.72, 1.3, 0, station);
box(0.22, 2.35, 0.22, M.steel, 0.72, 1.3, 0, station);
box(1.65, 0.22, 0.22, M.steel, 0, 2.43, 0, station);
const stationConsole = box(0.92, 0.78, 0.58, M.red, 0, 0.7, 0, station);
const stationLight = box(0.62, 0.18, 0.61, M.red, 0, 1.25, 0, station);
box(0.28, 0.035, 0.04, M.gold, 0, 0.83, 0.31, station);
box(0.17, 0.035, 0.04, M.gold, 0, 0.64, 0.31, station);
const deliveryLabel = makeLabel("DELIVERY", "#ff6978", 3.2);
deliveryLabel.position.set(11.5, 4.8, -6);
scene.add(deliveryLabel);
// -----------------------------------------------------------------------
// Player
// -----------------------------------------------------------------------
const playerRoot = new THREE.Group();
scene.add(playerRoot);
const playerBody = box(0.72, 0.78, 0.48, M.orange, 0, 1.04, 0, playerRoot);
box(0.57, 0.52, 0.53, M.white, 0, 1.68, 0, playerRoot);
box(0.47, 0.17, 0.035, M.cyan, 0, 1.72, 0.282, playerRoot);
box(0.76, 0.1, 0.54, M.dark, 0, 1.29, 0, playerRoot);
const leftLeg = box(0.25, 0.55, 0.28, M.dark, -0.2, 0.35, 0, playerRoot);
const rightLeg = box(0.25, 0.55, 0.28, M.dark, 0.2, 0.35, 0, playerRoot);
const leftArm = box(0.2, 0.65, 0.24, M.orange, -0.49, 1.02, 0, playerRoot);
const rightArm = box(0.2, 0.65, 0.24, M.orange, 0.49, 1.02, 0, playerRoot);
const carriedKey = createKey(0.48);
carriedKey.position.set(0.47, 1.0, 0.12);
carriedKey.rotation.z = -0.25;
carriedKey.visible = false;
playerRoot.add(carriedKey);
// -----------------------------------------------------------------------
// Game state
// -----------------------------------------------------------------------
const keys = new Set();
const state = {
hasKey: false,
delivered: false,
bridgeActive: false,
elapsed: 0,
jumpQueued: false,
toastTime: 0
};
const player = {
pos: new THREE.Vector3(8, 0, 7),
velY: 0,
grounded: true,
facing: Math.PI,
walkTime: 0,
moving: false
};
const crateHalf = 0.67;
const crate = {
pos: new THREE.Vector3(4, 0, 4),
mesh: createCrate(1.34, true)
};
crate.mesh.position.copy(crate.pos);
scene.add(crate.mesh);
let cameraYaw = 0;
let cameraZoom = 1.1;
let dragging = false;
let lastPointerX = 0;
let bridgeLift = 0;
let bridgeTarget = 0;
let audioContext = null;
const particles = [];
// -----------------------------------------------------------------------
// Height and collision functions
// -----------------------------------------------------------------------
function isGap(x, z) {
return x > -5 && x < -1 && z > 2 && z < 6;
}
function groundAt(x, z) {
return isGap(x, z) ? null : 0;
}
function surfaceAt(x, z) {
// West mezzanine.
if (x >= -14 && x <= -9 && z >= -8 && z <= 8) return 3;
// West ramp: low at x=-5, high at x=-9.
if (x >= -9 && x <= -5 && z >= 1 && z <= 5) {
return clamp((-5 - x) * 0.75, 0, 3);
}
// Delivery dock.
if (x >= 10 && x <= 13 && z >= -8 && z <= -4) return 1.5;
// Delivery ramp: low at x=7, high at x=10.
if (x >= 7 && x <= 10 && z >= -7 && z <= -5) {
return clamp((x - 7) * 0.5, 0, 1.5);
}
// Activated bridge.
if (state.bridgeActive && x >= -5 && x <= -1 && z >= 3 && z <= 5) {
return 0;
}
return groundAt(x, z);
}
function canPlaceCrate(x, z) {
const corners = [
[x - crateHalf * 0.85, z - crateHalf * 0.85],
[x + crateHalf * 0.85, z - crateHalf * 0.85],
[x - crateHalf * 0.85, z + crateHalf * 0.85],
[x + crateHalf * 0.85, z + crateHalf * 0.85]
];
for (const [cx, cz] of corners) {
if (groundAt(cx, cz) === null) return false;
}
for (const rect of staticColliders) {
if (rectOverlap(x, z, crateHalf, crateHalf, rect) && rect.yMin < 1.34 && rect.yMax > 0) {
return false;
}
}
return true;
}
function movePlayerAxis(axis, amount) {
if (Math.abs(amount) < 0.000001) return;
const proposed = player.pos.clone();
proposed[axis] += amount;
const support = surfaceAt(proposed.x, proposed.z);
if (support === null) return;
// Prevent walking up vertical sides, but allow smooth ramp slopes.
if (player.grounded && support > player.pos.y + 0.46) return;
const playerHalf = 0.34;
const playerHeight = 1.86;
for (const rect of staticColliders) {
if (
rectOverlap(proposed.x, proposed.z, playerHalf, playerHalf, rect) &&
verticalOverlap(player.pos.y, playerHeight, rect)
) {
return;
}
}
// Push interaction with the movable crate.
const overlapsCrate =
player.pos.y < 1.3 &&
player.pos.y + playerHeight > 0 &&
rectOverlap(proposed.x, proposed.z, playerHalf, playerHalf, {
x: crate.pos.x,
z: crate.pos.z,
w: crateHalf * 2,
d: crateHalf * 2
});
if (overlapsCrate) {
const destination = crate.pos.clone();
destination[axis] += amount;
if (!canPlaceCrate(destination.x, destination.z)) return;
crate.pos.copy(destination);
crate.mesh.position.copy(crate.pos);
// Leave the player just behind the moved crate.
if (axis === "x") {
player.pos.x = amount > 0
? crate.pos.x - crateHalf - playerHalf - 0.006
: crate.pos.x + crateHalf + playerHalf + 0.006;
} else {
player.pos.z = amount > 0
? crate.pos.z - crateHalf - playerHalf - 0.006
: crate.pos.z + crateHalf + playerHalf + 0.006;
}
return;
}
player.pos[axis] = proposed[axis];
}
// -----------------------------------------------------------------------
// Audio and UI helpers
// -----------------------------------------------------------------------
function initAudio() {
if (!audioContext) {
const AudioCtor = window.AudioContext || window.webkitAudioContext;
if (AudioCtor) audioContext = new AudioCtor();
}
if (audioContext?.state === "suspended") audioContext.resume();
}
function tone(frequency, duration = 0.12, type = "sine", volume = 0.05) {
if (!audioContext) return;
const oscillator = audioContext.createOscillator();
const gain = audioContext.createGain();
oscillator.type = type;
oscillator.frequency.value = frequency;
gain.gain.setValueAtTime(volume, audioContext.currentTime);
gain.gain.exponentialRampToValueAtTime(0.0001, audioContext.currentTime + duration);
oscillator.connect(gain);
gain.connect(audioContext.destination);
oscillator.start();
oscillator.stop(audioContext.currentTime + duration);
}
function showToast(message, duration = 2.6) {
$("toast").textContent = message;
$("toast").classList.add("show");
state.toastTime = duration;
}
function updateObjective() {
let number, text, tip, current;
if (state.delivered) {
number = "✓";
text = "Delivery complete";
tip = "The key has been delivered and the cargo gate is secure.";
current = 2;
} else if (state.hasKey) {
number = "03";
text = "Deliver the key to the red dock";
tip = "Descend the west ramp, follow the floor east, then climb the delivery ramp.";
current = 2;
} else if (state.bridgeActive) {
number = "02";
text = "Cross the bridge and retrieve the key";
tip = "Cross west over the blue bridge, then climb the yellow ramp.";
current = 1;
} else {
number = "01";
text = "Push the crate onto the floor switch";
tip = "Stand to the crate's right and hold A to push it left.";
current = 0;
}
$("objective-number").textContent = number;
$("objective-text").textContent = text;
$("objective-tip").textContent = tip;
for (let i = 0; i < 3; i++) {
$("step-" + i).classList.toggle("done", i < current);
$("step-" + i).classList.toggle("current", i === current);
}
}
function formatTime(seconds) {
const mins = Math.floor(seconds / 60).toString().padStart(2, "0");
const secs = Math.floor(seconds % 60).toString().padStart(2, "0");
return `${mins}:${secs}`;
}
// -----------------------------------------------------------------------
// Puzzle interactions
// -----------------------------------------------------------------------
function activateBridge() {
if (state.bridgeActive) return;
state.bridgeActive = true;
bridgeTarget = 1;
bridge.visible = true;
bridgeLabel.visible = true;
plateTop.material.color.setHex(0x72f0aa);
plateTop.material.emissive.setHex(0x1a8b50);
tone(280, 0.12, "square", 0.035);
setTimeout(() => tone(520, 0.18, "triangle", 0.045), 120);
showToast("Floor switch activated — the cargo bridge is deploying.");
updateObjective();
}
function checkPlate() {
if (
Math.abs(crate.pos.x - 1) < 0.68 &&
Math.abs(crate.pos.z - 4) < 0.68
) {
activateBridge();
}
}
function spawnConfetti() {
const colors = [M.cyan, M.green, M.gold, M.orange, M.white];
for (let i = 0; i < 44; i++) {
const mesh = box(
0.09 + Math.random() * 0.1,
0.05 + Math.random() * 0.1,
0.04 + Math.random() * 0.06,
colors[i % colors.length],
11.5 + (Math.random() - 0.5) * 1.2,
3.2 + Math.random() * 1.5,
-6 + (Math.random() - 0.5) * 0.8
);
particles.push({
mesh,
velocity: new THREE.Vector3(
(Math.random() - 0.5) * 4.8,
3.5 + Math.random() * 5,
(Math.random() - 0.5) * 4.8
),
life: 2.4 + Math.random() * 1.2
});
}
}
function deliverKey() {
if (state.delivered) return;
state.delivered = true;
stationConsole.material.color.setHex(0x72f0aa);
stationConsole.material.emissive.setHex(0x17653c);
stationLight.material.color.setHex(0x72f0aa);
stationLight.material.emissive.setHex(0x17653c);
carriedKey.visible = false;
spawnConfetti();
tone(520, 0.16, "triangle", 0.055);
setTimeout(() => tone(660, 0.18, "triangle", 0.05), 130);
setTimeout(() => tone(880, 0.25, "triangle", 0.055), 270);
$("final-time").textContent = formatTime(state.elapsed);
$("success").classList.add("show");
updateObjective();
}
function interact() {
initAudio();
const keyDistance = Math.hypot(player.pos.x - (-11), player.pos.z - 5);
const stationDistance = Math.hypot(player.pos.x - 11.5, player.pos.z - (-6));
if (!state.hasKey && keyDistance < 1.25 && Math.abs(player.pos.y - 3) < 0.7) {
state.hasKey = true;
key.visible = false;
keyLabel.visible = false;
carriedKey.visible = true;
tone(760, 0.18, "triangle", 0.05);
showToast("Key acquired. Return to the red delivery dock.");
updateObjective();
return;
}
if (state.hasKey && stationDistance < 1.25 && Math.abs(player.pos.y - 1.5) < 0.7) {
deliverKey();
return;
}
if (stationDistance < 1.25) {
showToast("The delivery console is locked. Find the gold key first.");
tone(150, 0.15, "square", 0.025);
}
}
function resetPuzzle() {
keys.clear();
state.hasKey = false;
state.delivered = false;
state.bridgeActive = false;
state.elapsed = 0;
state.jumpQueued = false;
player.pos.set(8, 0, 7);
player.velY = 0;
player.grounded = true;
player.facing = Math.PI;
player.walkTime = 0;
crate.pos.set(4, 0, 4);
crate.mesh.position.copy(crate.pos);
bridge.visible = false;
bridgeLabel.visible = false;
bridgeLift = 0;
bridgeTarget = 0;
key.visible = true;
keyLabel.visible = true;
carriedKey.visible = false;
plateTop.material.color.setHex(0x60e7a1);
plateTop.material.emissive.setHex(0x155f3a);
stationConsole.material.color.setHex(0xff586a);
stationConsole.material.emissive.setHex(0x781522);
stationLight.material.color.setHex(0xff586a);
stationLight.material.emissive.setHex(0x781522);
for (const particle of particles) scene.remove(particle.mesh);
particles.length = 0;
$("success").classList.remove("show");
$("toast").classList.remove("show");
updateObjective();
}
// -----------------------------------------------------------------------
// Input and direct camera controls
// -----------------------------------------------------------------------
const movementCodes = new Set([
"KeyW", "KeyA", "KeyS", "KeyD",
"ArrowUp", "ArrowLeft", "ArrowDown", "ArrowRight",
"Space"
]);
window.addEventListener("keydown", event => {
if (movementCodes.has(event.code)) event.preventDefault();
keys.add(event.code);
if (event.code === "Space" && !event.repeat) state.jumpQueued = true;
if (event.code === "KeyE" && !event.repeat) interact();
if (event.code === "KeyR" && !event.repeat) resetPuzzle();
if (event.code === "KeyC" && !event.repeat) {
cameraYaw = 0;
cameraZoom = 1.1;
}
});
window.addEventListener("keyup", event => keys.delete(event.code));
window.addEventListener("blur", () => keys.clear());
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
lastPointerX = event.clientX;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const delta = event.clientX - lastPointerX;
cameraYaw -= delta * 0.006;
lastPointerX = event.clientX;
});
renderer.domElement.addEventListener("pointerup", event => {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
cameraZoom = clamp(cameraZoom * (event.deltaY > 0 ? 0.92 : 1.08), 0.72, 1.75);
}, { passive: false });
document.querySelectorAll("[data-code]").forEach(button => {
const code = button.dataset.code;
button.addEventListener("pointerdown", event => {
event.preventDefault();
keys.add(code);
button.setPointerCapture(event.pointerId);
});
const release = () => keys.delete(code);
button.addEventListener("pointerup", release);
button.addEventListener("pointercancel", release);
button.addEventListener("lostpointercapture", release);
});
$("touch-jump").addEventListener("pointerdown", event => {
event.preventDefault();
state.jumpQueued = true;
});
$("touch-use").addEventListener("pointerdown", event => {
event.preventDefault();
interact();
});
$("play-again").addEventListener("click", resetPuzzle);
// -----------------------------------------------------------------------
// Minimap
// -----------------------------------------------------------------------
const mapCanvas = $("map");
const mapContext = mapCanvas.getContext("2d");
function mapX(x) { return 12 + (x + 14) * (216 / 28); }
function mapY(z) { return 12 + (z + 10) * (146 / 20); }
function drawMap() {
const ctx = mapContext;
ctx.clearRect(0, 0, 240, 170);
ctx.fillStyle = "#07111a";
ctx.fillRect(0, 0, 240, 170);
// Floor.
ctx.fillStyle = "#263744";
ctx.fillRect(mapX(-14), mapY(-10), mapX(-5) - mapX(-14), mapY(10) - mapY(-10));
ctx.fillRect(mapX(-1), mapY(-10), mapX(14) - mapX(-1), mapY(10) - mapY(-10));
ctx.fillRect(mapX(-5), mapY(-10), mapX(-1) - mapX(-5), mapY(2) - mapY(-10));
ctx.fillRect(mapX(-5), mapY(6), mapX(-1) - mapX(-5), mapY(10) - mapY(6));
// Gap.
ctx.fillStyle = "#05090d";
ctx.fillRect(mapX(-5), mapY(2), mapX(-1) - mapX(-5), mapY(6) - mapY(2));
// Bridge.
ctx.fillStyle = state.bridgeActive ? "#54d9f6" : "#344751";
ctx.fillRect(mapX(-5), mapY(3), mapX(-1) - mapX(-5), mapY(5) - mapY(3));
// Platform and ramps.
ctx.fillStyle = "#3c4c58";
ctx.fillRect(mapX(-14), mapY(-8), mapX(-9) - mapX(-14), mapY(8) - mapY(-8));
ctx.fillStyle = "#b98239";
ctx.fillRect(mapX(-9), mapY(1), mapX(-5) - mapX(-9), mapY(5) - mapY(1));
ctx.fillStyle = "#3c4c58";
ctx.fillRect(mapX(10), mapY(-8), mapX(13) - mapX(10), mapY(-4) - mapY(-8));
ctx.fillStyle = "#b98239";
ctx.fillRect(mapX(7), mapY(-7), mapX(10) - mapX(7), mapY(-5) - mapY(-7));
// Suggested route.
const route = state.hasKey
? [[-11, 5], [-7, 4], [5, 4], [5, -6], [11.5, -6]]
: [[8, 7], [4, 4], [1, 4], [-3, 4], [-7, 4], [-11, 5]];
ctx.setLineDash([3, 3]);
ctx.strokeStyle = "rgba(255,255,255,0.26)";
ctx.lineWidth = 1;
ctx.beginPath();
route.forEach(([x, z], index) => {
if (index === 0) ctx.moveTo(mapX(x), mapY(z));
else ctx.lineTo(mapX(x), mapY(z));
});
ctx.stroke();
ctx.setLineDash([]);
// Plate.
ctx.fillStyle = state.bridgeActive ? "#72f0aa" : "#397a58";
ctx.fillRect(mapX(1) - 3, mapY(4) - 3, 6, 6);
// Crate.
ctx.fillStyle = "#d29a5c";
ctx.fillRect(mapX(crate.pos.x) - 3, mapY(crate.pos.z) - 3, 6, 6);
// Key.
if (!state.hasKey) {
ctx.fillStyle = "#ffd166";
ctx.beginPath();
ctx.arc(mapX(-11), mapY(5), 3.3, 0, Math.PI * 2);
ctx.fill();
}
// Station.
ctx.fillStyle = state.delivered ? "#72f0aa" : "#ff6978";
ctx.fillRect(mapX(11.5) - 3.5, mapY(-6) - 3.5, 7, 7);
// Player and facing arrow.
const px = mapX(player.pos.x);
const py = mapY(player.pos.z);
ctx.save();
ctx.translate(px, py);
ctx.rotate(-player.facing + Math.PI);
ctx.fillStyle = "#ff8a3d";
ctx.beginPath();
ctx.moveTo(0, -5);
ctx.lineTo(3.6, 4);
ctx.lineTo(0, 2);
ctx.lineTo(-3.6, 4);
ctx.closePath();
ctx.fill();
ctx.restore();
ctx.strokeStyle = "rgba(255,255,255,0.13)";
ctx.strokeRect(0.5, 0.5, 239, 169);
}
// -----------------------------------------------------------------------
// Main update loop
// -----------------------------------------------------------------------
const clock = new THREE.Clock();
let walkSoundTime = 0;
function update(delta) {
if (!state.delivered) state.elapsed += delta;
const forward =
(keys.has("KeyW") || keys.has("ArrowUp") ? 1 : 0) -
(keys.has("KeyS") || keys.has("ArrowDown") ? 1 : 0);
const sideways =
(keys.has("KeyD") || keys.has("ArrowRight") ? 1 : 0) -
(keys.has("KeyA") || keys.has("ArrowLeft") ? 1 : 0);
const forwardX = -Math.sin(cameraYaw);
const forwardZ = -Math.cos(cameraYaw);
const rightX = Math.cos(cameraYaw);
const rightZ = -Math.sin(cameraYaw);
const movement = new THREE.Vector3(
forwardX * forward + rightX * sideways,
0,
forwardZ * forward + rightZ * sideways
);
const hasMovement = movement.lengthSq() > 0;
if (hasMovement) movement.normalize();
const sprint = keys.has("ShiftLeft") || keys.has("ShiftRight");
const speed = sprint ? 7.2 : 5.2;
if (hasMovement) {
movePlayerAxis("x", movement.x * speed * delta);
movePlayerAxis("z", movement.z * speed * delta);
const targetFacing = Math.atan2(movement.x, movement.z);
const difference = Math.atan2(
Math.sin(targetFacing - player.facing),
Math.cos(targetFacing - player.facing)
);
player.facing += difference * Math.min(1, delta * 13);
}
player.moving = hasMovement;
if (state.jumpQueued && player.grounded) {
player.velY = 6.8;
player.grounded = false;
tone(230, 0.08, "triangle", 0.018);
}
state.jumpQueued = false;
const support = surfaceAt(player.pos.x, player.pos.z);
if (player.grounded) {
if (support !== null && Math.abs(player.pos.y - support) < 0.5) {
player.pos.y = support;
} else {
player.grounded = false;
player.velY = 0;
}
}
if (!player.grounded) {
player.velY -= 18 * delta;
player.pos.y += player.velY * delta;
if (support !== null && player.pos.y <= support) {
player.pos.y = support;
player.velY = 0;
player.grounded = true;
}
}
checkPlate();
// Player animation.
playerRoot.position.copy(player.pos);
playerRoot.rotation.y = player.facing;
if (player.moving && player.grounded) {
player.walkTime += delta * 10;
const swing = Math.sin(player.walkTime) * 0.55;
leftLeg.rotation.x = swing;
rightLeg.rotation.x = -swing;
leftArm.rotation.x = -swing * 0.65;
rightArm.rotation.x = swing * 0.65;
playerRoot.position.y += Math.abs(Math.sin(player.walkTime)) * 0.035;
walkSoundTime -= delta;
if (walkSoundTime <= 0) {
walkSoundTime = 0.28;
tone(105 + Math.random() * 20, 0.035, "triangle", 0.009);
}
} else {
leftLeg.rotation.x *= 0.8;
rightLeg.rotation.x *= 0.8;
leftArm.rotation.x *= 0.8;
rightArm.rotation.x *= 0.8;
}
// Floating key and switch pulse.
key.position.y = 3.72 + Math.sin(performance.now() * 0.003) * 0.12;
key.rotation.y += delta * 1.7;
carriedKey.rotation.y -= delta * 2.1;
plateGroup.position.y = Math.sin(performance.now() * 0.004) * 0.015;
stationLight.scale.y = 1 + Math.sin(performance.now() * 0.006) * 0.12;
// Bridge deployment animation.
bridgeLift += (bridgeTarget - bridgeLift) * Math.min(1, delta * 4);
bridge.position.y = -1.5 * (1 - bridgeLift);
// Confetti.
for (let i = particles.length - 1; i >= 0; i--) {
const particle = particles[i];
particle.life -= delta;
particle.velocity.y -= 9 * delta;
particle.mesh.position.addScaledVector(particle.velocity, delta);
particle.mesh.rotation.x += delta * 5;
particle.mesh.rotation.z += delta * 4;
if (particle.life <= 0) {
scene.remove(particle.mesh);
particles.splice(i, 1);
}
}
// Context prompt.
const keyDistance = Math.hypot(player.pos.x - (-11), player.pos.z - 5);
const stationDistance = Math.hypot(player.pos.x - 11.5, player.pos.z - (-6));
let prompt = "";
if (!state.hasKey && keyDistance < 1.25 && Math.abs(player.pos.y - 3) < 0.7) {
prompt = "E • Collect gold key";
} else if (state.hasKey && stationDistance < 1.25 && Math.abs(player.pos.y - 1.5) < 0.7) {
prompt = "E • Deliver key";
} else if (!state.hasKey && stationDistance < 1.25 && Math.abs(player.pos.y - 1.5) < 0.7) {
prompt = "Console locked • Find the gold key";
}
$("prompt").textContent = prompt;
$("prompt").classList.toggle("show", Boolean(prompt));
if (state.toastTime > 0) {
state.toastTime -= delta;
if (state.toastTime <= 0) $("toast").classList.remove("show");
}
$("timer-value").textContent = formatTime(state.elapsed);
drawMap();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
const halfHeight = Math.max(11.7 / cameraZoom, 16 / (aspect * cameraZoom));
camera.left = -halfHeight * aspect;
camera.right = halfHeight * aspect;
camera.top = halfHeight;
camera.bottom = -halfHeight;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.04);
camera.position.set(
Math.sin(cameraYaw) * 26,
24,
Math.cos(cameraYaw) * 26
);
camera.lookAt(0, 0.4, 0);
update(delta);
renderer.render(scene, camera);
}
updateObjective();
resize();
animate();
$("loading").style.display = "none";
</script>
</body>
</html>
```