Prompt specification
Build or customize the Three.js 3D scene "Village Workshop Resource Allocation" 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: Village Workshop Resource Allocation
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/village-workshop-resource-allocation-x3159
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring allocation of scarce resources among competing uses. Arrange a small village around shared wood, stone, and labor stores. Let users choose two construction projects and expose the material shortage that prevents completing everything. Voxel style.
Working Three.js Starter (village-workshop-resource-allocation-x3159):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>The Commons — Voxel Resource Planner</title>
<style>
:root {
--ink: #18352f;
--muted: #6c7c73;
--paper: #f5f1e7;
--paper-2: #eae4d7;
--line: rgba(24, 53, 47, 0.14);
--wood: #c98649;
--stone: #82938b;
--labor: #e6aa49;
--accent: #de7145;
--good: #3f8065;
--bad: #b64e3f;
--shadow: 0 20px 55px rgba(5, 20, 17, 0.22);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #17342f;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
.scene-wash {
position: fixed;
inset: 0;
z-index: 1;
pointer-events: none;
background:
radial-gradient(circle at 52% 44%, transparent 30%, rgba(7, 27, 23, 0.14) 100%),
linear-gradient(180deg, rgba(10, 31, 27, 0.16), transparent 24%);
}
.brand {
position: fixed;
z-index: 6;
top: 22px;
left: 24px;
display: flex;
align-items: center;
gap: 12px;
color: #f7f2e6;
pointer-events: none;
}
.brand-mark {
width: 44px;
height: 44px;
display: grid;
place-items: center;
border: 1px solid rgba(255, 255, 255, 0.22);
background: rgba(255, 255, 255, 0.09);
box-shadow: 0 10px 28px rgba(0, 0, 0, 0.16);
backdrop-filter: blur(10px);
}
.brand-mark svg {
width: 27px;
height: 27px;
}
.brand h1 {
margin: 0;
font-size: 20px;
line-height: 1;
letter-spacing: -0.04em;
}
.brand p {
margin: 5px 0 0;
color: rgba(247, 242, 230, 0.66);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.stocks {
position: fixed;
z-index: 6;
top: 22px;
right: 24px;
display: flex;
overflow: hidden;
color: var(--ink);
border: 1px solid rgba(255, 255, 255, 0.35);
border-radius: 16px;
background: rgba(246, 242, 232, 0.94);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
}
.stock {
min-width: 112px;
padding: 11px 14px;
display: grid;
grid-template-columns: 25px 1fr;
grid-template-rows: auto auto;
column-gap: 9px;
align-items: center;
border-right: 1px solid var(--line);
}
.stock:last-child {
border-right: 0;
}
.stock-icon {
grid-row: 1 / 3;
width: 25px;
height: 25px;
display: grid;
place-items: center;
border-radius: 7px;
color: white;
font-size: 10px;
font-weight: 900;
box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.12);
}
.stock-icon.wood {
background: var(--wood);
}
.stock-icon.stone {
background: var(--stone);
}
.stock-icon.labor {
background: var(--labor);
}
.stock strong {
font-size: 16px;
line-height: 1;
}
.stock small {
margin-top: 3px;
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.stock em {
grid-column: 2;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
font-style: normal;
}
.panel {
position: fixed;
z-index: 5;
top: 112px;
width: 316px;
max-height: calc(100vh - 136px);
overflow: auto;
padding: 20px;
border: 1px solid rgba(255, 255, 255, 0.58);
border-radius: 20px;
background: rgba(247, 243, 233, 0.96);
box-shadow: var(--shadow);
backdrop-filter: blur(16px);
scrollbar-width: thin;
scrollbar-color: rgba(24, 53, 47, 0.25) transparent;
}
.projects-panel {
left: 24px;
}
.allocation-panel {
right: 24px;
width: 356px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
color: var(--accent);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 18px;
height: 2px;
background: currentColor;
}
.panel-heading {
margin: 9px 0 7px;
font-size: 23px;
line-height: 1.08;
letter-spacing: -0.045em;
}
.panel-copy {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.picker-meta {
display: flex;
justify-content: space-between;
align-items: center;
margin: 19px 0 10px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.selection-count {
padding: 5px 8px;
border-radius: 20px;
color: var(--ink);
background: var(--paper-2);
}
.project-list {
display: grid;
gap: 9px;
}
.project-card {
position: relative;
width: 100%;
display: grid;
grid-template-columns: 43px 1fr 24px;
gap: 11px;
align-items: center;
padding: 12px;
text-align: left;
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(255, 255, 255, 0.38);
cursor: pointer;
transition:
transform 160ms ease,
border-color 160ms ease,
background 160ms ease,
box-shadow 160ms ease;
}
.project-card:hover {
transform: translateY(-1px);
border-color: rgba(24, 53, 47, 0.3);
background: rgba(255, 255, 255, 0.7);
}
.project-card.selected {
border-color: var(--accent);
background: #fffaf0;
box-shadow: 0 8px 24px rgba(123, 76, 46, 0.09);
}
.project-card.built {
opacity: 0.72;
}
.project-icon {
width: 43px;
height: 43px;
display: grid;
place-items: center;
border-radius: 11px;
color: var(--ink);
background: #dce6dc;
}
.project-card[data-project="granary"] .project-icon {
background: #e4dfd2;
}
.project-card[data-project="bridge"] .project-icon {
background: #d5e4e5;
}
.project-icon svg {
width: 26px;
height: 26px;
}
.project-title-line {
display: flex;
align-items: center;
gap: 7px;
}
.project-name {
font-size: 13px;
font-weight: 850;
letter-spacing: -0.015em;
}
.priority-badge {
display: none;
min-width: 18px;
height: 18px;
place-items: center;
border-radius: 6px;
color: white;
background: var(--accent);
font-size: 9px;
font-weight: 900;
}
.selected .priority-badge {
display: grid;
}
.project-description {
margin-top: 3px;
color: var(--muted);
font-size: 10px;
line-height: 1.35;
}
.costs {
display: flex;
gap: 5px;
margin-top: 7px;
}
.cost {
display: flex;
align-items: center;
gap: 4px;
color: #56665f;
font-size: 9px;
font-weight: 800;
}
.cost i {
width: 7px;
height: 7px;
border-radius: 2px;
}
.cost i.wood {
background: var(--wood);
}
.cost i.stone {
background: var(--stone);
}
.cost i.labor {
background: var(--labor);
}
.check {
width: 22px;
height: 22px;
display: grid;
place-items: center;
border: 1px solid rgba(24, 53, 47, 0.18);
border-radius: 7px;
color: transparent;
font-size: 12px;
font-weight: 900;
}
.selected .check {
color: white;
border-color: var(--accent);
background: var(--accent);
}
.picker-note {
margin: 12px 0 0;
padding: 10px 11px;
border-radius: 10px;
color: #596b61;
background: #e8eee7;
font-size: 10px;
line-height: 1.45;
}
.picker-note strong {
color: var(--ink);
}
.allocation-topline {
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
}
.text-button {
padding: 4px 0;
border: 0;
color: var(--muted);
background: transparent;
font-size: 10px;
font-weight: 800;
cursor: pointer;
}
.text-button:hover {
color: var(--accent);
}
.shortage-card {
margin: 16px 0 13px;
padding: 14px;
border-radius: 14px;
color: #f7f2e8;
background: var(--ink);
box-shadow: 0 12px 28px rgba(24, 53, 47, 0.17);
}
.shortage-card .label {
color: #f2aa77;
font-size: 9px;
font-weight: 900;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.shortage-card h3 {
margin: 7px 0 5px;
font-size: 15px;
line-height: 1.25;
letter-spacing: -0.025em;
}
.shortage-card p {
margin: 0;
color: rgba(247, 242, 232, 0.68);
font-size: 10px;
line-height: 1.45;
}
.resource-summary {
display: grid;
gap: 6px;
margin-top: 12px;
}
.summary-row {
display: grid;
grid-template-columns: 61px 1fr auto;
gap: 8px;
align-items: center;
padding-top: 6px;
border-top: 1px solid rgba(255, 255, 255, 0.12);
font-size: 10px;
}
.summary-row span {
color: rgba(247, 242, 232, 0.7);
}
.summary-bar {
height: 5px;
overflow: hidden;
border-radius: 10px;
background: rgba(255, 255, 255, 0.12);
}
.summary-bar i {
display: block;
height: 100%;
border-radius: inherit;
background: #e5a668;
}
.summary-row b {
min-width: 45px;
text-align: right;
font-size: 9px;
}
.summary-row b.short {
color: #f2aa77;
}
.allocation-list {
display: grid;
gap: 10px;
}
.allocation-card {
padding: 13px;
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(255, 255, 255, 0.45);
}
.allocation-card-header {
display: flex;
justify-content: space-between;
align-items: center;
gap: 10px;
margin-bottom: 12px;
}
.allocation-name {
display: flex;
align-items: center;
gap: 8px;
min-width: 0;
}
.allocation-name strong {
overflow: hidden;
font-size: 12px;
text-overflow: ellipsis;
white-space: nowrap;
}
.allocation-priority {
width: 20px;
height: 20px;
flex: 0 0 auto;
display: grid;
place-items: center;
border-radius: 6px;
color: white;
background: var(--accent);
font-size: 9px;
font-weight: 900;
}
.project-status {
flex: 0 0 auto;
padding: 4px 7px;
border-radius: 20px;
color: #8b5547;
background: #f1ded4;
font-size: 8px;
font-weight: 900;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.project-status.ready {
color: #2e7055;
background: #dcebe1;
}
.project-status.built {
color: #53645c;
background: #e0e6e0;
}
.resource-line {
display: grid;
grid-template-columns: 26px 1fr 51px;
gap: 9px;
align-items: center;
margin-top: 9px;
}
.resource-key {
width: 26px;
height: 26px;
display: grid;
place-items: center;
border-radius: 7px;
color: white;
font-size: 9px;
font-weight: 900;
}
.resource-key.wood {
background: var(--wood);
}
.resource-key.stone {
background: var(--stone);
}
.resource-key.labor {
background: var(--labor);
}
.range-wrap {
min-width: 0;
}
.range-label {
display: flex;
justify-content: space-between;
margin-bottom: 5px;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.04em;
text-transform: uppercase;
}
input[type="range"] {
--fill: 0%;
width: 100%;
height: 5px;
display: block;
margin: 0;
appearance: none;
border-radius: 20px;
outline: none;
background: linear-gradient(
to right,
var(--accent) 0%,
var(--accent) var(--fill),
#d9ddd5 var(--fill),
#d9ddd5 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 14px;
height: 14px;
appearance: none;
border: 3px solid white;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 1px 5px rgba(24, 53, 47, 0.3);
}
input[type="range"]::-moz-range-thumb {
width: 10px;
height: 10px;
border: 3px solid white;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 1px 5px rgba(24, 53, 47, 0.3);
}
input[type="range"]:disabled {
opacity: 0.4;
cursor: default;
}
.resource-value {
text-align: right;
font-size: 10px;
font-weight: 850;
}
.resource-value span {
color: var(--muted);
font-weight: 600;
}
.panel-actions {
display: grid;
grid-template-columns: 1fr 1.35fr;
gap: 8px;
margin-top: 13px;
}
.button {
min-height: 42px;
padding: 10px 12px;
border: 1px solid var(--line);
border-radius: 11px;
font-size: 10px;
font-weight: 900;
cursor: pointer;
transition:
transform 150ms ease,
opacity 150ms ease,
background 150ms ease;
}
.button:hover:not(:disabled) {
transform: translateY(-1px);
}
.button.secondary {
background: rgba(255, 255, 255, 0.5);
}
.button.primary {
color: white;
border-color: var(--ink);
background: var(--ink);
box-shadow: 0 8px 18px rgba(24, 53, 47, 0.16);
}
.button:disabled {
opacity: 0.42;
cursor: not-allowed;
}
.build-hint {
margin: 9px 1px 0;
color: var(--muted);
font-size: 9px;
line-height: 1.45;
text-align: center;
}
#labels {
position: fixed;
inset: 0;
z-index: 3;
pointer-events: none;
}
.world-label {
position: absolute;
display: none;
transform: translate(-50%, -100%);
padding: 6px 8px;
border: 1px solid rgba(255, 255, 255, 0.24);
border-radius: 8px;
color: white;
background: rgba(18, 43, 37, 0.86);
box-shadow: 0 7px 20px rgba(0, 0, 0, 0.16);
backdrop-filter: blur(8px);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.06em;
text-transform: uppercase;
white-space: nowrap;
}
.world-label.project {
pointer-events: auto;
cursor: pointer;
}
.world-label.project.ready {
border-color: rgba(148, 219, 174, 0.75);
}
.world-label.project.built {
background: rgba(42, 78, 62, 0.94);
}
.world-status {
margin-left: 5px;
color: #f0b186;
font-size: 8px;
}
.world-label.ready .world-status {
color: #9ed9b8;
}
.world-label.built .world-status {
color: #d9e6d9;
}
.scene-help {
position: fixed;
z-index: 4;
left: 50%;
bottom: 19px;
transform: translateX(-50%);
display: flex;
align-items: center;
gap: 9px;
padding: 8px 12px;
border: 1px solid rgba(255, 255, 255, 0.18);
border-radius: 30px;
color: rgba(255, 255, 255, 0.7);
background: rgba(13, 36, 31, 0.62);
backdrop-filter: blur(10px);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.03em;
pointer-events: none;
}
.scene-help i {
width: 4px;
height: 4px;
border-radius: 50%;
background: #efad79;
}
.toast {
position: fixed;
z-index: 10;
left: 50%;
bottom: 67px;
transform: translate(-50%, 12px);
max-width: min(420px, calc(100vw - 30px));
padding: 10px 15px;
border-radius: 10px;
color: white;
background: #193c31;
box-shadow: 0 12px 32px rgba(0, 0, 0, 0.24);
font-size: 11px;
font-weight: 700;
text-align: center;
opacity: 0;
pointer-events: none;
transition:
opacity 180ms ease,
transform 180ms ease;
}
.toast.visible {
transform: translate(-50%, 0);
opacity: 1;
}
.mobile-tabs {
display: none;
}
@media (max-width: 1050px) {
.panel {
width: 286px;
padding: 17px;
}
.allocation-panel {
width: 326px;
}
.stock {
min-width: 96px;
padding: 10px;
}
}
@media (max-width: 760px) {
.brand {
top: 12px;
left: 12px;
}
.brand-mark {
width: 38px;
height: 38px;
}
.brand h1 {
font-size: 17px;
}
.brand p {
font-size: 8px;
}
.stocks {
top: 61px;
left: 12px;
right: 12px;
}
.stock {
min-width: 0;
flex: 1;
padding: 8px;
grid-template-columns: 22px 1fr;
column-gap: 6px;
}
.stock-icon {
width: 22px;
height: 22px;
}
.stock em {
display: none;
}
.mobile-tabs {
position: fixed;
z-index: 8;
top: 119px;
left: 12px;
right: 12px;
display: grid;
grid-template-columns: 1fr 1fr;
gap: 5px;
padding: 4px;
border: 1px solid rgba(255, 255, 255, 0.25);
border-radius: 12px;
background: rgba(15, 40, 34, 0.86);
backdrop-filter: blur(12px);
}
.mobile-tabs button {
height: 32px;
border: 0;
border-radius: 8px;
color: rgba(255, 255, 255, 0.65);
background: transparent;
font-size: 10px;
font-weight: 850;
}
body.mobile-projects .mobile-tabs [data-mobile="projects"],
body.mobile-allocate .mobile-tabs [data-mobile="allocate"] {
color: var(--ink);
background: var(--paper);
}
.panel {
top: 162px;
bottom: 55px;
left: 12px;
right: 12px;
width: auto;
max-height: none;
border-radius: 16px;
}
body.mobile-projects .allocation-panel,
body.mobile-allocate .projects-panel {
display: none;
}
.panel-heading {
font-size: 20px;
}
.scene-help {
bottom: 10px;
font-size: 8px;
}
}
</style>
</head>
<body class="mobile-projects">
<canvas id="scene" aria-label="Interactive voxel village resource planning scene"></canvas>
<div class="scene-wash"></div>
<header class="brand">
<div class="brand-mark" aria-hidden="true">
<svg viewBox="0 0 32 32" fill="none">
<path d="M5 14.5 16 7l11 7.5v11H5v-11Z" fill="#f2ead7"/>
<path d="M10 16h4v4h-4zm8 0h4v4h-4z" fill="#de7145"/>
<path d="M14 25v-5h4v5" fill="#335f54"/>
</svg>
</div>
<div>
<h1>The Commons</h1>
<p>A village allocation puzzle</p>
</div>
</header>
<div class="stocks" aria-label="Shared village stores">
<div class="stock">
<span class="stock-icon wood">W</span>
<strong data-stock="wood">7</strong>
<small>Wood</small>
<em data-left="wood">0 unassigned</em>
</div>
<div class="stock">
<span class="stock-icon stone">S</span>
<strong data-stock="stone">7</strong>
<small>Stone</small>
<em data-left="stone">0 unassigned</em>
</div>
<div class="stock">
<span class="stock-icon labor">L</span>
<strong data-stock="labor">10</strong>
<small>Labor</small>
<em data-left="labor">0 unassigned</em>
</div>
</div>
<section class="panel projects-panel" aria-labelledby="projects-heading">
<div class="eyebrow">Step 1</div>
<h2 class="panel-heading" id="projects-heading">Choose two projects</h2>
<p class="panel-copy">
Pick the projects the village would most like to complete.
The first selection receives funding priority.
</p>
<div class="picker-meta">
<span>Available projects</span>
<span class="selection-count" id="selection-count">2 / 2 selected</span>
</div>
<div class="project-list">
<button class="project-card selected" data-project="school" aria-pressed="true">
<span class="project-icon" aria-hidden="true">
<svg viewBox="0 0 32 32" fill="none">
<path d="M5 14 16 7l11 7v13H5V14Z" fill="#e9ddc4" stroke="#18352f" stroke-width="1.6"/>
<path d="m4 14 12-8 12 8" stroke="#18352f" stroke-width="2" stroke-linejoin="round"/>
<path d="M13 27v-8h6v8" fill="#de7145"/>
<path d="M8 16h4v4H8zm12 0h4v4h-4z" fill="#70a1a4"/>
</svg>
</span>
<span>
<span class="project-title-line">
<span class="project-name">Community School</span>
<span class="priority-badge">1</span>
</span>
<span class="project-description">A safe classroom for village children.</span>
<span class="costs">
<span class="cost"><i class="wood"></i>4 wood</span>
<span class="cost"><i class="stone"></i>3 stone</span>
<span class="cost"><i labor></i>5 labor</span>
</span>
</span>
<span class="check">✓</span>
</button>
<button class="project-card" data-project="granary" aria-pressed="false">
<span class="project-icon" aria-hidden="true">
<svg viewBox="0 0 32 32" fill="none">
<path d="M6 14h20v14H6V14Z" fill="#d7d0bd" stroke="#18352f" stroke-width="1.6"/>
<path d="m4 14 12-9 12 9" stroke="#18352f" stroke-width="2" stroke-linejoin="round"/>
<path d="M12 20h8v8h-8z" fill="#9a6842"/>
<path d="M10 7h12" stroke="#18352f" stroke-width="2"/>
</svg>
</span>
<span>
<span class="project-title-line">
<span class="project-name">Stone Granary</span>
<span class="priority-badge">2</span>
</span>
<span class="project-description">Dry, protected storage for harvests.</span>
<span class="costs">
<span class="cost"><i class="wood"></i>2 wood</span>
<span class="cost"><i class="stone"></i>5 stone</span>
<span class="cost"><i labor></i>4 labor</span>
</span>
</span>
<span class="check">✓</span>
</button>
<button class="project-card" data-project="bridge" aria-pressed="false">
<span class="project-icon" aria-hidden="true">
<svg viewBox="0 0 32 32" fill="none">
<path d="M3 19h26M7 19v8m18-8v8" stroke="#18352f" stroke-width="2"/>
<path d="M5 15h22M8 10v5m8-5v5m8-5v5" stroke="#18352f" stroke-width="1.8"/>
<path d="M4 23c5 2 8-2 12 0s8-2 12 0" stroke="#70a1a4" stroke-width="2"/>
</svg>
</span>
<span>
<span class="project-title-line">
<span class="project-name">River Footbridge</span>
<span class="priority-badge">2</span>
</span>
<span class="project-description">A reliable route across the stream.</span>
<span class="costs">
<span class="cost"><i class="wood"></i>6 wood</span>
<span class="cost"><i class="stone"></i>2 stone</span>
<span class="cost"><i labor></i>6 labor</span>
</span>
</span>
<span class="check">✓</span>
</button>
</div>
<p class="picker-note">
<strong>Every pair has a trade-off.</strong>
The village cannot complete any two projects at the same time with its current stores.
</p>
</section>
<aside class="panel allocation-panel" aria-labelledby="allocation-heading">
<div class="allocation-topline">
<div class="eyebrow">Step 2</div>
<button class="text-button" id="reset-village" type="button">Restart village</button>
</div>
<h2 class="panel-heading" id="allocation-heading">Allocate the commons</h2>
<p class="panel-copy">
Move shared resources between the selected projects. Reduce one allocation before increasing the other.
</p>
<div class="shortage-card" id="shortage-card"></div>
<div class="allocation-list" id="allocation-list"></div>
<div class="panel-actions">
<button class="button secondary" id="auto-allocate" type="button">
Auto-fill by priority
</button>
<button class="button primary" id="build-project" type="button">
Build ready project
</button>
</div>
<p class="build-hint" id="build-hint">
A fully funded project can be built even while the other remains blocked.
</p>
</aside>
<div id="labels"></div>
<div class="scene-help">
Drag to orbit <i></i> Scroll to zoom <i></i> Click a project in the village
</div>
<div class="toast" id="toast" role="status" aria-live="polite"></div>
<nav class="mobile-tabs" aria-label="Mobile panels">
<button type="button" data-mobile="projects">Projects</button>
<button type="button" data-mobile="allocate">Allocate</button>
</nav>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// State
// -------------------------------------------------------------------------
const projects = {
school: {
name: "Community School",
costs: { wood: 4, stone: 3, labor: 5 },
position: new THREE.Vector3(-4.7, 0.1, -3.6)
},
granary: {
name: "Stone Granary",
costs: { wood: 2, stone: 5, labor: 4 },
position: new THREE.Vector3(3.25, 0.1, 4.3)
},
bridge: {
name: "River Footbridge",
costs: { wood: 6, stone: 2, labor: 6 },
position: new THREE.Vector3(6.2, 0.1, -3.55)
}
};
const resources = ["wood", "stone", "labor"];
const resourceNames = {
wood: "wood",
stone: "stone",
labor: "worker-days"
};
const initialStock = { wood: 7, stone: 7, labor: 10 };
const state = {
selected: ["school", "bridge"],
stock: { ...initialStock },
allocation: {
school: { wood: 0, stone: 0, labor: 0 },
granary: { wood: 0, stone: 0, labor: 0 },
bridge: { wood: 0, stone: 0, labor: 0 }
},
built: new Set()
};
const canvas = document.querySelector("#scene");
const labelsRoot = document.querySelector("#labels");
const allocationList = document.querySelector("#allocation-list");
const shortageCard = document.querySelector("#shortage-card");
const buildButton = document.querySelector("#build-project");
const buildHint = document.querySelector("#build-hint");
const toast = document.querySelector("#toast");
let allocationSignature = "";
let toastTimer;
function emptyAllocations() {
for (const id of Object.keys(state.allocation)) {
state.allocation[id] = { wood: 0, stone: 0, labor: 0 };
}
}
function autoAllocate() {
emptyAllocations();
for (const resource of resources) {
let available = state.stock[resource];
for (const id of state.selected) {
if (state.built.has(id)) continue;
const amount = Math.min(
projects[id].costs[resource],
available
);
state.allocation[id][resource] = amount;
available -= amount;
}
}
}
function isReady(id) {
if (state.built.has(id)) return false;
return resources.every(
resource =>
state.allocation[id][resource] >= projects[id].costs[resource]
);
}
function getDemand() {
const demand = { wood: 0, stone: 0, labor: 0 };
for (const id of state.selected) {
if (state.built.has(id)) continue;
for (const resource of resources) {
demand[resource] += projects[id].costs[resource];
}
}
return demand;
}
function plannedAmount(resource) {
return state.selected.reduce((total, id) => {
if (state.built.has(id)) return total;
return total + state.allocation[id][resource];
}, 0);
}
function showToast(message) {
toast.textContent = message;
toast.classList.add("visible");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove("visible"), 2600);
}
function selectProject(id) {
const existingIndex = state.selected.indexOf(id);
if (existingIndex !== -1) {
if (state.selected.length === 1) {
showToast("Keep at least one project selected.");
return;
}
state.selected.splice(existingIndex, 1);
} else if (state.selected.length < 2) {
state.selected.push(id);
} else {
state.selected[1] = id;
}
autoAllocate();
updateUI();
}
// -------------------------------------------------------------------------
// Three.js setup
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x17342f);
scene.fog = new THREE.Fog(0x17342f, 31, 58);
const camera = new THREE.OrthographicCamera(-14, 14, 9, -9, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 0.35, 0);
const cameraState = {
yaw: 0.67,
pitch: 0.83,
zoom: 1
};
function updateCamera() {
const radius = 30;
camera.position.set(
cameraTarget.x + radius * Math.sin(cameraState.pitch) * Math.sin(cameraState.yaw),
cameraTarget.y + radius * Math.cos(cameraState.pitch),
cameraTarget.z + radius * Math.sin(cameraState.pitch) * Math.cos(cameraState.yaw)
);
camera.lookAt(cameraTarget);
camera.zoom = cameraState.zoom;
camera.updateProjectionMatrix();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const frustumHeight = Math.max(15, 19 / aspect);
renderer.setSize(width, height, false);
camera.left = (-frustumHeight * aspect) / 2;
camera.right = (frustumHeight * aspect) / 2;
camera.top = frustumHeight / 2;
camera.bottom = -frustumHeight / 2;
camera.updateProjectionMatrix();
updateCamera();
}
window.addEventListener("resize", resize);
const hemisphere = new THREE.HemisphereLight(0xdcefe5, 0x435247, 2.1);
scene.add(hemisphere);
const sunlight = new THREE.DirectionalLight(0xffe4bd, 3.2);
sunlight.position.set(-9, 17, 11);
sunlight.castShadow = true;
sunlight.shadow.mapSize.set(2048, 2048);
sunlight.shadow.camera.left = -15;
sunlight.shadow.camera.right = 15;
sunlight.shadow.camera.top = 15;
sunlight.shadow.camera.bottom = -15;
sunlight.shadow.camera.near = 1;
sunlight.shadow.camera.far = 45;
sunlight.shadow.bias = -0.0004;
scene.add(sunlight);
const fillLight = new THREE.DirectionalLight(0x8fc3c1, 1.0);
fillLight.position.set(10, 8, -12);
scene.add(fillLight);
const geometryCache = new Map();
const materialCache = new Map();
function getGeometry(w, h, d) {
const key = `${w}:${h}:${d}`;
if (!geometryCache.has(key)) {
geometryCache.set(key, new THREE.BoxGeometry(w, h, d));
}
return geometryCache.get(key);
}
function getMaterial(color) {
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.88,
metalness: 0
})
);
}
return materialCache.get(color);
}
function box(parent, size, position, color, castShadow = true) {
const mesh = new THREE.Mesh(
getGeometry(size[0], size[1], size[2]),
getMaterial(color)
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeVoxelRoof(parent, width, depth, baseY, color) {
box(parent, [width + 0.5, 0.38, depth + 0.5], [0, baseY, 0], color);
box(parent, [width * 0.76, 0.36, depth * 0.78], [0, baseY + 0.35, 0], color);
box(parent, [width * 0.42, 0.33, depth * 0.46], [0, baseY + 0.68, 0], color);
}
function makeHouse(x, z, width, depth, wallColor, roofColor) {
const group = new THREE.Group();
group.position.set(x, 0.08, z);
scene.add(group);
box(group, [width, 2.05, depth], [0, 1.05, 0], wallColor);
box(group, [width + 0.12, 0.16, depth + 0.12], [0, 0.12, 0], 0x9c8263);
makeVoxelRoof(group, width, depth, 2.28, roofColor);
box(group, [0.58, 1.25, 0.09], [0, 0.78, depth / 2 + 0.055], 0x4c5f55);
box(group, [0.72, 0.57, 0.09], [-width * 0.28, 1.3, depth / 2 + 0.06], 0x78a5a8);
box(group, [0.72, 0.57, 0.09], [width * 0.28, 1.3, depth / 2 + 0.06], 0x78a5a8);
box(group, [0.32, 0.72, 0.32], [width * 0.25, 2.88, -depth * 0.18], 0x755e4b);
const light = box(
group,
[0.65, 0.35, 0.65],
[0, 3.16, -depth * 0.18],
0x9b9182
);
light.castShadow = false;
return group;
}
function makeTree(x, z, scale = 1, leafColor = 0x47745c) {
const group = new THREE.Group();
group.position.set(x, 0.06, z);
group.scale.setScalar(scale);
scene.add(group);
box(group, [0.28, 1.5, 0.28], [0, 0.75, 0], 0x78583d);
box(group, [1.35, 1.05, 1.25], [0, 1.75, 0], leafColor);
box(group, [0.95, 0.9, 0.95], [0.35, 2.4, -0.12], 0x628966);
box(group, [0.75, 0.7, 0.78], [-0.38, 2.35, 0.16], 0x3f6954);
}
function makeWorker(parent, x, z, shirt = 0xde7145, scale = 1) {
const worker = new THREE.Group();
worker.position.set(x, 0, z);
worker.scale.setScalar(scale);
parent.add(worker);
box(worker, [0.24, 0.43, 0.24], [0, 0.38, 0], shirt);
box(worker, [0.22, 0.22, 0.22], [0, 0.72, 0], 0xc9956e);
box(worker, [0.3, 0.08, 0.3], [0, 0.85, 0], 0xe5a54b);
box(worker, [0.12, 0.22, 0.12], [-0.07, 0.11, 0], 0x314b43);
box(worker, [0.12, 0.22, 0.12], [0.07, 0.11, 0], 0x314b43);
worker.userData.phase = Math.random() * Math.PI * 2;
animatedWorkers.push(worker);
return worker;
}
const animatedWorkers = [];
// -------------------------------------------------------------------------
// Terrain and village
// -------------------------------------------------------------------------
box(scene, [18, 0.85, 16], [0, -0.48, 0], 0x4e6958);
box(scene, [17.8, 0.28, 15.8], [0, -0.015, 0], 0x789566);
box(scene, [17.3, 0.08, 15.3], [0, 0.14, 0], 0x84a16e);
// Paths and central plaza.
box(scene, [4.6, 0.11, 4.0], [0, 0.24, 0.1], 0xc6ad83);
box(scene, [4.6, 0.10, 1.0], [-2.3, 0.23, -3.6], 0xc6ad83);
box(scene, [3.4, 0.10, 1.0], [1.65, 0.23, 4.3], 0xc6ad83);
box(scene, [3.0, 0.10, 1.0], [4.75, 0.23, -3.55], 0xc6ad83);
box(scene, [1.0, 0.10, 4.5], [0, 0.23, -2.1], 0xc6ad83);
// Stream.
box(scene, [2.15, 0.1, 15.45], [6.2, 0.25, 0], 0x4f8d98, false);
box(scene, [0.22, 0.16, 15.5], [5.02, 0.28, 0], 0xc0a77d);
box(scene, [0.22, 0.16, 15.5], [7.38, 0.28, 0], 0xc0a77d);
const waterRipples = [];
for (let i = 0; i < 8; i++) {
const ripple = box(
scene,
[0.85, 0.025, 0.07],
[6.2, 0.325, -6.5 + i * 1.75],
0x8bc3c7,
false
);
waterRipples.push(ripple);
}
// Existing homes.
makeHouse(-6.15, 3.55, 2.7, 2.25, 0xe2d2ae, 0xb75d43);
makeHouse(-1.85, 6.05, 2.35, 2.05, 0xd3c39c, 0x355f58);
makeHouse(2.7, -6.05, 2.65, 2.2, 0xe7dcc1, 0x8d5142);
makeHouse(-7.0, -0.15, 2.25, 2.05, 0xd7c5a3, 0x536f5c);
// Market stall.
const stall = new THREE.Group();
stall.position.set(-0.5, 0.25, -2.45);
scene.add(stall);
box(stall, [2.0, 0.18, 1.15], [0, 0.08, 0], 0x8c6745);
box(stall, [0.12, 1.4, 0.12], [-0.82, 0.7, -0.4], 0x654f39);
box(stall, [0.12, 1.4, 0.12], [0.82, 0.7, -0.4], 0x654f39);
box(stall, [2.15, 0.18, 1.25], [0, 1.38, 0], 0xe0b85e);
box(stall, [1.75, 0.38, 0.78], [0, 0.34, 0.05], 0xc48145);
for (let i = 0; i < 5; i++) {
box(stall, [0.2, 0.2, 0.2], [-0.58 + i * 0.29, 0.62, 0.08], 0x9a5738);
}
// Fences.
function makeFence(x, z, length, axis = "x") {
const count = Math.floor(length / 0.65);
for (let i = 0; i <= count; i++) {
const offset = -length / 2 + (length / count) * i;
const px = axis === "x" ? x + offset : x;
const pz = axis === "x" ? z : z + offset;
box(scene, [0.13, 0.7, 0.13], [px, 0.45, pz], 0x876a49);
}
const size = axis === "x" ? [length, 0.11, 0.1] : [0.1, 0.11, length];
box(scene, size, [x, 0.52, z], 0xa17c54);
}
makeFence(-5.6, 1.35, 3.4);
makeFence(-7.25, 2.0, 2.1, "z");
// Trees and crops.
makeTree(-8.0, 6.0, 1.05);
makeTree(8.0, 6.0, 0.95, 0x537d60);
makeTree(-8.0, -6.0, 0.9);
makeTree(8.0, -6.0, 1.05, 0x52765b);
makeTree(3.7, -6.8, 0.8);
makeTree(-3.4, 7.15, 0.82, 0x5c805e);
const field = new THREE.Group();
field.position.set(-5.2, 0.2, 6.0);
scene.add(field);
box(field, [3.0, 0.12, 1.5], [0, 0, 0], 0x92744f);
for (let row = 0; row < 3; row++) {
for (let col = 0; col < 6; col++) {
box(
field,
[0.22, 0.3, 0.22],
[-1.15 + col * 0.46, 0.18, -0.45 + row * 0.45],
col % 2 ? 0xd6ad4f : 0x79944e
);
}
}
// -------------------------------------------------------------------------
// Shared stores
// -------------------------------------------------------------------------
const woodMarkers = [];
const stoneMarkers = [];
const laborMarkers = [];
// Wood pile.
box(scene, [2.6, 0.16, 1.7], [-1.65, 0.29, -1.55], 0x9b7c55);
for (let i = 0; i < 10; i++) {
const group = new THREE.Group();
const row = i % 5;
const level = Math.floor(i / 5);
group.position.set(-2.55 + row * 0.45, 0.52 + level * 0.3, -1.62);
scene.add(group);
box(group, [0.72, 0.25, 0.29], [0, 0, 0], 0xa66e3f);
box(group, [0.08, 0.17, 0.21], [0.34, 0, 0], 0xd5a56a);
woodMarkers.push(group);
}
// Stone pile.
box(scene, [2.2, 0.14, 1.7], [1.55, 0.28, -1.45], 0x9a9588);
for (let i = 0; i < 10; i++) {
const group = new THREE.Group();
const row = i % 5;
const level = Math.floor(i / 5);
group.position.set(0.78 + row * 0.38, 0.48 + level * 0.35, -1.5);
scene.add(group);
const stone = box(
group,
[0.34, 0.31, 0.34],
[0, 0, 0],
i % 3 === 0 ? 0x9ca49e : 0x7d8b85
);
stone.rotation.y = (i % 4) * 0.17;
stoneMarkers.push(group);
}
// Labor store and ten worker tokens.
box(scene, [3.1, 0.15, 1.25], [0, 0.29, 2.45], 0xb39268);
box(scene, [0.1, 1.25, 0.1], [-1.3, 0.87, 2.0], 0x6d573e);
box(scene, [0.1, 1.25, 0.1], [1.3, 0.87, 2.0], 0x6d573e);
box(scene, [2.8, 0.16, 1.15], [0, 1.53, 2.0], 0xdba856);
for (let i = 0; i < 10; i++) {
const x = -0.95 + (i % 5) * 0.48;
const z = 2.25 + Math.floor(i / 5) * 0.48;
const worker = makeWorker(scene, x, z, i % 2 ? 0x5f8a77 : 0xcb7149, 0.7);
laborMarkers.push(worker);
}
// -------------------------------------------------------------------------
// Construction sites
// -------------------------------------------------------------------------
const projectRoots = {};
const projectVisuals = {};
const projectLabels = {};
const storeLabels = {};
function createProjectRoot(id, position) {
const root = new THREE.Group();
root.position.copy(position);
root.userData.projectId = id;
scene.add(root);
projectRoots[id] = root;
return root;
}
function makePad(root, size) {
const material = getMaterial(0x8caeaa).clone();
const pad = box(root, [size[0], 0.12, size[1]], [0, 0.08, 0], 0x8caeaa, false);
pad.material = material;
return pad;
}
function makeScaffold(parent, x, z, width, depth, height) {
const group = new THREE.Group();
group.position.set(x, 0, z);
parent.add(group);
box(group, [0.09, height, 0.09], [-width / 2, height / 2, -depth / 2], 0x9b744b);
box(group, [0.09, height, 0.09], [width / 2, height / 2, -depth / 2], 0x9b744b);
box(group, [0.09, height, 0.09], [-width / 2, height / 2, depth / 2], 0x9b744b);
box(group, [0.09, height, 0.09], [width / 2, height / 2, depth / 2], 0x9b744b);
box(group, [width + 0.1, 0.1, 0.12], [0, height, -depth / 2], 0xb78b59);
box(group, [width + 0.1, 0.1, 0.12], [0, height, depth / 2], 0xb78b59);
return group;
}
// School.
{
const root = createProjectRoot("school", projects.school.position);
const pad = makePad(root, [3.9, 3.5]);
const stones = [];
for (let i = 0; i < 6; i++) {
stones.push(
box(
root,
[0.72, 0.24, 0.5],
[-1.15 + (i % 3) * 1.15, 0.26, i < 3 ? -1.0 : 1.0],
0x87938d
)
);
}
const woodStages = [];
const frame1 = new THREE.Group();
root.add(frame1);
for (const x of [-1.35, 1.35]) {
for (const z of [-1.05, 1.05]) {
box(frame1, [0.16, 1.55, 0.16], [x, 1.0, z], 0x9a7046);
}
}
woodStages.push(frame1);
const frame2 = new THREE.Group();
root.add(frame2);
box(frame2, [2.95, 0.14, 0.16], [0, 1.75, -1.05], 0x9a7046);
box(frame2, [2.95, 0.14, 0.16], [0, 1.75, 1.05], 0x9a7046);
box(frame2, [0.14, 0.14, 2.25], [-1.35, 1.75, 0], 0x9a7046);
box(frame2, [0.14, 0.14, 2.25], [1.35, 1.75, 0], 0x9a7046);
woodStages.push(frame2);
const frame3 = new THREE.Group();
root.add(frame3);
box(frame3, [3.2, 0.13, 0.16], [0, 2.32, -1.05], 0x9a7046);
box(frame3, [3.2, 0.13, 0.16], [0, 2.32, 1.05], 0x9a7046);
woodStages.push(frame3);
const workers = [
makeWorker(root, -1.75, 0.3, 0xde7145, 0.78),
makeWorker(root, 1.75, -0.4, 0xe5a54b, 0.78),
makeWorker(root, 0.1, 1.75, 0x5f8a77, 0.78)
];
const complete = new THREE.Group();
root.add(complete);
box(complete, [3.0, 1.8, 2.35], [0, 1.15, 0], 0xe9dfc7);
box(complete, [3.15, 0.16, 2.5], [0, 0.2, 0], 0xa4815e);
makeVoxelRoof(complete, 3.0, 2.35, 2.27, 0xb95e43);
box(complete, [0.65, 1.3, 0.1], [0, 0.82, 1.22], 0x4e6459);
box(complete, [0.7, 0.58, 0.1], [-0.86, 1.38, 1.22], 0x6f9da1);
box(complete, [0.7, 0.58, 0.1], [0.86, 1.38, 1.22], 0x6f9da1);
box(complete, [0.33, 0.76, 0.33], [1.0, 2.98, -0.5], 0x755e4b);
projectVisuals.school = {
root,
pad,
stones,
woodStages,
workers,
complete
};
}
// Granary.
{
const root = createProjectRoot("granary", projects.granary.position);
const pad = makePad(root, [3.7, 3.6]);
const stones = [];
for (let i = 0; i < 8; i++) {
stones.push(
box(
root,
[0.75, 0.28, 0.47],
[-1.25 + (i % 4) * 0.82, 0.29, i < 4 ? -1.15 : 1.15],
i % 3 === 0 ? 0x9aa39d : 0x7f8e87
)
);
}
const woodStages = [];
const frame1 = new THREE.Group();
root.add(frame1);
for (const x of [-1.2, 1.2]) {
for (const z of [-1.05, 1.05]) {
box(frame1, [0.16, 1.75, 0.16], [x, 1.1, z], 0x9b7148);
}
}
woodStages.push(frame1);
const frame2 = new THREE.Group();
root.add(frame2);
box(frame2, [2.65, 0.15, 0.15], [0, 1.95, -1.05], 0x9b7148);
box(frame2, [2.65, 0.15, 0.15], [0, 1.95, 1.05], 0x9b7148);
woodStages.push(frame2);
const workers = [
makeWorker(root, -1.8, 0.25, 0x5f8a77, 0.78),
makeWorker(root, 1.8, 0.4, 0xde7145, 0.78),
makeWorker(root, 0, -1.85, 0xe5a54b, 0.78)
];
const complete = new THREE.Group();
root.add(complete);
box(complete, [2.9, 1.25, 2.55], [0, 0.88, 0], 0x929b94);
box(complete, [2.65, 1.0, 2.3], [0, 2.0, 0], 0xc1a271);
makeVoxelRoof(complete, 2.75, 2.4, 2.63, 0x77503b);
box(complete, [0.8, 1.2, 0.12], [0, 0.83, 1.32], 0x7a583c);
box(complete, [0.36, 0.36, 0.16], [0, 1.58, 1.38], 0xbd764a);
box(complete, [0.12, 0.8, 0.12], [0, 3.0, 0], 0x684e38);
box(complete, [0.7, 0.18, 0.18], [0.2, 3.25, 0], 0x684e38);
projectVisuals.granary = {
root,
pad,
stones,
woodStages,
workers,
complete
};
}
// Bridge.
{
const root = createProjectRoot("bridge", projects.bridge.position);
const pad = makePad(root, [4.2, 2.05]);
const stones = [];
for (const x of [-1.35, 1.35]) {
for (const z of [-0.5, 0.5]) {
stones.push(
box(root, [0.38, 0.82, 0.38], [x, 0.62, z], 0x7f8e87)
);
}
}
const woodStages = [];
const frame1 = new THREE.Group();
root.add(frame1);
box(frame1, [4.0, 0.16, 1.35], [0, 0.98, 0], 0xa47447);
woodStages.push(frame1);
const frame2 = new THREE.Group();
root.add(frame2);
box(frame2, [0.12, 0.9, 0.12], [-1.85, 1.45, -0.58], 0x8e653f);
box(frame2, [0.12, 0.9, 0.12], [1.85, 1.45, -0.58], 0x8e653f);
box(frame2, [0.12, 0.9, 0.12], [-1.85, 1.45, 0.58], 0x8e653f);
box(frame2, [0.12, 0.9, 0.12], [1.85, 1.45, 0.58], 0x8e653f);
woodStages.push(frame2);
const frame3 = new THREE.Group();
root.add(frame3);
box(frame3, [3.9, 0.11, 0.12], [0, 1.86, -0.58], 0x9d7045);
box(frame3, [3.9, 0.11, 0.12], [0, 1.86, 0.58], 0x9d7045);
woodStages.push(frame3);
const workers = [
makeWorker(root, -2.2, 0.4, 0xde7145, 0.75),
makeWorker(root, 2.2, -0.4, 0xe5a54b, 0.75),
makeWorker(root, 0.1, 1.0, 0x5f8a77, 0.75)
];
const complete = new THREE.Group();
root.add(complete);
for (let i = 0; i < 9; i++) {
box(
complete,
[0.4, 0.17, 1.45],
[-1.8 + i * 0.45, 1.05, 0],
i % 2 ? 0xa87849 : 0xb48150
);
}
box(complete, [0.2, 1.0, 0.16], [-1.9, 1.55, -0.62], 0x76523a);
box(complete, [0.2, 1.0, 0.16], [1.9, 1.55, -0.62], 0x76523a);
box(complete, [0.2, 1.0, 0.16], [-1.9, 1.55, 0.62], 0x76523a);
box(complete, [0.2, 1.0, 0.16], [1.9, 1.55, 0.62], 0x76523a);
box(complete, [4.0, 0.12, 0.14], [0, 2.0, -0.62], 0x76523a);
box(complete, [4.0, 0.12, 0.14], [0, 2.0, 0.62], 0x76523a);
projectVisuals.bridge = {
root,
pad,
stones,
woodStages,
workers,
complete
};
}
// -------------------------------------------------------------------------
// Overlay labels
// -------------------------------------------------------------------------
function addWorldLabel(className, text, anchor) {
const element = document.createElement("div");
element.className = `world-label ${className}`;
element.innerHTML = text;
labelsRoot.appendChild(element);
return { element, anchor };
}
for (const id of Object.keys(projects)) {
const data = projects[id];
const label = addWorldLabel(
"project",
`${data.name}<span class="world-status">Blocked</span>`,
data.position.clone().add(new THREE.Vector3(0, 2.9, 0))
);
label.element.dataset.project = id;
label.element.addEventListener("click", () => selectProject(id));
projectLabels[id] = label;
}
storeLabels.wood = addWorldLabel(
"",
"Wood store",
new THREE.Vector3(-1.65, 1.25, -1.55)
);
storeLabels.stone = addWorldLabel(
"",
"Stone store",
new THREE.Vector3(1.55, 1.15, -1.45)
);
storeLabels.labor = addWorldLabel(
"",
"Labor store",
new THREE.Vector3(0, 2.1, 2.15)
);
// -------------------------------------------------------------------------
// Visual updates
// -------------------------------------------------------------------------
function updateProjectVisual(id) {
const visual = projectVisuals[id];
const allocation = state.allocation[id];
const built = state.built.has(id);
visual.complete.visible = built;
visual.pad.material.color.set(
built ? 0x7e9d83 : state.selected.includes(id) ? 0x9dc5c5 : 0x718f88
);
if (built) {
visual.stones.forEach(item => (item.visible = false));
visual.woodStages.forEach(item => (item.visible = false));
visual.workers.forEach(item => (item.visible = false));
return;
}
visual.stones.forEach((item, index) => {
item.visible = index < allocation.stone;
});
visual.woodStages.forEach((item, index) => {
item.visible = allocation.wood > index;
});
const workerCount = Math.min(
visual.workers.length,
Math.ceil(allocation.labor / 2)
);
visual.workers.forEach((item, index) => {
item.visible = index < workerCount;
});
}
function updateStoreVisuals() {
woodMarkers.forEach((marker, index) => {
marker.visible = index < state.stock.wood;
});
stoneMarkers.forEach((marker, index) => {
marker.visible = index < state.stock.stone;
});
laborMarkers.forEach((marker, index) => {
marker.visible = index < state.stock.labor;
});
storeLabels.wood.element.textContent = `Wood · ${state.stock.wood}`;
storeLabels.stone.element.textContent = `Stone · ${state.stock.stone}`;
storeLabels.labor.element.textContent = `Labor · ${state.stock.labor}`;
}
// -------------------------------------------------------------------------
// Interface
// -------------------------------------------------------------------------
function renderAllocationCards() {
const signature = state.selected.join("|");
if (signature === allocationSignature) return;
allocationSignature = signature;
allocationList.innerHTML = state.selected
.map((id, index) => {
const project = projects[id];
return `
<div class="allocation-card" data-allocation-project="${id}">
<div class="allocation-card-header">
<div class="allocation-name">
<span class="allocation-priority">${index + 1}</span>
<strong>${project.name}</strong>
</div>
<span class="project-status" data-status="${id}">Blocked</span>
</div>
${resources
.map(
resource => `
<div class="resource-line">
<span class="resource-key ${resource}">${
resource === "wood"
? "W"
: resource === "stone"
? "S"
: "L"
}</span>
<div class="range-wrap">
<div class="range-label">
<span>${resourceNames[resource]}</span>
</div>
<input
type="range"
min="0"
max="${project.costs[resource]}"
step="1"
value="0"
data-project="${id}"
data-resource="${resource}"
aria-label="${project.name} ${resource} allocation"
/>
</div>
<div class="resource-value" data-value="${id}-${resource}">
0 <span>/ ${project.costs[resource]}</span>
</div>
</div>
`
)
.join("")}
</div>
`;
})
.join("");
}
function syncAllocationControls() {
for (const id of state.selected) {
const built = state.built.has(id);
const status = document.querySelector(`[data-status="${id}"]`);
if (built) {
status.textContent = "Built";
status.className = "project-status built";
} else if (isReady(id)) {
status.textContent = "Ready";
status.className = "project-status ready";
} else {
const missing = resources.filter(
resource =>
state.allocation[id][resource] < projects[id].costs[resource]
);
status.textContent = `${missing.length} short`;
status.className = "project-status";
}
for (const resource of resources) {
const input = allocationList.querySelector(
`input[data-project="${id}"][data-resource="${resource}"]`
);
const value = state.allocation[id][resource];
const max = projects[id].costs[resource];
const percent = max ? (value / max) * 100 : 0;
input.value = value;
input.disabled = built;
input.style.setProperty("--fill", `${percent}%`);
const valueElement = allocationList.querySelector(
`[data-value="${id}-${resource}"]`
);
valueElement.innerHTML = `${value} <span>/ ${max}</span>`;
}
}
}
function updateShortageSummary() {
const demand = getDemand();
const deficits = resources.map(resource =>
Math.max(0, demand[resource] - state.stock[resource])
);
const totalDeficit = deficits.reduce((sum, value) => sum + value, 0);
let heading;
let description;
if (state.selected.length < 2) {
heading = "Choose one more project";
description =
"The comparison will show whether the pair can fit inside the village's shared stores.";
} else if (totalDeficit > 0) {
const parts = [];
if (deficits[0]) parts.push(`${deficits[0]} more wood`);
if (deficits[1]) parts.push(`${deficits[1]} more stone`);
if (deficits[2]) parts.push(`${deficits[2]} more worker-days`);
heading = "The village is short";
description = `This pair needs ${parts.join(" and ")} than the commons can provide.`;
} else {
heading = "The stores can cover this pair";
description =
"Resources are sufficient in total, but you still need to divide them fairly.";
}
const rows = resources
.map((resource, index) => {
const available = state.stock[resource];
const required = demand[resource];
const deficit = deficits[index];
const percentage = available
? Math.min(100, (required / available) * 100)
: 0;
return `
<div class="summary-row">
<span>${resourceNames[resource]}</span>
<div class="summary-bar">
<i style="width: ${percentage}%"></i>
</div>
<b class="${deficit ? "short" : ""}">
${required} / ${available}${deficit ? ` · −${deficit}` : ""}
</b>
</div>
`;
})
.join("");
shortageCard.innerHTML = `
<div class="label">${totalDeficit ? "Shared shortfall" : "Demand vs. stores"}</div>
<h3>${heading}</h3>
<p>${description}</p>
<div class="resource-summary">${rows}</div>
`;
}
function updateUI() {
document.querySelectorAll(".project-card").forEach(card => {
const id = card.dataset.project;
const index = state.selected.indexOf(id);
const selected = index !== -1;
card.classList.toggle("selected", selected);
card.classList.toggle("built", state.built.has(id));
card.setAttribute("aria-pressed", String(selected));
card.querySelector(".priority-badge").textContent = String(index + 1);
});
document.querySelector("#selection-count").textContent =
`${state.selected.length} / 2 selected`;
for (const resource of resources) {
document.querySelector(`[data-stock="${resource}"]`).textContent =
state.stock[resource];
const remaining = Math.max(
0,
state.stock[resource] - plannedAmount(resource)
);
document.querySelector(`[data-left="${resource}"]`).textContent =
`${remaining} unassigned`;
}
updateShortageSummary();
renderAllocationCards();
syncAllocationControls();
const ready = state.selected.filter(id => isReady(id));
if (ready.length) {
buildButton.disabled = false;
buildButton.textContent = `Build ${projects[ready[0]].name}`;
buildHint.textContent =
"The other selected project remains blocked by the shared shortage.";
} else {
buildButton.disabled = true;
buildButton.textContent = "No project fully funded";
buildHint.textContent =
"Allocate at least one project's full requirements before building.";
}
for (const id of Object.keys(projects)) {
updateProjectVisual(id);
updateProjectLabel(id);
}
updateStoreVisuals();
}
function updateProjectLabel(id) {
const label = projectLabels[id].element;
const status = label.querySelector(".world-status");
label.classList.remove("ready", "built");
if (state.built.has(id)) {
label.classList.add("built");
status.textContent = "Built";
} else if (isReady(id)) {
label.classList.add("ready");
status.textContent = "Ready";
} else {
status.textContent = "Short";
}
}
document.querySelectorAll(".project-card").forEach(card => {
card.addEventListener("click", () => selectProject(card.dataset.project));
});
allocationList.addEventListener("input", event => {
const input = event.target;
if (!input.matches("input[data-resource]")) return;
const id = input.dataset.project;
const resource = input.dataset.resource;
if (state.built.has(id)) return;
const allocatedElsewhere = state.selected.reduce((sum, selectedId) => {
if (selectedId === id || state.built.has(selectedId)) return sum;
return sum + state.allocation[selectedId][resource];
}, 0);
const available = Math.max(0, state.stock[resource] - allocatedElsewhere);
const maximum = Math.min(projects[id].costs[resource], available);
const value = THREE.MathUtils.clamp(Number(input.value), 0, maximum);
state.allocation[id][resource] = value;
updateUI();
});
document.querySelector("#auto-allocate").addEventListener("click", () => {
autoAllocate();
updateUI();
showToast("Resources filled in priority order.");
});
buildButton.addEventListener("click", () => {
const ready = state.selected.filter(id => isReady(id));
if (!ready.length) return;
const id = ready[0];
const project = projects[id];
for (const resource of resources) {
state.stock[resource] -= project.costs[resource];
}
state.built.add(id);
emptyAllocations();
autoAllocate();
updateUI();
celebrate(project.position);
showToast(`${project.name} completed. The second project is still waiting.`);
});
document.querySelector("#reset-village").addEventListener("click", () => {
state.stock = { ...initialStock };
state.built.clear();
state.selected = ["school", "bridge"];
emptyAllocations();
autoAllocate();
updateUI();
showToast("The village has been restored.");
});
document.querySelectorAll("[data-mobile]").forEach(button => {
button.addEventListener("click", () => {
document.body.classList.toggle(
`mobile-${button.dataset.mobile}`,
button.dataset.mobile === "projects"
? !document.body.classList.contains("mobile-projects")
: !document.body.classList.contains("mobile-allocate")
);
if (button.dataset.mobile === "projects") {
document.body.classList.add("mobile-projects");
document.body.classList.remove("mobile-allocate");
} else {
document.body.classList.add("mobile-allocate");
document.body.classList.remove("mobile-projects");
}
});
});
// -------------------------------------------------------------------------
// Custom camera controls
// -------------------------------------------------------------------------
let dragging = false;
let pointerStart = { x: 0, y: 0 };
let previousPointer = { x: 0, y: 0 };
let pointerMoved = 0;
canvas.addEventListener("pointerdown", event => {
if (event.button !== 0) return;
dragging = true;
pointerMoved = 0;
pointerStart = { x: event.clientX, y: event.clientY };
previousPointer = { x: event.clientX, y: event.clientY };
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - previousPointer.x;
const dy = event.clientY - previousPointer.y;
pointerMoved += Math.abs(dx) + Math.abs(dy);
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.004,
0.45,
1.18
);
previousPointer = { x: event.clientX, y: event.clientY };
updateCamera();
});
canvas.addEventListener("pointerup", event => {
dragging = false;
if (
pointerMoved < 8 &&
Math.abs(event.clientX - pointerStart.x) < 8 &&
Math.abs(event.clientY - pointerStart.y) < 8
) {
pickProject(event.clientX, event.clientY);
}
});
canvas.addEventListener("pointercancel", () => {
dragging = false;
});
canvas.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraState.zoom = THREE.MathUtils.clamp(
cameraState.zoom * Math.exp(-event.deltaY * 0.001),
0.68,
1.75
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => {
cameraState.yaw = 0.67;
cameraState.pitch = 0.83;
cameraState.zoom = 1;
updateCamera();
});
canvas.addEventListener("contextmenu", event => event.preventDefault());
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function pickProject(clientX, clientY) {
pointer.x = (clientX / window.innerWidth) * 2 - 1;
pointer.y = -(clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(
Object.values(projectRoots),
true
);
if (!hits.length) return;
let object = hits[0].object;
while (object && !object.userData.projectId) {
object = object.parent;
}
if (object?.userData.projectId) {
selectProject(object.userData.projectId);
}
}
// -------------------------------------------------------------------------
// Construction celebration
// -------------------------------------------------------------------------
const particles = [];
function celebrate(position) {
const colors = [0xde7145, 0xe5aa4b, 0x78a49b, 0xf3dfb4];
for (let i = 0; i < 20; i++) {
const color = colors[i % colors.length];
const mesh = box(
scene,
[0.16, 0.16, 0.16],
[
position.x + (Math.random() - 0.5) * 1.8,
position.y + 1.5 + Math.random(),
position.z + (Math.random() - 0.5) * 1.8
],
color
);
particles.push({
mesh,
velocity: new THREE.Vector3(
(Math.random() - 0.5) * 2.2,
2 + Math.random() * 2,
(Math.random() - 0.5) * 2.2
),
life: 1.5
});
}
}
// -------------------------------------------------------------------------
// Projection and animation
// -------------------------------------------------------------------------
const projected = new THREE.Vector3();
function positionLabel(label) {
projected.copy(label.anchor).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.15 &&
Math.abs(projected.y) < 1.15;
label.element.style.display = visible ? "block" : "none";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
}
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
for (const worker of animatedWorkers) {
worker.position.y = Math.sin(elapsed * 2.4 + worker.userData.phase) * 0.025;
}
waterRipples.forEach((ripple, index) => {
ripple.position.x = 6.2 + Math.sin(elapsed * 0.65 + index) * 0.08;
ripple.material = getMaterial(0x8bc3c7);
});
for (let i = particles.length - 1; i >= 0; i--) {
const particle = particles[i];
particle.life -= delta;
particle.velocity.y -= 5 * delta;
particle.mesh.position.addScaledVector(particle.velocity, delta);
particle.mesh.rotation.x += delta * 4;
particle.mesh.rotation.z += delta * 3;
if (particle.life <= 0) {
scene.remove(particle.mesh);
particles.splice(i, 1);
}
}
for (const id of Object.keys(projectLabels)) {
positionLabel(projectLabels[id]);
}
for (const resource of Object.keys(storeLabels)) {
positionLabel(storeLabels[resource]);
}
renderer.render(scene, camera);
}
// -------------------------------------------------------------------------
// Start
// -------------------------------------------------------------------------
autoAllocate();
resize();
updateUI();
animate();
</script>
</body>
</html>
```