Prompt specification
Build or customize the Three.js 3D scene "Warehouse Portal Delivery Room" 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 Portal Delivery Room
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/warehouse-portal-delivery-room-x3483
Scene Description Prompt:
Build a voxel warehouse puzzle with paired portal doors and cargo targets; preview exit orientation before traversal. One HTML file using Three.js from cdnjs.
Working Three.js Starter (warehouse-portal-delivery-room-x3483):
```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>PORT//SHIFT — Voxel Warehouse</title>
<style>
:root {
--bg: #071015;
--panel: rgba(8, 18, 24, .88);
--panel-light: rgba(16, 32, 40, .94);
--line: rgba(180, 230, 240, .18);
--text: #eaf8f7;
--muted: #8da8ad;
--acid: #d8ff4f;
--cyan: #42d9ff;
--amber: #ffb84a;
--violet: #bd79ff;
--danger: #ff6474;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button { font: inherit; }
#app, #scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
}
#scene {
display: block;
touch-action: none;
cursor: grab;
}
#scene:active { cursor: grabbing; }
.hud {
position: fixed;
inset: 0;
z-index: 5;
pointer-events: none;
}
.topbar {
position: absolute;
top: 22px;
left: 24px;
right: 24px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 20px;
}
.brand {
display: flex;
align-items: center;
gap: 13px;
filter: drop-shadow(0 5px 14px rgba(0,0,0,.45));
}
.brand-mark {
width: 42px;
height: 42px;
border: 2px solid var(--acid);
position: relative;
transform: rotate(45deg);
box-shadow: inset 0 0 0 5px rgba(216,255,79,.08), 0 0 22px rgba(216,255,79,.12);
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
background: var(--acid);
}
.brand-mark::before {
width: 16px;
height: 2px;
left: 11px;
top: 18px;
}
.brand-mark::after {
width: 2px;
height: 16px;
left: 18px;
top: 11px;
}
.brand h1 {
margin: 0;
font-size: 20px;
line-height: 1;
letter-spacing: .16em;
font-weight: 850;
}
.brand p {
margin: 7px 0 0;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: .21em;
}
.mission {
pointer-events: auto;
width: 280px;
background: var(--panel);
border: 1px solid var(--line);
border-radius: 14px;
box-shadow: 0 18px 55px rgba(0,0,0,.32);
backdrop-filter: blur(18px);
overflow: hidden;
}
.mission-head {
display: flex;
justify-content: space-between;
align-items: center;
padding: 15px 16px 12px;
border-bottom: 1px solid var(--line);
}
.eyebrow {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: .2em;
text-transform: uppercase;
}
#objectiveCount {
color: var(--acid);
font-size: 17px;
font-weight: 900;
letter-spacing: .05em;
}
.cargo-list {
padding: 8px 12px 10px;
display: grid;
gap: 3px;
}
.cargo-row {
display: flex;
align-items: center;
gap: 10px;
min-height: 31px;
padding: 5px 4px;
color: #a7bdc1;
font-size: 11px;
letter-spacing: .08em;
transition: .25s ease;
}
.cargo-row.done { color: var(--text); }
.cargo-dot {
width: 10px;
height: 10px;
border-radius: 2px;
background: var(--cargo);
box-shadow: 0 0 12px color-mix(in srgb, var(--cargo) 60%, transparent);
}
.cargo-row .status {
margin-left: auto;
color: var(--muted);
font-size: 9px;
font-weight: 800;
}
.cargo-row.done .status { color: var(--acid); }
.mini-wrap {
border-top: 1px solid var(--line);
padding: 11px 14px 13px;
background: rgba(0,0,0,.13);
}
.mini-title {
display: flex;
justify-content: space-between;
margin-bottom: 8px;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: .15em;
}
#minimap {
display: grid;
grid-template-columns: repeat(13, 1fr);
gap: 2px;
aspect-ratio: 13 / 11;
}
.mini-cell {
position: relative;
min-width: 0;
border-radius: 1px;
background: #13252b;
overflow: hidden;
}
.mini-cell.wall { background: #26383d; }
.mini-cell.block { background: #46565a; }
.mini-cell.portal { background: var(--portal); box-shadow: 0 0 7px var(--portal); }
.mini-cell.target::after,
.mini-cell.crate::after,
.mini-cell.player::after {
content: "";
position: absolute;
inset: 25%;
border-radius: 2px;
}
.mini-cell.target::after { border: 1px solid var(--target); background: color-mix(in srgb, var(--target) 25%, transparent); }
.mini-cell.crate::after { inset: 18%; background: var(--crate); }
.mini-cell.player::after { inset: 10%; background: white; border-radius: 50%; box-shadow: 0 0 5px white; }
.bottom-left {
position: absolute;
left: 24px;
bottom: 22px;
display: flex;
flex-direction: column;
gap: 10px;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 7px;
max-width: 510px;
}
.key {
display: flex;
align-items: center;
gap: 7px;
padding: 7px 9px;
border: 1px solid var(--line);
border-radius: 7px;
background: rgba(7,16,21,.82);
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: .06em;
backdrop-filter: blur(12px);
}
kbd {
min-width: 21px;
height: 21px;
padding: 0 5px;
display: inline-grid;
place-items: center;
border: 1px solid rgba(255,255,255,.18);
border-bottom-color: rgba(255,255,255,.35);
border-radius: 4px;
background: #17272d;
color: white;
font: 800 9px/1 inherit;
box-shadow: 0 2px 0 #020608;
}
.buttons {
display: flex;
gap: 7px;
pointer-events: auto;
}
.action {
height: 35px;
padding: 0 13px;
border: 1px solid var(--line);
border-radius: 7px;
background: var(--panel);
color: var(--text);
cursor: pointer;
font-size: 9px;
font-weight: 850;
letter-spacing: .12em;
transition: .18s ease;
}
.action:hover { border-color: rgba(216,255,79,.55); color: var(--acid); transform: translateY(-1px); }
.action:active { transform: translateY(1px); }
.stats {
position: absolute;
left: 50%;
bottom: 23px;
transform: translateX(-50%);
display: flex;
gap: 1px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 9px;
background: var(--panel);
box-shadow: 0 12px 35px rgba(0,0,0,.3);
backdrop-filter: blur(14px);
}
.stat {
min-width: 78px;
padding: 10px 14px;
text-align: center;
border-right: 1px solid var(--line);
}
.stat:last-child { border-right: 0; }
.stat span { display: block; color: var(--muted); font-size: 8px; letter-spacing: .14em; }
.stat strong { display: block; margin-top: 3px; font-size: 13px; letter-spacing: .06em; }
.bottom-right {
position: absolute;
right: 24px;
bottom: 23px;
pointer-events: auto;
}
#toast {
position: absolute;
left: 50%;
top: 92px;
transform: translate(-50%, -12px);
max-width: min(520px, calc(100vw - 30px));
padding: 11px 16px;
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(8,18,24,.94);
color: #dbeceb;
font-size: 11px;
font-weight: 650;
letter-spacing: .04em;
text-align: center;
opacity: 0;
transition: .25s ease;
box-shadow: 0 10px 30px rgba(0,0,0,.3);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
.overlay {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
padding: 20px;
background: rgba(3,9,13,.58);
backdrop-filter: blur(7px);
}
[hidden] { display: none !important; }
.portal-card {
width: min(470px, 100%);
border: 1px solid rgba(180,230,240,.24);
border-radius: 18px;
background: linear-gradient(145deg, rgba(17,34,42,.98), rgba(6,15,20,.98));
box-shadow: 0 35px 100px rgba(0,0,0,.65), inset 0 1px rgba(255,255,255,.04);
overflow: hidden;
animation: cardIn .22s ease-out;
}
@keyframes cardIn {
from { opacity: 0; transform: translateY(12px) scale(.98); }
to { opacity: 1; transform: none; }
}
.portal-card-head {
padding: 20px 22px 16px;
border-bottom: 1px solid var(--line);
display: flex;
align-items: center;
justify-content: space-between;
}
#portalTitle {
margin: 5px 0 0;
font-size: 21px;
font-weight: 900;
letter-spacing: .05em;
}
.link-badge {
padding: 7px 9px;
border: 1px solid var(--portal);
border-radius: 6px;
color: var(--portal);
font-size: 9px;
font-weight: 900;
letter-spacing: .14em;
box-shadow: inset 0 0 18px color-mix(in srgb, var(--portal) 10%, transparent);
}
.orientation {
padding: 25px 24px 20px;
display: grid;
grid-template-columns: 120px 1fr;
align-items: center;
gap: 23px;
}
.arrow-box {
height: 120px;
display: grid;
place-items: center;
border: 1px solid var(--line);
border-radius: 14px;
background:
linear-gradient(rgba(255,255,255,.025) 1px, transparent 1px),
linear-gradient(90deg, rgba(255,255,255,.025) 1px, transparent 1px),
rgba(0,0,0,.15);
background-size: 18px 18px;
}
#orientationArrow {
display: block;
color: var(--portal);
font-size: 78px;
line-height: 1;
font-weight: 400;
filter: drop-shadow(0 0 13px color-mix(in srgb, var(--portal) 55%, transparent));
transition: transform .2s ease;
}
.orientation-copy h3 {
margin: 0 0 9px;
font-size: 13px;
line-height: 1.45;
letter-spacing: .07em;
}
.orientation-copy p {
margin: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.6;
}
.route {
margin: 0 22px;
padding: 13px 14px;
display: grid;
grid-template-columns: 1fr auto 1fr;
align-items: center;
gap: 12px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(0,0,0,.17);
text-align: center;
}
.route b { display: block; font-size: 12px; letter-spacing: .08em; }
.route small { display: block; margin-top: 4px; color: var(--muted); font-size: 8px; letter-spacing: .12em; }
.route-arrow { color: var(--portal); font-size: 21px; }
.portal-card-foot {
margin-top: 19px;
padding: 15px 22px 19px;
border-top: 1px solid var(--line);
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
}
#exitStatus {
color: var(--acid);
font-size: 9px;
font-weight: 800;
letter-spacing: .1em;
}
#exitStatus.blocked { color: var(--danger); }
.modal-actions { display: flex; gap: 8px; }
.primary {
height: 38px;
padding: 0 16px;
border: 0;
border-radius: 7px;
background: var(--acid);
color: #111b0b;
cursor: pointer;
font-size: 10px;
font-weight: 900;
letter-spacing: .1em;
box-shadow: 0 6px 18px rgba(216,255,79,.15);
}
.primary:disabled {
opacity: .35;
cursor: not-allowed;
}
.secondary {
height: 38px;
padding: 0 14px;
border: 1px solid var(--line);
border-radius: 7px;
background: transparent;
color: var(--muted);
cursor: pointer;
font-size: 9px;
font-weight: 800;
letter-spacing: .1em;
}
.win-card {
width: min(440px, 100%);
padding: 34px;
border: 1px solid rgba(216,255,79,.35);
border-radius: 18px;
background: linear-gradient(145deg, rgba(19,39,39,.98), rgba(6,15,20,.98));
text-align: center;
box-shadow: 0 35px 100px rgba(0,0,0,.7);
animation: cardIn .3s ease-out;
}
.win-icon {
width: 64px;
height: 64px;
margin: 0 auto 20px;
display: grid;
place-items: center;
border: 1px solid var(--acid);
border-radius: 50%;
color: var(--acid);
font-size: 28px;
box-shadow: 0 0 35px rgba(216,255,79,.14), inset 0 0 25px rgba(216,255,79,.08);
}
.win-card h2 {
margin: 0;
font-size: 27px;
letter-spacing: .07em;
}
.win-card p {
margin: 11px 0 24px;
color: var(--muted);
font-size: 12px;
line-height: 1.6;
}
.win-stats {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
margin-bottom: 24px;
}
.win-stats div {
padding: 12px 7px;
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(0,0,0,.15);
}
.win-stats span { display: block; color: var(--muted); font-size: 8px; letter-spacing: .12em; }
.win-stats b { display: block; margin-top: 5px; font-size: 15px; }
.dpad {
display: none;
position: absolute;
left: 18px;
bottom: 86px;
grid-template-columns: repeat(3, 42px);
grid-template-rows: repeat(2, 42px);
gap: 5px;
pointer-events: auto;
}
.dpad button {
border: 1px solid var(--line);
border-radius: 8px;
background: var(--panel);
color: white;
font-size: 17px;
backdrop-filter: blur(12px);
}
.dpad .up { grid-column: 2; }
.dpad .left { grid-column: 1; grid-row: 2; }
.dpad .down { grid-column: 2; grid-row: 2; }
.dpad .right { grid-column: 3; grid-row: 2; }
@media (pointer: coarse), (max-width: 760px) {
.dpad { display: grid; }
.bottom-left { bottom: 184px; }
.legend { display: none; }
.stats { bottom: 19px; left: auto; right: 18px; transform: none; }
.bottom-right { display: none; }
.mission { width: 205px; }
.mini-wrap { display: none; }
.topbar { top: 15px; left: 15px; right: 15px; }
.brand h1 { font-size: 16px; }
.brand-mark { width: 34px; height: 34px; }
.brand-mark::before { left: 8px; top: 15px; }
.brand-mark::after { left: 15px; top: 8px; }
.orientation { grid-template-columns: 95px 1fr; gap: 15px; }
.arrow-box { height: 95px; }
#orientationArrow { font-size: 60px; }
}
@media (max-width: 520px) {
.brand p { display: none; }
.mission { width: 172px; }
.mission-head { padding: 11px 12px; }
.cargo-list { padding: 5px 9px; }
.cargo-row { font-size: 9px; }
.stat { min-width: 59px; padding: 8px; }
.portal-card-head, .portal-card-foot { padding-left: 17px; padding-right: 17px; }
.orientation { padding: 20px 17px; grid-template-columns: 78px 1fr; }
.arrow-box { height: 78px; }
#orientationArrow { font-size: 48px; }
.route { margin: 0 17px; }
.orientation-copy h3 { font-size: 11px; }
}
</style>
</head>
<body>
<div id="app">
<canvas id="scene"></canvas>
<div class="hud">
<div class="topbar">
<div class="brand">
<div class="brand-mark"></div>
<div>
<h1>PORT//SHIFT</h1>
<p>WAREHOUSE 07 · CARGO ROUTING</p>
</div>
</div>
<section class="mission">
<div class="mission-head">
<div>
<div class="eyebrow">Shipment status</div>
<div id="objectiveCount">0 / 3 DOCKED</div>
</div>
<div class="eyebrow">ACTIVE</div>
</div>
<div class="cargo-list">
<div class="cargo-row" id="cargo-amber" style="--cargo:#ffb84a">
<span class="cargo-dot"></span><span>AMBER CARGO</span><span class="status">PENDING</span>
</div>
<div class="cargo-row" id="cargo-blue" style="--cargo:#42d9ff">
<span class="cargo-dot"></span><span>COBALT CARGO</span><span class="status">PENDING</span>
</div>
<div class="cargo-row" id="cargo-violet" style="--cargo:#bd79ff">
<span class="cargo-dot"></span><span>VIOLET CARGO</span><span class="status">PENDING</span>
</div>
</div>
<div class="mini-wrap">
<div class="mini-title"><span>WAREHOUSE GRID</span><span>N ↑</span></div>
<div id="minimap"></div>
</div>
</section>
</div>
<div id="toast" aria-live="polite"></div>
<div class="bottom-left">
<div class="legend">
<div class="key"><kbd>WASD</kbd><span>MOVE / PUSH</span></div>
<div class="key"><kbd>SPACE</kbd><span>CONFIRM PORTAL</span></div>
<div class="key"><kbd>ESC</kbd><span>HOLD POSITION</span></div>
<div class="key"><span>DRAG</span><span>ORBIT VIEW</span></div>
</div>
<div class="buttons">
<button class="action" id="resetBtn">↻ RESET</button>
<button class="action" id="hintBtn">? HINT</button>
<button class="action" id="viewBtn">⌖ VIEW</button>
<button class="action" id="soundBtn">♪ SOUND</button>
</div>
</div>
<div class="stats">
<div class="stat"><span>TIME</span><strong id="timeStat">00:00</strong></div>
<div class="stat"><span>MOVES</span><strong id="moveStat">000</strong></div>
<div class="stat"><span>HINTS</span><strong id="hintStat">00</strong></div>
</div>
<div class="bottom-right">
<button class="action" id="resetBtn2">RESTART SHIFT</button>
</div>
<div class="dpad">
<button class="up" data-dir="north">↑</button>
<button class="left" data-dir="west">←</button>
<button class="down" data-dir="south">↓</button>
<button class="right" data-dir="east">→</button>
</div>
</div>
<div class="overlay" id="portalOverlay" hidden>
<section class="portal-card" role="dialog" aria-modal="true" aria-labelledby="portalTitle">
<header class="portal-card-head">
<div>
<div class="eyebrow" id="portalEyebrow">PORTAL LINK</div>
<h2 id="portalTitle">EXIT ORIENTATION</h2>
</div>
<div class="link-badge" id="linkBadge">LINK ACTIVE</div>
</header>
<div class="orientation">
<div class="arrow-box"><span id="orientationArrow">↑</span></div>
<div class="orientation-copy">
<h3 id="orientationText">YOU WILL EXIT FACING NORTH</h3>
<p id="orientationDescription">Review the destination before committing to the transfer.</p>
</div>
</div>
<div class="route">
<div><b id="sourceDoor">V-2</b><small>ENTRY THRESHOLD</small></div>
<div class="route-arrow">→</div>
<div><b id="destinationDoor">V-1</b><small id="destinationLabel">EXIT THRESHOLD</small></div>
</div>
<footer class="portal-card-foot">
<div id="exitStatus">EXIT CELL CLEAR</div>
<div class="modal-actions">
<button class="secondary" id="cancelPortalBtn">ESC · HOLD</button>
<button class="primary" id="confirmPortalBtn">SPACE · LINK</button>
</div>
</footer>
</section>
</div>
<div class="overlay" id="winOverlay" hidden>
<section class="win-card" role="dialog" aria-modal="true">
<div class="win-icon">✓</div>
<div class="eyebrow">Shift complete</div>
<h2>SHIPMENT SECURED</h2>
<p>All cargo is docked and the paired portal network is stable.</p>
<div class="win-stats">
<div><span>TIME</span><b id="winTime">00:00</b></div>
<div><span>MOVES</span><b id="winMoves">000</b></div>
<div><span>HINTS</span><b id="winHints">00</b></div>
</div>
<button class="primary" id="replayBtn">RUN ANOTHER SHIFT</button>
</section>
</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
/* -------------------------------------------------------------------------- */
/* Data and game state */
/* -------------------------------------------------------------------------- */
const $ = id => document.getElementById(id);
const GRID_W = 13;
const GRID_H = 11;
const CELL = 1;
const DIRS = {
north: { x: 0, z: -1, label: "NORTH", angle: Math.PI },
east: { x: 1, z: 0, label: "EAST", angle: Math.PI / 2 },
south: { x: 0, z: 1, label: "SOUTH", angle: 0 },
west: { x: -1, z: 0, label: "WEST", angle: -Math.PI / 2 }
};
const obstacles = new Set([
"5,4", "6,4", "7,4",
"8,5",
"4,7", "5,7"
]);
const portals = [
{
id: "V-1",
pairId: "V-2",
color: "#bd79ff",
colorValue: 0xbd79ff,
x: 5, z: 0,
out: DIRS.south,
pairName: "VIOLET LINK"
},
{
id: "V-2",
pairId: "V-1",
color: "#bd79ff",
colorValue: 0xbd79ff,
x: 6, z: 10,
out: DIRS.north,
pairName: "VIOLET LINK"
},
{
id: "O-1",
pairId: "O-2",
color: "#ffb84a",
colorValue: 0xffb84a,
x: 12, z: 4,
out: DIRS.west,
pairName: "AMBER LINK"
},
{
id: "O-2",
pairId: "O-1",
color: "#ffb84a",
colorValue: 0xffb84a,
x: 0, z: 7,
out: DIRS.east,
pairName: "AMBER LINK"
}
];
const targets = [
{ id: "amber", color: "#ffb84a", colorValue: 0xffb84a, x: 10, z: 2, code: "A-17" },
{ id: "blue", color: "#42d9ff", colorValue: 0x42d9ff, x: 3, z: 6, code: "B-08" },
{ id: "violet", color: "#bd79ff", colorValue: 0xbd79ff, x: 5, z: 1, code: "V-03" }
];
const initialCrates = [
{ id: "amber", color: "#ffb84a", colorValue: 0xffb84a, x: 4, z: 2 },
{ id: "blue", color: "#42d9ff", colorValue: 0x42d9ff, x: 8, z: 6 },
{ id: "violet", color: "#bd79ff", colorValue: 0xbd79ff, x: 6, z: 8 }
];
const playerStart = { x: 2, z: 2 };
let playerCell = { ...playerStart };
let crates = [];
let moves = 0;
let hintsUsed = 0;
let elapsed = 0;
let won = false;
let pending = null;
let lastMoveAt = -1000;
let audioEnabled = true;
let audioContext = null;
function gridToWorld(x, z, y = 0) {
return new THREE.Vector3((x - (GRID_W - 1) / 2) * CELL, y, (z - (GRID_H - 1) / 2) * CELL);
}
function portalAt(x, z) {
return portals.find(p => p.x === x && p.z === z) || null;
}
function isWall(x, z) {
if (portalAt(x, z)) return false;
if (x < 0 || x >= GRID_W || z < 0 || z >= GRID_H) return true;
return obstacles.has(`${x},${z}`);
}
function crateAt(x, z, ignored = null) {
return crates.find(c => c !== ignored && c.x === x && c.z === z) || null;
}
function targetAt(x, z) {
return targets.find(t => t.x === x && t.z === z) || null;
}
function portalPartner(portal) {
return portals.find(p => p.id === portal.pairId);
}
function portalDestination(portal) {
const partner = portalPartner(portal);
return {
x: partner.x + partner.out.x,
z: partner.z + partner.out.z,
orientation: partner.out,
partner
};
}
/* -------------------------------------------------------------------------- */
/* Renderer, scene, and camera */
/* -------------------------------------------------------------------------- */
const canvas = $("scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
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.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071015);
scene.fog = new THREE.Fog(0x071015, 22, 42);
const camera = new THREE.OrthographicCamera(-10, 10, 8, -8, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 0.25, 0);
let cameraYaw = 0.58;
let cameraPitch = 0.78;
let zoom = 1;
function updateCamera() {
const aspect = window.innerWidth / window.innerHeight;
const minimumHeight = aspect < 1 ? 14 / aspect : 14;
const height = minimumHeight * zoom;
camera.left = -height * aspect / 2;
camera.right = height * aspect / 2;
camera.top = height / 2;
camera.bottom = -height / 2;
const radius = 24;
camera.position.set(
cameraTarget.x + Math.sin(cameraYaw) * Math.cos(cameraPitch) * radius,
cameraTarget.y + Math.sin(cameraPitch) * radius,
cameraTarget.z + Math.cos(cameraYaw) * Math.cos(cameraPitch) * radius
);
camera.lookAt(cameraTarget);
camera.updateProjectionMatrix();
}
function resetCamera() {
cameraYaw = 0.58;
cameraPitch = 0.78;
zoom = 1;
updateCamera();
}
const hemi = new THREE.HemisphereLight(0xc9efff, 0x18232b, 1.75);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff2d5, 3.3);
sun.position.set(8, 15, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -11;
sun.shadow.camera.right = 11;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x66cfff, 1.25);
fill.position.set(-9, 8, -7);
scene.add(fill);
/* -------------------------------------------------------------------------- */
/* Warehouse construction */
/* -------------------------------------------------------------------------- */
const warehouse = new THREE.Group();
scene.add(warehouse);
const floorGeo = new THREE.BoxGeometry(0.98, 0.16, 0.98);
const floorMatA = new THREE.MeshStandardMaterial({ color: 0x1c3036, roughness: 0.92, metalness: 0.05 });
const floorMatB = new THREE.MeshStandardMaterial({ color: 0x20363b, roughness: 0.9, metalness: 0.05 });
for (let z = 0; z < GRID_H; z++) {
for (let x = 0; x < GRID_W; x++) {
if (x > 0 && x < GRID_W - 1 && z > 0 && z < GRID_H - 1) {
const tile = new THREE.Mesh(floorGeo, (x + z) % 2 ? floorMatA : floorMatB);
tile.position.copy(gridToWorld(x, z, -0.08));
tile.receiveShadow = true;
warehouse.add(tile);
}
}
}
const foundation = new THREE.Mesh(
new THREE.BoxGeometry(13.6, 0.5, 11.6),
new THREE.MeshStandardMaterial({ color: 0x101c22, roughness: 0.72, metalness: 0.3 })
);
foundation.position.y = -0.38;
foundation.receiveShadow = true;
warehouse.add(foundation);
const wallMat = new THREE.MeshStandardMaterial({ color: 0x34474c, roughness: 0.78, metalness: 0.12 });
const wallTopMat = new THREE.MeshStandardMaterial({ color: 0x506166, roughness: 0.65, metalness: 0.18 });
const blockMat = new THREE.MeshStandardMaterial({ color: 0x3c4e52, roughness: 0.72, metalness: 0.18 });
const wallGeo = new THREE.BoxGeometry(0.98, 1.25, 0.98);
const blockGeo = new THREE.BoxGeometry(0.98, 1.05, 0.98);
function addWall(x, z, boundary = false) {
const wall = new THREE.Mesh(boundary ? wallGeo : blockGeo, boundary ? wallMat : blockMat);
wall.position.copy(gridToWorld(x, z, boundary ? 0.55 : 0.45));
wall.castShadow = true;
wall.receiveShadow = true;
warehouse.add(wall);
const cap = new THREE.Mesh(
new THREE.BoxGeometry(0.84, 0.08, 0.84),
boundary ? wallTopMat : wallMat
);
cap.position.copy(gridToWorld(x, z, boundary ? 1.2 : 0.995));
cap.castShadow = true;
warehouse.add(cap);
}
for (let x = 0; x < GRID_W; x++) {
for (let z = 0; z < GRID_H; z++) {
const boundary = x === 0 || x === GRID_W - 1 || z === 0 || z === GRID_H - 1;
if (boundary && !portalAt(x, z)) addWall(x, z, true);
else if (obstacles.has(`${x},${z}`)) addWall(x, z, false);
}
}
const stripeMat = new THREE.MeshStandardMaterial({
color: 0xf3b83e,
emissive: 0x6b3f00,
emissiveIntensity: 0.15,
roughness: 0.65
});
for (let z = 1; z <= 9; z += 2) {
const stripe = new THREE.Mesh(new THREE.BoxGeometry(0.08, 0.025, 0.62), stripeMat);
stripe.position.copy(gridToWorld(11.45, z, 0.02));
warehouse.add(stripe);
}
function makeLabel(text, bg = "#10242b", fg = "#eaf8f7", width = 1.4, height = 0.48) {
const c = document.createElement("canvas");
c.width = 256;
c.height = 96;
const ctx = c.getContext("2d");
ctx.clearRect(0, 0, c.width, c.height);
ctx.fillStyle = bg;
ctx.fillRect(4, 8, 248, 80);
ctx.strokeStyle = fg;
ctx.globalAlpha = 0.5;
ctx.strokeRect(5, 9, 246, 78);
ctx.globalAlpha = 1;
ctx.fillStyle = fg;
ctx.font = "800 38px ui-monospace, monospace";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 128, 49);
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, height, 1);
sprite.renderOrder = 20;
return sprite;
}
function makeShelf(x, z, color) {
const group = new THREE.Group();
group.position.copy(gridToWorld(x, z, 0));
const frameMat = new THREE.MeshStandardMaterial({ color: 0x17262c, roughness: 0.7, metalness: 0.4 });
const boxMat = new THREE.MeshStandardMaterial({ color, roughness: 0.82 });
for (const sx of [-0.36, 0.36]) {
const post = new THREE.Mesh(new THREE.BoxGeometry(0.07, 1.1, 0.07), frameMat);
post.position.set(sx, 0.55, 0);
group.add(post);
}
for (const sy of [0.15, 0.56, 0.96]) {
const shelf = new THREE.Mesh(new THREE.BoxGeometry(0.82, 0.06, 0.35), frameMat);
shelf.position.set(0, sy, 0);
group.add(shelf);
}
for (let i = 0; i < 3; i++) {
const box = new THREE.Mesh(new THREE.BoxGeometry(0.23, 0.25, 0.25), boxMat);
box.position.set(-0.25 + i * 0.25, 0.285 + (i % 2) * 0.41, 0);
box.castShadow = true;
group.add(box);
}
group.scale.setScalar(0.72);
warehouse.add(group);
}
makeShelf(-6.1, -3.5, 0x835f3b);
makeShelf(-6.1, 3.3, 0x376d7e);
makeShelf(4.9, 5.1, 0x765a85);
/* -------------------------------------------------------------------------- */
/* Targets */
/* -------------------------------------------------------------------------- */
for (const target of targets) {
const group = new THREE.Group();
group.position.copy(gridToWorld(target.x, target.z, 0.025));
const pad = new THREE.Mesh(
new THREE.BoxGeometry(0.9, 0.055, 0.9),
new THREE.MeshBasicMaterial({
color: target.colorValue,
transparent: true,
opacity: 0.2,
depthWrite: false
})
);
group.add(pad);
const outline = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxGeometry(0.91, 0.055, 0.91)),
new THREE.LineBasicMaterial({ color: target.colorValue, transparent: true, opacity: 0.95 })
);
group.add(outline);
const beam = new THREE.Mesh(
new THREE.CylinderGeometry(0.38, 0.46, 0.85, 20, 1, true),
new THREE.MeshBasicMaterial({
color: target.colorValue,
transparent: true,
opacity: 0.065,
side: THREE.DoubleSide,
depthWrite: false
})
);
beam.position.y = 0.45;
group.add(beam);
const label = makeLabel(`${target.code} · ${target.id.toUpperCase()}`, "#0c1b21", target.color, 1.25, 0.38);
label.position.set(0, 0.95, 0);
group.add(label);
target.mesh = group;
target.outline = outline;
target.pad = pad;
target.beam = beam;
warehouse.add(group);
}
/* -------------------------------------------------------------------------- */
/* Portal doors */
/* -------------------------------------------------------------------------- */
function createPortal(portal) {
const group = new THREE.Group();
group.position.copy(gridToWorld(portal.x, portal.z, 0.02));
group.rotation.y = Math.atan2(portal.out.x, portal.out.z);
const frameMat = new THREE.MeshStandardMaterial({
color: 0x24343b,
roughness: 0.4,
metalness: 0.7
});
const glowMat = new THREE.MeshStandardMaterial({
color: portal.colorValue,
emissive: portal.colorValue,
emissiveIntensity: 1.25,
roughness: 0.28,
metalness: 0.15
});
const panelMat = new THREE.MeshBasicMaterial({
color: portal.colorValue,
transparent: true,
opacity: 0.42,
side: THREE.DoubleSide,
depthWrite: false
});
for (const x of [-0.47, 0.47]) {
const post = new THREE.Mesh(new THREE.BoxGeometry(0.16, 1.65, 0.24), frameMat);
post.position.set(x, 0.82, 0);
post.castShadow = true;
group.add(post);
const light = new THREE.Mesh(new THREE.BoxGeometry(0.055, 1.28, 0.27), glowMat);
light.position.set(x, 0.82, 0.01);
group.add(light);
}
const lintel = new THREE.Mesh(new THREE.BoxGeometry(1.08, 0.16, 0.25), frameMat);
lintel.position.y = 1.65;
lintel.castShadow = true;
group.add(lintel);
const panel = new THREE.Mesh(new THREE.PlaneGeometry(0.83, 1.45), panelMat);
panel.position.set(0, 0.84, 0);
group.add(panel);
const threshold = new THREE.Mesh(
new THREE.BoxGeometry(0.94, 0.07, 0.94),
new THREE.MeshStandardMaterial({
color: portal.colorValue,
emissive: portal.colorValue,
emissiveIntensity: 0.55,
roughness: 0.45
})
);
threshold.position.y = 0.025;
group.add(threshold);
const arrow = new THREE.ArrowHelper(
new THREE.Vector3(0, 0, 1),
new THREE.Vector3(0, 0.16, 0),
0.72,
portal.colorValue,
0.22,
0.17
);
group.add(arrow);
const label = makeLabel(portal.id, "#0c171c", portal.color, 0.85, 0.34);
label.position.set(0, 1.98, 0);
group.add(label);
const light = new THREE.PointLight(portal.colorValue, 1.5, 4.5, 2);
light.position.set(0, 1.1, 0.4);
group.add(light);
portal.mesh = group;
portal.panel = panel;
portal.panelMat = panelMat;
portal.glowMat = glowMat;
portal.light = light;
warehouse.add(group);
}
portals.forEach(createPortal);
function createPortalArc(a, b) {
const p1 = gridToWorld(a.x, a.z, 0.9);
const p2 = gridToWorld(b.x, b.z, 0.9);
const middle = p1.clone().lerp(p2, 0.5);
middle.y = 2.6 + p1.distanceTo(p2) * 0.035;
const curve = new THREE.QuadraticBezierCurve3(p1, middle, p2);
const geometry = new THREE.TubeGeometry(curve, 40, 0.018, 6, false);
const material = new THREE.MeshBasicMaterial({
color: a.colorValue,
transparent: true,
opacity: 0.22,
depthWrite: false
});
const arc = new THREE.Mesh(geometry, material);
scene.add(arc);
}
createPortalArc(portals[0], portals[1]);
createPortalArc(portals[2], portals[3]);
/* -------------------------------------------------------------------------- */
/* Crates */
/* -------------------------------------------------------------------------- */
function createCrate(data) {
const group = new THREE.Group();
const mat = new THREE.MeshStandardMaterial({
color: data.colorValue,
roughness: 0.56,
metalness: 0.14,
emissive: data.colorValue,
emissiveIntensity: 0.035
});
const body = new THREE.Mesh(new THREE.BoxGeometry(0.78, 0.78, 0.78), mat);
body.position.y = 0.42;
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(body.geometry),
new THREE.LineBasicMaterial({ color: 0xffffff, transparent: true, opacity: 0.28 })
);
edges.position.copy(body.position);
group.add(edges);
const strapMat = new THREE.MeshStandardMaterial({
color: 0xe8f1e9,
roughness: 0.55,
metalness: 0.22
});
const strapX = new THREE.Mesh(new THREE.BoxGeometry(0.1, 0.81, 0.81), strapMat);
strapX.position.y = 0.42;
group.add(strapX);
const strapZ = new THREE.Mesh(new THREE.BoxGeometry(0.81, 0.81, 0.08), strapMat);
strapZ.position.y = 0.42;
group.add(strapZ);
const label = makeLabel(data.id.slice(0, 1).toUpperCase(), "#142126", data.color, 0.48, 0.34);
label.position.set(0, 1.02, 0);
group.add(label);
const glow = new THREE.PointLight(data.colorValue, 0, 2.2, 2);
glow.position.y = 0.7;
group.add(glow);
const crate = {
...data,
locked: false,
mesh: group,
material: mat,
glow
};
group.position.copy(gridToWorld(crate.x, crate.z, 0.02));
warehouse.add(group);
return crate;
}
crates = initialCrates.map(createCrate);
/* -------------------------------------------------------------------------- */
/* Player robot */
/* -------------------------------------------------------------------------- */
const playerMesh = new THREE.Group();
const playerGoal = gridToWorld(playerStart.x, playerStart.z, 0.02);
playerMesh.position.copy(playerGoal);
const robotMat = new THREE.MeshStandardMaterial({
color: 0x9fc5cc,
roughness: 0.42,
metalness: 0.48
});
const robotDarkMat = new THREE.MeshStandardMaterial({
color: 0x172a31,
roughness: 0.58,
metalness: 0.48
});
const body = new THREE.Mesh(new THREE.BoxGeometry(0.56, 0.58, 0.45), robotMat);
body.position.y = 0.43;
body.castShadow = true;
playerMesh.add(body);
const chest = new THREE.Mesh(new THREE.BoxGeometry(0.31, 0.18, 0.035), robotDarkMat);
chest.position.set(0, 0.49, 0.244);
playerMesh.add(chest);
const head = new THREE.Mesh(new THREE.BoxGeometry(0.46, 0.36, 0.42), robotDarkMat);
head.position.y = 0.88;
head.castShadow = true;
playerMesh.add(head);
const eyeMat = new THREE.MeshStandardMaterial({
color: 0xd8ff4f,
emissive: 0xd8ff4f,
emissiveIntensity: 2.5
});
for (const x of [-0.11, 0.11]) {
const eye = new THREE.Mesh(new THREE.BoxGeometry(0.1, 0.08, 0.025), eyeMat);
eye.position.set(x, 0.91, 0.222);
playerMesh.add(eye);
}
for (const x of [-0.17, 0.17]) {
const leg = new THREE.Mesh(new THREE.BoxGeometry(0.16, 0.2, 0.2), robotDarkMat);
leg.position.set(x, 0.11, 0);
leg.castShadow = true;
playerMesh.add(leg);
}
const antenna = new THREE.Mesh(new THREE.CylinderGeometry(0.025, 0.025, 0.24, 8), robotDarkMat);
antenna.position.y = 1.14;
playerMesh.add(antenna);
const antennaTip = new THREE.Mesh(new THREE.SphereGeometry(0.055, 10, 8), eyeMat);
antennaTip.position.y = 1.28;
playerMesh.add(antennaTip);
const playerLabel = makeLabel("UNIT 04", "#0d1d22", "#d8ff4f", 0.85, 0.28);
playerLabel.position.y = 1.48;
playerMesh.add(playerLabel);
warehouse.add(playerMesh);
let playerFacing = DIRS.east.angle;
/* -------------------------------------------------------------------------- */
/* Particles */
/* -------------------------------------------------------------------------- */
const particles = [];
function spawnBurst(position, color, count = 18) {
for (let i = 0; i < count; i++) {
const mat = new THREE.MeshBasicMaterial({ color, transparent: true, opacity: 0.9 });
const mesh = new THREE.Mesh(new THREE.BoxGeometry(0.07, 0.07, 0.07), mat);
mesh.position.copy(position);
mesh.position.y += 0.45;
scene.add(mesh);
particles.push({
mesh,
life: 0.65 + Math.random() * 0.4,
maxLife: 1.05,
velocity: new THREE.Vector3(
(Math.random() - 0.5) * 2.8,
1.2 + Math.random() * 2.4,
(Math.random() - 0.5) * 2.8
)
});
}
}
function updateParticles(dt) {
for (let i = particles.length - 1; i >= 0; i--) {
const p = particles[i];
p.life -= dt;
p.velocity.y -= dt * 3.7;
p.mesh.position.addScaledVector(p.velocity, dt);
p.mesh.rotation.x += dt * 4;
p.mesh.rotation.y += dt * 5;
p.mesh.material.opacity = Math.max(0, p.life / p.maxLife);
if (p.life <= 0) {
scene.remove(p.mesh);
p.mesh.geometry.dispose();
p.mesh.material.dispose();
particles.splice(i, 1);
}
}
}
/* -------------------------------------------------------------------------- */
/* Audio */
/* -------------------------------------------------------------------------- */
function initAudio() {
if (!audioContext) {
const AudioCtor = window.AudioContext || window.webkitAudioContext;
if (AudioCtor) audioContext = new AudioCtor();
}
if (audioContext?.state === "suspended") audioContext.resume();
}
function tone(freq, duration = 0.08, type = "sine", volume = 0.035, delay = 0) {
if (!audioEnabled || !audioContext) return;
const now = audioContext.currentTime + delay;
const osc = audioContext.createOscillator();
const gain = audioContext.createGain();
osc.type = type;
osc.frequency.setValueAtTime(freq, now);
gain.gain.setValueAtTime(0.0001, now);
gain.gain.exponentialRampToValueAtTime(volume, now + 0.008);
gain.gain.exponentialRampToValueAtTime(0.0001, now + duration);
osc.connect(gain).connect(audioContext.destination);
osc.start(now);
osc.stop(now + duration + 0.02);
}
function soundMove() { tone(170, 0.045, "triangle", 0.018); }
function soundPush() { tone(85, 0.11, "square", 0.025); }
function soundPortal() {
tone(210, 0.15, "sine", 0.025);
tone(315, 0.18, "sine", 0.02, 0.07);
}
function soundDock() {
tone(440, 0.12, "sine", 0.03);
tone(660, 0.17, "sine", 0.025, 0.09);
}
/* -------------------------------------------------------------------------- */
/* Interface and minimap */
/* -------------------------------------------------------------------------- */
const minimapCells = [];
for (let z = 0; z < GRID_H; z++) {
for (let x = 0; x < GRID_W; x++) {
const cell = document.createElement("div");
cell.className = "mini-cell";
$("minimap").appendChild(cell);
minimapCells.push({ x, z, el: cell });
}
}
function updateMinimap() {
for (const { x, z, el } of minimapCells) {
el.className = "mini-cell";
el.style.removeProperty("--portal");
el.style.removeProperty("--target");
el.style.removeProperty("--crate");
if (portalAt(x, z)) {
el.classList.add("portal");
el.style.setProperty("--portal", portalAt(x, z).color);
} else if (isWall(x, z)) {
el.classList.add(obstacles.has(`${x},${z}`) ? "block" : "wall");
}
const target = targetAt(x, z);
const crate = crateAt(x, z);
const isPlayer = playerCell.x === x && playerCell.z === z;
if (target && !crate) {
el.classList.add("target");
el.style.setProperty("--target", target.color);
}
if (crate) {
el.classList.add("crate");
el.style.setProperty("--crate", crate.color);
}
if (isPlayer) el.classList.add("player");
}
}
function updateStats() {
const completed = crates.filter(c => c.locked).length;
$("objectiveCount").textContent = `${completed} / 3 DOCKED`;
$("moveStat").textContent = String(moves).padStart(3, "0");
$("hintStat").textContent = String(hintsUsed).padStart(2, "0");
for (const crate of crates) {
const row = $(`cargo-${crate.id}`);
row.classList.toggle("done", crate.locked);
row.querySelector(".status").textContent = crate.locked ? "DOCKED" : "PENDING";
}
}
let toastTimer;
function showToast(text, duration = 3300) {
$("toast").textContent = text;
$("toast").classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => $("toast").classList.remove("show"), duration);
}
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}`;
}
/* -------------------------------------------------------------------------- */
/* Game mechanics */
/* -------------------------------------------------------------------------- */
function setPlayerCell(x, z, teleport = false) {
playerCell = { x, z };
playerGoal.copy(gridToWorld(x, z, 0.02));
if (teleport) playerMesh.position.copy(playerGoal);
}
function setCrateCell(crate, x, z, teleport = false) {
crate.x = x;
crate.z = z;
const goal = gridToWorld(x, z, 0.02);
if (teleport) {
crate.mesh.position.copy(goal);
} else {
crate.goal = goal;
}
}
function lockCrate(crate) {
const target = targetAt(crate.x, crate.z);
if (!target || target.id !== crate.id) return;
crate.locked = true;
crate.material.emissiveIntensity = 0.38;
crate.glow.intensity = 1.2;
target.completed = true;
spawnBurst(gridToWorld(crate.x, crate.z, 0), crate.colorValue, 20);
soundDock();
showToast(`${crate.id.toUpperCase()} CARGO DOCKED AT ${target.code}.`);
checkWin();
}
function checkWin() {
if (won || !crates.every(c => c.locked)) return;
won = true;
$("winTime").textContent = formatTime(elapsed);
$("winMoves").textContent = String(moves).padStart(3, "0");
$("winHints").textContent = String(hintsUsed).padStart(2, "0");
setTimeout(() => {
$("winOverlay").hidden = false;
tone(523, 0.16, "sine", 0.035);
tone(659, 0.18, "sine", 0.03, 0.12);
tone(784, 0.25, "sine", 0.025, 0.25);
}, 350);
}
function tryMove(directionName) {
if (pending || won) return;
const now = performance.now();
if (now - lastMoveAt < 85) return;
lastMoveAt = now;
const dir = DIRS[directionName];
if (!dir) return;
const nx = playerCell.x + dir.x;
const nz = playerCell.z + dir.z;
if (isWall(nx, nz)) {
tone(65, 0.05, "square", 0.012);
return;
}
const portal = portalAt(nx, nz);
if (portal) {
setPlayerCell(nx, nz);
moves++;
playerFacing = dir.angle;
soundPortal();
openPortalPreview(portal, "player");
updateStats();
updateMinimap();
return;
}
const crate = crateAt(nx, nz);
if (crate) {
if (crate.locked) {
showToast("That cargo is locked to its dock.");
return;
}
const bx = nx + dir.x;
const bz = nz + dir.z;
const nextPortal = portalAt(bx, bz);
if (nextPortal) {
soundPortal();
openPortalPreview(nextPortal, "cargo", crate, { x: nx, z: nz });
return;
}
if (isWall(bx, bz) || crateAt(bx, bz, crate)) {
showToast("No room to push that cargo.");
tone(62, 0.06, "square", 0.014);
return;
}
setCrateCell(crate, bx, bz);
setPlayerCell(nx, nz);
playerFacing = dir.angle;
moves++;
soundPush();
lockCrate(crate);
updateStats();
updateMinimap();
return;
}
setPlayerCell(nx, nz);
playerFacing = dir.angle;
moves++;
soundMove();
updateStats();
updateMinimap();
}
function openPortalPreview(portal, mode, crate = null, nextCell = null) {
const destination = portalDestination(portal);
const occupied = crateAt(destination.x, destination.z);
const blocked = isWall(destination.x, destination.z) || Boolean(occupied);
pending = {
portal,
mode,
crate,
nextCell,
destination,
blocked
};
const card = document.querySelector(".portal-card");
card.style.setProperty("--portal", portal.color);
$("linkBadge").style.setProperty("--portal", portal.color);
$("linkBadge").textContent = portal.pairName;
$("portalEyebrow").textContent = mode === "cargo" ? "CARGO TRANSFER PREVIEW" : "PORTAL LINK / ORIENTATION CHECK";
$("portalTitle").textContent = mode === "cargo" ? "CARGO TRANSFER" : "EXIT ORIENTATION";
$("sourceDoor").textContent = portal.id;
$("destinationDoor").textContent = destination.partner.id;
$("destinationLabel").textContent = mode === "cargo" ? "CARGO LANDING" : "EXIT THRESHOLD";
const angle = { NORTH: 0, EAST: 90, SOUTH: 180, WEST: 270 }[destination.orientation.label];
$("orientationArrow").style.transform = `rotate(${angle}deg)`;
if (mode === "cargo") {
$("orientationText").textContent = `${crate.id.toUpperCase()} CARGO WILL EXIT ${destination.orientation.label}`;
$("orientationDescription").textContent = "The cargo will be transferred through the paired door and emerge at the opposite threshold.";
} else {
$("orientationText").textContent = `YOU WILL EXIT FACING ${destination.orientation.label}`;
$("orientationDescription").textContent = "This is the direction you will face after emerging. Confirm the transfer or hold your current position.";
}
$("exitStatus").textContent = blocked ? "EXIT CELL OCCUPIED" : "EXIT CELL CLEAR";
$("exitStatus").classList.toggle("blocked", blocked);
$("confirmPortalBtn").disabled = blocked;
$("portalOverlay").hidden = false;
portals.forEach(p => {
const active = p.id === portal.id || p.id === portal.pairId;
p.glowMat.emissiveIntensity = active ? 2.2 : 0.45;
p.light.intensity = active ? 2.4 : 0.65;
});
const sourcePos = gridToWorld(portal.x, portal.z, 0);
spawnBurst(sourcePos, portal.colorValue, 12);
}
function closePortalPreview() {
pending = null;
$("portalOverlay").hidden = true;
portals.forEach(p => {
p.glowMat.emissiveIntensity = 1.25;
p.light.intensity = 1.5;
});
}
function confirmPortal() {
if (!pending || pending.blocked) return;
const { mode, portal, destination, crate, nextCell } = pending;
if (mode === "player") {
setPlayerCell(destination.x, destination.z, true);
playerFacing = destination.orientation.angle;
spawnBurst(gridToWorld(destination.x, destination.z, 0), portal.colorValue, 22);
showToast(`LINK CONFIRMED · EXITING ${destination.orientation.label}.`);
} else {
setCrateCell(crate, destination.x, destination.z, true);
if (nextCell) setPlayerCell(nextCell.x, nextCell.z);
playerFacing = DIRS.south.angle;
spawnBurst(gridToWorld(portal.x, portal.z, 0), portal.colorValue, 18);
spawnBurst(gridToWorld(destination.x, destination.z, 0), crate.colorValue, 18);
moves++;
showToast(`${crate.id.toUpperCase()} CARGO TRANSFERRED ${portal.id} → ${destination.partner.id}.`);
lockCrate(crate);
}
soundPortal();
closePortalPreview();
updateStats();
updateMinimap();
}
function cancelPortal() {
if (!pending) return;
closePortalPreview();
showToast("TRANSFER HELD · POSITION MAINTAINED.", 1600);
}
function giveHint() {
if (won) return;
hintsUsed++;
let hint;
const amber = crates.find(c => c.id === "amber");
const blue = crates.find(c => c.id === "blue");
const violet = crates.find(c => c.id === "violet");
if (!amber.locked) {
hint = "AMBER: stand immediately west of the amber crate, then push east along the clear lane until it reaches dock A-17.";
} else if (!blue.locked) {
hint = "COBALT: circle around to the east side of the cobalt crate, then push west along the open lane into dock B-08.";
} else if (!violet.locked) {
hint = "VIOLET: get north of the violet crate and push it south into portal V-2. Preview the transfer, then land it on dock V-03.";
} else {
hint = "All docks are occupied. Reset the shift to try a faster route.";
}
showToast(`HINT ${String(hintsUsed).padStart(2, "0")} · ${hint}`, 6500);
updateStats();
}
function resetGame() {
closePortalPreview();
$("winOverlay").hidden = true;
playerCell = { ...playerStart };
playerGoal.copy(gridToWorld(playerStart.x, playerStart.z, 0.02));
playerMesh.position.copy(playerGoal);
playerFacing = DIRS.east.angle;
playerMesh.rotation.y = playerFacing;
crates.forEach((crate, i) => {
const initial = initialCrates[i];
crate.locked = false;
crate.material.emissiveIntensity = 0.035;
crate.glow.intensity = 0;
setCrateCell(crate, initial.x, initial.z, true);
});
targets.forEach(t => {
t.completed = false;
});
moves = 0;
hintsUsed = 0;
elapsed = 0;
won = false;
lastMoveAt = -1000;
updateStats();
updateMinimap();
showToast("SHIFT RESET · THREE CARGO UNITS REQUIRE DOCKING.");
}
$("confirmPortalBtn").addEventListener("click", confirmPortal);
$("cancelPortalBtn").addEventListener("click", cancelPortal);
$("resetBtn").addEventListener("click", resetGame);
$("resetBtn2").addEventListener("click", resetGame);
$("replayBtn").addEventListener("click", resetGame);
$("hintBtn").addEventListener("click", giveHint);
$("viewBtn").addEventListener("click", resetCamera);
$("soundBtn").addEventListener("click", () => {
audioEnabled = !audioEnabled;
$("soundBtn").textContent = audioEnabled ? "♪ SOUND" : "× MUTED";
if (audioEnabled) {
initAudio();
tone(440, 0.08);
}
});
document.querySelectorAll(".dpad button").forEach(button => {
button.addEventListener("pointerdown", event => {
event.preventDefault();
initAudio();
tryMove(button.dataset.dir);
});
});
document.addEventListener("keydown", event => {
const key = event.key.toLowerCase();
if (pending) {
if (event.code === "Space" || key === "enter") {
event.preventDefault();
confirmPortal();
} else if (key === "escape") {
event.preventDefault();
cancelPortal();
}
return;
}
initAudio();
if (key === "w" || key === "arrowup") {
event.preventDefault();
tryMove("north");
} else if (key === "d" || key === "arrowright") {
event.preventDefault();
tryMove("east");
} else if (key === "s" || key === "arrowdown") {
event.preventDefault();
tryMove("south");
} else if (key === "a" || key === "arrowleft") {
event.preventDefault();
tryMove("west");
} else if (key === "r") {
resetGame();
} else if (key === "h") {
giveHint();
}
});
/* -------------------------------------------------------------------------- */
/* Direct camera controls */
/* -------------------------------------------------------------------------- */
let dragging = false;
let dragX = 0;
let dragY = 0;
canvas.addEventListener("pointerdown", event => {
dragging = true;
dragX = event.clientX;
dragY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - dragX;
const dy = event.clientY - dragY;
dragX = event.clientX;
dragY = event.clientY;
cameraYaw -= dx * 0.006;
cameraPitch = THREE.MathUtils.clamp(cameraPitch + dy * 0.0045, 0.42, 1.22);
updateCamera();
});
canvas.addEventListener("pointerup", event => {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) canvas.releasePointerCapture(event.pointerId);
});
canvas.addEventListener("pointercancel", () => { dragging = false; });
canvas.addEventListener("wheel", event => {
event.preventDefault();
zoom = THREE.MathUtils.clamp(zoom + event.deltaY * 0.001, 0.72, 1.65);
updateCamera();
}, { passive: false });
window.addEventListener("resize", () => {
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
updateCamera();
});
/* -------------------------------------------------------------------------- */
/* Animation */
/* -------------------------------------------------------------------------- */
const clock = new THREE.Clock();
let portalTime = 0;
function lerpAngle(current, target, amount) {
const delta = ((target - current + Math.PI) % (Math.PI * 2) + Math.PI * 2) % (Math.PI * 2) - Math.PI;
return current + delta * amount;
}
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
portalTime += dt;
if (!won) elapsed += dt;
const smoothing = 1 - Math.exp(-dt * 13);
playerMesh.position.lerp(playerGoal, smoothing);
playerMesh.rotation.y = lerpAngle(playerMesh.rotation.y, playerFacing, smoothing);
crates.forEach(crate => {
if (crate.goal) crate.mesh.position.lerp(crate.goal, smoothing);
if (crate.locked) {
crate.glow.intensity = 1.05 + Math.sin(portalTime * 3 + crate.x) * 0.18;
}
});
portals.forEach((portal, i) => {
const pulse = Math.sin(portalTime * 3.3 + i * 1.4) * 0.5 + 0.5;
if (!pending) {
portal.panelMat.opacity = 0.3 + pulse * 0.17;
portal.glowMat.emissiveIntensity = 1.0 + pulse * 0.55;
}
portal.panel.scale.y = 0.97 + pulse * 0.035;
});
targets.forEach((target, i) => {
const pulse = Math.sin(portalTime * 2.8 + i) * 0.5 + 0.5;
target.outline.material.opacity = target.completed ? 1 : 0.58 + pulse * 0.32;
target.pad.material.opacity = target.completed ? 0.42 : 0.13 + pulse * 0.1;
target.beam.material.opacity = target.completed ? 0.12 : 0.045 + pulse * 0.035;
target.beam.rotation.y = portalTime * 0.22;
});
updateParticles(dt);
$("timeStat").textContent = formatTime(elapsed);
renderer.render(scene, camera);
}
updateCamera();
updateStats();
updateMinimap();
animate();
setTimeout(() => {
showToast("DELIVER MATCHING CARGO TO EACH DOCK · STEP INTO A PORTAL TO PREVIEW ITS EXIT.", 5200);
}, 500);
</script>
</body>
</html>
```