PROCITY/web/js/world/hud.js
m3ultra 73694c97aa Lane B round 19 (v4.0-alpha): in-game town selector (ledger #4, beta pull-forward, non-blocking)
Shipped the town picker in hud.js (which already owns its DOM overlay -> NO index.html seam). A compact
<select> in the HUD (top-left, pointer-events:auto), 9 options: synthetic + 3 OSM fixtures (osmTownKeys
minus _real) + 5 real-road caches (Katoomba/Newtown/Fremantle/Bendigo/Castlemaine, hardcoded REAL_TOWNS --
no manifest yet, beta derives it). Selecting navigates: synthetic clears plansrc/town; a town sets
?plansrc=osm&town=<key>, preserving all other params. Current pre-selected; unknown-current still shown.
Verified end-to-end: picked katoomba_real through the dropdown -> booted real Katoomba (966 edges/323
intersections/3 venues), 0 errors. Lives inside #pc-hud so hud.setVisible(false) hides it for money shots.

-> F (B->F seam): NONE -- self-contained in hud.js (already constructed by createHUD). Gate it behind ?dbg
   with one setVisible call if you prefer; left always-in-HUD as a modest debug-grade feature.

Corner-strips spot-check (R18 failure-item #3): ground fills real intersections OK -- degree-4 junction
(58 exist) shows road strips overlap-filling the junction, lane markings + zebra crossing render, no gross
gap. Acceptable for alpha; finer acute-angle pass = beta.

Budget note -> F (measured vs A's UNCOMMITTED WIP -- caveat, not a defect): F's BIG_CITY one-liner is live
(chunks 50->26). But venue block still ~237k tris at night against the current tree, which carries A's
uncommitted R19 finalization (plan_osm modified; graph grew 799->966 edges, junction-joining in but the
far-island CULL not yet). Fable's ruling: the cull is "half of B's tris problem" -> the <=200k gate needs
F's fix AND A's committed cull. Re-measure the busiest real block after A pins (the deferred tris-post-cull
re-measure genuinely waits on A's commit; the dev server serves other lanes' uncommitted edits).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-16 17:38:59 +10:00

158 lines
8.1 KiB
JavaScript

// PROCITY Lane B — hud.js
// The street HUD: crosshair, shop-name tooltip (throttled centre-screen raycast, far = 6m),
// time-of-day + seed + live perf readout (renderer.info: draw calls & triangles), and the
// door-click → `procity:enterShop` hand-off. Creates its own DOM overlay so index.html stays thin.
import * as THREE from 'three';
import { osmTownKeys } from '../citygen/index.js'; // R19 town selector: enumerate OSM fixtures
// R19 (v4.0-alpha, beta pull-forward): the real-roads town caches under web/assets/towns/*.json. Hardcoded
// for the alpha — there is no towns manifest yet; beta derives this from an index file. New towns: add here.
const REAL_TOWNS = ['katoomba_real', 'newtown_real', 'fremantle_real', 'bendigo_real', 'castlemaine_real'];
const CSS = `
#pc-hud{position:fixed;inset:0;pointer-events:none;font:13px/1.4 -apple-system,Segoe UI,Roboto,sans-serif;color:#f4efe6;z-index:10}
#pc-cross{position:absolute;left:50%;top:50%;width:6px;height:6px;margin:-3px 0 0 -3px;border-radius:50%;background:#fff;opacity:.7;box-shadow:0 0 3px #000}
#pc-tip{position:absolute;left:50%;top:calc(50% + 22px);transform:translateX(-50%);background:rgba(20,16,10,.82);padding:6px 12px;border-radius:6px;white-space:nowrap;opacity:0;transition:opacity .12s;text-shadow:0 1px 2px #000}
#pc-dbg{position:absolute;right:10px;top:10px;text-align:right;background:rgba(20,16,10,.55);padding:8px 11px;border-radius:8px;min-width:150px}
#pc-dbg b{color:#ffd75e;font-weight:600}
#pc-dbg .warn{color:#ff9a6a}
#pc-clock{font-size:15px;margin-bottom:3px}
#pc-toast{position:absolute;left:50%;bottom:64px;transform:translateX(-50%);background:rgba(20,16,10,.9);padding:10px 18px;border-radius:10px;font-size:15px;opacity:0;transition:opacity .25s;text-shadow:0 1px 2px #000}
#pc-help{position:absolute;left:12px;bottom:12px;background:rgba(20,16,10,.5);padding:7px 11px;border-radius:8px;opacity:.85}
#pc-townpick{position:absolute;left:10px;top:10px;pointer-events:auto;opacity:.8;transition:opacity .12s}
#pc-townpick:hover{opacity:1}
#pc-townpick select{background:rgba(20,16,10,.7);color:#f4efe6;border:1px solid rgba(255,215,94,.35);border-radius:8px;padding:5px 9px;font:12px/1.3 -apple-system,Segoe UI,Roboto,sans-serif;max-width:230px;cursor:pointer}
#pc-townpick select:focus{outline:1px solid rgba(255,215,94,.6)}
`;
export function createHUD({ camera, renderer, plan, getDoorMeshes, onEnterShop }) {
if (!document.getElementById('pc-hud-style')) {
const st = document.createElement('style'); st.id = 'pc-hud-style'; st.textContent = CSS;
document.head.appendChild(st);
}
const root = document.createElement('div'); root.id = 'pc-hud';
root.innerHTML = `
<div id="pc-cross"></div>
<div id="pc-tip"></div>
<div id="pc-dbg">
<div id="pc-clock">—</div>
<div>town <b id="pc-town">—</b></div>
<div>seed <b id="pc-seed">—</b></div>
<div>fps <b id="pc-fps">—</b></div>
<div>draws <b id="pc-draws">—</b> · tris <b id="pc-tris">—</b></div>
<div>chunks <b id="pc-chunks">—</b></div>
</div>
<div id="pc-toast"></div>
<div id="pc-townpick"><select id="pc-town-select" title="Load a different town (Esc to release the pointer first)"></select></div>
<div id="pc-help">WASD move · shift run · mouse look · click a door · [ ] time · P shot · Esc release</div>`;
document.body.appendChild(root);
const $ = (id) => root.querySelector('#' + id);
const elTip = $('pc-tip'), elClock = $('pc-clock'), elFps = $('pc-fps'), elDraws = $('pc-draws'),
elTris = $('pc-tris'), elChunks = $('pc-chunks'), elToast = $('pc-toast');
$('pc-town').textContent = plan.name || '—';
$('pc-seed').textContent = plan.citySeed;
// ── town selector (R19, beta pull-forward) — synthetic + OSM fixtures + real-road caches. Navigates
// to the matching URL params, preserving everything else (seed, flags). Lives in the HUD overlay, so it
// hides with the HUD for money shots; usable when the pointer is free (Esc). No index.html seam. ──
(() => {
const sel = $('pc-town-select'); if (!sel) return;
const params = new URLSearchParams(location.search);
const cur = params.get('plansrc') === 'osm' ? (params.get('town') || '') : '';
const cap = (k) => k.replace(/_real$/, '').replace(/(^|[\s_])\w/g, (c) => c.toUpperCase()).replace(/_/g, ' ');
const add = (key, label) => { const o = document.createElement('option'); o.value = key; o.textContent = label; if (key === cur) o.selected = true; sel.appendChild(o); };
add('', '▸ Synthetic — procedural');
for (const k of osmTownKeys().filter((k) => !k.endsWith('_real'))) add(k, `${cap(k)} · OSM fixture`);
for (const k of REAL_TOWNS) add(k, `${cap(k)} · real roads`);
if (cur && !Array.from(sel.options).some((o) => o.value === cur)) add(cur, `${cap(cur)} · (current)`); // unknown current town → still shown
sel.onchange = () => {
const v = sel.value, p = new URLSearchParams(location.search);
if (!v) { p.delete('plansrc'); p.delete('town'); } else { p.set('plansrc', 'osm'); p.set('town', v); }
location.search = p.toString(); // reload into the chosen town
};
})();
const ray = new THREE.Raycaster();
ray.far = 6;
const centre = new THREE.Vector2(0, 0);
let hovered = null; // { shopId, name }
let frame = 0, fpsAcc = 0, fpsCount = 0, fpsShown = 0;
function nearestDoor(rects, point) {
let best = null, bd = Infinity;
for (const r of rects) {
const d = (r.x - point.x) ** 2 + (r.z - point.z) ** 2;
if (d < bd) { bd = d; best = r; }
}
return best;
}
function raycastDoor() {
const meshes = getDoorMeshes ? getDoorMeshes() : [];
if (!meshes.length) { hovered = null; return; }
ray.setFromCamera(centre, camera);
const hits = ray.intersectObjects(meshes, false);
if (hits.length) {
const h = hits[0];
const rects = h.object.userData.doorRects || [];
const door = nearestDoor(rects, h.point);
hovered = door ? { shopId: door.shopId, name: door.name, hours: door.hours } : null;
} else {
hovered = null;
}
}
function update(dt, info) {
// fps (smoothed over ~0.4s)
fpsAcc += dt; fpsCount++;
if (fpsAcc >= 0.4) { fpsShown = Math.round(fpsCount / fpsAcc); fpsAcc = 0; fpsCount = 0; }
frame++;
if (frame % 6 === 0) raycastDoor();
// tooltip — shows CLOSED (with opening hour) when the shop is shut at the current hour (§3.5)
if (hovered) {
const h = hovered.hours;
const now = window.PROCITY && window.PROCITY.currentHour ? window.PROCITY.currentHour() : null;
const closed = h && now != null && !(now >= h[0] && now < h[1]);
elTip.textContent = closed
? `🔒 ${hovered.name} — CLOSED · opens ${String(h[0]).padStart(2, '0')}:00`
: `🚪 ${hovered.name} — click to enter`;
elTip.style.opacity = 1;
} else elTip.style.opacity = 0;
// debug readout
if (info) {
if (info.clock) elClock.textContent = `${info.clock.label} ${info.clock.hour}${info.clock.night ? ' 🌙' : ''}`;
elFps.textContent = fpsShown;
const draws = renderer.info.render.calls, tris = renderer.info.render.triangles;
elDraws.innerHTML = draws > 300 ? `<span class="warn">${draws}</span>` : draws;
elTris.innerHTML = tris > 200000 ? `<span class="warn">${(tris / 1000 | 0)}k</span>` : `${(tris / 1000 | 0)}k`;
if (info.chunks != null) elChunks.textContent = info.chunks;
}
}
let toastT = 0;
function showToast(msg, secs = 2.6) {
elToast.textContent = msg; elToast.style.opacity = 1; toastT = secs;
}
function tickToast(dt) { if (toastT > 0) { toastT -= dt; if (toastT <= 0) elToast.style.opacity = 0; } }
// door click → enter shop (only when hovering one)
function onClick() {
if (hovered && onEnterShop) onEnterShop(hovered.shopId, hovered.name);
}
renderer.domElement.addEventListener('click', onClick);
function setVisible(v) { root.style.display = v ? 'block' : 'none'; }
function dispose() {
renderer.domElement.removeEventListener('click', onClick);
root.remove();
}
return { update, tickToast, showToast, setVisible, dispose, getHovered: () => hovered,
getFps: () => fpsShown };
}