Prompt specification
Build or customize the Three.js 3D scene "Relative Superlative Treasure 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: Relative Superlative Treasure Puzzle
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/relative-superlative-treasure-puzzle-x3368
Scene Description Prompt:
Relative superlative treasure puzzle. Build groups of towers where the tallest local tower differs from the tallest overall. Let players choose a reference set before evaluating comparative instructions. Please build this with Three.js from cdnjs in one self-contained HTML file, using low-poly forms.
Working Three.js Starter (relative-superlative-treasure-puzzle-x3368):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Crown & Compass — Relative Treasure Puzzle</title>
<style>
:root {
--bg: #071a22;
--panel: rgba(8, 30, 39, 0.88);
--panel-soft: rgba(11, 40, 50, 0.72);
--line: rgba(169, 225, 220, 0.18);
--text: #eef8f3;
--muted: #9dbab8;
--gold: #f3c969;
--gold-bright: #ffe39a;
--teal: #67dfcf;
--red: #ff7d73;
--shadow: 0 20px 55px rgba(0, 0, 0, 0.35);
}
* {
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;
}
button:focus-visible {
outline: 3px solid var(--teal);
outline-offset: 3px;
}
[hidden] {
display: none !important;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.topbar {
position: fixed;
z-index: 10;
top: 18px;
left: 22px;
right: 22px;
height: 58px;
display: flex;
align-items: center;
justify-content: space-between;
pointer-events: none;
}
.brand {
display: flex;
align-items: center;
gap: 12px;
padding: 9px 14px 9px 10px;
border: 1px solid var(--line);
border-radius: 16px;
background: rgba(7, 26, 34, 0.78);
box-shadow: var(--shadow);
backdrop-filter: blur(12px);
}
.brand-mark {
position: relative;
width: 38px;
height: 38px;
display: grid;
place-items: center;
border: 1px solid rgba(243, 201, 105, 0.55);
border-radius: 12px;
color: var(--gold);
background: linear-gradient(145deg, rgba(243, 201, 105, 0.15), transparent);
font-size: 22px;
}
.brand-copy small {
display: block;
margin-bottom: 2px;
color: var(--teal);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.19em;
}
.brand-copy strong {
display: block;
font-family: Georgia, "Times New Roman", serif;
font-size: 19px;
font-weight: 600;
letter-spacing: 0.03em;
}
.hud {
display: flex;
align-items: center;
gap: 8px;
pointer-events: auto;
}
.hud-pill,
.phase-pill {
min-width: 74px;
padding: 9px 13px;
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(7, 26, 34, 0.8);
text-align: center;
box-shadow: var(--shadow);
backdrop-filter: blur(12px);
}
.hud-pill b,
.phase-pill b {
display: block;
color: var(--gold-bright);
font-size: 17px;
line-height: 1;
}
.hud-pill span,
.phase-pill span {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.phase-pill {
min-width: 104px;
}
.glass {
position: fixed;
z-index: 8;
border: 1px solid var(--line);
border-radius: 20px;
background: var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(16px);
}
#mission {
top: 94px;
left: 22px;
width: min(390px, calc(100vw - 44px));
padding: 20px;
}
.steps {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin-bottom: 18px;
}
.step {
position: relative;
padding: 9px 7px 8px;
border: 1px solid rgba(169, 225, 220, 0.12);
border-radius: 11px;
color: #789695;
background: rgba(0, 0, 0, 0.12);
text-align: center;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.07em;
text-transform: uppercase;
transition: 180ms ease;
}
.step::after {
content: "";
display: block;
width: 7px;
height: 7px;
margin: 0 auto 6px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 0 4px rgba(255, 255, 255, 0.03);
}
.step.active {
border-color: rgba(243, 201, 105, 0.45);
color: var(--gold-bright);
background: rgba(243, 201, 105, 0.08);
}
.step.done {
color: var(--teal);
border-color: rgba(103, 223, 207, 0.25);
}
.eyebrow {
margin-bottom: 8px;
color: var(--teal);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.mission-title {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(25px, 3vw, 34px);
font-weight: 500;
line-height: 1.04;
letter-spacing: -0.025em;
}
.mission-copy {
margin: 12px 0 16px;
color: #b7ccca;
font-size: 13px;
line-height: 1.55;
}
.rule-box {
margin-bottom: 15px;
padding: 12px 13px;
border-left: 3px solid var(--gold);
border-radius: 4px 12px 12px 4px;
background: rgba(243, 201, 105, 0.075);
color: #d8e8e4;
font-size: 12px;
line-height: 1.5;
}
.status-row {
display: flex;
align-items: center;
gap: 8px;
min-height: 28px;
margin: 8px 0 14px;
color: var(--muted);
font-size: 11px;
}
.status-dot {
width: 7px;
height: 7px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--gold);
box-shadow: 0 0 12px rgba(243, 201, 105, 0.75);
}
.button-row {
display: flex;
gap: 8px;
}
.primary,
.secondary,
.hint-button {
border-radius: 12px;
cursor: pointer;
transition: transform 150ms ease, filter 150ms ease, opacity 150ms ease;
}
.primary {
flex: 1;
min-height: 45px;
padding: 11px 14px;
border: 1px solid #f8dc91;
color: #182126;
background: linear-gradient(135deg, var(--gold-bright), var(--gold));
font-size: 12px;
font-weight: 900;
letter-spacing: 0.025em;
box-shadow: 0 9px 24px rgba(243, 201, 105, 0.16);
}
.primary:hover:not(:disabled),
.secondary:hover:not(:disabled),
.hint-button:hover {
transform: translateY(-1px);
filter: brightness(1.07);
}
.primary:disabled,
.secondary:disabled {
cursor: not-allowed;
opacity: 0.38;
transform: none;
filter: none;
}
.secondary,
.hint-button {
min-height: 43px;
padding: 10px 13px;
border: 1px solid var(--line);
color: #c4d9d6;
background: rgba(255, 255, 255, 0.045);
font-size: 11px;
font-weight: 800;
}
.hint-button {
width: 100%;
margin-top: 9px;
color: var(--gold-bright);
border-color: rgba(243, 201, 105, 0.25);
}
#mapPanel {
top: 94px;
right: 22px;
width: 302px;
max-height: calc(100vh - 118px);
padding: 17px;
overflow: auto;
scrollbar-width: thin;
scrollbar-color: rgba(103, 223, 207, 0.3) transparent;
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 13px;
}
.panel-heading h2 {
margin: 0;
color: #d9e9e5;
font-size: 11px;
font-weight: 900;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.sealed-badge {
padding: 5px 7px;
border: 1px solid rgba(103, 223, 207, 0.22);
border-radius: 8px;
color: var(--teal);
background: rgba(103, 223, 207, 0.07);
font-size: 8px;
font-weight: 900;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.reference-slots {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-bottom: 17px;
}
.reference-slot {
min-height: 67px;
padding: 10px;
border: 1px dashed rgba(169, 225, 220, 0.24);
border-radius: 12px;
background: rgba(0, 0, 0, 0.12);
}
.reference-slot.filled {
border-style: solid;
border-color: rgba(243, 201, 105, 0.38);
background: rgba(243, 201, 105, 0.06);
}
.slot-number {
display: block;
margin-bottom: 6px;
color: var(--gold);
font-size: 8px;
font-weight: 900;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.slot-name {
display: block;
overflow: hidden;
color: #e6f1ee;
font-size: 11px;
font-weight: 800;
text-overflow: ellipsis;
white-space: nowrap;
}
.slot-meta {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 9px;
}
.subheading {
margin: 17px 0 9px;
color: #809c9a;
font-size: 9px;
font-weight: 900;
letter-spacing: 0.15em;
text-transform: uppercase;
}
#groupList {
display: grid;
gap: 7px;
}
.group-card {
width: 100%;
display: grid;
grid-template-columns: 30px 1fr auto;
align-items: center;
gap: 9px;
padding: 9px;
border: 1px solid rgba(169, 225, 220, 0.12);
border-radius: 12px;
color: var(--text);
background: rgba(255, 255, 255, 0.035);
text-align: left;
cursor: pointer;
transition: 160ms ease;
}
.group-card:hover:not(:disabled) {
transform: translateX(-2px);
border-color: rgba(103, 223, 207, 0.4);
background: rgba(103, 223, 207, 0.07);
}
.group-card.selected {
border-color: rgba(243, 201, 105, 0.55);
background: rgba(243, 201, 105, 0.09);
}
.group-card:disabled {
cursor: default;
opacity: 0.78;
}
.group-card__index {
display: grid;
place-items: center;
width: 30px;
height: 30px;
border-radius: 9px;
color: #071a22;
background: var(--card-color, var(--teal));
font-size: 10px;
font-weight: 1000;
box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.22);
}
.group-card.selected .group-card__index {
background: var(--gold);
}
.group-card__name {
display: block;
font-size: 11px;
font-weight: 800;
}
.group-card__meta {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
}
.group-card__state {
color: #718d8b;
font-size: 9px;
font-weight: 900;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.group-card.selected .group-card__state {
color: var(--gold);
}
.sovereign-box {
margin-top: 13px;
padding: 11px;
border: 1px solid rgba(243, 201, 105, 0.19);
border-radius: 12px;
background: rgba(243, 201, 105, 0.055);
}
.sovereign-box span {
display: block;
color: var(--muted);
font-size: 8px;
font-weight: 900;
letter-spacing: 0.13em;
text-transform: uppercase;
}
#sovereignText {
display: block;
margin-top: 5px;
color: var(--gold-bright);
font-size: 11px;
font-weight: 800;
}
.legend {
display: grid;
gap: 8px;
margin-top: 13px;
padding-top: 13px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 9px;
}
.legend-item {
display: flex;
align-items: center;
gap: 8px;
}
.legend-symbol {
display: grid;
place-items: center;
width: 20px;
height: 20px;
flex: 0 0 auto;
border-radius: 6px;
color: var(--gold);
background: rgba(243, 201, 105, 0.1);
font-size: 12px;
}
.controls-note {
position: fixed;
z-index: 6;
right: 22px;
bottom: 17px;
padding: 8px 12px;
border: 1px solid var(--line);
border-radius: 12px;
color: #a8c0bd;
background: rgba(7, 26, 34, 0.72);
font-size: 9px;
letter-spacing: 0.04em;
backdrop-filter: blur(10px);
}
#toast {
position: fixed;
z-index: 30;
top: 86px;
left: 50%;
max-width: min(460px, calc(100vw - 32px));
padding: 11px 16px;
border: 1px solid rgba(243, 201, 105, 0.3);
border-radius: 12px;
color: #fff3cf;
background: rgba(19, 35, 36, 0.94);
box-shadow: var(--shadow);
font-size: 12px;
font-weight: 700;
text-align: center;
opacity: 0;
pointer-events: none;
transform: translate(-50%, -12px);
transition: 200ms ease;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
#toast.error {
color: #ffd6d2;
border-color: rgba(255, 125, 115, 0.38);
}
@media (max-width: 800px) {
.topbar {
top: 10px;
left: 10px;
right: 10px;
}
.brand {
padding: 7px 10px 7px 7px;
}
.brand-mark {
width: 32px;
height: 32px;
}
.brand-copy small {
display: none;
}
.brand-copy strong {
font-size: 15px;
}
.hud-pill {
display: none;
}
.phase-pill {
min-width: 82px;
padding: 8px 9px;
}
#mapPanel {
top: 70px;
left: 10px;
right: 10px;
width: auto;
max-height: 205px;
padding: 12px;
}
.reference-slots,
.legend,
.sovereign-box {
display: none;
}
#groupList {
grid-template-columns: repeat(5, minmax(0, 1fr));
gap: 5px;
}
.group-card {
display: block;
min-width: 0;
padding: 7px 5px;
text-align: center;
}
.group-card__index {
width: 25px;
height: 25px;
margin: 0 auto 5px;
}
.group-card__name {
overflow: hidden;
font-size: 9px;
text-overflow: ellipsis;
white-space: nowrap;
}
.group-card__meta,
.group-card__state {
display: none;
}
#mission {
top: auto;
left: 10px;
right: 10px;
bottom: 10px;
width: auto;
max-height: 46vh;
padding: 15px;
overflow: auto;
}
.steps {
margin-bottom: 12px;
}
.mission-title {
font-size: 25px;
}
.mission-copy {
margin: 9px 0 11px;
}
.rule-box {
margin-bottom: 10px;
}
.controls-note {
display: none;
}
#toast {
top: 286px;
}
}
@media (max-height: 680px) and (min-width: 801px) {
#mission,
#mapPanel {
top: 82px;
}
#mission {
padding: 15px;
}
.mission-title {
font-size: 26px;
}
.mission-copy {
margin: 8px 0 11px;
}
.steps {
margin-bottom: 12px;
}
}
</style>
</head>
<body>
<div id="scene" aria-label="Interactive three-dimensional tower islands"></div>
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true">♜</div>
<div class="brand-copy">
<small>RELATIVE TREASURE HUNT</small>
<strong>Crown & Compass</strong>
</div>
</div>
<div class="hud">
<div class="hud-pill">
<b id="score">0</b>
<span>Score</span>
</div>
<div class="hud-pill">
<b id="round">1</b>
<span>Round</span>
</div>
<div class="hud-pill">
<b id="streak">0</b>
<span>Streak</span>
</div>
<div class="phase-pill">
<b id="phaseLabel">Reference</b>
<span>Current phase</span>
</div>
</div>
</header>
<section id="mission" class="glass" aria-live="polite">
<div class="steps" aria-label="Puzzle phases">
<div id="step1" class="step active">Reference</div>
<div id="step2" class="step">Compare</div>
<div id="step3" class="step">Treasure</div>
</div>
<div id="clueEyebrow" class="eyebrow">Step 1 · Reference first</div>
<h1 id="clueTitle" class="mission-title">Choose two islands</h1>
<p id="clueCopy" class="mission-copy"></p>
<div id="ruleText" class="rule-box"></div>
<div class="status-row">
<span class="status-dot" aria-hidden="true"></span>
<span id="statusText"></span>
</div>
<div class="button-row">
<button id="primaryBtn" class="primary" type="button">
Seal reference & reveal clue
</button>
<button id="shuffleBtn" class="secondary" type="button">New map</button>
</div>
<button id="hintBtn" class="hint-button" type="button" hidden>
Show comparison scales
</button>
</section>
<aside id="mapPanel" class="glass">
<div class="panel-heading">
<h2>Reference set</h2>
<span id="sealBadge" class="sealed-badge">Unsealed</span>
</div>
<div class="reference-slots">
<div id="refSlot0" class="reference-slot">
<span class="slot-number">Reference I</span>
<span class="slot-name">Choose an island</span>
<span class="slot-meta">Awaiting selection</span>
</div>
<div id="refSlot1" class="reference-slot">
<span class="slot-number">Reference II</span>
<span class="slot-name">Choose an island</span>
<span class="slot-meta">Awaiting selection</span>
</div>
</div>
<div class="subheading">Island index · click to reference</div>
<div id="groupList"></div>
<div class="sovereign-box">
<span>Overall sovereign</span>
<strong id="sovereignText"></strong>
</div>
<div class="legend">
<div class="legend-item">
<span class="legend-symbol">⚑</span>
<span>A flag marks each island's local champion.</span>
</div>
<div class="legend-item">
<span class="legend-symbol">✦</span>
<span>A star marks the tallest tower on the entire map.</span>
</div>
</div>
</aside>
<div class="controls-note">Drag to orbit · Scroll to zoom · Click a tower to answer</div>
<div id="toast" role="status" aria-live="polite"></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 GROUP_DEFS = [
{
name: "Coral Crown",
realm: "Sunken Reach",
color: 0xef786f,
land: 0xc58d67,
edge: 0x8d5f51
},
{
name: "Mosswatch",
realm: "Fern Hollow",
color: 0x70c79e,
land: 0x789b68,
edge: 0x4e7157
},
{
name: "Gilded Dunes",
realm: "Amber Sands",
color: 0xe4b75d,
land: 0xc6a565,
edge: 0x947447
},
{
name: "Blueglass Bay",
realm: "Tideglass Shore",
color: 0x67b8da,
land: 0x7297a4,
edge: 0x496c7b
},
{
name: "Frost Spire",
realm: "Silver North",
color: 0xb8d6e5,
land: 0x9eafb5,
edge: 0x6e818b
}
];
const TYPE_COPY = {
above: {
eyebrow: "Comparative clue · high side",
title: "Rise above the reference",
copy:
"Of the islands whose tallest tower rises above every tower in your sealed reference set, find the tallest tower.",
rule:
"Compare each island’s local champion with the highest tower in your reference set. Only champions above that ceiling qualify."
},
below: {
eyebrow: "Comparative clue · low side",
title: "Stay below the reference",
copy:
"Of the islands whose tallest tower stays below every tower in your sealed reference set, find the tallest tower.",
rule:
"A qualifying island must have its local champion below the lowest reference tower. Then choose the tallest qualifying champion."
},
outside: {
eyebrow: "Relative clue · excluded set",
title: "Look beyond the seal",
copy:
"Ignore the two sealed reference islands. Of all the remaining islands, find the tallest tower.",
rule:
"Exclude the entire reference set, then compare the local champions of the remaining islands."
},
inside: {
eyebrow: "Relative clue · sealed set",
title: "Search within the seal",
copy:
"Within your sealed reference set, find the tallest tower.",
rule:
"Compare only the towers belonging to the two islands you sealed before the clue appeared."
}
};
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071a22);
scene.fog = new THREE.Fog(0x071a22, 18, 43);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
$("scene").appendChild(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xc9f4ef, 0x263334, 2.25);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffe5b3, 3.25);
sun.position.set(8, 15, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -14;
sun.shadow.camera.right = 14;
sun.shadow.camera.top = 14;
sun.shadow.camera.bottom = -14;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 38;
sun.shadow.bias = -0.0004;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x5ac8d2, 1.4);
rim.position.set(-10, 6, -12);
scene.add(rim);
const boardRoot = new THREE.Group();
scene.add(boardRoot);
const ocean = new THREE.Mesh(
new THREE.CircleGeometry(34, 48),
new THREE.MeshStandardMaterial({
color: 0x0d5360,
roughness: 0.88,
metalness: 0.03,
flatShading: true
})
);
ocean.rotation.x = -Math.PI / 2;
ocean.position.y = -0.56;
ocean.receiveShadow = true;
scene.add(ocean);
const deepOcean = new THREE.Mesh(
new THREE.CircleGeometry(45, 32),
new THREE.MeshBasicMaterial({ color: 0x082f3b })
);
deepOcean.rotation.x = -Math.PI / 2;
deepOcean.position.y = -0.59;
scene.add(deepOcean);
const cloudMaterial = new THREE.MeshLambertMaterial({
color: 0xb9e1df,
transparent: true,
opacity: 0.22,
flatShading: true
});
const clouds = [];
for (let i = 0; i < 6; i++) {
const cloud = new THREE.Group();
const pieces = 3 + (i % 3);
for (let p = 0; p < pieces; p++) {
const puff = new THREE.Mesh(
new THREE.DodecahedronGeometry(0.75 + (p % 2) * 0.22, 0),
cloudMaterial
);
puff.position.set(p * 0.85, Math.sin(p * 1.7) * 0.2, 0);
puff.scale.y = 0.55;
cloud.add(puff);
}
cloud.position.set(
-16 + Math.random() * 32,
8 + Math.random() * 5,
-15 + Math.random() * 30
);
cloud.userData.speed = 0.12 + Math.random() * 0.16;
clouds.push(cloud);
scene.add(cloud);
}
const particleGeometry = new THREE.BoxGeometry(0.11, 0.11, 0.11);
const particleMaterial = new THREE.MeshBasicMaterial({ color: 0xf3c969 });
let groupStates = [];
let globalGroup = null;
let towerHitMeshes = [];
let islandHitMeshes = [];
let hoveredTowerId = null;
let toastTimer = null;
const state = {
phase: "select",
selectedRefs: new Set(),
puzzle: null,
score: 0,
round: 1,
streak: 0,
attempts: 0,
hinted: false,
lastGain: 0
};
const orbit = {
yaw: 0.48,
pitch: 0.64,
radius: window.innerWidth < 800 ? 22 : 18.5,
target: new THREE.Vector3(0, 2.1, 0)
};
function randomShuffle(array) {
const result = [...array];
for (let i = result.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[result[i], result[j]] = [result[j], result[i]];
}
return result;
}
function disposeTree(root) {
root.traverse((object) => {
if (object.geometry) object.geometry.dispose();
if (object.material) {
const materials = Array.isArray(object.material)
? object.material
: [object.material];
materials.forEach((material) => material.dispose());
}
});
}
function createFlag() {
const flag = new THREE.Group();
const pole = new THREE.Mesh(
new THREE.CylinderGeometry(0.025, 0.025, 0.82, 6),
new THREE.MeshStandardMaterial({
color: 0xe8d39c,
roughness: 0.6
})
);
pole.position.y = 0.41;
flag.add(pole);
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(
[0, 0, 0, 0.48, -0.11, 0, 0, -0.27, 0],
3
)
);
geometry.computeVertexNormals();
const cloth = new THREE.Mesh(
geometry,
new THREE.MeshStandardMaterial({
color: 0xf3c969,
side: THREE.DoubleSide,
roughness: 0.7,
flatShading: true
})
);
cloth.position.set(0.025, 0.77, 0);
flag.add(cloth);
return flag;
}
function createTower(groupState, towerData, isLocalMax) {
const height = towerData.height;
const tower = new THREE.Group();
tower.position.set(towerData.x, 0.255, towerData.z);
const bodyColor = new THREE.Color(groupState.color);
const roofColor = bodyColor.clone().multiplyScalar(0.58);
const bodyMaterial = new THREE.MeshStandardMaterial({
color: bodyColor,
roughness: 0.82,
metalness: 0.04,
flatShading: true,
emissive: 0x000000,
emissiveIntensity: 0
});
const roofMaterial = new THREE.MeshStandardMaterial({
color: roofColor,
roughness: 0.75,
flatShading: true
});
const trimMaterial = new THREE.MeshStandardMaterial({
color: 0xe9d6a5,
roughness: 0.62,
flatShading: true
});
const body = new THREE.Mesh(
new THREE.CylinderGeometry(0.32, 0.43, height, 6, 1, false),
bodyMaterial
);
body.position.y = height / 2;
body.castShadow = true;
body.receiveShadow = true;
body.userData.towerId = towerData.id;
tower.add(body);
towerHitMeshes.push(body);
const roof = new THREE.Mesh(
new THREE.ConeGeometry(0.57, 0.76, 6),
roofMaterial
);
roof.position.y = height + 0.38;
roof.castShadow = true;
roof.userData.towerId = towerData.id;
tower.add(roof);
towerHitMeshes.push(roof);
const band = new THREE.Mesh(
new THREE.CylinderGeometry(0.355, 0.375, 0.13, 6),
trimMaterial
);
band.position.y = height * 0.64;
band.castShadow = true;
tower.add(band);
const windowMaterial = new THREE.MeshBasicMaterial({ color: 0xffdc82 });
const windowHeight = Math.min(0.2, height * 0.13);
for (let i = 0; i < 2; i++) {
const windowMesh = new THREE.Mesh(
new THREE.BoxGeometry(0.13, windowHeight, 0.045),
windowMaterial
);
windowMesh.position.set(
i === 0 ? -0.13 : 0.13,
height * 0.42,
0.33
);
tower.add(windowMesh);
}
if (isLocalMax) {
const flag = createFlag();
flag.position.y = height + 0.73;
tower.add(flag);
towerData.flag = flag;
}
towerData.group = tower;
towerData.bodyMaterial = bodyMaterial;
towerData.flash = 0;
towerData.flashColor = new THREE.Color(0xffffff);
towerData.isLocalMax = isLocalMax;
groupState.root.add(tower);
return tower;
}
function createGroup(def, id, peak) {
const root = new THREE.Group();
const angle = (id / GROUP_DEFS.length) * Math.PI * 2 + 0.24;
const radius = 5.25;
root.position.set(Math.cos(angle) * radius, 0, Math.sin(angle) * radius);
root.userData.baseY = 0;
boardRoot.add(root);
const lower = new THREE.Mesh(
new THREE.CylinderGeometry(2.15, 2.28, 0.55, 10),
new THREE.MeshStandardMaterial({
color: def.edge,
roughness: 0.95,
flatShading: true
})
);
lower.position.y = -0.29;
lower.receiveShadow = true;
lower.castShadow = true;
lower.userData.groupId = id;
root.add(lower);
islandHitMeshes.push(lower);
const upper = new THREE.Mesh(
new THREE.CylinderGeometry(2.04, 2.13, 0.35, 10),
new THREE.MeshStandardMaterial({
color: def.land,
roughness: 0.96,
flatShading: true
})
);
upper.position.y = 0.075;
upper.receiveShadow = true;
root.add(upper);
const ringMaterial = new THREE.MeshBasicMaterial({
color: 0xf3c969,
transparent: true,
opacity: 0.9,
depthWrite: false
});
const referenceRing = new THREE.Mesh(
new THREE.TorusGeometry(2.13, 0.045, 4, 48),
ringMaterial
);
referenceRing.rotation.x = Math.PI / 2;
referenceRing.position.y = 0.275;
referenceRing.visible = false;
root.add(referenceRing);
const halo = new THREE.Mesh(
new THREE.CircleGeometry(2.07, 40),
new THREE.MeshBasicMaterial({
color: 0xf3c969,
transparent: true,
opacity: 0.075,
depthWrite: false,
side: THREE.DoubleSide
})
);
halo.rotation.x = -Math.PI / 2;
halo.position.y = 0.272;
halo.visible = false;
root.add(halo);
const wave = new THREE.Mesh(
new THREE.TorusGeometry(2.45, 0.025, 4, 48),
new THREE.MeshBasicMaterial({
color: 0x72d3cc,
transparent: true,
opacity: 0.22,
depthWrite: false
})
);
wave.rotation.x = Math.PI / 2;
wave.position.y = -0.48;
root.add(wave);
const rockMaterial = new THREE.MeshStandardMaterial({
color: def.edge,
roughness: 1,
flatShading: true
});
for (let i = 0; i < 7; i++) {
const rock = new THREE.Mesh(
new THREE.DodecahedronGeometry(0.13 + Math.random() * 0.11, 0),
rockMaterial
);
const rockAngle = (i / 7) * Math.PI * 2 + 0.2;
rock.position.set(
Math.cos(rockAngle) * 1.76,
0.25,
Math.sin(rockAngle) * 1.76
);
rock.scale.y = 0.65 + Math.random() * 0.35;
rock.rotation.set(Math.random(), Math.random(), Math.random());
rock.castShadow = true;
root.add(rock);
}
const compassStone = new THREE.Mesh(
new THREE.CylinderGeometry(0.26, 0.32, 0.18, 6),
new THREE.MeshStandardMaterial({
color: 0xc7b991,
roughness: 0.85,
flatShading: true
})
);
compassStone.position.y = 0.34;
compassStone.castShadow = true;
root.add(compassStone);
const groupState = {
...def,
id,
root,
referenceRing,
halo,
towers: [],
max: 0,
maxTower: null
};
const positions = [
[-0.73, -0.61],
[0.72, -0.57],
[-0.62, 0.72],
[0.72, 0.68]
];
const factors = randomShuffle([0.48, 0.63, 0.78, 0.88]);
const heights = [peak];
for (let i = 0; i < 3; i++) {
heights.push(
Number(
Math.max(
1.65,
peak * factors[i] + (Math.random() - 0.5) * 0.24
).toFixed(2)
)
);
}
for (let i = 0; i < 4; i++) {
const towerData = {
id: `tower-${id}-${i}`,
groupId: id,
height: heights[i],
x: positions[i][0],
z: positions[i][1],
isLocalMax: heights[i] === peak
};
groupState.towers.push(towerData);
}
groupState.towers.forEach((tower) => {
createTower(groupState, tower, tower.isLocalMax);
});
groupState.maxTower = groupState.towers.reduce((best, tower) =>
tower.height > best.height ? tower : best
);
groupState.max = groupState.maxTower.height;
return groupState;
}
function buildBoard() {
disposeTree(boardRoot);
boardRoot.clear();
towerHitMeshes = [];
islandHitMeshes = [];
const peaks = randomShuffle([3.8, 5.0, 6.3, 7.7, 9.2]);
groupStates = GROUP_DEFS.map((def, id) =>
createGroup(def, id, peaks[id])
);
globalGroup = groupStates.reduce((best, group) =>
group.max > best.max ? group : best
);
const star = new THREE.Mesh(
new THREE.OctahedronGeometry(0.29, 0),
new THREE.MeshStandardMaterial({
color: 0xffdf7a,
emissive: 0xa86b0c,
emissiveIntensity: 0.55,
roughness: 0.25,
metalness: 0.25,
flatShading: true
})
);
star.position.y = globalGroup.maxTower.height + 1.62;
star.userData.baseY = star.position.y;
star.castShadow = true;
globalGroup.maxTower.group.add(star);
globalGroup.star = star;
$("sovereignText").textContent =
`${globalGroup.name} · ${globalGroup.max.toFixed(1)}m`;
}
function setupGroupCards() {
$("groupList").innerHTML = "";
GROUP_DEFS.forEach((def, id) => {
const button = document.createElement("button");
button.type = "button";
button.className = "group-card";
button.style.setProperty("--card-color", `#${def.color.toString(16).padStart(6, "0")}`);
button.innerHTML = `
<span class="group-card__index">◆</span>
<span>
<span class="group-card__name">${def.name}</span>
<span class="group-card__meta">4 towers</span>
</span>
<span class="group-card__state">Open</span>
`;
button.addEventListener("click", () => toggleReference(id));
$("groupList").appendChild(button);
});
}
function announce(message, isError = false) {
const toast = $("toast");
toast.textContent = message;
toast.classList.toggle("error", isError);
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove("show"), 2400);
}
function toggleReference(groupId) {
if (state.phase !== "select") return;
if (state.selectedRefs.has(groupId)) {
state.selectedRefs.delete(groupId);
} else {
if (state.selectedRefs.size >= 2) {
announce("Your reference set holds two islands. Deselect one first.", true);
return;
}
state.selectedRefs.add(groupId);
}
renderUI();
}
function buildPuzzle(referenceIds) {
const referenceTowers = referenceIds.flatMap((id) => groupStates[id].towers);
const referenceMax = Math.max(...referenceTowers.map((tower) => tower.height));
const referenceMin = Math.min(...referenceTowers.map((tower) => tower.height));
const outside = groupStates
.filter((group) => !referenceIds.includes(group.id))
.sort((a, b) => b.max - a.max);
const above = outside
.filter((group) => group.max > referenceMax)
.sort((a, b) => b.max - a.max);
const below = outside
.filter((group) => group.max < referenceMin)
.sort((a, b) => b.max - a.max);
const compareOptions = [];
if (above.length) {
compareOptions.push({
type: "above",
targetTower: above[0].maxTower,
targetGroupId: above[0].id
});
}
if (below.length) {
compareOptions.push({
type: "below",
targetTower: below[0].maxTower,
targetGroupId: below[0].id
});
}
const outsideOption = {
type: "outside",
targetTower: outside[0].maxTower,
targetGroupId: outside[0].id
};
const insideTower = referenceTowers.reduce((best, tower) =>
tower.height > best.height ? tower : best
);
const insideOption = {
type: "inside",
targetTower: insideTower,
targetGroupId: insideTower.groupId
};
let chosen;
if (compareOptions.length && Math.random() < 0.76) {
chosen = compareOptions[Math.floor(Math.random() * compareOptions.length)];
} else if (Math.random() < 0.18) {
chosen = insideOption;
} else {
chosen = outsideOption;
}
return {
...chosen,
referenceIds: [...referenceIds],
referenceMax,
referenceMin,
eligibleCount:
chosen.type === "above"
? above.length
: chosen.type === "below"
? below.length
: 3
};
}
function sealReference() {
if (state.phase !== "select" || state.selectedRefs.size !== 2) return;
state.puzzle = buildPuzzle([...state.selectedRefs]);
state.phase = "clue";
state.hinted = false;
state.attempts = 0;
renderUI();
announce("Reference sealed. Compare the island champions and choose a tower.");
}
function showHint() {
if (state.phase !== "clue" || !state.puzzle) return;
const puzzle = state.puzzle;
let message = "";
if (puzzle.type === "above") {
message =
`The reference ceiling is ${puzzle.referenceMax.toFixed(1)}m. ` +
`A qualifying island's local champion must be higher than ${puzzle.referenceMax.toFixed(1)}m.`;
} else if (puzzle.type === "below") {
message =
`The reference floor is ${puzzle.referenceMin.toFixed(1)}m. ` +
`A qualifying island's local champion must be lower than ${puzzle.referenceMin.toFixed(1)}m.`;
} else if (puzzle.type === "outside") {
message =
"The reference islands are excluded completely. Compare the local champions of the other three islands.";
} else {
message =
"Only the towers on your two sealed islands are eligible. Find the highest tower among them.";
}
state.hinted = true;
$("ruleText").textContent = message;
announce("Comparison scales revealed. The answer is still yours to find.");
}
function checkTower(towerId) {
if (state.phase !== "clue" || !state.puzzle) return;
const tower = groupStates
.flatMap((group) => group.towers)
.find((candidate) => candidate.id === towerId);
if (!tower) return;
if (tower.id === state.puzzle.targetTower.id) {
state.phase = "win";
state.streak += 1;
state.lastGain = 100 + Math.min(state.streak - 1, 4) * 25;
state.score += state.lastGain;
tower.flash = 1;
tower.flashColor.set(0xf3c969);
revealTreasure(tower);
renderUI();
announce(
`Treasure claimed! +${state.lastGain} points. The answer was ${groupStates[tower.groupId].name}.`
);
} else {
state.attempts += 1;
tower.flash = 1;
tower.flashColor.set(0xff554d);
announce("Not that tower. Compare the local champion of each eligible island.", true);
}
}
function nextChallenge() {
if (state.phase !== "win") return;
state.round += 1;
state.phase = "select";
state.selectedRefs.clear();
state.puzzle = null;
state.hinted = false;
state.attempts = 0;
renderUI();
announce("A new clue is waiting. Choose a fresh two-island reference set.");
}
function newMap() {
if (state.phase === "clue") return;
buildBoard();
state.phase = "select";
state.selectedRefs.clear();
state.puzzle = null;
state.round = 1;
state.streak = 0;
state.hinted = false;
state.attempts = 0;
renderUI();
announce("The islands have been reshuffled. Seal a new reference set.");
}
function updateStep(id, status) {
const element = $(id);
element.classList.toggle("active", status === "active");
element.classList.toggle("done", status === "done");
}
function renderUI() {
$("score").textContent = state.score;
$("round").textContent = state.round;
$("streak").textContent = state.streak;
if (state.phase === "select") {
updateStep("step1", "active");
updateStep("step2", "");
updateStep("step3", "");
$("phaseLabel").textContent = "Reference";
$("clueEyebrow").textContent = "Step 1 · Reference first";
$("clueTitle").textContent = "Choose two islands";
$("clueCopy").textContent =
"Choose the islands that will act as your measuring sticks. Their tower heights define every comparison. The treasure clue remains hidden until you commit.";
$("ruleText").textContent =
"A local champion is the tallest tower on one island. The map sovereign is the tallest tower overall. Choose two islands before evaluating the clue.";
$("statusText").textContent =
`${state.selectedRefs.size} of 2 reference islands selected · clue still hidden`;
$("primaryBtn").hidden = false;
$("primaryBtn").disabled = state.selectedRefs.size !== 2;
$("primaryBtn").textContent = "Seal reference & reveal clue";
$("hintBtn").hidden = true;
$("shuffleBtn").disabled = false;
$("sealBadge").textContent = "Unsealed";
} else if (state.phase === "clue") {
updateStep("step1", "done");
updateStep("step2", "active");
updateStep("step3", "");
$("phaseLabel").textContent = "Compare";
const copy = TYPE_COPY[state.puzzle.type];
$("clueEyebrow").textContent = copy.eyebrow;
$("clueTitle").textContent = copy.title;
$("clueCopy").textContent = copy.copy;
$("ruleText").textContent = copy.rule;
$("statusText").textContent =
"Click the tower itself · island stones and water are scenery";
$("primaryBtn").hidden = true;
$("hintBtn").hidden = false;
$("shuffleBtn").disabled = true;
$("sealBadge").textContent = "Sealed";
} else {
updateStep("step1", "done");
updateStep("step2", "done");
updateStep("step3", "active");
$("phaseLabel").textContent = "Treasure";
const targetGroup = groupStates[state.puzzle.targetGroupId];
$("clueEyebrow").textContent = "Treasure recovered";
$("clueTitle").textContent = "The crown is yours";
$("clueCopy").textContent =
`You correctly identified the ${targetGroup.name} tower. Its height is ${state.puzzle.targetTower.height.toFixed(1)}m.`;
$("ruleText").textContent =
`+${state.lastGain} points · ${state.streak} challenge streak · The next clue will use a newly sealed reference set.`;
$("statusText").textContent =
"Choose another reference pair for the next treasure clue";
$("primaryBtn").hidden = false;
$("primaryBtn").disabled = false;
$("primaryBtn").textContent = "Next treasure challenge";
$("hintBtn").hidden = true;
$("shuffleBtn").disabled = false;
$("sealBadge").textContent = "Complete";
}
const selectedOrder = [...state.selectedRefs];
[0, 1].forEach((slotIndex) => {
const slot = $(`refSlot${slotIndex}`);
const groupId = selectedOrder[slotIndex];
const group = groupId === undefined ? null : groupStates[groupId];
slot.classList.toggle("filled", Boolean(group));
slot.querySelector(".slot-name").textContent = group
? group.name
: "Choose an island";
slot.querySelector(".slot-meta").textContent = group
? state.phase === "select"
? group.realm
: `local max ${group.max.toFixed(1)}m`
: "Awaiting selection";
});
groupStates.forEach((group) => {
const card = $("groupList").children[group.id];
const selected = state.selectedRefs.has(group.id);
const selectionNumber = selectedOrder.indexOf(group.id);
card.classList.toggle("selected", selected);
card.disabled = state.phase !== "select";
card.setAttribute("aria-pressed", String(selected));
card.querySelector(".group-card__index").textContent = selected
? String(selectionNumber + 1)
: "◆";
card.querySelector(".group-card__meta").textContent =
state.phase === "select"
? "4 towers · click to reference"
: `local max ${group.max.toFixed(1)}m`;
card.querySelector(".group-card__state").textContent = selected
? state.phase === "select"
? "Reference"
: "Sealed"
: "Open";
group.referenceRing.visible = selected;
group.halo.visible = selected;
group.referenceRing.material.color.set(
state.phase === "select" ? 0xf3c969 : 0x67dfcf
);
group.halo.material.color.set(
state.phase === "select" ? 0xf3c969 : 0x67dfcf
);
});
}
const treasures = [];
const particles = [];
function revealTreasure(tower) {
const chest = new THREE.Group();
chest.position.set(0.52, 0.08, 0.31);
chest.scale.setScalar(0.01);
const wood = new THREE.MeshStandardMaterial({
color: 0x704328,
roughness: 0.78,
flatShading: true
});
const gold = new THREE.MeshStandardMaterial({
color: 0xf2c75c,
roughness: 0.28,
metalness: 0.55,
flatShading: true
});
const body = new THREE.Mesh(
new THREE.BoxGeometry(0.58, 0.37, 0.44),
wood
);
body.position.y = 0.185;
body.castShadow = true;
chest.add(body);
const bandA = new THREE.Mesh(
new THREE.BoxGeometry(0.07, 0.39, 0.46),
gold
);
bandA.position.set(-0.17, 0.185, 0);
chest.add(bandA);
const bandB = bandA.clone();
bandB.position.x = 0.17;
chest.add(bandB);
const lidPivot = new THREE.Group();
lidPivot.position.set(0, 0.37, -0.2);
const lid = new THREE.Mesh(
new THREE.BoxGeometry(0.6, 0.16, 0.45),
wood
);
lid.position.set(0, 0.05, 0.2);
lid.castShadow = true;
lidPivot.add(lid);
const lidBand = new THREE.Mesh(
new THREE.BoxGeometry(0.07, 0.18, 0.47),
gold
);
lidBand.position.set(0, 0.05, 0.2);
lidPivot.add(lidBand);
chest.add(lidPivot);
for (let i = 0; i < 5; i++) {
const coin = new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.055, 0.025, 7),
gold
);
coin.rotation.x = Math.PI / 2;
coin.position.set(
-0.16 + i * 0.08,
0.34 + (i % 2) * 0.025,
(i % 3 - 1) * 0.07
);
chest.add(coin);
}
tower.group.add(chest);
treasures.push({ root: chest, lid: lidPivot, age: 0 });
tower.group.updateMatrixWorld(true);
const worldPosition = new THREE.Vector3();
chest.getWorldPosition(worldPosition);
for (let i = 0; i < 38; i++) {
const particle = new THREE.Mesh(
particleGeometry,
particleMaterial
);
particle.position.copy(worldPosition);
particle.position.y += 0.4;
const angle = Math.random() * Math.PI * 2;
const speed = 1.6 + Math.random() * 3.3;
particles.push({
mesh: particle,
velocity: new THREE.Vector3(
Math.cos(angle) * speed,
2.4 + Math.random() * 4,
Math.sin(angle) * speed
),
spin: new THREE.Vector3(
Math.random() * 8,
Math.random() * 8,
Math.random() * 8
),
age: 0
});
scene.add(particle);
}
}
function updateCamera() {
const horizontal = orbit.radius * Math.cos(orbit.pitch);
camera.position.set(
orbit.target.x + Math.sin(orbit.yaw) * horizontal,
orbit.target.y + Math.sin(orbit.pitch) * orbit.radius,
orbit.target.z + Math.cos(orbit.yaw) * horizontal
);
camera.lookAt(orbit.target);
}
updateCamera();
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function getHit(clientX, clientY) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(
[...towerHitMeshes, ...islandHitMeshes],
false
);
return hits[0] || null;
}
function handlePick(hit) {
if (!hit) return;
const object = hit.object;
if (object.userData.towerId !== undefined) {
const tower = groupStates
.flatMap((group) => group.towers)
.find((candidate) => candidate.id === object.userData.towerId);
if (!tower) return;
if (state.phase === "select") {
toggleReference(tower.groupId);
} else if (state.phase === "clue") {
checkTower(tower.id);
}
} else if (object.userData.groupId !== undefined) {
if (state.phase === "select") {
toggleReference(object.userData.groupId);
}
}
}
const pointerState = {
down: false,
moved: false,
lastX: 0,
lastY: 0,
startX: 0,
startY: 0
};
renderer.domElement.addEventListener("pointerdown", (event) => {
pointerState.down = true;
pointerState.moved = false;
pointerState.lastX = event.clientX;
pointerState.lastY = event.clientY;
pointerState.startX = event.clientX;
pointerState.startY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (pointerState.down) {
const dx = event.clientX - pointerState.lastX;
const dy = event.clientY - pointerState.lastY;
const travel = Math.hypot(
event.clientX - pointerState.startX,
event.clientY - pointerState.startY
);
if (travel > 5) pointerState.moved = true;
orbit.yaw -= dx * 0.005;
orbit.pitch = THREE.MathUtils.clamp(
orbit.pitch + dy * 0.004,
0.28,
1.13
);
pointerState.lastX = event.clientX;
pointerState.lastY = event.clientY;
updateCamera();
} else {
const hit = getHit(event.clientX, event.clientY);
const nextHover =
hit && hit.object.userData.towerId !== undefined
? hit.object.userData.towerId
: null;
hoveredTowerId = nextHover;
renderer.domElement.style.cursor = nextHover ? "pointer" : "grab";
}
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (pointerState.down && !pointerState.moved) {
handlePick(getHit(event.clientX, event.clientY));
}
pointerState.down = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerState.down = false;
pointerState.moved = false;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius + event.deltaY * 0.012,
12.5,
28
);
updateCamera();
},
{ passive: false }
);
$("primaryBtn").addEventListener("click", () => {
if (state.phase === "select") {
sealReference();
} else if (state.phase === "win") {
nextChallenge();
}
});
$("hintBtn").addEventListener("click", showHint);
$("shuffleBtn").addEventListener("click", newMap);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
});
const clock = new THREE.Clock();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
const time = clock.elapsedTime;
groupStates.forEach((group, index) => {
group.root.position.y =
Math.sin(time * 0.7 + index * 1.7) * 0.025;
if (group.star) {
group.star.rotation.y += delta * 1.5;
group.star.position.y =
group.star.userData.baseY + Math.sin(time * 2.2) * 0.09;
}
group.towers.forEach((tower) => {
if (tower.flag) {
tower.flag.rotation.y = Math.sin(time * 1.8 + index) * 0.12;
}
if (tower.flash > 0) {
tower.flash = Math.max(0, tower.flash - delta * 1.8);
tower.bodyMaterial.emissive.copy(tower.flashColor);
tower.bodyMaterial.emissiveIntensity = tower.flash * 0.9;
} else if (hoveredTowerId === tower.id) {
tower.bodyMaterial.emissive.set(0x2f786d);
tower.bodyMaterial.emissiveIntensity = 0.34;
} else {
tower.bodyMaterial.emissive.set(0x000000);
tower.bodyMaterial.emissiveIntensity = 0;
}
});
});
clouds.forEach((cloud) => {
cloud.position.x += cloud.userData.speed * delta;
if (cloud.position.x > 19) cloud.position.x = -19;
});
for (let i = treasures.length - 1; i >= 0; i--) {
const treasure = treasures[i];
treasure.age += delta;
const scale = Math.min(1, treasure.age * 2.8);
treasure.root.scale.setScalar(scale);
treasure.lid.rotation.x = -Math.min(1.15, treasure.age * 1.9);
}
for (let i = particles.length - 1; i >= 0; i--) {
const particle = particles[i];
particle.age += delta;
particle.velocity.y -= 7.2 * delta;
particle.mesh.position.addScaledVector(particle.velocity, delta);
particle.mesh.rotation.x += particle.spin.x * delta;
particle.mesh.rotation.y += particle.spin.y * delta;
particle.mesh.rotation.z += particle.spin.z * delta;
if (particle.age > 2.4) {
scene.remove(particle.mesh);
particles.splice(i, 1);
}
}
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
setupGroupCards();
buildBoard();
renderUI();
animate();
</script>
</body>
</html>
```