Prompt specification
Build or customize the Three.js 3D scene "Cold-chain gap detection game" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Cold-chain gap detection game
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/cold-chain-gap-detection-game-x1044
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring a low-poly logistics game about a fictional cooling requirement. A shipment travels from a storage room to a van, a transfer bay, and a final cabinet. Link the physical stations to a timeline containing travel, waiting, and loading intervals. Use invented condition units and an explicitly fictional allowed interruption, with no real food-safety thresholds. The player moves a cooling pack or reschedules a transfer, then replays the shipment to identify where the stated condition was lost. Keep the same package visible throughout, and distinguish time spent moving from time spent waiting at each handoff.
Working Three.js Starter (cold-chain-gap-detection-game-x1044):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Parcel Condition Lab</title>
<style>
:root {
--bg: #07111e;
--panel: rgba(10, 23, 39, 0.94);
--panel-2: rgba(15, 34, 53, 0.9);
--line: rgba(159, 195, 224, 0.16);
--text: #edf6ff;
--muted: #91a9be;
--cyan: #59d8ff;
--blue: #4c91ff;
--amber: #ffbe55;
--green: #63e6b4;
--red: #ff647c;
--orange: #ff985f;
}
* {
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;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#scene canvas:active {
cursor: grabbing;
}
.glass {
background:
linear-gradient(145deg, rgba(255, 255, 255, 0.035), transparent 45%),
var(--panel);
border: 1px solid var(--line);
box-shadow:
0 18px 55px rgba(0, 0, 0, 0.28),
inset 0 1px rgba(255, 255, 255, 0.04);
backdrop-filter: blur(16px);
}
.topbar {
position: fixed;
top: 18px;
left: 20px;
right: 370px;
display: flex;
align-items: flex-start;
gap: 14px;
pointer-events: none;
z-index: 5;
}
.brand {
padding: 14px 17px;
border-radius: 16px;
min-width: 320px;
}
.eyebrow {
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.brand h1 {
margin: 5px 0 3px;
font-size: 21px;
line-height: 1.05;
letter-spacing: -0.035em;
}
.brand p {
margin: 7px 0 0;
color: var(--muted);
font-size: 12px;
}
#conditionCard {
width: 235px;
border-radius: 16px;
padding: 13px 15px;
}
.condition-row {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
}
.condition-row strong {
font-size: 23px;
letter-spacing: -0.04em;
}
.condition-row span {
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
}
.meter {
position: relative;
height: 7px;
margin-top: 9px;
overflow: hidden;
border-radius: 999px;
background: rgba(255, 255, 255, 0.08);
}
#conditionFill {
width: 100%;
height: 100%;
border-radius: inherit;
background: var(--green);
transition: width 180ms ease, background 180ms ease;
}
.meter::after {
content: "";
position: absolute;
top: -2px;
bottom: -2px;
left: 65%;
width: 2px;
background: rgba(255, 255, 255, 0.8);
box-shadow: 0 0 8px white;
}
.meter-caption {
display: flex;
justify-content: space-between;
margin-top: 6px;
color: var(--muted);
font-size: 10px;
}
#panel {
position: fixed;
z-index: 6;
top: 18px;
right: 20px;
bottom: 222px;
width: 330px;
overflow: auto;
padding: 18px;
border-radius: 18px;
scrollbar-width: thin;
scrollbar-color: rgba(255, 255, 255, 0.18) transparent;
}
#panel h2 {
margin: 6px 0 5px;
font-size: 18px;
letter-spacing: -0.025em;
}
.intro {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.fiction-note {
margin: 13px 0 0;
padding: 10px 11px;
border: 1px solid rgba(255, 190, 85, 0.2);
border-radius: 11px;
background: rgba(255, 190, 85, 0.07);
color: #ffd79a;
font-size: 10.5px;
line-height: 1.45;
}
.rules {
margin: 15px 0;
padding: 12px;
border-radius: 12px;
background: var(--panel-2);
border: 1px solid var(--line);
}
.rule {
display: grid;
grid-template-columns: 22px 1fr;
gap: 9px;
align-items: start;
margin: 8px 0;
color: #c8d9e9;
font-size: 11px;
line-height: 1.35;
}
.rule:first-child {
margin-top: 0;
}
.rule:last-child {
margin-bottom: 0;
}
.rule b {
color: var(--text);
}
.rule-icon {
width: 21px;
height: 21px;
display: grid;
place-items: center;
border-radius: 7px;
color: var(--cyan);
background: rgba(89, 216, 255, 0.1);
font-size: 11px;
font-weight: 900;
}
.section-label {
margin: 17px 0 7px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.station-list {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
}
.station-button {
min-height: 45px;
padding: 8px 9px;
color: #bcd0df;
text-align: left;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.025);
cursor: pointer;
transition: 140ms ease;
}
.station-button:hover {
border-color: rgba(89, 216, 255, 0.45);
color: white;
}
.station-button.active {
border-color: var(--cyan);
background: rgba(89, 216, 255, 0.1);
color: white;
box-shadow: inset 0 0 0 1px rgba(89, 216, 255, 0.1);
}
.station-button strong {
display: block;
font-size: 11px;
}
.station-button small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
}
.selection-card {
margin-top: 10px;
padding: 12px;
border-radius: 12px;
border: 1px solid var(--line);
background: rgba(255, 255, 255, 0.025);
}
.selection-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
}
.selection-title strong {
font-size: 13px;
}
.chip {
padding: 4px 7px;
border-radius: 999px;
background: rgba(89, 216, 255, 0.1);
color: var(--cyan);
font-size: 9px;
font-weight: 800;
text-transform: uppercase;
letter-spacing: 0.06em;
}
.selection-meta {
margin-top: 7px;
color: var(--muted);
font-size: 10.5px;
line-height: 1.45;
}
.button-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 10px;
}
button.action {
min-height: 39px;
padding: 9px 10px;
color: var(--text);
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.04);
cursor: pointer;
transition: 140ms ease;
}
button.action:hover:not(:disabled) {
transform: translateY(-1px);
border-color: rgba(255, 255, 255, 0.34);
background: rgba(255, 255, 255, 0.075);
}
button.action:disabled {
opacity: 0.38;
cursor: not-allowed;
}
button.action.full {
grid-column: 1 / -1;
}
.pack-action {
border-color: rgba(89, 216, 255, 0.24);
color: #bcefff;
background: rgba(89, 216, 255, 0.08);
}
.speed-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
margin-top: 12px;
}
.speed-row span {
color: var(--muted);
font-size: 10px;
}
.speed-buttons {
display: flex;
gap: 4px;
}
.speed-buttons button {
padding: 5px 8px;
color: var(--muted);
border: 1px solid var(--line);
border-radius: 7px;
background: transparent;
cursor: pointer;
font-size: 10px;
}
.speed-buttons button.active {
color: var(--text);
border-color: var(--cyan);
background: rgba(89, 216, 255, 0.1);
}
#replayBtn {
width: 100%;
min-height: 44px;
margin-top: 13px;
padding: 11px 14px;
border: 0;
border-radius: 11px;
color: #061522;
background: linear-gradient(135deg, #74e3ff, #49b5ff);
box-shadow: 0 8px 24px rgba(73, 181, 255, 0.2);
cursor: pointer;
font-weight: 900;
letter-spacing: 0.01em;
}
#replayBtn:hover:not(:disabled) {
filter: brightness(1.08);
transform: translateY(-1px);
}
#replayBtn:disabled {
opacity: 0.55;
cursor: wait;
}
#resultCard {
margin-top: 12px;
padding: 12px;
border-radius: 12px;
border: 1px solid var(--line);
background: rgba(255, 255, 255, 0.025);
}
#resultCard.success {
border-color: rgba(99, 230, 180, 0.3);
background: rgba(99, 230, 180, 0.07);
}
#resultCard.failure {
border-color: rgba(255, 100, 124, 0.3);
background: rgba(255, 100, 124, 0.07);
}
#resultTitle {
margin: 0;
font-size: 13px;
}
#resultCopy {
margin: 5px 0 0;
color: #afc3d5;
font-size: 10.5px;
line-height: 1.45;
}
.event-list {
display: grid;
gap: 6px;
margin-top: 10px;
}
.event {
display: grid;
grid-template-columns: 7px 1fr auto;
gap: 8px;
align-items: center;
padding: 8px 9px;
border-radius: 9px;
background: rgba(255, 255, 255, 0.03);
font-size: 10px;
}
.event-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--green);
}
.event.loss .event-dot {
background: var(--red);
}
.event.protected .event-dot {
background: var(--cyan);
}
.event strong {
font-weight: 750;
}
.event small {
color: var(--muted);
}
.event-value {
color: var(--muted);
font-weight: 800;
white-space: nowrap;
}
.event.loss .event-value {
color: var(--red);
}
.event.protected .event-value {
color: var(--cyan);
}
#timelinePanel {
position: fixed;
z-index: 5;
left: 20px;
right: 370px;
bottom: 18px;
height: 185px;
padding: 14px 16px 12px;
border-radius: 18px;
}
.timeline-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.timeline-heading h3 {
margin: 0;
font-size: 13px;
letter-spacing: -0.01em;
}
.timeline-heading span {
color: var(--muted);
font-size: 10px;
}
.station-ruler {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 7px;
margin: 10px 0 8px;
}
.station-ruler button {
padding: 4px 7px;
overflow: hidden;
color: var(--muted);
text-align: left;
text-overflow: ellipsis;
white-space: nowrap;
border: 0;
border-bottom: 1px solid var(--line);
background: transparent;
cursor: pointer;
font-size: 9px;
font-weight: 750;
}
.station-ruler button.active {
color: var(--cyan);
border-color: var(--cyan);
}
.timeline-track-wrap {
position: relative;
padding-top: 2px;
}
#timelineSegments {
display: flex;
gap: 3px;
min-height: 57px;
overflow: hidden;
border-radius: 10px;
background: rgba(0, 0, 0, 0.14);
}
.segment {
position: relative;
display: flex;
min-width: 31px;
flex-direction: column;
justify-content: center;
padding: 7px 6px;
overflow: hidden;
border: 1px solid transparent;
border-radius: 7px;
cursor: pointer;
transition:
transform 120ms ease,
filter 120ms ease,
border-color 120ms ease;
}
.segment:hover {
filter: brightness(1.13);
}
.segment.current {
z-index: 2;
transform: translateY(-2px);
border-color: white;
box-shadow: 0 6px 14px rgba(0, 0, 0, 0.24);
}
.segment.travel {
color: #d9e8ff;
background: rgba(76, 145, 255, 0.72);
}
.segment.wait {
color: #241708;
background: rgba(255, 190, 85, 0.84);
}
.segment.loading {
color: #062019;
background: rgba(99, 230, 180, 0.82);
}
.segment.loss {
box-shadow: inset 0 -4px var(--red);
}
.segment.breach {
outline: 2px solid var(--red);
outline-offset: -2px;
}
.segment.protected {
box-shadow: inset 0 -4px var(--cyan);
}
.seg-kicker {
font-size: 8px;
font-weight: 900;
letter-spacing: 0.08em;
}
.seg-name {
margin-top: 3px;
overflow: hidden;
font-size: 9px;
font-weight: 800;
text-overflow: ellipsis;
white-space: nowrap;
}
.seg-duration {
margin-top: 2px;
opacity: 0.75;
font-size: 8px;
}
#playhead {
position: absolute;
z-index: 5;
top: -2px;
bottom: -2px;
left: 0;
width: 2px;
pointer-events: none;
background: white;
box-shadow: 0 0 9px rgba(255, 255, 255, 0.9);
transform: translateX(-1px);
}
#playhead::before {
content: "";
position: absolute;
top: -4px;
left: -4px;
width: 10px;
height: 10px;
border-radius: 50%;
background: white;
}
.timeline-footer {
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
margin-top: 9px;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 12px;
color: var(--muted);
font-size: 9px;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 5px;
}
.legend i {
width: 8px;
height: 8px;
border-radius: 3px;
}
.legend .move {
background: var(--blue);
}
.legend .wait {
background: var(--amber);
}
.legend .load {
background: var(--green);
}
.timeline-note {
color: #718ca3;
font-size: 9px;
text-align: right;
}
#toast {
position: fixed;
z-index: 20;
left: 50%;
top: 24px;
max-width: min(520px, calc(100vw - 40px));
padding: 10px 14px;
border: 1px solid rgba(89, 216, 255, 0.3);
border-radius: 999px;
color: #dff7ff;
background: rgba(10, 29, 45, 0.94);
box-shadow: 0 10px 35px rgba(0, 0, 0, 0.3);
opacity: 0;
pointer-events: none;
transform: translate(-50%, -15px);
transition: 180ms ease;
font-size: 11px;
text-align: center;
}
#toast.visible {
opacity: 1;
transform: translate(-50%, 0);
}
@media (max-width: 1050px) {
.topbar {
right: 340px;
}
#panel {
width: 300px;
}
#timelinePanel {
right: 340px;
}
.brand {
min-width: 255px;
}
}
@media (max-width: 780px) {
.topbar {
left: 10px;
right: 10px;
}
.brand {
min-width: 0;
flex: 1;
}
.brand p {
display: none;
}
#conditionCard {
width: 170px;
}
#panel {
top: 92px;
left: 10px;
right: 10px;
bottom: 220px;
width: auto;
}
#timelinePanel {
left: 10px;
right: 10px;
bottom: 10px;
height: 190px;
}
.timeline-note {
display: none;
}
}
@media (max-width: 520px) {
.brand h1 {
font-size: 16px;
}
.brand .eyebrow {
font-size: 8px;
}
#conditionCard {
width: 140px;
padding: 10px;
}
.condition-row strong {
font-size: 18px;
}
.condition-row span {
font-size: 8px;
}
.segment {
min-width: 24px;
padding: 5px 3px;
}
.seg-name,
.seg-duration {
display: none;
}
}
</style>
</head>
<body>
<div id="scene" aria-label="Interactive low-poly logistics scene"></div>
<header class="topbar">
<div class="brand glass">
<div class="eyebrow">Replay diagnostic / fictional logistics</div>
<h1>Parcel Condition Lab</h1>
<p>Protect one parcel. Find the handoff where its condition fails.</p>
</div>
<div id="conditionCard" class="glass">
<div class="condition-row">
<strong id="conditionValue">100 FU</strong>
<span>CONDITION</span>
</div>
<div class="meter"><div id="conditionFill"></div></div>
<div class="meter-caption">
<span>0</span>
<span>required ≥ 65 FU</span>
<span>100</span>
</div>
</div>
</header>
<aside id="panel" class="glass">
<div class="eyebrow">Route intervention</div>
<h2>Change the plan, then replay.</h2>
<p class="intro">
Select a physical station below. Move the single cooling pack there, or
reschedule its transfer. The same parcel remains visible through the
complete replay.
</p>
<div class="fiction-note">
<strong>Entirely fictional rules.</strong>
FU, beats, and the interruption allowance are game mechanics only—not
temperature, real time, or food-safety guidance.
</div>
<div class="rules">
<div class="rule">
<span class="rule-icon">1</span>
<span>Start with <b>100 FU</b>; delivery needs <b>at least 65 FU</b>.</span>
</div>
<div class="rule">
<span class="rule-icon">2</span>
<span>Each handoff gets a fictional <b>2-beat interruption allowance</b>.</span>
</div>
<div class="rule">
<span class="rule-icon">3</span>
<span>Each extra waiting beat removes <b>8 FU</b>. Moving and loading remove none.</span>
</div>
<div class="rule">
<span class="rule-icon">4</span>
<span>One portable cooling pack can protect the waiting interval at <b>one handoff</b>.</span>
</div>
</div>
<div class="section-label">Physical stations</div>
<div id="stationList" class="station-list"></div>
<div class="selection-card">
<div class="selection-title">
<strong id="selectedName">Transfer Bay</strong>
<span id="selectedChip" class="chip">Handoff</span>
</div>
<div id="selectedMeta" class="selection-meta"></div>
<div class="button-grid">
<button id="pullBtn" class="action" type="button">
← Pull forward −2 beats
</button>
<button id="deferBtn" class="action" type="button">
Defer +2 beats →
</button>
<button id="packBtn" class="action pack-action full" type="button">
Move cooling pack here
</button>
</div>
</div>
<div class="speed-row">
<span>Replay speed</span>
<div class="speed-buttons">
<button type="button" data-speed="1">1×</button>
<button type="button" data-speed="2" class="active">2×</button>
<button type="button" data-speed="4">4×</button>
</div>
</div>
<button id="replayBtn" type="button">▶ Replay shipment</button>
<button id="resetBtn" class="action full" type="button">Reset route</button>
<div id="resultCard">
<h3 id="resultTitle">No diagnostic yet</h3>
<p id="resultCopy">
Replay the route to reveal condition loss and the first failing handoff.
</p>
</div>
<div class="section-label">Waiting diagnostics</div>
<div id="eventList" class="event-list">
<div class="event">
<span class="event-dot"></span>
<span><strong>Waiting log</strong><br /><small>Replay to populate</small></span>
<span class="event-value">—</span>
</div>
</div>
</aside>
<section id="timelinePanel" class="glass">
<div class="timeline-heading">
<h3>Shipment timeline</h3>
<span id="totalDuration">43 fictional beats</span>
</div>
<div id="stationRuler" class="station-ruler"></div>
<div class="timeline-track-wrap">
<div id="timelineSegments"></div>
<div id="playhead"></div>
</div>
<div class="timeline-footer">
<div class="legend">
<span><i class="move"></i>Moving</span>
<span><i class="wait"></i>Waiting at handoff</span>
<span><i class="load"></i>Loading</span>
</div>
<div class="timeline-note">
Segment width = duration · red underline = condition loss · red outline = first breach
</div>
</div>
</section>
<div id="toast"></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);
// -------------------------------------------------------------------------
// Game rules. Every value here is intentionally fictional.
// -------------------------------------------------------------------------
const START_CONDITION = 100;
const REQUIRED_CONDITION = 65;
const INTERRUPTION_ALLOWANCE = 2; // fictional beats per handoff
const CONDITION_LOSS_PER_EXTRA_BEAT = 8; // fictional units
const BASE_WAITS = {
van: 4,
bay: 5,
cabinet: 3
};
const waits = { ...BASE_WAITS };
const stations = {
storage: {
id: "storage",
name: "Storage Room",
short: "Storage",
role: "Origin",
kind: "origin",
x: -13
},
van: {
id: "van",
name: "Van Handoff",
short: "Van",
role: "Handoff 01",
kind: "handoff",
x: -5
},
bay: {
id: "bay",
name: "Transfer Bay",
short: "Bay",
role: "Handoff 02",
kind: "handoff",
x: 4
},
cabinet: {
id: "cabinet",
name: "Final Cabinet",
short: "Cabinet",
role: "Delivery",
kind: "handoff",
x: 13
}
};
const stationOrder = ["storage", "van", "bay", "cabinet"];
let selectedStation = "bay";
let packStation = "storage";
let toastTimer = 0;
const state = {
playing: false,
finished: false,
elapsed: 0,
phaseIndex: -1,
condition: START_CONDITION,
speed: 2,
phases: [],
totalBeats: 0,
events: [],
firstBreach: null
};
// -------------------------------------------------------------------------
// Three.js setup.
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111e);
scene.fog = new THREE.Fog(0x07111e, 28, 58);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
120
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
$("scene").appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xc6efff, 0x162334, 2.0);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffffff, 2.5);
sun.position.set(-10, 24, 16);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -24;
sun.shadow.camera.right = 24;
sun.shadow.camera.top = 18;
sun.shadow.camera.bottom = -18;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 55;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x56cfff, 1.1);
rimLight.position.set(16, 10, -14);
scene.add(rimLight);
// -------------------------------------------------------------------------
// Geometry helpers.
// -------------------------------------------------------------------------
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.72,
metalness: 0.04,
flatShading: true,
...options
});
}
function addBox(parent, w, h, d, x, y, z, color, options = {}) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(w, h, d),
standardMaterial(color, options)
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinder(
parent,
radiusTop,
radiusBottom,
height,
x,
y,
z,
color,
options = {}
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radiusTop, radiusBottom, height, 10),
standardMaterial(color, options)
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function roundedRect(ctx, x, y, w, h, r) {
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + w - r, y);
ctx.quadraticCurveTo(x + w, y, x + w, y + r);
ctx.lineTo(x + w, y + h - r);
ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
ctx.lineTo(x + r, y + h);
ctx.quadraticCurveTo(x, y + h, x, y + h - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
}
function createLabel(text, accent = "#59d8ff", width = 4.4) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const ctx = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
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, width * 0.25, 1);
sprite.renderOrder = 20;
function setText(value) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
roundedRect(ctx, 8, 10, 496, 108, 20);
ctx.fillStyle = "rgba(7, 17, 30, 0.91)";
ctx.fill();
ctx.strokeStyle = accent;
ctx.lineWidth = 4;
ctx.stroke();
ctx.fillStyle = accent;
ctx.fillRect(27, 35, 9, 58);
ctx.fillStyle = "#edf6ff";
ctx.font = "800 34px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(value, 271, 65, 430);
texture.needsUpdate = true;
}
setText(text);
return { sprite, setText };
}
// -------------------------------------------------------------------------
// Ground and route.
// -------------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(42, 16),
standardMaterial(0x0a1a29, { roughness: 0.96 })
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.03;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(40, 40, 0x28475d, 0x162d40);
grid.position.y = 0.01;
grid.material.transparent = true;
grid.material.opacity = 0.32;
scene.add(grid);
const routePoints = stationOrder.map((id) => {
const s = stations[id];
return new THREE.Vector3(s.x, 1.24, 0.18);
});
const routeGeometry = new THREE.BufferGeometry().setFromPoints(routePoints);
const route = new THREE.Line(
routeGeometry,
new THREE.LineDashedMaterial({
color: 0x4c91ff,
dashSize: 0.55,
gapSize: 0.3,
transparent: true,
opacity: 0.7
})
);
route.computeLineDistances();
scene.add(route);
for (let i = 0; i < routePoints.length - 1; i++) {
const a = routePoints[i];
const b = routePoints[i + 1];
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(0.16, 0.48, 5),
standardMaterial(0x59d8ff, {
emissive: 0x113e52,
emissiveIntensity: 0.45
})
);
arrow.rotation.z = -Math.PI / 2;
arrow.position.lerpVectors(a, b, 0.54);
scene.add(arrow);
}
// -------------------------------------------------------------------------
// Physical stations.
// -------------------------------------------------------------------------
const stationGroups = {};
function tagStation(group, id) {
group.userData.stationId = id;
stationGroups[id] = group;
scene.add(group);
}
// Storage room: open-front, cutaway low-poly room.
{
const g = new THREE.Group();
g.position.x = stations.storage.x;
addBox(g, 5.4, 0.28, 5.2, 0, 0.14, -0.25, 0x18364a);
addBox(g, 5.3, 3.2, 0.22, 0, 1.8, -2.62, 0x2e5369);
addBox(g, 0.22, 3.2, 4.8, -2.6, 1.8, -0.25, 0x24495e);
addBox(g, 0.22, 3.2, 4.8, 2.6, 1.8, -0.25, 0x24495e);
addBox(g, 5.5, 0.24, 1.6, 0, 3.5, -1.95, 0x3a6176);
for (let y = 0.8; y <= 2.8; y += 1.05) {
addBox(g, 4.5, 0.12, 0.9, 0, y, -2.15, 0x59798a);
}
addBox(g, 1.0, 0.8, 0.9, -1.5, 0.7, -1.8, 0x9b6848);
addBox(g, 1.2, 0.95, 1.0, 0.2, 0.82, -1.7, 0x58725f);
addBox(g, 0.75, 0.7, 0.8, 1.55, 0.7, -1.7, 0x6f6387);
const label = createLabel("STORAGE / ORIGIN", "#59d8ff", 4.6);
label.sprite.position.set(0, 4.35, 0);
g.add(label.sprite);
tagStation(g, "storage");
}
// Van: open-sided cargo section so the same parcel stays visible.
{
const g = new THREE.Group();
g.position.x = stations.van.x;
addBox(g, 5.5, 0.42, 3.3, 0, 0.55, -0.25, 0x253d4f);
addBox(g, 4.6, 0.2, 3.15, -0.45, 0.86, -0.22, 0x4f6875);
addBox(g, 4.8, 0.22, 3.1, -0.45, 3.25, -0.25, 0xb8d0d7);
addBox(g, 4.8, 2.3, 0.16, -0.45, 2.0, -1.72, 0x6c8795);
// Front cabin.
addBox(g, 1.55, 1.9, 2.9, 2.05, 1.45, -0.45, 0xd28b50);
addBox(g, 1.15, 0.75, 0.08, 2.15, 1.9, 1.03, 0x23495e, {
metalness: 0.18,
roughness: 0.35
});
addBox(g, 0.12, 0.72, 2.95, 2.84, 1.45, -0.43, 0xe1a56b);
for (const x of [-1.65, 2.0]) {
for (const z of [-1.48, 1.03]) {
const wheel = addCylinder(
g,
0.48,
0.48,
0.33,
x,
0.48,
z,
0x111820,
{ roughness: 0.95 }
);
wheel.rotation.x = Math.PI / 2;
}
}
const label = createLabel("VAN / HANDOFF 01", "#ffbe55", 4.8);
label.sprite.position.set(0, 4.15, 0);
g.add(label.sprite);
tagStation(g, "van");
}
// Transfer bay: open canopy and loading table.
{
const g = new THREE.Group();
g.position.x = stations.bay.x;
addBox(g, 5.6, 0.3, 4.7, 0, 0.15, -0.25, 0x203d4d);
addBox(g, 5.6, 0.22, 1.8, 0, 3.45, -1.65, 0x365d6d);
for (const x of [-2.35, 2.35]) {
for (const z of [-1.8, 1.25]) {
addBox(g, 0.18, 3.25, 0.18, x, 1.8, z, 0x5d8292);
}
}
addBox(g, 4.6, 0.25, 1.2, 0, 0.72, -0.25, 0x627b86);
addBox(g, 0.3, 0.62, 0.9, -1.9, 0.38, -0.25, 0x314f60);
addBox(g, 0.3, 0.62, 0.9, 1.9, 0.38, -0.25, 0x314f60);
addBox(g, 0.95, 0.8, 0.85, -1.4, 1.25, -0.25, 0x765b86);
addBox(g, 1.0, 0.75, 0.8, 1.25, 1.22, -0.25, 0x9b7148);
addBox(g, 0.7, 0.6, 0.75, 0.15, 1.18, -0.35, 0x47746a);
const label = createLabel("TRANSFER BAY / 02", "#ffbe55", 5.1);
label.sprite.position.set(0, 4.35, 0);
g.add(label.sprite);
tagStation(g, "bay");
}
// Final cabinet: open-front delivery cabinet.
{
const g = new THREE.Group();
g.position.x = stations.cabinet.x;
addBox(g, 4.5, 0.34, 4.0, 0, 0.17, -0.35, 0x213c4d);
addBox(g, 3.9, 3.5, 0.3, 0, 1.95, -2.1, 0x355a6c);
addBox(g, 0.28, 3.5, 3.9, -1.95, 1.95, -0.35, 0x416a7c);
addBox(g, 0.28, 3.5, 3.9, 1.95, 1.95, -0.35, 0x416a7c);
addBox(g, 4.2, 0.3, 3.9, 0, 3.82, -0.35, 0x5a7b88);
// Door parked to one side, preserving visibility.
addBox(g, 0.18, 3.2, 1.65, 2.15, 1.9, 0.7, 0x274b5e);
addBox(g, 0.65, 0.8, 0.12, -1.38, 2.0, -1.9, 0x172a37);
addBox(g, 0.45, 0.12, 0.15, -1.38, 2.05, -1.8, 0x63e6b4, {
emissive: 0x1b654d,
emissiveIntensity: 0.8
});
const label = createLabel("FINAL CABINET", "#63e6b4", 4.8);
label.sprite.position.set(0, 4.55, 0);
g.add(label.sprite);
tagStation(g, "cabinet");
}
// Selection ring.
const selectionRing = new THREE.Mesh(
new THREE.TorusGeometry(2.35, 0.045, 6, 48),
new THREE.MeshBasicMaterial({ color: 0x59d8ff })
);
selectionRing.rotation.x = Math.PI / 2;
selectionRing.position.set(stations[selectedStation].x, 0.36, 0.2);
scene.add(selectionRing);
// -------------------------------------------------------------------------
// The same parcel for the entire route.
// -------------------------------------------------------------------------
const packageGroup = new THREE.Group();
const parcelBody = addBox(
packageGroup,
1.65,
1.12,
1.3,
0,
0,
0,
0xff985f,
{ roughness: 0.82 }
);
const parcelEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(parcelBody.geometry),
new THREE.LineBasicMaterial({ color: 0x6f311d, transparent: true, opacity: 0.65 })
);
parcelEdges.position.copy(parcelBody.position);
packageGroup.add(parcelEdges);
addBox(packageGroup, 0.18, 1.14, 1.32, -0.44, 0, 0, 0xffd29a);
addBox(packageGroup, 0.18, 1.14, 1.32, 0.44, 0, 0, 0xffd29a);
addBox(packageGroup, 0.72, 0.12, 0.72, 0, 0.63, 0, 0xffd29a);
const packageLabel = createLabel("PARCEL 01 · 100 FU", "#ff985f", 3.65);
packageLabel.sprite.position.set(0, 1.16, 0);
packageGroup.add(packageLabel.sprite);
packageGroup.position.set(stations.storage.x, 1.45, 0.25);
scene.add(packageGroup);
// Portable cooling pack.
const coolingPack = new THREE.Group();
addBox(
coolingPack,
0.9,
0.88,
0.68,
0,
0,
0,
0x59d8ff,
{
emissive: 0x0b5e7d,
emissiveIntensity: 0.7,
roughness: 0.4
}
);
addBox(coolingPack, 1.02, 0.12, 0.78, 0, 0.25, 0, 0xc8f5ff);
addBox(coolingPack, 1.02, 0.12, 0.78, 0, -0.25, 0, 0xc8f5ff);
addBox(coolingPack, 0.16, 0.22, 0.18, 0.28, 0.55, 0, 0x63e6b4, {
emissive: 0x1f7b5b,
emissiveIntensity: 0.9
});
const packLabel = createLabel("COOLING PACK", "#59d8ff", 2.5);
packLabel.sprite.position.set(0, 0.95, 0);
coolingPack.add(packLabel.sprite);
coolingPack.position.set(stations.storage.x + 1.75, 0.85, 1.45);
scene.add(coolingPack);
const packTarget = new THREE.Vector3().copy(coolingPack.position);
// -------------------------------------------------------------------------
// Custom camera controls: no addons.
// -------------------------------------------------------------------------
const cameraState = {
azimuth: 0.035,
polar: 0.69,
radius: 32,
target: new THREE.Vector3(0, 1, 0)
};
function updateCamera() {
const s = Math.sin(cameraState.polar);
const c = Math.cos(cameraState.polar);
camera.position.set(
cameraState.target.x + cameraState.radius * s * Math.sin(cameraState.azimuth),
cameraState.target.y + cameraState.radius * c,
cameraState.target.z + cameraState.radius * s * Math.cos(cameraState.azimuth)
);
camera.lookAt(cameraState.target);
}
updateCamera();
const pointer = new THREE.Vector2();
const raycaster = new THREE.Raycaster();
let dragging = false;
let dragMoved = false;
let panning = false;
let previousPointer = { x: 0, y: 0 };
let pointerStart = { x: 0, y: 0 };
function pointerToNDC(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;
}
function findStationFromIntersection(intersection) {
let object = intersection.object;
while (object) {
if (object.userData.stationId) {
return object.userData.stationId;
}
object = object.parent;
}
return null;
}
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
dragMoved = false;
panning = event.button === 2 || event.shiftKey;
previousPointer = { x: event.clientX, y: event.clientY };
pointerStart = { x: event.clientX, y: event.clientY };
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousPointer.x;
const dy = event.clientY - previousPointer.y;
if (Math.abs(event.clientX - pointerStart.x) + Math.abs(event.clientY - pointerStart.y) > 4) {
dragMoved = true;
}
if (panning) {
const right = new THREE.Vector3(
Math.cos(cameraState.azimuth),
0,
-Math.sin(cameraState.azimuth)
);
const forward = new THREE.Vector3(
Math.sin(cameraState.azimuth),
0,
Math.cos(cameraState.azimuth)
);
const panScale = cameraState.radius * 0.0015;
cameraState.target.addScaledVector(right, -dx * panScale);
cameraState.target.addScaledVector(forward, -dy * panScale);
} else {
cameraState.azimuth -= dx * 0.006;
cameraState.polar = THREE.MathUtils.clamp(
cameraState.polar - dy * 0.005,
0.3,
1.28
);
}
previousPointer = { x: event.clientX, y: event.clientY };
updateCamera();
});
renderer.domElement.addEventListener("pointerup", (event) => {
dragging = false;
renderer.domElement.releasePointerCapture(event.pointerId);
if (!dragMoved) {
pointerToNDC(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(scene.children, true);
for (const hit of hits) {
const id = findStationFromIntersection(hit);
if (id) {
selectStation(id);
break;
}
}
}
});
renderer.domElement.addEventListener("pointercancel", () => {
dragging = false;
});
renderer.domElement.addEventListener("contextmenu", (event) => event.preventDefault());
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.018,
16,
49
);
updateCamera();
},
{ passive: false }
);
// -------------------------------------------------------------------------
// Timeline construction.
// -------------------------------------------------------------------------
const timelineElements = new Map();
function createPhases() {
const phases = [];
let cursor = 0;
function add(type, station, duration, extra = {}) {
const phase = {
id: `${type}-${phases.length}`,
type,
station,
duration,
start: cursor,
end: cursor + duration,
...extra
};
cursor += duration;
phases.push(phase);
}
add("loading", "storage", 3);
add("travel", "van", 6, { from: "storage", to: "van" });
add("waiting", "van", waits.van);
add("loading", "van", 3);
add("travel", "bay", 7, { from: "van", to: "bay" });
add("waiting", "bay", waits.bay);
add("loading", "bay", 4);
add("travel", "cabinet", 6, { from: "bay", to: "cabinet" });
add("waiting", "cabinet", waits.cabinet);
add("loading", "cabinet", 2);
return phases;
}
function phaseTitle(phase) {
if (phase.type === "travel") {
return `MOVE → ${stations[phase.to].short}`;
}
if (phase.type === "waiting") {
return `${stations[phase.station].short} wait`;
}
return `${stations[phase.station].short} load`;
}
function buildTimeline() {
state.phases = createPhases();
state.totalBeats = state.phases[state.phases.length - 1].end;
timelineElements.clear();
const container = $("timelineSegments");
container.innerHTML = "";
for (const phase of state.phases) {
const segment = document.createElement("button");
segment.type = "button";
segment.className = `segment ${phase.type}`;
segment.style.flex = `${phase.duration} 1 0`;
segment.title = `${phaseTitle(phase)} — ${phase.duration} fictional beats`;
const typeName =
phase.type === "travel" ? "MOVING" : phase.type === "waiting" ? "WAITING" : "LOADING";
segment.innerHTML = `
<span class="seg-kicker">${typeName}</span>
<span class="seg-name">${phaseTitle(phase)}</span>
<span class="seg-duration">${phase.duration} beats</span>
`;
segment.addEventListener("click", () => {
const station = phase.station || phase.to;
if (station) selectStation(station);
});
container.appendChild(segment);
timelineElements.set(phase.id, segment);
}
$("totalDuration").textContent = `${state.totalBeats} fictional beats`;
updatePlayhead();
}
function updatePlayhead() {
const ratio = state.totalBeats ? state.elapsed / state.totalBeats : 0;
$("playhead").style.left = `${THREE.MathUtils.clamp(ratio, 0, 1) * 100}%`;
}
// -------------------------------------------------------------------------
// UI and controls.
// -------------------------------------------------------------------------
function buildStationButtons() {
const list = $("stationList");
const ruler = $("stationRuler");
list.innerHTML = "";
ruler.innerHTML = "";
for (const id of stationOrder) {
const s = stations[id];
const button = document.createElement("button");
button.type = "button";
button.className = "station-button";
button.dataset.station = id;
button.innerHTML = `<strong>${s.name}</strong><small>${s.role}</small>`;
button.addEventListener("click", () => selectStation(id));
list.appendChild(button);
const rulerButton = document.createElement("button");
rulerButton.type = "button";
rulerButton.dataset.station = id;
rulerButton.textContent = `${s.short} · ${s.role}`;
rulerButton.addEventListener("click", () => selectStation(id));
ruler.appendChild(rulerButton);
}
}
function selectStation(id) {
selectedStation = id;
document.querySelectorAll("[data-station]").forEach((element) => {
element.classList.toggle("active", element.dataset.station === id);
});
selectionRing.position.x = stations[id].x;
updateUI();
}
function updatePackTarget() {
const s = stations[packStation];
packTarget.set(s.x + 1.75, 0.85, 1.45);
}
function showToast(message) {
$("toast").textContent = message;
$("toast").classList.add("visible");
clearTimeout(toastTimer);
toastTimer = window.setTimeout(() => $("toast").classList.remove("visible"), 2400);
}
function updateConditionUI() {
$("conditionValue").textContent = `${state.condition} FU`;
const fill = $("conditionFill");
fill.style.width = `${THREE.MathUtils.clamp(state.condition, 0, 100)}%`;
fill.style.background =
state.condition < REQUIRED_CONDITION
? "var(--red)"
: state.condition < 80
? "var(--amber)"
: "var(--green)";
packageLabel.setText(`PARCEL 01 · ${state.condition} FU`);
}
function updateSelectedUI() {
const s = stations[selectedStation];
$("selectedName").textContent = s.name;
$("selectedChip").textContent = s.role;
if (s.kind === "origin") {
$("selectedMeta").textContent =
"Origin only. It has no waiting interval, so it cannot be rescheduled or protected.";
} else {
$("selectedMeta").textContent =
`Wait: ${waits[s.id]} fictional beats. The first ${INTERRUPTION_ALLOWANCE} are free; ` +
`${Math.max(0, waits[s.id] - INTERRUPTION_ALLOWANCE)} beat(s) can remove condition. ` +
`Cooling pack is currently at ${stations[packStation].name}.`;
}
}
function updateControlAvailability() {
const editable = selectedStation !== "storage" && !state.playing;
$("pullBtn").disabled =
!editable || waits[selectedStation] <= 1;
$("deferBtn").disabled =
!editable || waits[selectedStation] >= 9;
$("packBtn").disabled =
!editable || packStation === selectedStation;
$("packBtn").textContent =
packStation === selectedStation
? "Cooling pack is already here"
: `Move cooling pack to ${stations[selectedStation].short}`;
$("replayBtn").disabled = state.playing;
$("replayBtn").textContent = state.playing ? "Replaying shipment…" : "▶ Replay shipment";
$("resetBtn").disabled = state.playing;
}
function updateUI() {
updateConditionUI();
updateSelectedUI();
updateControlAvailability();
}
function renderEvents() {
const list = $("eventList");
list.innerHTML = "";
if (!state.events.length) {
list.innerHTML = `
<div class="event">
<span class="event-dot"></span>
<span><strong>Waiting log</strong><br /><small>Replay to populate</small></span>
<span class="event-value">—</span>
</div>
`;
return;
}
for (const event of state.events) {
const element = document.createElement("div");
element.className = "event";
if (event.loss > 0) element.classList.add("loss");
if (event.protected) element.classList.add("protected");
let detail = "Within fictional allowance";
let value = "0 FU lost";
if (event.protected) {
detail = "Protected by cooling pack";
value = "0 FU lost";
} else if (event.loss > 0) {
detail = `${event.chargeable} extra waiting beat(s)`;
value = `−${event.loss} FU`;
}
element.innerHTML = `
<span class="event-dot"></span>
<span><strong>${stations[event.station].name}</strong><br /><small>${detail}</small></span>
<span class="event-value">${value}</span>
`;
list.appendChild(element);
}
}
function clearOutcomeUI() {
const card = $("resultCard");
card.className = "";
$("resultTitle").textContent = "No diagnostic yet";
$("resultCopy").textContent =
"Replay the route to reveal condition loss and the first failing handoff.";
}
function resetPlayback(rebuild = true) {
state.playing = false;
state.finished = false;
state.elapsed = 0;
state.phaseIndex = -1;
state.condition = START_CONDITION;
state.events = [];
state.firstBreach = null;
packageGroup.position.set(stations.storage.x, 1.45, 0.25);
packageGroup.rotation.y = 0;
if (rebuild) {
buildTimeline();
} else {
for (const element of timelineElements.values()) {
element.classList.remove("current", "loss", "breach", "protected");
}
}
clearOutcomeUI();
renderEvents();
updatePlayhead();
updateUI();
}
$("pullBtn").addEventListener("click", () => {
if (selectedStation === "storage" || state.playing) return;
waits[selectedStation] = Math.max(1, waits[selectedStation] - 2);
resetPlayback(true);
showToast(`${stations[selectedStation].name} transfer pulled forward by 2 beats.`);
});
$("deferBtn").addEventListener("click", () => {
if (selectedStation === "storage" || state.playing) return;
waits[selectedStation] = Math.min(9, waits[selectedStation] + 2);
resetPlayback(true);
showToast(`${stations[selectedStation].name} transfer deferred by 2 beats.`);
});
$("packBtn").addEventListener("click", () => {
if (selectedStation === "storage" || state.playing) return;
packStation = selectedStation;
updatePackTarget();
resetPlayback(true);
showToast(`Cooling pack moved to ${stations[selectedStation].name}.`);
});
$("replayBtn").addEventListener("click", () => {
resetPlayback(true);
state.playing = true;
updateUI();
});
$("resetBtn").addEventListener("click", () => {
Object.assign(waits, BASE_WAITS);
packStation = "storage";
updatePackTarget();
resetPlayback(true);
showToast("Default route restored.");
});
document.querySelectorAll("[data-speed]").forEach((button) => {
button.addEventListener("click", () => {
state.speed = Number(button.dataset.speed);
document.querySelectorAll("[data-speed]").forEach((b) => {
b.classList.toggle("active", b === button);
});
});
});
// -------------------------------------------------------------------------
// Replay simulation.
// -------------------------------------------------------------------------
function handlePhase(phase) {
if (phase.type !== "waiting") return;
const chargeable = Math.max(0, phase.duration - INTERRUPTION_ALLOWANCE);
const protectedByPack = packStation === phase.station;
const loss = protectedByPack
? 0
: chargeable * CONDITION_LOSS_PER_EXTRA_BEAT;
state.condition -= loss;
const event = {
station: phase.station,
duration: phase.duration,
chargeable,
loss,
protected: protectedByPack,
conditionAfter: state.condition
};
state.events.push(event);
const element = timelineElements.get(phase.id);
if (element) {
if (loss > 0) element.classList.add("loss");
if (protectedByPack) element.classList.add("protected");
}
if (
state.condition < REQUIRED_CONDITION &&
state.firstBreach === null
) {
state.firstBreach = {
station: phase.station,
phaseId: phase.id,
conditionAfter: state.condition
};
const breachElement = timelineElements.get(phase.id);
if (breachElement) breachElement.classList.add("breach");
}
updateConditionUI();
renderEvents();
}
function positionPackage(phase, localProgress) {
const p = THREE.MathUtils.clamp(localProgress, 0, 1);
if (phase.type === "travel") {
const from = stations[phase.from].x;
const to = stations[phase.to].x;
packageGroup.position.x = THREE.MathUtils.lerp(from, to, p);
packageGroup.position.y = 1.45 + Math.sin(p * Math.PI) * 0.58;
packageGroup.position.z = 0.25;
packageGroup.rotation.y = 0;
} else {
const stationId = phase.station;
packageGroup.position.x = stations[stationId].x;
packageGroup.position.y = 1.45 + (phase.type === "loading" ? Math.sin(p * Math.PI) * 0.035 : 0);
packageGroup.position.z = 0.25;
packageGroup.rotation.y =
phase.type === "loading" ? Math.sin(p * Math.PI) * 0.08 : 0;
}
}
function finishPlayback() {
state.playing = false;
state.finished = true;
state.elapsed = state.totalBeats;
const card = $("resultCard");
card.className = "";
if (state.firstBreach) {
const breach = state.firstBreach;
card.classList.add("failure");
$("resultTitle").textContent = `Requirement breached at ${stations[breach.station].name}`;
const lossLocations = state.events
.filter((event) => event.loss > 0)
.map((event) => stations[event.station].name)
.join(", ");
$("resultCopy").textContent =
`During waiting at ${stations[breach.station].name}, condition fell to ` +
`${breach.conditionAfter} FU, below the fictional ${REQUIRED_CONDITION} FU minimum. ` +
`Final condition: ${state.condition} FU. Loss locations: ${lossLocations}.`;
} else {
card.classList.add("success");
$("resultTitle").textContent = "Requirement met";
const protectedStations = state.events
.filter((event) => event.protected)
.map((event) => stations[event.station].name);
$("resultCopy").textContent =
`The parcel finished at ${state.condition} FU, so it never crossed the fictional ` +
`${REQUIRED_CONDITION} FU requirement.` +
(protectedStations.length
? ` Protected waiting: ${protectedStations.join(", ")}.`
: " No cooling pack was needed.");
}
updateUI();
}
function advancePlayback(deltaSeconds) {
if (!state.playing) return;
state.elapsed += deltaSeconds * 3 * state.speed;
while (
state.phaseIndex + 1 < state.phases.length &&
state.elapsed >= state.phases[state.phaseIndex + 1].end
) {
state.phaseIndex += 1;
const phase = state.phases[state.phaseIndex];
handlePhase(phase);
}
for (const element of timelineElements.values()) {
element.classList.remove("current");
}
const phase = state.phases[state.phaseIndex];
if (phase) {
const currentElement = timelineElements.get(phase.id);
if (currentElement) currentElement.classList.add("current");
const localProgress =
(state.elapsed - phase.start) / Math.max(0.001, phase.duration);
positionPackage(phase, localProgress);
}
updatePlayhead();
if (state.elapsed >= state.totalBeats) {
const finalPhase = state.phases[state.phases.length - 1];
positionPackage(finalPhase, 1);
finishPlayback();
}
}
// -------------------------------------------------------------------------
// Main loop.
// -------------------------------------------------------------------------
buildStationButtons();
buildTimeline();
updatePackTarget();
selectStation("bay");
resetPlayback(true);
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
advancePlayback(delta);
coolingPack.position.lerp(packTarget, 1 - Math.exp(-delta * 7));
coolingPack.rotation.y += delta * 0.45;
renderer.render(scene, camera);
}
animate();
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
</script>
</body>
</html>
```