Prompt specification
Build or customize the Three.js 3D scene "Move-order doorway puzzle" 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: Move-order doorway puzzle
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/move-order-doorway-puzzle-x3884
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing a household move and labeled box destinations. Create a furnished apartment with narrow passages and labeled boxes; users choose an unloading order that keeps routes open until every item reaches its intended room. Low-poly.
Working Three.js Starter (move-order-doorway-puzzle-x3884):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Narrow Passages — Household Move Puzzle</title>
<style>
:root {
--ink: #24312d;
--muted: #69756f;
--panel: rgba(255, 255, 255, 0.92);
--line: rgba(36, 49, 45, 0.12);
--green: #218565;
--red: #c64d49;
--amber: #d89a28;
--shadow: 0 14px 40px rgba(45, 59, 53, 0.16);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dfe7e1;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
[hidden] {
display: none !important;
}
#app,
#app canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
}
#app canvas {
display: block;
cursor: grab;
touch-action: none;
}
#app canvas.grabbing {
cursor: grabbing;
}
#hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.panel {
pointer-events: auto;
background: var(--panel);
border: 1px solid rgba(255, 255, 255, 0.8);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#brief {
position: absolute;
top: 20px;
left: 20px;
width: 310px;
padding: 18px;
border-radius: 18px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 9px;
color: var(--green);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 0 4px rgba(33, 133, 101, 0.13);
}
h1 {
margin: 0;
font-size: 29px;
line-height: 1.05;
letter-spacing: -0.04em;
}
.subtitle {
margin: 10px 0 15px;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.rule-grid {
display: grid;
gap: 8px;
}
.rule {
display: grid;
grid-template-columns: 28px 1fr;
gap: 9px;
align-items: center;
padding: 9px 10px;
border-radius: 11px;
background: rgba(239, 243, 239, 0.8);
font-size: 11px;
line-height: 1.4;
}
.rule strong {
display: block;
margin-bottom: 1px;
font-size: 12px;
}
.rule-icon {
display: grid;
width: 28px;
height: 28px;
place-items: center;
border-radius: 8px;
color: #fff;
background: #54756a;
font-size: 15px;
font-weight: 800;
}
.help-line {
margin-top: 13px;
color: #7b857f;
font-size: 10px;
line-height: 1.45;
}
#status {
position: absolute;
top: 20px;
right: 20px;
width: 282px;
padding: 16px;
border-radius: 18px;
}
.status-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 13px;
}
.status-top span {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
#progressText {
font-size: 19px;
font-weight: 800;
}
.progress {
height: 7px;
overflow: hidden;
border-radius: 999px;
background: #e5eae6;
}
#progressFill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #4f9f83, #77bd8d);
transition: width 280ms ease;
}
#missionText {
min-height: 32px;
margin: 10px 0 12px;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.route-heading {
margin: 13px 0 7px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.route-row {
padding: 10px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(248, 250, 248, 0.8);
}
.route-row + .route-row {
margin-top: 7px;
}
.route-label {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
}
.route-label b {
font-size: 11px;
}
.route-label small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 9px;
}
.route-badge {
flex: 0 0 auto;
padding: 4px 7px;
border-radius: 999px;
background: #e0f1e9;
color: #23795e;
font-size: 9px;
font-weight: 900;
letter-spacing: 0.05em;
}
.route-badge.blocked {
background: #f7dfdd;
color: #a33f3c;
}
.route-badge.sealed {
background: #e8ebe9;
color: #69736e;
}
.meter {
height: 4px;
margin-top: 8px;
overflow: hidden;
border-radius: 99px;
background: #e2e7e3;
}
.meter > div {
height: 100%;
width: 0;
border-radius: inherit;
background: var(--amber);
transition: width 250ms ease;
}
.order-section {
margin-top: 13px;
}
#unloadLog {
display: grid;
gap: 5px;
color: var(--muted);
font-size: 10px;
}
#unloadLog div {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
#unloadLog b {
color: var(--ink);
}
.status-actions {
display: flex;
gap: 7px;
margin-top: 13px;
}
.small-button {
flex: 1;
padding: 8px 9px;
border: 1px solid var(--line);
border-radius: 9px;
background: #fff;
color: var(--ink);
cursor: pointer;
font-size: 10px;
font-weight: 800;
}
.small-button:hover {
background: #f0f4f1;
}
#queue {
position: absolute;
bottom: 18px;
left: 50%;
width: min(1040px, calc(100% - 32px));
transform: translateX(-50%);
padding: 13px 14px 14px;
border-radius: 18px;
}
.queue-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
margin: 0 2px 10px;
}
.queue-heading strong {
font-size: 11px;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.queue-heading span {
color: var(--muted);
font-size: 10px;
}
#cards {
display: flex;
gap: 9px;
overflow-x: auto;
padding: 2px 2px 6px;
scrollbar-width: thin;
scrollbar-color: #c9d2cc transparent;
}
.queue-card {
position: relative;
flex: 0 0 150px;
min-height: 105px;
padding: 10px 10px 9px;
overflow: hidden;
border: 1px solid var(--line);
border-top: 4px solid var(--dest);
border-radius: 11px;
background: rgba(255, 255, 255, 0.94);
color: var(--ink);
cursor: pointer;
text-align: left;
transition:
transform 140ms ease,
box-shadow 140ms ease,
opacity 140ms ease;
}
.queue-card::after {
content: "";
position: absolute;
right: -13px;
bottom: -18px;
width: 52px;
height: 52px;
border-radius: 50%;
background: color-mix(in srgb, var(--dest) 16%, transparent);
}
.queue-card:hover:not(:disabled) {
transform: translateY(-3px);
box-shadow: 0 9px 20px rgba(40, 55, 49, 0.12);
}
.queue-card:disabled {
cursor: default;
}
.queue-card.blocked {
border-color: rgba(198, 77, 73, 0.35);
background: #fff7f6;
}
.queue-card.delivered {
opacity: 0.66;
background: #f0f7f3;
}
.queue-card.moving {
opacity: 0.78;
background: #f5f8ff;
}
.card-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 6px;
}
.destination {
color: var(--dest);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.08em;
}
.units {
color: #929b96;
font-size: 9px;
font-weight: 700;
}
.queue-card > strong {
display: block;
margin-top: 7px;
font-size: 14px;
}
.queue-card > small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
}
.card-state {
position: relative;
z-index: 1;
margin-top: 9px;
color: var(--green);
font-size: 8px;
font-weight: 900;
letter-spacing: 0.04em;
line-height: 1.25;
}
.queue-card.blocked .card-state {
color: var(--red);
}
.queue-card.delivered .card-state {
color: var(--green);
}
.queue-card.moving .card-state {
color: #5779aa;
}
@keyframes shake {
0%,
100% {
transform: translateX(0);
}
25% {
transform: translateX(-4px);
}
75% {
transform: translateX(4px);
}
}
.queue-card.shake {
animation: shake 220ms ease;
}
#toast {
position: absolute;
top: 20px;
left: 50%;
max-width: min(440px, calc(100% - 32px));
padding: 11px 15px;
transform: translate(-50%, -18px);
border-radius: 12px;
background: #263a33;
box-shadow: var(--shadow);
color: #fff;
font-size: 12px;
font-weight: 700;
line-height: 1.4;
opacity: 0;
pointer-events: none;
transition:
opacity 180ms ease,
transform 180ms ease;
}
#toast.visible {
transform: translate(-50%, 0);
opacity: 1;
}
#toast.bad {
background: #a8423f;
}
.modal {
position: absolute;
inset: 0;
display: grid;
place-items: center;
padding: 20px;
background: rgba(32, 45, 40, 0.42);
pointer-events: auto;
backdrop-filter: blur(6px);
}
.win-card {
width: min(440px, 100%);
padding: 30px;
border-radius: 22px;
background: #fff;
box-shadow: 0 25px 80px rgba(25, 42, 34, 0.3);
text-align: center;
}
.win-icon {
display: grid;
width: 64px;
height: 64px;
margin: 0 auto 14px;
place-items: center;
border-radius: 50%;
background: #e1f2e9;
color: var(--green);
font-size: 30px;
font-weight: 900;
}
.win-card h2 {
margin: 0;
font-size: 27px;
letter-spacing: -0.04em;
}
.win-card p {
margin: 12px 0 20px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.primary-button {
padding: 12px 20px;
border: 0;
border-radius: 11px;
background: var(--green);
color: #fff;
cursor: pointer;
font-weight: 800;
}
@media (max-width: 760px) {
#brief {
top: 10px;
left: 10px;
width: 270px;
padding: 14px;
}
#brief .rule-grid,
#brief .help-line {
display: none;
}
#status {
top: 10px;
right: 10px;
width: 238px;
padding: 13px;
}
.order-section {
display: none;
}
#queue {
bottom: 10px;
width: calc(100% - 20px);
}
}
@media (max-width: 560px) {
#brief {
right: 10px;
width: auto;
}
#brief .subtitle {
margin-bottom: 0;
}
#status {
top: 94px;
right: auto;
left: 10px;
width: calc(100% - 20px);
}
#status .route-heading,
#status .route-row,
#status .status-actions {
display: none;
}
#missionText {
min-height: 0;
margin-bottom: 0;
}
.queue-heading span {
display: none;
}
.queue-card {
flex-basis: 138px;
}
}
</style>
</head>
<body>
<div id="app"></div>
<div id="hud">
<section id="brief" class="panel">
<div class="eyebrow">Household route puzzle</div>
<h1>Narrow Passages</h1>
<p class="subtitle">
Choose the unloading order. Every box must reach the room printed on its label
without sealing a route that another box still needs.
</p>
<div class="rule-grid">
<div class="rule">
<div class="rule-icon">↔</div>
<div>
<strong>Watch the through-rooms</strong>
BATH boxes cross the Kitchen aisle. BEDROOM boxes cross the Living passage.
</div>
</div>
<div class="rule">
<div class="rule-icon">3</div>
<div>
<strong>Three clearance units</strong>
Standard boxes use 1 unit. Bulky boxes use 2. A route closes at 3 units.
</div>
</div>
<div class="rule">
<div class="rule-icon">✓</div>
<div>
<strong>Deliver in the right order</strong>
Do not fill a through-room to 3 units while a dependent box still needs it.
</div>
</div>
</div>
<div class="help-line">
Drag the apartment to orbit · Scroll to zoom · Click a box or queue card to unload it.
</div>
</section>
<section id="status" class="panel">
<div class="status-top">
<span>Move progress</span>
<div id="progressText">0 / 10</div>
</div>
<div class="progress"><div id="progressFill"></div></div>
<div id="missionText">Choose the next safe box to unload.</div>
<div class="route-heading">Required passage status</div>
<div class="route-row">
<div class="route-label">
<div>
<b>Kitchen aisle</b>
<small id="kitchenMeta">to the bathroom</small>
</div>
<span id="kitchenBadge" class="route-badge">OPEN</span>
</div>
<div class="meter"><div id="kitchenMeter"></div></div>
</div>
<div class="route-row">
<div class="route-label">
<div>
<b>Living passage</b>
<small id="livingMeta">to the bedroom</small>
</div>
<span id="livingBadge" class="route-badge">OPEN</span>
</div>
<div class="meter"><div id="livingMeter"></div></div>
</div>
<div class="order-section">
<div class="route-heading">Mia's unload log</div>
<div id="unloadLog"><div>No boxes unloaded yet.</div></div>
</div>
<div class="status-actions">
<button id="isoView" class="small-button" type="button">Isometric view</button>
<button id="topView" class="small-button" type="button">Top view</button>
<button id="resetButton" class="small-button" type="button">Reset</button>
</div>
</section>
<section id="queue" class="panel">
<div class="queue-heading">
<strong>Unload queue</strong>
<span>Select a box · hover to preview its route</span>
</div>
<div id="cards"></div>
</section>
<div id="toast" role="status" aria-live="polite"></div>
<div id="win" class="modal" hidden>
<div class="win-card">
<div class="win-icon">✓</div>
<h2>Move complete!</h2>
<p id="winText"></p>
<button id="playAgain" class="primary-button" type="button">Plan another move</button>
</div>
</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (id) => document.getElementById(id);
const FLOOR = 0.12;
const ROUTE_CAPACITY = 3;
const DESTINATION_COLORS = {
KITCHEN: 0xe1aa3b,
BATH: 0x4f9fd8,
LIVING: 0xde735d,
BEDROOM: 0x8b72c7
};
const DESTINATION_CSS = {
KITCHEN: "#d99d27",
BATH: "#438fc8",
LIVING: "#cf6652",
BEDROOM: "#8068bd"
};
const ITEMS = [
{
id: "towels",
name: "Towels",
destination: "BATH",
via: "KITCHEN",
clearance: 1,
bulky: false,
start: [-4, 9.1],
target: [-5, -7.7]
},
{
id: "books",
name: "Books",
destination: "BEDROOM",
via: "LIVING",
clearance: 1,
bulky: false,
start: [-2, 9.1],
target: [2.8, -7.5]
},
{
id: "plates",
name: "Plates",
destination: "KITCHEN",
via: null,
clearance: 1,
bulky: false,
start: [0, 9.1],
target: [-2.8, 2.5]
},
{
id: "lamp",
name: "Floor Lamp",
destination: "LIVING",
via: null,
clearance: 1,
bulky: false,
start: [2, 9.1],
target: [2.9, 2.5]
},
{
id: "linens",
name: "Linens",
destination: "BEDROOM",
via: "LIVING",
clearance: 1,
bulky: false,
start: [4, 9.1],
target: [2.8, -4.6]
},
{
id: "shampoo",
name: "Shampoo",
destination: "BATH",
via: "KITCHEN",
clearance: 1,
bulky: false,
start: [-4, 10.7],
target: [-3.1, -4.6]
},
{
id: "cookware",
name: "Cookware",
destination: "KITCHEN",
via: null,
clearance: 1,
bulky: false,
start: [-2, 10.7],
target: [-7.6, 2.3]
},
{
id: "frames",
name: "Picture Frames",
destination: "LIVING",
via: null,
clearance: 1,
bulky: false,
start: [0, 10.7],
target: [8.1, 1.2]
},
{
id: "pantry",
name: "Pantry Crate",
destination: "KITCHEN",
via: null,
clearance: 2,
bulky: true,
start: [2, 10.7],
target: [-3.4, -2.4]
},
{
id: "cushions",
name: "Sofa Cushions",
destination: "LIVING",
via: null,
clearance: 2,
bulky: true,
start: [4, 10.7],
target: [7.4, -2.3]
}
];
const itemById = Object.fromEntries(ITEMS.map((item) => [item.id, item]));
const roomLoads = {
KITCHEN: 0,
BATH: 0,
LIVING: 0,
BEDROOM: 0
};
let activeMotion = null;
let hoveredItem = null;
let routeLine = null;
let routeMarker = null;
let toastTimer = null;
let rejectionCount = 0;
const unloadLog = [];
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdde6df);
scene.fog = new THREE.Fog(0xdde6df, 35, 65);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
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.12;
$("app").appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xffffff, 0x74847a, 2.05);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff4dc, 3.1);
sun.position.set(-10, 18, 11);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -17;
sun.shadow.camera.right = 17;
sun.shadow.camera.top = 17;
sun.shadow.camera.bottom = -17;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 45;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const fillLight = new THREE.DirectionalLight(0xc9e0ff, 0.65);
fillLight.position.set(12, 9, -8);
scene.add(fillLight);
const standardMaterial = (color, extra = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.84,
metalness: 0,
flatShading: true,
...extra
});
const MATERIALS = {
foundation: standardMaterial(0xb7c0b8),
ground: standardMaterial(0xd9e3dc),
wall: standardMaterial(0xf1e4d0),
frame: standardMaterial(0x54776b),
kitchenFloor: standardMaterial(0xe8dfc8),
livingFloor: standardMaterial(0xd9b988),
bathFloor: standardMaterial(0xbcd7da),
bedroomFloor: standardMaterial(0xd8bb8f),
hallFloor: standardMaterial(0xcbd2cd),
entryFloor: standardMaterial(0xc9bda8),
dock: standardMaterial(0xbfc8c1),
counter: standardMaterial(0x6f9487),
counterTop: standardMaterial(0xf6f1e7),
wood: standardMaterial(0x9a6848),
woodLight: standardMaterial(0xc59463),
dark: standardMaterial(0x37413e),
sofa: standardMaterial(0x4f7d72),
sofaLight: standardMaterial(0x739b8e),
rug: standardMaterial(0xd48770),
cream: standardMaterial(0xf0e6d4),
blue: standardMaterial(0x78a9bf),
plant: standardMaterial(0x5d8f64),
pot: standardMaterial(0xb87555),
tape: standardMaterial(0xd6b983)
};
function addBox(width, height, depth, x, y, z, material, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinder(
radiusTop,
radiusBottom,
height,
segments,
x,
y,
z,
material,
parent = scene
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radiusTop, radiusBottom, height, segments),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + width, y, x + width, y + height, r);
ctx.arcTo(x + width, y + height, x, y + height, r);
ctx.arcTo(x, y + height, x, y, r);
ctx.arcTo(x, y, x + width, y, r);
ctx.closePath();
}
function makeFloorLabel(text, color, x, z, width = 2.7) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, 512, 128);
roundedRect(ctx, 8, 14, 496, 96, 28);
ctx.fillStyle = "rgba(255,255,255,0.80)";
ctx.fill();
ctx.strokeStyle = `#${new THREE.Color(color).getHexString()}`;
ctx.lineWidth = 6;
ctx.stroke();
ctx.fillStyle = `#${new THREE.Color(color).getHexString()}`;
ctx.font = "800 42px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 256, 62);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.MeshBasicMaterial({
map: texture,
transparent: true,
depthWrite: false,
side: THREE.DoubleSide
});
const label = new THREE.Mesh(
new THREE.PlaneGeometry(width, width * 0.25),
material
);
label.rotation.x = -Math.PI / 2;
label.position.set(x, FLOOR + 0.025, z);
label.renderOrder = 2;
scene.add(label);
}
function makeBoxLabel(destination, name) {
const canvas = document.createElement("canvas");
canvas.width = 384;
canvas.height = 128;
const ctx = canvas.getContext("2d");
roundedRect(ctx, 5, 5, 374, 118, 22);
ctx.fillStyle = "rgba(255,255,255,0.95)";
ctx.fill();
const color = `#${new THREE.Color(DESTINATION_COLORS[destination]).getHexString()}`;
roundedRect(ctx, 5, 5, 374, 42, 22);
ctx.fillStyle = color;
ctx.fill();
ctx.fillStyle = "#ffffff";
ctx.font = "900 25px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(destination, 192, 27);
ctx.fillStyle = "#29342f";
ctx.font = "800 24px system-ui, sans-serif";
ctx.fillText(name, 192, 82);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(2.0, 0.67, 1);
sprite.renderOrder = 20;
return sprite;
}
// Apartment foundation and exterior ground.
addBox(60, 0.2, 60, 0, -0.36, 0, MATERIALS.ground);
addBox(18.5, 0.28, 15.8, 0, -0.17, -0.7, MATERIALS.foundation);
// Room floors.
addBox(7.2, 0.12, 7, -5.4, 0.06, 0, MATERIALS.kitchenFloor);
addBox(7.2, 0.12, 7, 5.4, 0.06, 0, MATERIALS.livingFloor);
addBox(7.2, 0.12, 5, -5.4, 0.06, -6, MATERIALS.bathFloor);
addBox(7.2, 0.12, 5, 5.4, 0.06, -6, MATERIALS.bedroomFloor);
addBox(3.6, 0.12, 7, 0, 0.06, 0, MATERIALS.hallFloor);
addBox(3.6, 0.12, 3.5, 0, 0.06, 5.25, MATERIALS.entryFloor);
// Unloading dock.
addBox(11.2, 0.14, 5.1, 0, 0.02, 9.55, MATERIALS.dock);
addBox(3.6, 0.08, 1.05, 0, 0.075, 7.55, MATERIALS.entryFloor);
function addWall(x, z, width, depth) {
return addBox(
width,
1.2,
depth,
x,
FLOOR + 0.6,
z,
MATERIALS.wall
);
}
// Outer walls.
addWall(-9, -0.7, 0.2, 15.4);
addWall(9, -0.7, 0.2, 15.4);
addWall(0, -8.5, 18.2, 0.2);
addWall(-5, 7, 8, 0.2);
addWall(5, 7, 8, 0.2);
// Hall walls, leaving side-room doorways.
addWall(-1.8, -2.175, 0.2, 2.65);
addWall(-1.8, 2.175, 0.2, 2.65);
addWall(1.8, -2.175, 0.2, 2.65);
addWall(1.8, 2.175, 0.2, 2.65);
// Entry walls.
addWall(-1.8, 5.25, 0.2, 3.5);
addWall(1.8, 5.25, 0.2, 3.5);
addWall(-5.4, 3.5, 7.2, 0.2);
addWall(5.4, 3.5, 7.2, 0.2);
// Kitchen-to-bath and living-to-bedroom walls with narrow doorways.
addWall(-6.475, -3.5, 5.05, 0.2);
addWall(-2.125, -3.5, 0.65, 0.2);
addWall(2.125, -3.5, 0.65, 0.2);
addWall(6.475, -3.5, 5.05, 0.2);
function addDoorFrame(x, z, orientation, color = MATERIALS.frame) {
if (orientation === "vertical") {
addBox(0.34, 0.035, 1.55, x, FLOOR + 0.02, z, color);
addBox(0.15, 1.42, 0.15, x, FLOOR + 0.71, z - 0.82, color);
addBox(0.15, 1.42, 0.15, x, FLOOR + 0.71, z + 0.82, color);
} else {
addBox(1.55, 0.035, 0.34, x, FLOOR + 0.02, z, color);
addBox(0.15, 1.42, 0.15, x - 0.82, FLOOR + 0.71, z, color);
addBox(0.15, 1.42, 0.15, x + 0.82, FLOOR + 0.71, z, color);
}
}
addDoorFrame(-1.8, 0, "vertical");
addDoorFrame(1.8, 0, "vertical");
addDoorFrame(-3.2, -3.5, "horizontal");
addDoorFrame(3.2, -3.5, "horizontal");
addDoorFrame(0, 7, "horizontal", MATERIALS.woodLight);
// Kitchen furniture.
addBox(0.9, 0.82, 4.8, -8.35, FLOOR + 0.41, -0.05, MATERIALS.counter);
addBox(1.02, 0.12, 4.92, -8.35, FLOOR + 0.88, -0.05, MATERIALS.counterTop);
addBox(1.25, 1.72, 1.15, -7.75, FLOOR + 0.86, -2.65, MATERIALS.cream);
addBox(0.08, 0.55, 0.08, -7.25, FLOOR + 0.95, -2.65, MATERIALS.dark);
addBox(2.45, 0.84, 1.05, -4.7, FLOOR + 0.42, 1.25, MATERIALS.counter);
addBox(2.58, 0.12, 1.18, -4.7, FLOOR + 0.9, 1.25, MATERIALS.counterTop);
addCylinder(1.05, 1.05, 0.13, 10, -6.75, FLOOR + 0.84, 1.15, MATERIALS.woodLight);
addCylinder(0.12, 0.16, 0.75, 8, -6.75, FLOOR + 0.38, 1.15, MATERIALS.dark);
addCylinder(0.45, 0.45, 0.12, 10, -7.95, FLOOR + 0.48, 1.15, MATERIALS.sofaLight);
addCylinder(0.45, 0.45, 0.12, 10, -5.55, FLOOR + 0.48, 1.15, MATERIALS.sofaLight);
// Living room.
addBox(4.8, 0.025, 3.55, 5.45, FLOOR + 0.025, 0.95, MATERIALS.rug);
addBox(3.8, 0.56, 1.25, 5.55, FLOOR + 0.42, 2.35, MATERIALS.sofa);
addBox(3.8, 0.7, 0.32, 5.55, FLOOR + 0.78, 2.86, MATERIALS.sofaLight);
addBox(0.28, 0.55, 1.25, 3.72, FLOOR + 0.55, 2.35, MATERIALS.sofa);
addBox(0.28, 0.55, 1.25, 7.38, FLOOR + 0.55, 2.35, MATERIALS.sofa);
addBox(2.2, 0.38, 1.05, 5.55, FLOOR + 0.28, 0.45, MATERIALS.woodLight);
addBox(0.58, 0.55, 2.35, 8.35, FLOOR + 0.28, -0.5, MATERIALS.wood);
addBox(0.18, 1.25, 2.45, 8.58, FLOOR + 1.25, -0.5, MATERIALS.dark);
// Bedroom.
addBox(3.05, 0.42, 3.3, 5.65, FLOOR + 0.32, -6.45, MATERIALS.wood);
addBox(2.95, 0.34, 3.2, 5.65, FLOOR + 0.67, -6.45, MATERIALS.cream);
addBox(2.96, 0.12, 1.55, 5.65, FLOOR + 0.89, -5.8, MATERIALS.blue);
addBox(2.6, 0.9, 0.16, 5.65, FLOOR + 0.72, -8.02, MATERIALS.wood);
addBox(1.05, 1.8, 2.7, 8.42, FLOOR + 0.9, -6.45, MATERIALS.woodLight);
addBox(0.72, 0.55, 0.72, 7.45, FLOOR + 0.28, -5.25, MATERIALS.woodLight);
addCylinder(0.2, 0.25, 0.38, 8, 7.45, FLOOR + 0.72, -5.25, MATERIALS.cream);
// Bathroom.
addBox(2.65, 0.55, 3.25, -7.05, FLOOR + 0.28, -6.35, MATERIALS.cream);
addBox(2.3, 0.06, 2.9, -7.05, FLOOR + 0.57, -6.35, MATERIALS.blue);
addBox(1.55, 0.78, 0.95, -3, FLOOR + 0.39, -7.7, MATERIALS.cream);
addBox(1.2, 0.88, 1.05, -3.05, FLOOR + 0.44, -5.05, MATERIALS.cream);
addBox(0.72, 0.72, 0.04, -3.05, FLOOR + 0.5, -4.51, MATERIALS.dark);
// Entry and small decorative plants.
addBox(0.62, 0.48, 1.55, -1.25, FLOOR + 0.24, 5.75, MATERIALS.wood);
addCylinder(0.24, 0.2, 0.38, 8, 1.28, FLOOR + 0.19, 5.85, MATERIALS.pot);
const plant = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.52, 0),
MATERIALS.plant
);
plant.position.set(1.28, FLOOR + 0.7, 5.85);
plant.castShadow = true;
scene.add(plant);
// A hand truck beside the queue.
const dolly = new THREE.Group();
dolly.position.set(5.35, FLOOR, 10.35);
scene.add(dolly);
addBox(0.85, 0.1, 0.65, 0, 0.23, 0, MATERIALS.wood, dolly);
addBox(0.09, 1.35, 0.09, -0.35, 0.83, -0.24, MATERIALS.dark, dolly);
addBox(0.09, 1.35, 0.09, 0.35, 0.83, -0.24, MATERIALS.dark, dolly);
addBox(0.8, 0.09, 0.09, 0, 1.46, -0.24, MATERIALS.dark, dolly);
addCylinder(0.16, 0.16, 0.12, 10, -0.35, 0.14, 0.2, MATERIALS.dark, dolly);
addCylinder(0.16, 0.16, 0.12, 10, 0.35, 0.14, 0.2, MATERIALS.dark, dolly);
makeFloorLabel("KITCHEN", DESTINATION_COLORS.KITCHEN, -5.35, 3.02, 2.65);
makeFloorLabel("LIVING", DESTINATION_COLORS.LIVING, 5.35, 3.02, 2.45);
makeFloorLabel("BATH", DESTINATION_COLORS.BATH, -5.45, -4.02, 2.25);
makeFloorLabel("BEDROOM", DESTINATION_COLORS.BEDROOM, 5.45, -4.02, 2.7);
makeFloorLabel("UNLOAD DOCK", 0x54776b, 0, 7.88, 2.6);
// Low-poly mover used during delivery animations.
const mover = new THREE.Group();
mover.visible = false;
scene.add(mover);
const moverUniform = standardMaterial(0x287f72);
const moverSkin = standardMaterial(0xc98e67);
const moverPants = standardMaterial(0x344b56);
addCylinder(0.23, 0.3, 0.68, 6, 0, 0.86, 0, moverUniform, mover);
const moverHead = new THREE.Mesh(
new THREE.SphereGeometry(0.19, 8, 6),
moverSkin
);
moverHead.position.set(0, 1.38, 0);
moverHead.castShadow = true;
mover.add(moverHead);
addBox(0.13, 0.48, 0.14, -0.12, 0.28, 0, moverPants, mover);
addBox(0.13, 0.48, 0.14, 0.12, 0.28, 0, moverPants, mover);
addCylinder(0.075, 0.075, 0.48, 6, -0.31, 0.9, 0, moverUniform, mover);
addCylinder(0.075, 0.075, 0.48, 6, 0.31, 0.9, 0, moverUniform, mover);
const pickables = [];
// Build every labeled moving box.
for (const item of ITEMS) {
const group = new THREE.Group();
group.position.set(item.start[0], FLOOR, item.start[1]);
scene.add(group);
const width = item.bulky ? 1.05 : 0.8;
const height = item.bulky ? 0.88 : 0.7;
const depth = item.bulky ? 0.92 : 0.72;
const bodyMaterial = standardMaterial(DESTINATION_COLORS[item.destination]);
const body = addBox(
width,
height,
depth,
0,
height / 2,
0,
bodyMaterial,
group
);
addBox(
width * 0.16,
0.025,
depth + 0.025,
0,
height + 0.015,
0,
MATERIALS.tape,
group
);
addBox(
width * 0.16,
height * 0.76,
0.025,
0,
height * 0.54,
depth / 2 + 0.018,
MATERIALS.tape,
group
);
const label = makeBoxLabel(item.destination, item.name);
label.position.set(0, height + 0.43, 0);
group.add(label);
body.userData.itemId = item.id;
label.userData.itemId = item.id;
pickables.push(body, label);
item.group = group;
item.body = body;
item.label = label;
item.status = "ready";
item.labelScale = label.scale.clone();
}
// Build the interactive queue cards.
const cardElements = {};
for (const item of ITEMS) {
const card = document.createElement("button");
card.type = "button";
card.className = "queue-card";
card.style.setProperty("--dest", DESTINATION_CSS[item.destination]);
card.innerHTML = `
<div class="card-top">
<span class="destination">${item.destination}</span>
<span class="units">${item.clearance} UNIT${item.clearance === 1 ? "" : "S"}</span>
</div>
<strong>${item.name}</strong>
<small>${item.via ? `VIA ${item.via}` : "DIRECT ROUTE"}</small>
<div class="card-state">READY · ROUTE CLEAR</div>
`;
card.addEventListener("mouseenter", () => {
if (item.status !== "ready" || activeMotion) return;
setHovered(item);
showPreview(item, validateItem(item).ok);
});
card.addEventListener("mouseleave", () => {
setHovered(null);
clearPreview();
});
card.addEventListener("focus", () => {
if (item.status !== "ready" || activeMotion) return;
setHovered(item);
showPreview(item, validateItem(item).ok);
});
card.addEventListener("blur", () => {
setHovered(null);
clearPreview();
});
card.addEventListener("click", () => attemptDelivery(item));
$("cards").appendChild(card);
cardElements[item.id] = card;
}
// Route logic.
function pendingItems() {
return ITEMS.filter((item) => item.status === "ready");
}
function routeName(route) {
return route === "KITCHEN" ? "Kitchen aisle" : "Living passage";
}
function validateItem(item) {
if (item.status !== "ready") {
return { ok: false, reason: "This box is no longer waiting." };
}
if (item.via && roomLoads[item.via] >= ROUTE_CAPACITY) {
return {
ok: false,
reason: `${routeName(item.via)} is already sealed.`
};
}
const postLoad = { ...roomLoads };
postLoad[item.destination] += item.clearance;
for (const other of pendingItems()) {
if (other.id === item.id || !other.via) continue;
if (postLoad[other.via] >= ROUTE_CAPACITY) {
return {
ok: false,
reason: `Would seal the ${other.via.toLowerCase()} route needed by ${other.name}.`
};
}
}
return { ok: true, reason: "Route clear." };
}
function updateRouteUI(route, badgeId, meterId, metaId) {
const load = roomLoads[route];
const needed = ITEMS.filter(
(item) =>
item.via === route &&
(item.status === "ready" || item.status === "moving")
).length;
const badge = $(badgeId);
const meter = $(meterId);
badge.classList.remove("blocked", "sealed");
if (load >= ROUTE_CAPACITY) {
badge.textContent = needed ? "BLOCKED" : "SEALED";
badge.classList.add(needed ? "blocked" : "sealed");
} else {
badge.textContent = "OPEN";
}
meter.style.width = `${Math.min(100, (load / ROUTE_CAPACITY) * 100)}%`;
$(metaId).textContent = `${load}/${ROUTE_CAPACITY} units · ${
needed ? `${needed} dependent box${needed === 1 ? "" : "es"} waiting` : "no dependent boxes left"
}`;
}
function updateUI() {
const delivered = ITEMS.filter((item) => item.status === "delivered").length;
const moving = ITEMS.find((item) => item.status === "moving");
$("progressText").textContent = `${delivered} / ${ITEMS.length}`;
$("progressFill").style.width = `${(delivered / ITEMS.length) * 100}%`;
if (moving) {
$("missionText").textContent = `Mia is taking ${moving.name} to ${moving.destination}.`;
} else if (delivered === ITEMS.length) {
$("missionText").textContent = "Every box is in its intended room.";
} else {
$("missionText").textContent = `${ITEMS.length - delivered} boxes remain. Choose the next safe unloading order.`;
}
for (const item of ITEMS) {
const card = cardElements[item.id];
const state = card.querySelector(".card-state");
const validation = validateItem(item);
card.classList.remove("ready", "blocked", "delivered", "moving");
if (item.status === "delivered") {
card.classList.add("delivered");
state.textContent = "✓ DELIVERED";
card.disabled = true;
card.setAttribute("aria-disabled", "true");
} else if (item.status === "moving") {
card.classList.add("moving");
state.textContent = "→ ON THE MOVE";
card.disabled = true;
card.setAttribute("aria-disabled", "true");
} else {
if (validation.ok) {
card.classList.add("ready");
state.textContent = "READY · ROUTE CLEAR";
} else {
card.classList.add("blocked");
state.textContent = `BLOCKED · ${validation.reason}`;
}
card.disabled = Boolean(activeMotion);
card.setAttribute("aria-disabled", String(!validation.ok || Boolean(activeMotion)));
}
}
updateRouteUI("KITCHEN", "kitchenBadge", "kitchenMeter", "kitchenMeta");
updateRouteUI("LIVING", "livingBadge", "livingMeter", "livingMeta");
$("unloadLog").innerHTML = unloadLog.length
? unloadLog
.slice(0, 4)
.map(
(entry, index) =>
`<div>${index + 1}. <b>${entry.name}</b> → ${entry.destination}</div>`
)
.join("")
: "<div>No boxes unloaded yet.</div>";
}
function showToast(message, bad = false) {
const toast = $("toast");
toast.textContent = message;
toast.classList.toggle("bad", bad);
toast.classList.add("visible");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove("visible"), 3200);
}
function setHovered(item) {
if (hoveredItem === item) return;
if (hoveredItem) {
hoveredItem.body.material.emissive.setHex(0x000000);
hoveredItem.label.scale.copy(hoveredItem.labelScale);
}
hoveredItem = item;
if (hoveredItem && hoveredItem.status === "ready") {
hoveredItem.body.material.emissive.setHex(0x17372e);
hoveredItem.label.scale.set(
hoveredItem.labelScale.x * 1.08,
hoveredItem.labelScale.y * 1.08,
1
);
}
}
function clearPreview() {
if (routeLine) {
scene.remove(routeLine);
routeLine.geometry.dispose();
routeLine.material.dispose();
routeLine = null;
}
if (routeMarker) {
scene.remove(routeMarker);
routeMarker.geometry.dispose();
routeMarker.material.dispose();
routeMarker = null;
}
}
function makePath(item) {
const points = [
new THREE.Vector3(item.start[0], 0.22, item.start[1]),
new THREE.Vector3(0, 0.22, 8.05),
new THREE.Vector3(0, 0.22, 5.2),
new THREE.Vector3(0, 0.22, 0.2)
];
const target = new THREE.Vector3(item.target[0], 0.22, item.target[1]);
if (item.destination === "KITCHEN") {
points.push(
new THREE.Vector3(-1.45, 0.22, 0),
new THREE.Vector3(-2.8, 0.22, 0),
target
);
} else if (item.destination === "LIVING") {
points.push(
new THREE.Vector3(1.45, 0.22, 0),
new THREE.Vector3(2.8, 0.22, 0),
target
);
} else if (item.destination === "BATH") {
points.push(
new THREE.Vector3(-2.7, 0.22, 0),
new THREE.Vector3(-3.2, 0.22, -1.6),
new THREE.Vector3(-3.2, 0.22, -3.5),
new THREE.Vector3(-3.2, 0.22, -4.3),
target
);
} else if (item.destination === "BEDROOM") {
points.push(
new THREE.Vector3(2.7, 0.22, 0),
new THREE.Vector3(3.2, 0.22, -1.6),
new THREE.Vector3(3.2, 0.22, -3.5),
new THREE.Vector3(3.2, 0.22, -4.3),
target
);
}
return points;
}
function showPreview(item, valid = true) {
clearPreview();
const path = makePath(item);
const color = valid ? 0x16a474 : 0xd14c48;
routeLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(path),
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity: 0.92,
depthTest: false,
depthWrite: false
})
);
routeLine.renderOrder = 12;
scene.add(routeLine);
routeMarker = new THREE.Mesh(
new THREE.RingGeometry(0.48, 0.66, 24),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.82,
side: THREE.DoubleSide,
depthTest: false,
depthWrite: false
})
);
routeMarker.rotation.x = -Math.PI / 2;
routeMarker.position.set(item.target[0], FLOOR + 0.05, item.target[1]);
routeMarker.renderOrder = 13;
scene.add(routeMarker);
}
function createPathData(path) {
const lengths = [0];
let total = 0;
for (let i = 1; i < path.length; i++) {
total += path[i].distanceTo(path[i - 1]);
lengths.push(total);
}
return { path, lengths, total };
}
function samplePath(data, distance) {
const { path, lengths, total } = data;
const d = THREE.MathUtils.clamp(distance, 0, total);
let segment = 1;
while (segment < lengths.length - 1 && lengths[segment] < d) {
segment++;
}
const start = lengths[segment - 1];
const end = lengths[segment];
const fraction = end === start ? 0 : (d - start) / (end - start);
const position = new THREE.Vector3().lerpVectors(
path[segment - 1],
path[segment],
fraction
);
const tangent = new THREE.Vector3()
.subVectors(path[segment], path[segment - 1])
.normalize();
return { position, tangent };
}
function attemptDelivery(item) {
if (activeMotion) {
showToast("Wait for the current box to finish moving.", true);
return;
}
if (item.status !== "ready") return;
const validation = validateItem(item);
if (!validation.ok) {
rejectionCount++;
const card = cardElements[item.id];
card.classList.remove("shake");
void card.offsetWidth;
card.classList.add("shake");
showToast(`${item.name}: ${validation.reason}`, true);
return;
}
clearPreview();
setHovered(null);
const path = makePath(item);
const pathData = createPathData(path);
item.status = "moving";
mover.visible = true;
activeMotion = {
item,
pathData,
startTime: performance.now(),
duration: THREE.MathUtils.clamp(pathData.total * 330, 3200, 6200)
};
updateUI();
showToast(`${item.name} is on the way to ${item.destination}.`);
}
function finishDelivery(item) {
item.status = "delivered";
roomLoads[item.destination] += item.clearance;
item.group.position.set(item.target[0], FLOOR, item.target[1]);
item.group.rotation.y = (item.target[0] + item.target[1]) * 0.17;
mover.visible = false;
activeMotion = null;
unloadLog.unshift({
name: item.name,
destination: item.destination
});
updateUI();
showToast(`${item.name} arrived in ${item.destination}.`);
if (ITEMS.every((box) => box.status === "delivered")) {
setTimeout(() => {
if (ITEMS.every((box) => box.status === "delivered")) {
$("winText").textContent =
`All ${ITEMS.length} boxes reached their intended rooms. Every required passage stayed usable until its final dependent box arrived.` +
(rejectionCount
? ` You tested ${rejectionCount} unsafe unloading choice${rejectionCount === 1 ? "" : "s"}.`
: " You found a safe order on the first try!");
$("win").hidden = false;
}
}, 650);
}
}
function updateMotion(now) {
if (!activeMotion) return;
const motion = activeMotion;
const progress = THREE.MathUtils.clamp(
(now - motion.startTime) / motion.duration,
0,
1
);
const eased = progress * progress * (3 - 2 * progress);
const distance = eased * motion.pathData.total;
const sample = samplePath(motion.pathData, distance);
motion.item.group.position.set(
sample.position.x,
FLOOR + Math.abs(Math.sin(progress * Math.PI * 7)) * 0.055,
sample.position.z
);
const moverSample = samplePath(
motion.pathData,
Math.max(0, distance - 0.58)
);
mover.position.set(moverSample.position.x, FLOOR, moverSample.position.z);
mover.rotation.y = Math.atan2(moverSample.tangent.x, moverSample.tangent.z);
if (progress >= 1) {
finishDelivery(motion.item);
}
}
function resetGame() {
activeMotion = null;
mover.visible = false;
rejectionCount = 0;
unloadLog.length = 0;
clearPreview();
setHovered(null);
for (const key of Object.keys(roomLoads)) {
roomLoads[key] = 0;
}
for (const item of ITEMS) {
item.status = "ready";
item.group.position.set(item.start[0], FLOOR, item.start[1]);
item.group.rotation.set(0, 0, 0);
item.body.material.emissive.setHex(0x000000);
item.label.scale.copy(item.labelScale);
}
$("win").hidden = true;
updateUI();
showToast("Move reset. Choose a new unloading order.");
}
// Direct, dependency-free camera controls.
const cameraState = {
yaw: 0,
pitch: 0.83,
distance: 28.5,
target: new THREE.Vector3(0, 0, 0.9)
};
function updateCamera() {
const cosPitch = Math.cos(cameraState.pitch);
const sinPitch = Math.sin(cameraState.pitch);
camera.position.set(
cameraState.target.x + Math.sin(cameraState.yaw) * cosPitch * cameraState.distance,
cameraState.target.y + sinPitch * cameraState.distance,
cameraState.target.z + Math.cos(cameraState.yaw) * cosPitch * cameraState.distance
);
camera.lookAt(cameraState.target);
}
function setCameraView(view) {
if (view === "top") {
cameraState.yaw = 0;
cameraState.pitch = 1.42;
cameraState.distance = 25.5;
cameraState.target.set(0, 0, 0.5);
} else {
cameraState.yaw = 0;
cameraState.pitch = 0.83;
cameraState.distance = 28.5;
cameraState.target.set(0, 0, 0.9);
}
updateCamera();
}
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let dragging = false;
let dragDistance = 0;
let lastPointerX = 0;
let lastPointerY = 0;
function pickItem(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(pickables, false);
for (const hit of hits) {
const item = itemById[hit.object.userData.itemId];
if (item && item.status === "ready") return item;
}
return null;
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
dragDistance = 0;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("grabbing");
clearPreview();
setHovered(null);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (dragging) {
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
dragDistance += Math.abs(dx) + Math.abs(dy);
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.005,
0.42,
1.43
);
updateCamera();
return;
}
const item = pickItem(event);
setHovered(item);
if (item) {
showPreview(item, validateItem(item).ok);
} else {
clearPreview();
}
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (!dragging) return;
const wasClick = dragDistance < 7;
dragging = false;
renderer.domElement.classList.remove("grabbing");
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
if (wasClick) {
const item = pickItem(event);
if (item) {
attemptDelivery(item);
} else {
clearPreview();
setHovered(null);
}
}
});
renderer.domElement.addEventListener("pointercancel", () => {
dragging = false;
renderer.domElement.classList.remove("grabbing");
});
renderer.domElement.addEventListener("pointerleave", () => {
if (!dragging) {
clearPreview();
setHovered(null);
}
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.distance = THREE.MathUtils.clamp(
cameraState.distance * Math.exp(event.deltaY * 0.001),
16,
39
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
$("isoView").addEventListener("click", () => setCameraView("iso"));
$("topView").addEventListener("click", () => setCameraView("top"));
$("resetButton").addEventListener("click", resetGame);
$("playAgain").addEventListener("click", resetGame);
window.addEventListener("keydown", (event) => {
if (event.key === "Escape") {
clearPreview();
setHovered(null);
}
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
updateCamera();
updateUI();
function animate(now) {
requestAnimationFrame(animate);
updateMotion(now);
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```