Static site
Browse files- index.html +4 -2
- static/main.js +36 -6
- static/net.js +65 -26
- static/style.css +9 -0
index.html
CHANGED
|
@@ -8,7 +8,7 @@
|
|
| 8 |
<meta name="theme-color" content="#10131b">
|
| 9 |
<link rel="preconnect" href="https://fonts.googleapis.com"><link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
| 10 |
<link href="https://fonts.googleapis.com/css2?family=Unbounded:wght@500;800;900&family=Inter:wght@400;500;600;700&family=Noto+Kufi+Arabic:wght@700&display=swap" rel="stylesheet">
|
| 11 |
-
<link rel="stylesheet" href="/static/style.css?v=
|
| 12 |
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns=%27http://www.w3.org/2000/svg%27 viewBox=%270 0 64 64%27%3E%3Crect width=%2764%27 height=%2764%27 rx=%2714%27 fill=%27%23050506%27/%3E%3Ccircle cx=%2732%27 cy=%2732%27 r=%2716%27 fill=%27none%27 stroke=%27%23fff%27 stroke-width=%275%27/%3E%3C/svg%3E">
|
| 13 |
<script type="importmap">
|
| 14 |
{ "imports": {
|
|
@@ -137,6 +137,7 @@
|
|
| 137 |
<label>Your name <input id="gNetName" maxlength="14" placeholder="Driver"></label>
|
| 138 |
<div class="row"><button class="pill solid" id="gNetPublic">Join the public city</button><button class="pill" id="gNetCreate">Create a private room</button></div>
|
| 139 |
<div class="row"><input id="gNetCode" maxlength="8" placeholder="Room code"><button class="pill" id="gNetJoin">Join room</button></div>
|
|
|
|
| 140 |
<div class="row"><button class="pill" id="gMic">🎙 Voice chat</button><button class="pill" id="gNetLeave">Leave</button><button class="pill" id="gNetClose">Close</button></div>
|
| 141 |
<div class="emotes" id="gEmotes"></div>
|
| 142 |
<p class="small" id="gNetStatus">Offline</p>
|
|
@@ -161,6 +162,7 @@
|
|
| 161 |
<label class="set">Screen shake and speed blur<span class="seg" data-set="motion"><button data-v="on">On</button><button data-v="off">Off</button></span></label>
|
| 162 |
<label class="set">Hoverboard<span class="seg" data-set="board"><button data-v="ion">Ion</button><button data-v="blaze">Blaze</button><button data-v="toxic">Toxic</button><button data-v="violet">Violet</button><button data-v="gold">Gold</button><button data-v="ghost">Ghost</button></span></label>
|
| 163 |
<label class="set">Traffic & crowds<span class="seg" data-set="crowd"><button data-v="few">Few</button><button data-v="normal">Normal</button><button data-v="busy">Busy</button></span></label>
|
|
|
|
| 164 |
<label class="set">Show FPS<span class="seg" data-set="fps"><button data-v="off">Off</button><button data-v="on">On</button></span></label>
|
| 165 |
<label class="set">Text size<span class="seg" data-set="text"><button data-v="normal">Normal</button><button data-v="large">Large</button></span></label>
|
| 166 |
<label class="set">Touch steering<span class="seg" data-set="steer"><button data-v="stick">Joystick</button><button data-v="buttons">Buttons</button></span></label>
|
|
@@ -219,6 +221,6 @@
|
|
| 219 |
<div id="toast" role="status"></div>
|
| 220 |
<noscript><p style="position:fixed;inset:auto 0 0;padding:16px;background:#111;color:#fff;text-align:center">VOID needs JavaScript for the 3D car.</p></noscript>
|
| 221 |
<script src="/static/hub.js" data-app="void" data-credit="none" defer></script>
|
| 222 |
-
<script type="module" src="/static/main.js?v=
|
| 223 |
</body>
|
| 224 |
</html>
|
|
|
|
| 8 |
<meta name="theme-color" content="#10131b">
|
| 9 |
<link rel="preconnect" href="https://fonts.googleapis.com"><link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
| 10 |
<link href="https://fonts.googleapis.com/css2?family=Unbounded:wght@500;800;900&family=Inter:wght@400;500;600;700&family=Noto+Kufi+Arabic:wght@700&display=swap" rel="stylesheet">
|
| 11 |
+
<link rel="stylesheet" href="/static/style.css?v=1794761969">
|
| 12 |
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns=%27http://www.w3.org/2000/svg%27 viewBox=%270 0 64 64%27%3E%3Crect width=%2764%27 height=%2764%27 rx=%2714%27 fill=%27%23050506%27/%3E%3Ccircle cx=%2732%27 cy=%2732%27 r=%2716%27 fill=%27none%27 stroke=%27%23fff%27 stroke-width=%275%27/%3E%3C/svg%3E">
|
| 13 |
<script type="importmap">
|
| 14 |
{ "imports": {
|
|
|
|
| 137 |
<label>Your name <input id="gNetName" maxlength="14" placeholder="Driver"></label>
|
| 138 |
<div class="row"><button class="pill solid" id="gNetPublic">Join the public city</button><button class="pill" id="gNetCreate">Create a private room</button></div>
|
| 139 |
<div class="row"><input id="gNetCode" maxlength="8" placeholder="Room code"><button class="pill" id="gNetJoin">Join room</button></div>
|
| 140 |
+
<div class="net-list" id="gNetPlayers"></div>
|
| 141 |
<div class="row"><button class="pill" id="gMic">🎙 Voice chat</button><button class="pill" id="gNetLeave">Leave</button><button class="pill" id="gNetClose">Close</button></div>
|
| 142 |
<div class="emotes" id="gEmotes"></div>
|
| 143 |
<p class="small" id="gNetStatus">Offline</p>
|
|
|
|
| 162 |
<label class="set">Screen shake and speed blur<span class="seg" data-set="motion"><button data-v="on">On</button><button data-v="off">Off</button></span></label>
|
| 163 |
<label class="set">Hoverboard<span class="seg" data-set="board"><button data-v="ion">Ion</button><button data-v="blaze">Blaze</button><button data-v="toxic">Toxic</button><button data-v="violet">Violet</button><button data-v="gold">Gold</button><button data-v="ghost">Ghost</button></span></label>
|
| 164 |
<label class="set">Traffic & crowds<span class="seg" data-set="crowd"><button data-v="few">Few</button><button data-v="normal">Normal</button><button data-v="busy">Busy</button></span></label>
|
| 165 |
+
<label class="set">Voice chat<span class="seg" data-set="voice"><button data-v="open">Open mic</button><button data-v="ptt">Push to talk (Z)</button></span></label>
|
| 166 |
<label class="set">Show FPS<span class="seg" data-set="fps"><button data-v="off">Off</button><button data-v="on">On</button></span></label>
|
| 167 |
<label class="set">Text size<span class="seg" data-set="text"><button data-v="normal">Normal</button><button data-v="large">Large</button></span></label>
|
| 168 |
<label class="set">Touch steering<span class="seg" data-set="steer"><button data-v="stick">Joystick</button><button data-v="buttons">Buttons</button></span></label>
|
|
|
|
| 221 |
<div id="toast" role="status"></div>
|
| 222 |
<noscript><p style="position:fixed;inset:auto 0 0;padding:16px;background:#111;color:#fff;text-align:center">VOID needs JavaScript for the 3D car.</p></noscript>
|
| 223 |
<script src="/static/hub.js" data-app="void" data-credit="none" defer></script>
|
| 224 |
+
<script type="module" src="/static/main.js?v=1794761969"></script>
|
| 225 |
</body>
|
| 226 |
</html>
|
static/main.js
CHANGED
|
@@ -66,7 +66,7 @@ setInterval(() => { if (document.hidden) net.tick(.5, null); }, 500); // a h
|
|
| 66 |
const GPU = (() => { try { const c = renderer.getContext(), e = c.getExtension("WEBGL_debug_renderer_info"); return e ? c.getParameter(e.UNMASKED_RENDERER_WEBGL) : ""; } catch { return ""; } })();
|
| 67 |
const STRONG = /RTX|GTX 1[06-9]|GTX 9[78]|Radeon RX|Radeon Pro|Apple M[1-9]/i.test(GPU) && !COARSE;
|
| 68 |
const TOP = /RTX\s?(40[6-9]0|50[5-9]0)|RTX\s?A?[45]\d{3}/i.test(GPU) && !COARSE; // recent RTX cards start on Ultra
|
| 69 |
-
const SET = Object.assign({ q: LITE ? "lite" : TOP ? "ultra" : STRONG ? "high" : COARSE ? "low" : "medium", sens: 1, autogas: "off", steer: "stick", weather: "clear", radio: "score", cycle: "off", handling: "arcade", motion: "on", text: "normal", board: "ion", crowd: LITE ? "few" : "normal", fps: "off" }, store.get("void.settings") || {});
|
| 70 |
const gameOpts = {
|
| 71 |
handling: () => SET.handling, buyFood: () => { if (cash < 10) return false; cash -= 10; store.set("void.cash", cash); earn(0); return true; }, heroOwned: () => !!store.get("void.hero"), starter: () => { const m = life.models.find(m => m.id === "compact-3") || life.models.find(m => m.kind.id === "compact"); return { model: { ...m, top: 31, acc: .7, grip: .9 }, color: 0x6b5a44, rust: .75, owned: true }; }, onSteal: c => { police && police.event("steal"); goals.stats.steals = (goals.stats.steals || 0) + 1; },
|
| 72 |
garage: () => openGarage(true), cycleTime, hornType: () => build.horn, autoGas: () => SET.autogas === "on", onRepair: () => car.repair && car.repair(),
|
|
@@ -113,6 +113,7 @@ menuEl.querySelectorAll("[data-veh]").forEach(b => b.onclick = () => game.api.se
|
|
| 113 |
document.getElementById("gFuelNav").onclick = () => { const st = game.state; if (!st) return; const best = W.city.stations.slice().sort((a, b) => Math.hypot(a.x - st.x, a.z - st.z) - Math.hypot(b.x - st.x, b.z - st.z))[0]; nav.setDest({ name: "⛽ Fuel station", x: best.x, z: best.z }); };
|
| 114 |
function applySettings(save = true) {
|
| 115 |
document.body.classList.toggle("show-fps", SET.fps === "on");
|
|
|
|
| 116 |
if (typeof boardMesh !== "undefined") paintBoard();
|
| 117 |
if (typeof life !== "undefined" && life.setVoidDetail) life.setVoidDetail(SET.q !== "lite"); // Lite: simple flying VOID cars
|
| 118 |
document.body.classList.toggle("big-ui", SET.text === "large");
|
|
@@ -262,6 +263,12 @@ document.getElementById("gSaveIn").onclick = () => {
|
|
| 262 |
// ---------- online: other real players' cars, name tags, emotes, proximity voice ----------
|
| 263 |
const remotes = new Map();
|
| 264 |
const tagTex = (text, col = "#ffffff") => W.canvasTex(512, 96, (g, w, h) => { g.fillStyle = "rgba(0,0,0,.55)"; g.fillRect(0, 0, w, h); g.fillStyle = col; g.font = "800 50px Inter, sans-serif"; g.textAlign = "center"; g.fillText(text, w / 2, 66); });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
function remoteFor(p) {
|
| 266 |
let r = remotes.get(p.id);
|
| 267 |
const model = Object.keys(MODELS)[p.model | 0] || "hydra";
|
|
@@ -271,7 +278,8 @@ function remoteFor(p) {
|
|
| 271 |
c.group.traverse(o => { if (o.isMesh) o.castShadow = true; });
|
| 272 |
const tag = new THREE.Sprite(new THREE.SpriteMaterial({ map: tagTex(p.name), depthTest: false, toneMapped: false })); tag.scale.set(5, .95, 1); tag.position.y = 2.6; root.add(tag);
|
| 273 |
const emo = new THREE.Sprite(new THREE.SpriteMaterial({ map: tagTex(""), depthTest: false, toneMapped: false })); emo.scale.set(6, 1.15, 1); emo.position.y = 3.8; emo.visible = false; root.add(emo);
|
| 274 |
-
|
|
|
|
| 275 |
}
|
| 276 |
if (r.name !== p.name) { r.name = p.name; r.tag.material.map = tagTex(p.name); }
|
| 277 |
if (p.paint && r.paint !== p.paint) { r.paint = p.paint; r.car.setPaint(p.paint); }
|
|
@@ -279,10 +287,22 @@ function remoteFor(p) {
|
|
| 279 |
}
|
| 280 |
net.on("change", () => { for (const id of [...remotes.keys()]) if (!net.players().some(p => p.id === id)) { scene.remove(remotes.get(id).root); remotes.delete(id); } renderPlayers(); });
|
| 281 |
net.on("status", t => { document.getElementById("gNetStatus").textContent = t; document.getElementById("gNetBtn").classList.toggle("on", net.online()); });
|
| 282 |
-
net.on("mic",
|
| 283 |
-
|
|
|
|
| 284 |
function feed(t) { const f = document.getElementById("gFeed"); const d = document.createElement("div"); d.textContent = t; f.prepend(d); setTimeout(() => d.remove(), 7000); while (f.children.length > 5) f.lastChild.remove(); }
|
| 285 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
{
|
| 287 |
const $ = id => document.getElementById(id), panel = $("gNetPanel");
|
| 288 |
$("gNetName").value = store.get("void.name") || "";
|
|
@@ -294,6 +314,13 @@ function renderPlayers() { document.getElementById("gPlayers").innerHTML = net.o
|
|
| 294 |
$("gNetJoin").onclick = () => { const code = $("gNetCode").value.trim().toLowerCase(); if (code) go("room-" + code); };
|
| 295 |
$("gNetLeave").onclick = () => { net.leave(); panel.hidden = true; };
|
| 296 |
$("gMic").onclick = () => net.setMic(!net.mic);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
$("gEmotes").innerHTML = EMOTES.map((e, k) => `<button class="pill" data-e="${k}">${e}</button>`).join("");
|
| 298 |
$("gEmotes").onclick = ev => { const b = ev.target.closest("[data-e]"); if (!b || !net.online()) return; net.send({ t: "emote", e: +b.dataset.e }); feed(`You: ${EMOTES[+b.dataset.e]}`); };
|
| 299 |
}
|
|
@@ -538,8 +565,10 @@ function frameLoop(fixed) {
|
|
| 538 |
document.getElementById("gStars").textContent = stars ? "★".repeat(stars) + "☆".repeat(5 - stars) : "";
|
| 539 |
document.getElementById("gStars").classList.toggle("bust", police.bust > .5);
|
| 540 |
const pr = races.progress();
|
| 541 |
-
net.tick(dt, { p: [st.x, st.y, st.z, st.yaw, st.pitch, st.roll].map(v => Math.round(v * 100) / 100), m: Object.keys(MODELS).indexOf(car.model), c: OPTS.paint.items[build.paint][1], v: Math.round(S.speed), a: S.air ? 1 : 0, r: pr ? pr.race : 0, rp: pr ? pr.rp : 0, rf: pr ? pr.rf : 0, w: stars });
|
| 542 |
net.listen(sound.ctx, camera);
|
|
|
|
|
|
|
| 543 |
for (const p of net.players()) {
|
| 544 |
const at = net.sample(p); const r = remoteFor(p); r.at = at;
|
| 545 |
if (!at || at.stale) { r.root.visible = false; continue; }
|
|
@@ -549,6 +578,7 @@ function frameLoop(fixed) {
|
|
| 549 |
if (r.emoT > 0 && (r.emoT -= dt) <= 0) r.emo.visible = false;
|
| 550 |
const dx = st.x - at.x, dz = st.z - at.z, d = Math.hypot(dx, dz);
|
| 551 |
net.placeVoice(p.id, at.x, at.y, at.z, Math.hypot(d, st.y - at.y));
|
|
|
|
| 552 |
if (d < 3.4 && d > .01 && Math.abs(st.y - at.y) < 2) { st.x += dx / d * (3.4 - d) * .5; st.z += dz / d * (3.4 - d) * .5; st.vel.multiplyScalar(.9); }
|
| 553 |
}
|
| 554 |
} else { for (const r of remotes.values()) r.root.visible = false; net.tick(dt, null); } // the host keeps relaying from the menus
|
|
|
|
| 66 |
const GPU = (() => { try { const c = renderer.getContext(), e = c.getExtension("WEBGL_debug_renderer_info"); return e ? c.getParameter(e.UNMASKED_RENDERER_WEBGL) : ""; } catch { return ""; } })();
|
| 67 |
const STRONG = /RTX|GTX 1[06-9]|GTX 9[78]|Radeon RX|Radeon Pro|Apple M[1-9]/i.test(GPU) && !COARSE;
|
| 68 |
const TOP = /RTX\s?(40[6-9]0|50[5-9]0)|RTX\s?A?[45]\d{3}/i.test(GPU) && !COARSE; // recent RTX cards start on Ultra
|
| 69 |
+
const SET = Object.assign({ q: LITE ? "lite" : TOP ? "ultra" : STRONG ? "high" : COARSE ? "low" : "medium", sens: 1, autogas: "off", steer: "stick", weather: "clear", radio: "score", cycle: "off", handling: "arcade", motion: "on", text: "normal", board: "ion", crowd: LITE ? "few" : "normal", fps: "off", voice: "open" }, store.get("void.settings") || {});
|
| 70 |
const gameOpts = {
|
| 71 |
handling: () => SET.handling, buyFood: () => { if (cash < 10) return false; cash -= 10; store.set("void.cash", cash); earn(0); return true; }, heroOwned: () => !!store.get("void.hero"), starter: () => { const m = life.models.find(m => m.id === "compact-3") || life.models.find(m => m.kind.id === "compact"); return { model: { ...m, top: 31, acc: .7, grip: .9 }, color: 0x6b5a44, rust: .75, owned: true }; }, onSteal: c => { police && police.event("steal"); goals.stats.steals = (goals.stats.steals || 0) + 1; },
|
| 72 |
garage: () => openGarage(true), cycleTime, hornType: () => build.horn, autoGas: () => SET.autogas === "on", onRepair: () => car.repair && car.repair(),
|
|
|
|
| 113 |
document.getElementById("gFuelNav").onclick = () => { const st = game.state; if (!st) return; const best = W.city.stations.slice().sort((a, b) => Math.hypot(a.x - st.x, a.z - st.z) - Math.hypot(b.x - st.x, b.z - st.z))[0]; nav.setDest({ name: "⛽ Fuel station", x: best.x, z: best.z }); };
|
| 114 |
function applySettings(save = true) {
|
| 115 |
document.body.classList.toggle("show-fps", SET.fps === "on");
|
| 116 |
+
net.setPTT(SET.voice === "ptt"); micLabel();
|
| 117 |
if (typeof boardMesh !== "undefined") paintBoard();
|
| 118 |
if (typeof life !== "undefined" && life.setVoidDetail) life.setVoidDetail(SET.q !== "lite"); // Lite: simple flying VOID cars
|
| 119 |
document.body.classList.toggle("big-ui", SET.text === "large");
|
|
|
|
| 263 |
// ---------- online: other real players' cars, name tags, emotes, proximity voice ----------
|
| 264 |
const remotes = new Map();
|
| 265 |
const tagTex = (text, col = "#ffffff") => W.canvasTex(512, 96, (g, w, h) => { g.fillStyle = "rgba(0,0,0,.55)"; g.fillRect(0, 0, w, h); g.fillStyle = col; g.font = "800 50px Inter, sans-serif"; g.textAlign = "center"; g.fillText(text, w / 2, 66); });
|
| 266 |
+
// the speaking icon: a green speaker with sound waves
|
| 267 |
+
const spkMat = new THREE.SpriteMaterial({ depthTest: false, toneMapped: false, map: W.canvasTex(128, 128, (g, w, h) => {
|
| 268 |
+
g.fillStyle = "rgba(0,0,0,.55)"; g.beginPath(); g.arc(64, 64, 62, 0, Math.PI * 2); g.fill(); g.fillStyle = g.strokeStyle = "#3dff8a"; g.lineWidth = 9; g.lineCap = "round";
|
| 269 |
+
g.beginPath(); g.moveTo(26, 50); g.lineTo(44, 50); g.lineTo(64, 30); g.lineTo(64, 98); g.lineTo(44, 78); g.lineTo(26, 78); g.closePath(); g.fill();
|
| 270 |
+
for (const r of [16, 30]) { g.beginPath(); g.arc(68, 64, r, -.8, .8); g.stroke(); }
|
| 271 |
+
}) });
|
| 272 |
function remoteFor(p) {
|
| 273 |
let r = remotes.get(p.id);
|
| 274 |
const model = Object.keys(MODELS)[p.model | 0] || "hydra";
|
|
|
|
| 278 |
c.group.traverse(o => { if (o.isMesh) o.castShadow = true; });
|
| 279 |
const tag = new THREE.Sprite(new THREE.SpriteMaterial({ map: tagTex(p.name), depthTest: false, toneMapped: false })); tag.scale.set(5, .95, 1); tag.position.y = 2.6; root.add(tag);
|
| 280 |
const emo = new THREE.Sprite(new THREE.SpriteMaterial({ map: tagTex(""), depthTest: false, toneMapped: false })); emo.scale.set(6, 1.15, 1); emo.position.y = 3.8; emo.visible = false; root.add(emo);
|
| 281 |
+
const spk = new THREE.Sprite(spkMat); spk.scale.set(1.1, 1.1, 1); spk.position.set(0, 2.6, 0); spk.center.set(2.85, .5); // anchored so it sits just left of the name tag spk.visible = false; root.add(spk);
|
| 282 |
+
scene.add(root); r = { root, car: c, tag, emo, spk, model, name: p.name, paint: null, emoT: 0 }; remotes.set(p.id, r);
|
| 283 |
}
|
| 284 |
if (r.name !== p.name) { r.name = p.name; r.tag.material.map = tagTex(p.name); }
|
| 285 |
if (p.paint && r.paint !== p.paint) { r.paint = p.paint; r.car.setPaint(p.paint); }
|
|
|
|
| 287 |
}
|
| 288 |
net.on("change", () => { for (const id of [...remotes.keys()]) if (!net.players().some(p => p.id === id)) { scene.remove(remotes.get(id).root); remotes.delete(id); } renderPlayers(); });
|
| 289 |
net.on("status", t => { document.getElementById("gNetStatus").textContent = t; document.getElementById("gNetBtn").classList.toggle("on", net.online()); });
|
| 290 |
+
net.on("mic", () => micLabel());
|
| 291 |
+
function micLabel() { const b = document.getElementById("gMic"), t = !net.mic ? "🎙 Voice chat" : !net.ptt ? "🎙 Mic on" : net.talking ? "🎙 Talking" : "🎙 Hold Z to talk"; b.classList.toggle("on", net.mic); if (b.textContent !== t) b.textContent = t; }
|
| 292 |
+
net.on("emote", (m, from) => { const r = remotes.get(from), e = EMOTES[m.e | 0]; if (!e || net.muted(from)) return; if (r) { r.emo.material.map = tagTex(e, "#ffd23d"); r.emo.visible = true; r.emoT = 4; } feed(`${net.nameOf(from)}: ${e}`); });
|
| 293 |
function feed(t) { const f = document.getElementById("gFeed"); const d = document.createElement("div"); d.textContent = t; f.prepend(d); setTimeout(() => d.remove(), 7000); while (f.children.length > 5) f.lastChild.remove(); }
|
| 294 |
+
const esc = s => String(s).replace(/[&<>"]/g, c => ({ "&": "&", "<": "<", ">": ">", '"': """ })[c]);
|
| 295 |
+
const pingDot = ms => ms == null ? "" : `<i class="ping ${ms < 90 ? "ok" : ms < 200 ? "mid" : "bad"}"></i>${ms} ms`;
|
| 296 |
+
let playersHTML = "", netListHTML = "", netUiT = 0, micLv = 0;
|
| 297 |
+
function renderPlayers() {
|
| 298 |
+
const me = net.online() ? `<b>${esc(net.name)} (you)${net.ping != null && !net.host ? " · " + pingDot(net.ping) : ""}</b>` : "";
|
| 299 |
+
const rows = net.online() ? [me, ...net.players().map(p => { const m = net.muted(p.id), sp = net.speaking(p.id);
|
| 300 |
+
return `<span class="${sp ? "speaking" : ""}">${m ? "🔇 " : sp ? "🔊 " : p.vc ? "🎙 " : ""}${esc(p.name)}${p.ping != null ? " · " + pingDot(p.ping) : ""}</span>`; })].join("") : "";
|
| 301 |
+
if (rows !== playersHTML) { playersHTML = rows; document.getElementById("gPlayers").innerHTML = rows; }
|
| 302 |
+
const list = net.online() ? net.players().map(p => { const m = net.muted(p.id);
|
| 303 |
+
return `<div class="net-row"><span>${p.vc ? "🎙 " : ""}${esc(p.name)} ${pingDot(p.ping)}</span><button class="pill${m ? " on" : ""}" data-mute="${esc(p.id)}">${m ? "🔇 Muted" : "🔊 Mute"}</button></div>`; }).join("") || `<p class="small">No one else here yet.</p>` : "";
|
| 304 |
+
if (list !== netListHTML) { netListHTML = list; document.getElementById("gNetPlayers").innerHTML = list; }
|
| 305 |
+
}
|
| 306 |
{
|
| 307 |
const $ = id => document.getElementById(id), panel = $("gNetPanel");
|
| 308 |
$("gNetName").value = store.get("void.name") || "";
|
|
|
|
| 314 |
$("gNetJoin").onclick = () => { const code = $("gNetCode").value.trim().toLowerCase(); if (code) go("room-" + code); };
|
| 315 |
$("gNetLeave").onclick = () => { net.leave(); panel.hidden = true; };
|
| 316 |
$("gMic").onclick = () => net.setMic(!net.mic);
|
| 317 |
+
$("gNetPlayers").onclick = e => { const b = e.target.closest("[data-mute]"); if (!b) return; const id = b.dataset.mute; net.mute(id, !net.muted(id)); toast(net.muted(id) ? `Muted ${net.nameOf(id)} (voice and emotes)` : `Unmuted ${net.nameOf(id)}`); };
|
| 318 |
+
// push-to-talk: hold Z (or hold the mic button on a touch screen)
|
| 319 |
+
addEventListener("keydown", e => { if (e.code === "KeyZ" && !e.repeat && net.ptt && net.mic && !/INPUT|TEXTAREA/.test(document.activeElement.tagName)) net.talk(true); });
|
| 320 |
+
addEventListener("keyup", e => { if (e.code === "KeyZ") net.talk(false); });
|
| 321 |
+
addEventListener("blur", () => net.talk(false));
|
| 322 |
+
$("gMic").addEventListener("pointerdown", () => { if (net.ptt && net.mic) net.talk(true); });
|
| 323 |
+
for (const ev of ["pointerup", "pointerleave", "pointercancel"]) $("gMic").addEventListener(ev, () => { if (net.ptt) net.talk(false); });
|
| 324 |
$("gEmotes").innerHTML = EMOTES.map((e, k) => `<button class="pill" data-e="${k}">${e}</button>`).join("");
|
| 325 |
$("gEmotes").onclick = ev => { const b = ev.target.closest("[data-e]"); if (!b || !net.online()) return; net.send({ t: "emote", e: +b.dataset.e }); feed(`You: ${EMOTES[+b.dataset.e]}`); };
|
| 326 |
}
|
|
|
|
| 565 |
document.getElementById("gStars").textContent = stars ? "★".repeat(stars) + "☆".repeat(5 - stars) : "";
|
| 566 |
document.getElementById("gStars").classList.toggle("bust", police.bust > .5);
|
| 567 |
const pr = races.progress();
|
| 568 |
+
net.tick(dt, { p: [st.x, st.y, st.z, st.yaw, st.pitch, st.roll].map(v => Math.round(v * 100) / 100), m: Object.keys(MODELS).indexOf(car.model), c: OPTS.paint.items[build.paint][1], v: Math.round(S.speed), a: S.air ? 1 : 0, r: pr ? pr.race : 0, rp: pr ? pr.rp : 0, rf: pr ? pr.rf : 0, w: stars, u: [st.vel.x, st.vel.y, st.vel.z].map(v => Math.round(v * 10) / 10) });
|
| 569 |
net.listen(sound.ctx, camera);
|
| 570 |
+
if ((netUiT += dt) > .15) { netUiT = 0; renderPlayers(); micLabel(); }
|
| 571 |
+
{ const lv = net.myLevel(); if (Math.abs(lv - micLv) > .03) { micLv = lv; document.getElementById("gMic").style.setProperty("--lvl", (lv * 100).toFixed(0) + "%"); } }
|
| 572 |
for (const p of net.players()) {
|
| 573 |
const at = net.sample(p); const r = remoteFor(p); r.at = at;
|
| 574 |
if (!at || at.stale) { r.root.visible = false; continue; }
|
|
|
|
| 578 |
if (r.emoT > 0 && (r.emoT -= dt) <= 0) r.emo.visible = false;
|
| 579 |
const dx = st.x - at.x, dz = st.z - at.z, d = Math.hypot(dx, dz);
|
| 580 |
net.placeVoice(p.id, at.x, at.y, at.z, Math.hypot(d, st.y - at.y));
|
| 581 |
+
{ const on = net.speaking(p.id); r.spk.visible = on; if (on) r.spk.scale.setScalar(1 + net.level(p.id) * .6); }
|
| 582 |
if (d < 3.4 && d > .01 && Math.abs(st.y - at.y) < 2) { st.x += dx / d * (3.4 - d) * .5; st.z += dz / d * (3.4 - d) * .5; st.vel.multiplyScalar(.9); }
|
| 583 |
}
|
| 584 |
} else { for (const r of remotes.values()) r.root.visible = false; net.tick(dt, null); } // the host keeps relaying from the menus
|
static/net.js
CHANGED
|
@@ -2,7 +2,8 @@
|
|
| 2 |
// A room is a well-known peer id; the first player to claim it is the host and relays everyone's car to everyone.
|
| 3 |
// If the host leaves, the others race to claim it again. Voice chat is a mesh of audio calls, off until you
|
| 4 |
// switch your mic on, and you hear each voice from where that player's car is: louder when close, left or right, ahead or behind. Chat is quick emotes only (strangers
|
| 5 |
-
// can join the public city), and names are short and filtered.
|
|
|
|
| 6 |
const PREFIX = "voidcity-awsaa-v1-";
|
| 7 |
const ICE = { iceServers: [{ urls: "stun:stun.l.google.com:19302" }, { urls: "stun:stun1.l.google.com:19302" }, { urls: "stun:global.stun.twilio.com:3478" }] };
|
| 8 |
// a TURN relay for players behind strict networks (some phone carriers, school and office wifi), where direct links fail.
|
|
@@ -19,7 +20,8 @@ export const cleanName = s => { s = String(s || "").replace(/[^\p{L}\p{N} _-]/gu
|
|
| 19 |
// what strangers may send: only these events, only sane numbers, and not too often
|
| 20 |
const EVENTS = new Set(["race", "emote"]);
|
| 21 |
const num = (v, lim) => Number.isFinite(v) && Math.abs(v) <= lim;
|
| 22 |
-
const okState = st => st && Array.isArray(st.p) && st.p.length === 6 && num(st.p[0], 8000) && num(st.p[1], 4000) && num(st.p[2], 8000) && st.p.slice(3).every(v => num(v, 50))
|
|
|
|
| 23 |
const flood = c => { const now = performance.now(); if (now - (c._t || 0) > 1000) { c._t = now; c._n = 0; } return ++c._n > 60; };
|
| 24 |
export const EMOTES = ["👋 Hi!", "🏁 Race?", "😂", "🔥 Nice!", "GG", "🚓 Cops!", "Follow me", "Wait up"];
|
| 25 |
|
|
@@ -33,8 +35,14 @@ export function createNet() {
|
|
| 33 |
let stream = null, micOn = false, calls = new Map();
|
| 34 |
// each voice: a media element (Chrome only feeds a remote WebRTC stream into Web Audio while an element plays it too, and it is
|
| 35 |
// the fallback when the game's audio isn't running) plus, once there is an audio context, a 3D panner at that player's car
|
| 36 |
-
const voices = new Map(); // id -> { el, src, pan, s }
|
| 37 |
-
let actx = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
async function lib() { if (!Peer) ({ Peer } = await import("https://cdn.jsdelivr.net/npm/peerjs@1.5.4/+esm")); return Peer; }
|
| 40 |
async function join(r, nm) {
|
|
@@ -78,14 +86,17 @@ export function createNet() {
|
|
| 78 |
function upsert(id, st) {
|
| 79 |
let p = players.get(id), fresh = !p; if (!p) { p = { id, name: "Driver", s: [] }; players.set(id, p); }
|
| 80 |
if (st.n && cleanName(st.n) !== p.name) { p.name = cleanName(st.n); fresh = true; }
|
| 81 |
-
Object.assign(p, { model: Number.isInteger(st.m) && st.m >= 0 && st.m < 20 ? st.m : 0, paint: /^#[0-9a-f]{6}$/i.test(st.c) ? st.c : "#888888", race: typeof st.r === "string" ? st.r.slice(0, 20) : 0, rp: num(st.rp, 1e7) ? st.rp : 0, rf: num(st.rf, 1e7) ? st.rf : 0, vc: !!st.vc, wanted: num(st.w, 5) ? st.w | 0 : 0 });
|
| 82 |
-
|
|
|
|
| 83 |
if (p.s.length > 12) p.s.shift();
|
| 84 |
-
if (fresh) emit("change");
|
| 85 |
}
|
| 86 |
function onData(c, d) {
|
| 87 |
if (!d || typeof d !== "object" || flood(c)) return;
|
| 88 |
if (d.t === "ev" && !EVENTS.has(d.k)) return;
|
|
|
|
|
|
|
| 89 |
if (host) {
|
| 90 |
if (d.t === "hello") { const p = players.get(c.peer) || { id: c.peer, s: [] }; p.name = cleanName(d.name); players.set(c.peer, p); emit("change"); return; }
|
| 91 |
if (d.t === "s") { if (okState(d)) upsert(c.peer, d); return; }
|
|
@@ -98,11 +109,24 @@ export function createNet() {
|
|
| 98 |
}
|
| 99 |
}
|
| 100 |
// voice: everyone with their mic on calls everyone; people without a mic still hear
|
| 101 |
-
|
| 102 |
-
function
|
| 103 |
-
calls.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
call.on("stream", s => voiceIn(call.peer, s));
|
| 105 |
-
call.on("close", () => { calls.delete(call.peer); });
|
| 106 |
}
|
| 107 |
function voiceIn(id, s) {
|
| 108 |
let v = voices.get(id); if (!v) { v = { el: new Audio() }; v.el.autoplay = true; voices.set(id, v); }
|
|
@@ -112,20 +136,20 @@ export function createNet() {
|
|
| 112 |
function wire(v) {
|
| 113 |
if (v.pan || !actx || !v.s) return;
|
| 114 |
try {
|
| 115 |
-
v.src = actx.createMediaStreamSource(v.s); v.pan = actx.createPanner();
|
| 116 |
Object.assign(v.pan, { panningModel: "HRTF", distanceModel: "linear", refDistance: 6, maxDistance: 260, rolloffFactor: 1 });
|
| 117 |
-
v.src.connect(v.pan).connect(actx.destination);
|
| 118 |
} catch { unwire(v); }
|
| 119 |
}
|
| 120 |
-
function unwire(v) { try { v.src && v.src.disconnect(); v.pan && v.pan.disconnect(); } catch { } v.src = v.pan = null; }
|
|
|
|
| 121 |
function dropAudio(id) { const v = voices.get(id); if (v) { unwire(v); v.el.srcObject = null; voices.delete(id); } const c = calls.get(id); if (c) { try { c.close(); } catch { } calls.delete(id); } }
|
|
|
|
| 122 |
const at = (node, x, y, z, set) => { if (node.positionX) { const t = actx.currentTime; node.positionX.setTargetAtTime(x, t, .03); node.positionY.setTargetAtTime(y, t, .03); node.positionZ.setTargetAtTime(z, t, .03); } else set.call(node, x, y, z); };
|
| 123 |
async function setMic(on) {
|
| 124 |
if (on && !stream) { try { stream = await navigator.mediaDevices.getUserMedia({ audio: { echoCancellation: true, noiseSuppression: true, autoGainControl: true } }); } catch { status("Microphone blocked"); emit("mic", false); return false; } }
|
| 125 |
-
micOn = on;
|
| 126 |
-
if (
|
| 127 |
-
if (on && peer) for (const p of players.values()) if (!calls.has(p.id)) { try { hook(peer.call(p.id, stream)); } catch { } }
|
| 128 |
-
if (!on && !stream) { }
|
| 129 |
emit("mic", on); return on;
|
| 130 |
}
|
| 131 |
|
|
@@ -145,15 +169,20 @@ export function createNet() {
|
|
| 145 |
// ~12 times a second: my car to the host; the host sends everyone to everyone
|
| 146 |
tick(dt, mine) {
|
| 147 |
if (!peer || !me) return;
|
| 148 |
-
lastState = mine;
|
| 149 |
-
|
| 150 |
-
|
|
|
|
|
|
|
| 151 |
else if (st && hostConn && hostConn.open) try { hostConn.send(st); } catch { }
|
| 152 |
},
|
| 153 |
-
// where a player's car should be drawn now: 120 ms behind, between two samples
|
|
|
|
| 154 |
sample(p) {
|
| 155 |
-
const
|
| 156 |
-
|
|
|
|
|
|
|
| 157 |
for (let k = 0; k < s.length - 1; k++) if (s[k].t <= t && s[k + 1].t >= t) { a = s[k]; b = s[k + 1]; break; }
|
| 158 |
const u = b.t > a.t ? Math.max(0, Math.min(1.5, (t - a.t) / (b.t - a.t))) : 1, L = (x, y) => x + (y - x) * u;
|
| 159 |
const ay = (x, y) => { let d = y - x; while (d > Math.PI) d -= 2 * Math.PI; while (d < -Math.PI) d += 2 * Math.PI; return x + d * u; };
|
|
@@ -173,9 +202,19 @@ export function createNet() {
|
|
| 173 |
const v = voices.get(id); if (!v) return;
|
| 174 |
const live = actx && actx.state === "running";
|
| 175 |
if (live) { wire(v); }
|
| 176 |
-
|
| 177 |
-
|
|
|
|
|
|
|
|
|
|
| 178 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
voiceIn, voice: id => voices.get(id),
|
| 180 |
};
|
| 181 |
}
|
|
|
|
| 2 |
// A room is a well-known peer id; the first player to claim it is the host and relays everyone's car to everyone.
|
| 3 |
// If the host leaves, the others race to claim it again. Voice chat is a mesh of audio calls, off until you
|
| 4 |
// switch your mic on, and you hear each voice from where that player's car is: louder when close, left or right, ahead or behind. Chat is quick emotes only (strangers
|
| 5 |
+
// can join the public city), and names are short and filtered. Anyone can be muted (voice and emotes), push-to-talk is
|
| 6 |
+
// a setting, a speaker icon shows who is talking, and everyone's ping to the host is shown.
|
| 7 |
const PREFIX = "voidcity-awsaa-v1-";
|
| 8 |
const ICE = { iceServers: [{ urls: "stun:stun.l.google.com:19302" }, { urls: "stun:stun1.l.google.com:19302" }, { urls: "stun:global.stun.twilio.com:3478" }] };
|
| 9 |
// a TURN relay for players behind strict networks (some phone carriers, school and office wifi), where direct links fail.
|
|
|
|
| 20 |
// what strangers may send: only these events, only sane numbers, and not too often
|
| 21 |
const EVENTS = new Set(["race", "emote"]);
|
| 22 |
const num = (v, lim) => Number.isFinite(v) && Math.abs(v) <= lim;
|
| 23 |
+
const okState = st => st && Array.isArray(st.p) && st.p.length === 6 && num(st.p[0], 8000) && num(st.p[1], 4000) && num(st.p[2], 8000) && st.p.slice(3).every(v => num(v, 50))
|
| 24 |
+
&& (st.u === undefined || (Array.isArray(st.u) && st.u.length === 3 && st.u.every(v => num(v, 500))));
|
| 25 |
const flood = c => { const now = performance.now(); if (now - (c._t || 0) > 1000) { c._t = now; c._n = 0; } return ++c._n > 60; };
|
| 26 |
export const EMOTES = ["👋 Hi!", "🏁 Race?", "😂", "🔥 Nice!", "GG", "🚓 Cops!", "Follow me", "Wait up"];
|
| 27 |
|
|
|
|
| 35 |
let stream = null, micOn = false, calls = new Map();
|
| 36 |
// each voice: a media element (Chrome only feeds a remote WebRTC stream into Web Audio while an element plays it too, and it is
|
| 37 |
// the fallback when the game's audio isn't running) plus, once there is an audio context, a 3D panner at that player's car
|
| 38 |
+
const voices = new Map(); // id -> { el, src, pan, gain, an, buf, level, loud, s }
|
| 39 |
+
let actx = null, mic = null; // mic: an analyser on my own microphone, for the level meter
|
| 40 |
+
let ptt = false, talking = false; // push-to-talk: the mic only sends while the key is held
|
| 41 |
+
const muted = new Set(), mutedNames = new Set(); // ids, and names so a muted player stays muted after reconnecting
|
| 42 |
+
let rtt = null, pingT = 0; // my round trip to the host (ms)
|
| 43 |
+
const isMuted = id => muted.has(id) || mutedNames.has((players.get(id) || {}).name);
|
| 44 |
+
const applyTracks = () => { if (stream) for (const t of stream.getAudioTracks()) t.enabled = micOn && (!ptt || talking); };
|
| 45 |
+
const rms = (an, buf) => { an.getFloatTimeDomainData(buf); let s = 0; for (let i = 0; i < buf.length; i++) s += buf[i] * buf[i]; return Math.sqrt(s / buf.length); };
|
| 46 |
|
| 47 |
async function lib() { if (!Peer) ({ Peer } = await import("https://cdn.jsdelivr.net/npm/peerjs@1.5.4/+esm")); return Peer; }
|
| 48 |
async function join(r, nm) {
|
|
|
|
| 86 |
function upsert(id, st) {
|
| 87 |
let p = players.get(id), fresh = !p; if (!p) { p = { id, name: "Driver", s: [] }; players.set(id, p); }
|
| 88 |
if (st.n && cleanName(st.n) !== p.name) { p.name = cleanName(st.n); fresh = true; }
|
| 89 |
+
Object.assign(p, { model: Number.isInteger(st.m) && st.m >= 0 && st.m < 20 ? st.m : 0, paint: /^#[0-9a-f]{6}$/i.test(st.c) ? st.c : "#888888", race: typeof st.r === "string" ? st.r.slice(0, 20) : 0, rp: num(st.rp, 1e7) ? st.rp : 0, rf: num(st.rf, 1e7) ? st.rf : 0, vc: !!st.vc, wanted: num(st.w, 5) ? st.w | 0 : 0, ping: num(st.q, 10000) ? st.q | 0 : null });
|
| 90 |
+
const u = st.u || [0, 0, 0];
|
| 91 |
+
p.s.push({ t: performance.now(), x: st.p[0], y: st.p[1], z: st.p[2], yaw: st.p[3], pitch: st.p[4], roll: st.p[5], vx: u[0], vy: u[1], vz: u[2], v: num(st.v, 2000) ? st.v : 0, air: !!st.a });
|
| 92 |
if (p.s.length > 12) p.s.shift();
|
| 93 |
+
if (fresh) { emit("change"); ring(id); } // someone new: if my mic is on, call them so they hear me
|
| 94 |
}
|
| 95 |
function onData(c, d) {
|
| 96 |
if (!d || typeof d !== "object" || flood(c)) return;
|
| 97 |
if (d.t === "ev" && !EVENTS.has(d.k)) return;
|
| 98 |
+
if (d.t === "pi") { if (host && num(d.ts, 1e12)) try { c.send({ t: "po", ts: d.ts }); } catch { } return; }
|
| 99 |
+
if (d.t === "po") { const r = performance.now() - d.ts; if (num(d.ts, 1e12) && r >= 0 && r < 10000) rtt = rtt == null ? r : rtt * .7 + r * .3; return; }
|
| 100 |
if (host) {
|
| 101 |
if (d.t === "hello") { const p = players.get(c.peer) || { id: c.peer, s: [] }; p.name = cleanName(d.name); players.set(c.peer, p); emit("change"); return; }
|
| 102 |
if (d.t === "s") { if (okState(d)) upsert(c.peer, d); return; }
|
|
|
|
| 109 |
}
|
| 110 |
}
|
| 111 |
// voice: everyone with their mic on calls everyone; people without a mic still hear
|
| 112 |
+
// one call per pair carries both voices. If both ring at once, the call started by the smaller id wins (both sides agree).
|
| 113 |
+
function answer(call) {
|
| 114 |
+
const old = calls.get(call.peer);
|
| 115 |
+
if (old && old._out && me < call.peer) { try { call.close(); } catch { } return; }
|
| 116 |
+
if (old) try { old.close(); } catch { }
|
| 117 |
+
call.answer(stream || undefined); hook(call, !!stream, false);
|
| 118 |
+
}
|
| 119 |
+
// call someone with my mic, unless a call already carries it (a call I answered without a mic is replaced)
|
| 120 |
+
function ring(id) {
|
| 121 |
+
if (!peer || !stream || !micOn || id === me) return;
|
| 122 |
+
const c = calls.get(id); if (c && c._sent) return;
|
| 123 |
+
if (c) { try { c.close(); } catch { } calls.delete(id); }
|
| 124 |
+
try { hook(peer.call(id, stream), true, true); } catch { }
|
| 125 |
+
}
|
| 126 |
+
function hook(call, sent, out) {
|
| 127 |
+
call._sent = sent; call._out = out; calls.set(call.peer, call);
|
| 128 |
call.on("stream", s => voiceIn(call.peer, s));
|
| 129 |
+
call.on("close", () => { if (calls.get(call.peer) === call) calls.delete(call.peer); });
|
| 130 |
}
|
| 131 |
function voiceIn(id, s) {
|
| 132 |
let v = voices.get(id); if (!v) { v = { el: new Audio() }; v.el.autoplay = true; voices.set(id, v); }
|
|
|
|
| 136 |
function wire(v) {
|
| 137 |
if (v.pan || !actx || !v.s) return;
|
| 138 |
try {
|
| 139 |
+
v.src = actx.createMediaStreamSource(v.s); v.pan = actx.createPanner(); v.gain = actx.createGain(); v.an = actx.createAnalyser(); v.an.fftSize = 256; v.buf = new Float32Array(256);
|
| 140 |
Object.assign(v.pan, { panningModel: "HRTF", distanceModel: "linear", refDistance: 6, maxDistance: 260, rolloffFactor: 1 });
|
| 141 |
+
v.src.connect(v.an); v.src.connect(v.pan).connect(v.gain).connect(actx.destination);
|
| 142 |
} catch { unwire(v); }
|
| 143 |
}
|
| 144 |
+
function unwire(v) { try { v.src && v.src.disconnect(); v.pan && v.pan.disconnect(); v.gain && v.gain.disconnect(); } catch { } v.src = v.pan = v.gain = v.an = null; }
|
| 145 |
+
function micMeter() { if (mic || !actx || !stream) return; try { const src = actx.createMediaStreamSource(stream), an = actx.createAnalyser(); an.fftSize = 256; src.connect(an); mic = { src, an, buf: new Float32Array(256), level: 0 }; } catch { } }
|
| 146 |
function dropAudio(id) { const v = voices.get(id); if (v) { unwire(v); v.el.srcObject = null; voices.delete(id); } const c = calls.get(id); if (c) { try { c.close(); } catch { } calls.delete(id); } }
|
| 147 |
+
const now = () => performance.now();
|
| 148 |
const at = (node, x, y, z, set) => { if (node.positionX) { const t = actx.currentTime; node.positionX.setTargetAtTime(x, t, .03); node.positionY.setTargetAtTime(y, t, .03); node.positionZ.setTargetAtTime(z, t, .03); } else set.call(node, x, y, z); };
|
| 149 |
async function setMic(on) {
|
| 150 |
if (on && !stream) { try { stream = await navigator.mediaDevices.getUserMedia({ audio: { echoCancellation: true, noiseSuppression: true, autoGainControl: true } }); } catch { status("Microphone blocked"); emit("mic", false); return false; } }
|
| 151 |
+
micOn = on; applyTracks(); micMeter();
|
| 152 |
+
if (on) for (const p of players.values()) ring(p.id);
|
|
|
|
|
|
|
| 153 |
emit("mic", on); return on;
|
| 154 |
}
|
| 155 |
|
|
|
|
| 169 |
// ~12 times a second: my car to the host; the host sends everyone to everyone
|
| 170 |
tick(dt, mine) {
|
| 171 |
if (!peer || !me) return;
|
| 172 |
+
lastState = mine;
|
| 173 |
+
if (!host && hostConn && hostConn.open && (pingT += dt) > 2) { pingT = 0; try { hostConn.send({ t: "pi", ts: performance.now() }); } catch { } }
|
| 174 |
+
sendT += dt; if (sendT < 1 / 12) return; sendT = 0;
|
| 175 |
+
const st = mine && { t: "s", n: name, ...mine, vc: micOn, q: rtt == null ? undefined : Math.round(rtt) };
|
| 176 |
+
if (host) { const all = [...(st ? [{ id: me, ...st, q: 0 }] : []), ...[...players.values()].filter(p => p.s.length).map(p => { const q = p.s[p.s.length - 1]; return { id: p.id, n: p.name, p: [q.x, q.y, q.z, q.yaw, q.pitch, q.roll], m: p.model, c: p.paint, r: p.race, rp: p.rp, rf: p.rf, vc: p.vc, v: q.v, a: q.air, w: p.wanted, u: [q.vx, q.vy, q.vz], q: p.ping ?? undefined }; })]; broadcast({ t: "w", all }); }
|
| 177 |
else if (st && hostConn && hostConn.open) try { hostConn.send(st); } catch { }
|
| 178 |
},
|
| 179 |
+
// where a player's car should be drawn now: 120 ms behind, between two samples; when packets are late it carries on
|
| 180 |
+
// along the last known velocity for up to 0.35 s (then holds) instead of freezing and jumping
|
| 181 |
sample(p) {
|
| 182 |
+
const now = performance.now(), t = now - 120, s = p.s; if (!s.length) return null;
|
| 183 |
+
const last = s[s.length - 1];
|
| 184 |
+
if (t >= last.t) { const k = Math.min(t - last.t, 350) / 1000; return { x: last.x + last.vx * k, y: last.y + last.vy * k, z: last.z + last.vz * k, yaw: last.yaw, pitch: last.pitch, roll: last.roll, v: last.v, air: last.air, stale: now - last.t > 4000 }; }
|
| 185 |
+
let a = s[0], b = last;
|
| 186 |
for (let k = 0; k < s.length - 1; k++) if (s[k].t <= t && s[k + 1].t >= t) { a = s[k]; b = s[k + 1]; break; }
|
| 187 |
const u = b.t > a.t ? Math.max(0, Math.min(1.5, (t - a.t) / (b.t - a.t))) : 1, L = (x, y) => x + (y - x) * u;
|
| 188 |
const ay = (x, y) => { let d = y - x; while (d > Math.PI) d -= 2 * Math.PI; while (d < -Math.PI) d += 2 * Math.PI; return x + d * u; };
|
|
|
|
| 202 |
const v = voices.get(id); if (!v) return;
|
| 203 |
const live = actx && actx.state === "running";
|
| 204 |
if (live) { wire(v); }
|
| 205 |
+
const off = isMuted(id);
|
| 206 |
+
if (live && v.pan) { at(v.pan, x, y + 1, z, v.pan.setPosition); v.el.volume = 0; v.gain.gain.value = off ? 0 : 1; }
|
| 207 |
+
else v.el.volume = off ? 0 : Math.max(0, Math.min(1, 1 - d / 260));
|
| 208 |
+
// the speaking icon: a smoothed level, held a moment so it doesn't flicker between words
|
| 209 |
+
if (v.an) { const r = rms(v.an, v.buf); v.level = Math.max(r, (v.level || 0) * .9); v.loud = r > .02 ? now() : v.loud || 0; }
|
| 210 |
},
|
| 211 |
+
speaking: id => { const v = voices.get(id); return !!(v && v.loud && now() - v.loud < 350 && !isMuted(id)); },
|
| 212 |
+
level: id => { const v = voices.get(id); return v ? Math.min(1, (v.level || 0) * 6) : 0; },
|
| 213 |
+
myLevel() { if (!mic) { micMeter(); return 0; } mic.level = Math.max(rms(mic.an, mic.buf), mic.level * .85); return micOn && (!ptt || talking) ? Math.min(1, mic.level * 6) : 0; },
|
| 214 |
+
setPTT(on) { ptt = !!on; applyTracks(); }, talk(down) { talking = !!down; applyTracks(); },
|
| 215 |
+
get ptt() { return ptt; }, get talking() { return talking; }, get ping() { return host ? 0 : rtt == null ? null : Math.round(rtt); },
|
| 216 |
+
mute(id, on) { const n = (players.get(id) || {}).name; if (on) { muted.add(id); if (n) mutedNames.add(n); } else { muted.delete(id); if (n) mutedNames.delete(n); } emit("change"); },
|
| 217 |
+
muted: isMuted,
|
| 218 |
voiceIn, voice: id => voices.get(id),
|
| 219 |
};
|
| 220 |
}
|
static/style.css
CHANGED
|
@@ -453,3 +453,12 @@ body.big-ui .g-hint, body.big-ui .g-job, body.big-ui .g-tut, body.big-ui .g-gps
|
|
| 453 |
/* fps counter (Settings → Show FPS) */
|
| 454 |
.fps-hud { display: none; position: absolute; left: 50%; bottom: calc(10px + var(--bot, 0px)); transform: translateX(-50%); padding: 3px 10px; border-radius: 999px; background: rgba(0,0,0,.6); font: 700 12px var(--body); color: #9dffb0; pointer-events: none; z-index: 5; }
|
| 455 |
body.show-fps .fps-hud { display: block; } .fps-hud.mid { color: #ffd23d; } .fps-hud.low { color: #ff6b6b; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 453 |
/* fps counter (Settings → Show FPS) */
|
| 454 |
.fps-hud { display: none; position: absolute; left: 50%; bottom: calc(10px + var(--bot, 0px)); transform: translateX(-50%); padding: 3px 10px; border-radius: 999px; background: rgba(0,0,0,.6); font: 700 12px var(--body); color: #9dffb0; pointer-events: none; z-index: 5; }
|
| 455 |
body.show-fps .fps-hud { display: block; } .fps-hud.mid { color: #ffd23d; } .fps-hud.low { color: #ff6b6b; }
|
| 456 |
+
|
| 457 |
+
/* online: who's talking, pings, mute buttons, the mic level */
|
| 458 |
+
.g-players .speaking { color: #3dff8a; }
|
| 459 |
+
.ping { display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin: 0 4px 1px 0; vertical-align: middle; background: #9dffb0; }
|
| 460 |
+
.ping.mid { background: #ffd23d; } .ping.bad { background: #ff6b6b; }
|
| 461 |
+
.net-list { display: grid; gap: 6px; margin: 4px 0 8px; max-height: 180px; overflow: auto; }
|
| 462 |
+
.net-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; font-size: 13px; }
|
| 463 |
+
.net-row .pill { padding: 4px 10px; font-size: 12px; }
|
| 464 |
+
#gMic.on { background: linear-gradient(90deg, #3dff8a var(--lvl, 0%), #3dfcff var(--lvl, 0%)) !important; }
|