Prompt specification
Build or customize the Three.js 3D scene "Two Tokens Compete for One Empty Cell" 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: Two Tokens Compete for One Empty Cell
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/two-tokens-compete-for-one-empty-cell-x3589
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing turn order and exclusive occupancy of a target cell. Place two tokens beside a shared target cell and a turn-order marker. Show the occupancy rule before either token moves into the space. Voxel style.
Working Three.js Starter (two-tokens-compete-for-one-empty-cell-x3589):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>One Cell, One Token</title>
<style>
:root {
--ink: #182438;
--paper: #f6f0df;
--azure: #22b8cf;
--coral: #ff6b5d;
--gold: #f5c84b;
--green: #39c979;
--active: var(--azure);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--paper);
color: var(--ink);
font-family: "Courier New", Courier, monospace;
}
body {
position: relative;
}
#stage {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at center, transparent 48%, rgba(24, 36, 56, 0.12) 100%);
z-index: 1;
}
.brand {
position: fixed;
z-index: 5;
top: 22px;
left: 24px;
max-width: 470px;
pointer-events: none;
}
.eyebrow {
display: inline-block;
margin-bottom: 8px;
padding: 5px 9px;
border: 3px solid var(--ink);
background: var(--gold);
box-shadow: 4px 4px 0 var(--ink);
font-size: 11px;
font-weight: 900;
letter-spacing: 2px;
}
h1 {
margin: 0;
font-size: clamp(26px, 4vw, 52px);
line-height: 0.94;
letter-spacing: -2px;
text-transform: uppercase;
text-shadow: 3px 3px 0 rgba(255, 255, 255, 0.75);
}
.subtitle {
margin: 10px 0 0;
font-size: 12px;
font-weight: 700;
letter-spacing: 1.5px;
text-transform: uppercase;
}
.rule-card {
position: fixed;
z-index: 5;
top: 22px;
right: 24px;
width: min(340px, calc(100vw - 48px));
padding: 18px;
border: 3px solid var(--ink);
background: rgba(246, 240, 223, 0.96);
box-shadow: 7px 7px 0 var(--ink);
}
.rule-heading {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 10px;
}
.rule-icon {
display: grid;
grid-template-columns: repeat(2, 8px);
grid-template-rows: repeat(2, 8px);
gap: 3px;
flex: 0 0 auto;
padding: 7px;
border: 2px solid var(--ink);
background: var(--gold);
}
.rule-icon span {
background: var(--ink);
}
.rule-card h2 {
margin: 0;
font-size: 17px;
line-height: 1;
text-transform: uppercase;
}
.rule-card p {
margin: 0 0 14px;
font-family: Arial, sans-serif;
font-size: 14px;
line-height: 1.45;
font-weight: 600;
}
.occupancy {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 10px;
border: 2px solid var(--ink);
background: #fffaf0;
font-size: 11px;
font-weight: 900;
letter-spacing: 1px;
text-transform: uppercase;
}
.slots {
display: flex;
gap: 5px;
}
.slot {
width: 20px;
height: 20px;
border: 2px solid var(--ink);
background: transparent;
transition: background 180ms ease;
}
.slot.filled {
background: var(--green);
}
.rule-note {
margin-top: 10px !important;
margin-bottom: 0 !important;
color: #5a6270;
font-family: "Courier New", monospace !important;
font-size: 10px !important;
font-weight: 900 !important;
letter-spacing: 0.7px;
text-transform: uppercase;
}
.control-panel {
position: fixed;
z-index: 6;
left: 50%;
bottom: 22px;
width: min(760px, calc(100vw - 32px));
transform: translateX(-50%);
display: grid;
grid-template-columns: 1fr auto;
gap: 16px;
align-items: center;
padding: 14px;
border: 3px solid var(--ink);
background: rgba(246, 240, 223, 0.96);
box-shadow: 7px 7px 0 var(--ink);
}
.turn-row {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 8px;
font-size: 11px;
font-weight: 900;
letter-spacing: 1px;
text-transform: uppercase;
}
.turn-step {
padding: 5px 8px;
border: 2px solid var(--ink);
background: #e5e0d3;
opacity: 0.55;
}
.turn-step.active {
background: var(--active);
opacity: 1;
box-shadow: 3px 3px 0 var(--ink);
}
.turn-step.complete {
background: var(--green);
opacity: 1;
}
.turn-arrow {
font-weight: 900;
}
#status {
min-height: 20px;
font-size: 14px;
font-weight: 900;
text-transform: uppercase;
}
#status-detail {
margin-top: 4px;
color: #596170;
font-family: Arial, sans-serif;
font-size: 12px;
font-weight: 600;
}
.actions {
display: flex;
gap: 9px;
}
button {
min-height: 47px;
padding: 10px 15px;
border: 3px solid var(--ink);
border-radius: 0;
background: var(--active);
color: var(--ink);
box-shadow: 4px 4px 0 var(--ink);
font-family: "Courier New", monospace;
font-size: 12px;
font-weight: 900;
letter-spacing: 0.5px;
text-transform: uppercase;
cursor: pointer;
transition:
transform 100ms ease,
box-shadow 100ms ease,
opacity 150ms ease;
}
button:hover:not(:disabled) {
transform: translate(-1px, -1px);
box-shadow: 5px 5px 0 var(--ink);
}
button:active:not(:disabled) {
transform: translate(3px, 3px);
box-shadow: 1px 1px 0 var(--ink);
}
button:disabled {
cursor: wait;
opacity: 0.6;
}
#reset-button {
background: #fffaf0;
}
.camera-hint {
position: fixed;
z-index: 4;
right: 25px;
bottom: 25px;
padding: 7px 9px;
border: 2px solid var(--ink);
background: rgba(246, 240, 223, 0.82);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.7px;
text-transform: uppercase;
pointer-events: none;
}
.scene-label {
position: fixed;
z-index: 3;
left: 0;
top: 0;
pointer-events: none;
white-space: nowrap;
will-change: transform;
}
.target-label {
padding: 5px 8px;
border: 2px solid var(--ink);
background: var(--gold);
box-shadow: 3px 3px 0 var(--ink);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.6px;
text-transform: uppercase;
}
.target-label.occupied {
background: var(--green);
}
.token-label {
padding: 4px 7px;
border: 2px solid var(--ink);
background: #fffaf0;
box-shadow: 3px 3px 0 var(--ink);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.6px;
text-transform: uppercase;
}
.token-label.azure {
background: var(--azure);
}
.token-label.coral {
background: var(--coral);
}
.turn-label {
padding: 4px 7px;
border: 2px solid var(--ink);
background: var(--gold);
box-shadow: 3px 3px 0 var(--ink);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.8px;
text-transform: uppercase;
}
#toast {
position: fixed;
z-index: 10;
top: 43%;
left: 50%;
padding: 13px 20px;
transform: translate(-50%, -50%) scale(0.7) rotate(-2deg);
border: 4px solid var(--ink);
background: var(--coral);
box-shadow: 7px 7px 0 var(--ink);
opacity: 0;
pointer-events: none;
font-size: clamp(18px, 3vw, 30px);
font-weight: 900;
letter-spacing: 1px;
text-transform: uppercase;
}
#toast.show {
animation: deny-pop 900ms ease both;
}
@keyframes deny-pop {
0% {
opacity: 0;
transform: translate(-50%, -50%) scale(0.5) rotate(-5deg);
}
18% {
opacity: 1;
transform: translate(-50%, -50%) scale(1.08) rotate(2deg);
}
70% {
opacity: 1;
transform: translate(-50%, -50%) scale(1) rotate(-2deg);
}
100% {
opacity: 0;
transform: translate(-50%, -50%) scale(0.95) rotate(-2deg);
}
}
#loading {
position: fixed;
z-index: 20;
inset: 0;
display: grid;
place-items: center;
background: var(--paper);
font-weight: 900;
letter-spacing: 2px;
text-transform: uppercase;
}
@media (max-width: 820px) {
.brand {
top: 14px;
left: 14px;
}
h1 {
max-width: 350px;
}
.rule-card {
top: auto;
right: 14px;
bottom: 146px;
width: min(300px, calc(100vw - 28px));
padding: 13px;
}
.rule-card p {
margin-bottom: 10px;
font-size: 12px;
}
.control-panel {
bottom: 14px;
grid-template-columns: 1fr;
gap: 10px;
}
.actions {
width: 100%;
}
button {
flex: 1;
}
.camera-hint {
display: none;
}
}
@media (max-width: 520px) {
.subtitle {
display: none;
}
.eyebrow {
font-size: 9px;
}
.rule-card {
bottom: 178px;
}
.rule-note {
display: none;
}
#status {
font-size: 12px;
}
#status-detail {
font-size: 11px;
}
.turn-row {
gap: 5px;
font-size: 9px;
}
.turn-step {
padding: 4px 5px;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<div class="vignette"></div>
<header class="brand">
<div class="eyebrow">Turn Order Demo</div>
<h1>One Cell,<br />One Token</h1>
<p class="subtitle">Exclusive occupancy / voxel board</p>
</header>
<section class="rule-card" aria-label="Occupancy rule">
<div class="rule-heading">
<div class="rule-icon" aria-hidden="true">
<span></span><span></span><span></span><span></span>
</div>
<h2>Exclusive Occupancy</h2>
</div>
<p>
A target cell has capacity <strong>1</strong>. The first token to enter
claims it. Any later entry is rejected.
</p>
<div class="occupancy">
<span>Target capacity</span>
<div class="slots" aria-label="Zero of one target slots occupied">
<span class="slot" id="target-slot"></span>
<span id="capacity-text">0 / 1</span>
</div>
</div>
<p class="rule-note">Rule shown before either token moves</p>
</section>
<section class="control-panel" aria-label="Turn controls">
<div>
<div class="turn-row">
<span>Turn order</span>
<span class="turn-step active" id="step-azure">1 Azure</span>
<span class="turn-arrow">→</span>
<span class="turn-step" id="step-coral">2 Coral</span>
</div>
<div id="status" aria-live="polite">Azure has turn 1</div>
<div id="status-detail">
The shared target is vacant. The first entry will claim it.
</div>
</div>
<div class="actions">
<button id="move-button" type="button">Move Azure → target</button>
<button id="reset-button" type="button">Reset</button>
</div>
</section>
<div id="target-label" class="scene-label target-label">Shared target · vacant</div>
<div id="label-azure" class="scene-label token-label azure">Azure · A</div>
<div id="label-coral" class="scene-label token-label coral">Coral · B</div>
<div id="turn-label" class="scene-label turn-label">Turn 1</div>
<div id="toast">Entry denied</div>
<div class="camera-hint">Drag to orbit · Scroll to zoom</div>
<div id="loading">Building voxel board…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const stage = document.querySelector("#stage");
const renderer = new THREE.WebGLRenderer({ antialias: true });
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.15;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf1ead8);
scene.fog = new THREE.Fog(0xf1ead8, 17, 32);
const camera = new THREE.OrthographicCamera(-7, 7, 5, -5, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 0.55, 0);
let cameraYaw = 0.68;
let cameraPitch = 0.72;
function updateCamera() {
const horizontal = Math.sin(cameraPitch);
const offset = new THREE.Vector3(
Math.sin(cameraYaw) * horizontal,
Math.cos(cameraPitch),
Math.cos(cameraYaw) * horizontal
);
camera.position.copy(cameraTarget).addScaledVector(offset, 14);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xfff8df, 0x51627a, 2.25);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1d1, 3.4);
sun.position.set(-5, 10, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -8;
sun.shadow.camera.right = 8;
sun.shadow.camera.top = 8;
sun.shadow.camera.bottom = -8;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 30;
sun.shadow.bias = -0.0004;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x72d9ef, 1.3);
rim.position.set(7, 5, -8);
scene.add(rim);
// -------------------------------------------------------------------------
// Materials and voxel construction helpers
// -------------------------------------------------------------------------
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x182438,
transparent: true,
opacity: 0.78
});
function standard(color, extra = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0.02,
flatShading: true,
...extra
});
}
function voxel(parent, size, position, material, outlined = false) {
const geometry = new THREE.BoxGeometry(size.x, size.y, size.z);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.copy(position);
mesh.castShadow = true;
mesh.receiveShadow = true;
if (outlined) {
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry),
edgeMaterial
);
mesh.add(edges);
}
parent.add(mesh);
return mesh;
}
const v = (x, y, z) => new THREE.Vector3(x, y, z);
// -------------------------------------------------------------------------
// Board and floor
// -------------------------------------------------------------------------
const floorMaterial = standard(0xd8dfd8);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
floorMaterial
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.55;
floor.receiveShadow = true;
scene.add(floor);
const board = new THREE.Group();
scene.add(board);
const boardBaseMaterial = standard(0x182438);
voxel(
board,
v(9.35, 0.46, 7.35),
v(0, -0.23, 0),
boardBaseMaterial,
true
);
const tileMaterials = [
standard(0xe6e6da),
standard(0xd6ded7),
standard(0xcdd8d2),
standard(0xedf0e6)
];
const tileGeometry = new THREE.BoxGeometry(0.94, 0.16, 0.94);
for (let x = -4; x <= 4; x++) {
for (let z = -3; z <= 3; z++) {
if (x === 0 && z === 0) continue;
const tile = new THREE.Mesh(
tileGeometry,
tileMaterials[(x * 7 + z * 3 + 20) % tileMaterials.length]
);
tile.position.set(x, 0.02, z);
tile.receiveShadow = true;
board.add(tile);
}
}
// Small voxel scenery around the board corners.
const sceneryDark = standard(0x66788a);
const sceneryLight = standard(0x9fb2b6);
voxel(board, v(0.42, 0.42, 0.42), v(-4.95, -0.03, -3.65), sceneryDark, true);
voxel(board, v(0.28, 0.65, 0.28), v(-4.95, 0.31, -3.65), sceneryLight, true);
voxel(board, v(0.30, 0.30, 0.30), v(-4.95, 0.78, -3.65), sceneryDark, true);
voxel(board, v(0.46, 0.46, 0.46), v(4.95, -0.01, 3.65), sceneryDark, true);
voxel(board, v(0.28, 0.70, 0.28), v(4.95, 0.35, 3.65), sceneryLight, true);
// -------------------------------------------------------------------------
// Exclusive target cell
// -------------------------------------------------------------------------
const target = new THREE.Group();
target.position.set(0, 0, 0);
board.add(target);
const targetFloorMaterial = standard(0xffe8a3, {
emissive: 0x7a5200,
emissiveIntensity: 0.12
});
const targetEdgeMaterial = standard(0xf2b84b, {
emissive: 0x9c6500,
emissiveIntensity: 0.15
});
voxel(
target,
v(0.88, 0.16, 0.88),
v(0, 0.14, 0),
targetFloorMaterial
);
// Four border beams and four corner blocks form a one-capacity cell.
voxel(target, v(0.88, 0.10, 0.07), v(0, 0.23, 0.44), targetEdgeMaterial);
voxel(target, v(0.88, 0.10, 0.07), v(0, 0.23, -0.44), targetEdgeMaterial);
voxel(target, v(0.07, 0.10, 0.88), v(0.44, 0.23, 0), targetEdgeMaterial);
voxel(target, v(0.07, 0.10, 0.88), v(-0.44, 0.23, 0), targetEdgeMaterial);
for (const x of [-0.44, 0.44]) {
for (const z of [-0.44, 0.44]) {
voxel(
target,
v(0.17, 0.32, 0.17),
v(x, 0.29, z),
targetEdgeMaterial,
true
);
}
}
// -------------------------------------------------------------------------
// Voxel tokens
// -------------------------------------------------------------------------
function createToken(mainColor, accentColor, name) {
const group = new THREE.Group();
const main = standard(mainColor);
const accent = standard(accentColor);
const dark = standard(0x182438);
const light = standard(0xfff8df, {
emissive: 0xfff2bd,
emissiveIntensity: 0.4
});
voxel(group, v(0.78, 0.18, 0.78), v(0, 0.19, 0), dark, true);
voxel(group, v(0.66, 0.55, 0.58), v(0, 0.66, 0), main, true);
voxel(group, v(0.68, 0.46, 0.62), v(0, 1.16, 0), accent, true);
// Arms.
voxel(group, v(0.20, 0.46, 0.25), v(-0.43, 0.67, 0), main, true);
voxel(group, v(0.20, 0.46, 0.25), v(0.43, 0.67, 0), main, true);
// Visor and eyes.
voxel(group, v(0.57, 0.17, 0.07), v(0, 1.18, 0.32), dark);
voxel(group, v(0.09, 0.09, 0.04), v(-0.15, 1.19, 0.37), light);
voxel(group, v(0.09, 0.09, 0.04), v(0.15, 1.19, 0.37), light);
// Antenna.
voxel(group, v(0.10, 0.19, 0.10), v(0, 1.49, 0), dark);
voxel(group, v(0.17, 0.17, 0.17), v(0, 1.66, 0), accent, true);
// Small feet.
voxel(group, v(0.24, 0.16, 0.30), v(-0.21, 0.08, 0.10), dark);
voxel(group, v(0.24, 0.16, 0.30), v(0.21, 0.08, 0.10), dark);
group.userData.name = name;
scene.add(group);
return group;
}
const azureToken = createToken(0x22b8cf, 0x80e5ed, "Azure");
const coralToken = createToken(0xff6b5d, 0xffb09f, "Coral");
const tokenA = {
group: azureToken,
home: v(-2.05, 0.12, 1.02),
color: 0x22b8cf
};
const tokenB = {
group: coralToken,
home: v(2.02, 0.12, -1.18),
color: 0xff6b5d
};
const tokens = [tokenA, tokenB];
const targetPosition = v(0, 0.12, 0);
tokenA.group.position.copy(tokenA.home);
tokenB.group.position.copy(tokenB.home);
function faceTarget(token) {
const dx = targetPosition.x - token.group.position.x;
const dz = targetPosition.z - token.group.position.z;
token.group.rotation.y = Math.atan2(dx, dz);
}
faceTarget(tokenA);
faceTarget(tokenB);
// -------------------------------------------------------------------------
// Turn-order marker: a chunky pixel arrow floating over the active token
// -------------------------------------------------------------------------
const turnMarker = new THREE.Group();
scene.add(turnMarker);
const markerMaterial = standard(0xf5c84b, {
emissive: 0x8c5d00,
emissiveIntensity: 0.22
});
const markerDark = standard(0x182438);
// Down-pointing pixel arrow.
const markerPixels = [
[-0.17, 0.18], [0, 0.18], [0.17, 0.18],
[-0.085, 0.01], [0.085, 0.01],
[0, -0.16]
];
for (const [x, y] of markerPixels) {
voxel(turnMarker, v(0.15, 0.15, 0.15), v(x, y, 0), markerMaterial, true);
}
voxel(turnMarker, v(0.43, 0.12, 0.12), v(0, 0.31, 0), markerDark);
// -------------------------------------------------------------------------
// UI state
// -------------------------------------------------------------------------
const moveButton = document.querySelector("#move-button");
const resetButton = document.querySelector("#reset-button");
const status = document.querySelector("#status");
const statusDetail = document.querySelector("#status-detail");
const stepAzure = document.querySelector("#step-azure");
const stepCoral = document.querySelector("#step-coral");
const targetSlot = document.querySelector("#target-slot");
const capacityText = document.querySelector("#capacity-text");
const targetLabel = document.querySelector("#target-label");
const turnLabel = document.querySelector("#turn-label");
const toast = document.querySelector("#toast");
let activeToken = 0;
let occupiedBy = null;
let moving = false;
let moveAnimation = null;
let denyAnimation = null;
function setTargetAppearance(mode) {
if (mode === "occupied") {
targetFloorMaterial.color.setHex(0xbff3c9);
targetFloorMaterial.emissive.setHex(0x0b6c2a);
targetFloorMaterial.emissiveIntensity = 0.16;
targetEdgeMaterial.color.setHex(0x39c979);
targetEdgeMaterial.emissive.setHex(0x08782f);
targetLabel.classList.add("occupied");
targetLabel.textContent = "Shared target · occupied";
} else if (mode === "denied") {
targetEdgeMaterial.color.setHex(0xff5147);
targetEdgeMaterial.emissive.setHex(0xa4160f);
targetEdgeMaterial.emissiveIntensity = 0.4;
} else {
targetFloorMaterial.color.setHex(0xffe8a3);
targetFloorMaterial.emissive.setHex(0x7a5200);
targetFloorMaterial.emissiveIntensity = 0.12;
targetEdgeMaterial.color.setHex(0xf2b84b);
targetEdgeMaterial.emissive.setHex(0x9c6500);
targetEdgeMaterial.emissiveIntensity = 0.15;
targetLabel.classList.remove("occupied");
targetLabel.textContent = "Shared target · vacant";
}
}
function updateTurnUI() {
stepAzure.classList.toggle("active", activeToken === 0 && occupiedBy !== 0);
stepAzure.classList.toggle("complete", occupiedBy === 0);
stepCoral.classList.toggle("active", activeToken === 1);
stepCoral.classList.toggle("complete", occupiedBy === 1);
turnLabel.textContent = `Turn ${activeToken + 1}`;
document.documentElement.style.setProperty(
"--active",
activeToken === 0 ? "var(--azure)" : "var(--coral)"
);
}
function updateOccupancyUI() {
const filled = occupiedBy !== null;
targetSlot.classList.toggle("filled", filled);
capacityText.textContent = filled ? "1 / 1" : "0 / 1";
targetSlot.parentElement.setAttribute(
"aria-label",
filled ? "One of one target slots occupied" : "Zero of one target slots occupied"
);
}
function updateStatus(title, detail) {
status.textContent = title;
statusDetail.textContent = detail;
}
function updateMoveButton() {
if (moving) {
moveButton.disabled = true;
moveButton.textContent = "Moving…";
return;
}
moveButton.disabled = false;
if (occupiedBy === null) {
moveButton.textContent = "Move Azure → target";
} else if (activeToken === 1) {
moveButton.textContent = "Attempt Coral’s entry";
} else {
moveButton.textContent = "Move Azure → target";
}
}
function shortestAngle(from, to) {
return from + Math.atan2(Math.sin(to - from), Math.cos(to - from));
}
function startMove() {
if (moving) return;
const token = tokens[activeToken];
if (occupiedBy !== null) {
startDeniedMove(token);
return;
}
moving = true;
updateMoveButton();
const startPosition = token.group.position.clone();
const startRotation = token.group.rotation.y;
const endRotation = Math.atan2(
targetPosition.x - startPosition.x,
targetPosition.z - startPosition.z
);
moveAnimation = {
token,
start: performance.now(),
duration: 1050,
startPosition,
endRotation,
startRotation
};
updateStatus(
`${token.group.userData.name} is moving`,
"The first entry claims the shared target."
);
}
function startDeniedMove(token) {
const from = token.group.position.clone();
const direction = targetPosition.clone().sub(from);
direction.y = 0;
direction.normalize();
denyAnimation = {
token,
from,
direction,
start: performance.now(),
duration: 700
};
setTargetAppearance("denied");
updateStatus(
"Entry denied",
"Capacity reached: the target cell is already occupied."
);
moveButton.textContent = "Retry Coral’s entry";
toast.classList.remove("show");
void toast.offsetWidth;
toast.classList.add("show");
}
function resetBoard() {
moving = false;
moveAnimation = null;
denyAnimation = null;
activeToken = 0;
occupiedBy = null;
tokenA.group.position.copy(tokenA.home);
tokenB.group.position.copy(tokenB.home);
faceTarget(tokenA);
faceTarget(tokenB);
setTargetAppearance("vacant");
updateTurnUI();
updateOccupancyUI();
updateMoveButton();
updateStatus(
"Azure has turn 1",
"The shared target is vacant. The first entry will claim it."
);
}
moveButton.addEventListener("click", startMove);
resetButton.addEventListener("click", resetBoard);
// -------------------------------------------------------------------------
// Direct camera controls — no addons
// -------------------------------------------------------------------------
let dragging = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraYaw -= deltaX * 0.006;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch + deltaY * 0.005,
0.45,
1.18
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
camera.zoom = THREE.MathUtils.clamp(
camera.zoom * Math.exp(-event.deltaY * 0.001),
0.7,
1.65
);
camera.updateProjectionMatrix();
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
if (event.key === "ArrowLeft") cameraYaw -= 0.1;
if (event.key === "ArrowRight") cameraYaw += 0.1;
if (event.key === "ArrowUp") cameraPitch = Math.max(0.45, cameraPitch - 0.08);
if (event.key === "ArrowDown") cameraPitch = Math.min(1.18, cameraPitch + 0.08);
if (event.key === "+" || event.key === "=") camera.zoom = Math.min(1.65, camera.zoom + 0.08);
if (event.key === "-") camera.zoom = Math.max(0.7, camera.zoom - 0.08);
camera.updateProjectionMatrix();
updateCamera();
});
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
const viewHeight = aspect < 1 ? 12 / aspect : 8.2;
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
updateCamera();
}
window.addEventListener("resize", resize);
resize();
// -------------------------------------------------------------------------
// Screen-space labels projected from the voxel scene
// -------------------------------------------------------------------------
function positionLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.2 &&
Math.abs(projected.y) < 1.2;
element.style.opacity = visible ? "1" : "0";
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.transform =
`translate(-50%, -50%) translate(${x}px, ${y}px)`;
}
// -------------------------------------------------------------------------
// Main animation loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate(now) {
requestAnimationFrame(animate);
if (moveAnimation) {
const animation = moveAnimation;
const raw = THREE.MathUtils.clamp(
(now - animation.start) / animation.duration,
0,
1
);
const eased = raw * raw * (3 - 2 * raw);
animation.token.group.position.lerpVectors(
animation.startPosition,
targetPosition,
eased
);
animation.token.group.position.y += Math.sin(Math.PI * raw) * 0.28;
animation.token.group.rotation.y = shortestAngle(
animation.startRotation,
animation.endRotation
);
if (raw >= 1) {
animation.token.group.position.copy(targetPosition);
occupiedBy = activeToken;
activeToken = 1;
moving = false;
moveAnimation = null;
setTargetAppearance("occupied");
updateTurnUI();
updateOccupancyUI();
updateMoveButton();
updateStatus(
"Coral has turn 2",
"Azure owns the target. Coral’s entry will be denied by the capacity rule."
);
}
}
if (denyAnimation) {
const animation = denyAnimation;
const raw = THREE.MathUtils.clamp(
(now - animation.start) / animation.duration,
0,
1
);
const push = raw < 0.5
? 4 * raw * raw * raw
: 1 - Math.pow(-2 * raw + 2, 3) / 2;
const offset = animation.direction.clone().multiplyScalar(0.38 * push);
animation.token.group.position.copy(animation.from).add(offset);
if (raw >= 1) {
animation.token.group.position.copy(animation.from);
denyAnimation = null;
setTargetAppearance("occupied");
}
}
// Floating turn marker follows the active token.
const active = tokens[activeToken];
turnMarker.position.set(
active.group.position.x,
active.group.position.y + 2.23 + Math.sin(clock.elapsedTime * 3.2) * 0.055,
active.group.position.z
);
turnMarker.quaternion.copy(camera.quaternion);
// Labels.
positionLabel(targetLabel, v(0, 0.46, 0.83));
const azureAnchor = tokenA.group.position.clone().add(v(0, 0.36, 0.58));
const coralAnchor = tokenB.group.position.clone().add(v(0, 0.36, 0.58));
positionLabel(document.querySelector("#label-azure"), azureAnchor);
positionLabel(document.querySelector("#label-coral"), coralAnchor);
positionLabel(
turnLabel,
turnMarker.position.clone().add(v(0, 0.55, 0))
);
renderer.render(scene, camera);
}
updateTurnUI();
updateOccupancyUI();
updateMoveButton();
updateStatus(
"Azure has turn 1",
"The shared target is vacant. The first entry will claim it."
);
document.querySelector("#loading").remove();
requestAnimationFrame(animate);
</script>
</body>
</html>
```