Static site
Browse files- index.html +12 -5
- static/dealer.js +77 -0
- static/game.js +0 -0
- static/goals.js +1 -1
- static/main.js +0 -0
- static/npc.js +122 -25
- static/police.js +2 -0
- static/race.js +253 -253
- static/style.css +27 -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": {
|
|
@@ -100,7 +100,7 @@
|
|
| 100 |
<!-- Play -->
|
| 101 |
<div id="playUI" class="mode">
|
| 102 |
<div class="hud-tl">
|
| 103 |
-
<div class="g-score"><span>
|
| 104 |
<div class="g-bars"><div class="bar hp" id="gHp"><i></i><span>Health</span></div><div class="bar nos" id="gNos"><i></i><span>Nitro</span></div><div class="bar fuel" id="gFuel"><i></i><span>Fuel</span></div></div>
|
| 105 |
<div class="g-stars" id="gStars"></div>
|
| 106 |
</div>
|
|
@@ -116,6 +116,7 @@
|
|
| 116 |
<div class="g-job" id="gJob" hidden></div>
|
| 117 |
<div class="g-job g-con" id="gCon" hidden></div>
|
| 118 |
<div class="g-build" id="gBuild" hidden></div>
|
|
|
|
| 119 |
<div class="g-gps" id="gGps"></div>
|
| 120 |
<div class="g-hint" id="gHint"></div>
|
| 121 |
<div class="g-target"><i id="gArrow">⬆</i><span id="gTgt"></span></div>
|
|
@@ -125,6 +126,7 @@
|
|
| 125 |
<div class="g-count" id="gCount" hidden></div>
|
| 126 |
<div class="g-card g-raceres" id="gRaceRes" hidden></div>
|
| 127 |
<button class="pill solid g-racego" id="gRaceGo" hidden>Start race</button>
|
|
|
|
| 128 |
<div class="g-card g-panel" id="gGpsPanel" hidden></div>
|
| 129 |
<div class="g-feed" id="gFeed"></div>
|
| 130 |
<div class="g-lookhint">Click the game to look around with the mouse · Esc opens the menu</div>
|
|
@@ -143,7 +145,7 @@
|
|
| 143 |
<div class="stats-row" id="gStatsRow"></div>
|
| 144 |
<div class="menu-grid">
|
| 145 |
<section><h4>Vehicle</h4><div class="tiles">
|
| 146 |
-
<button class="tile" data-veh="car"><b>🚗</b>Car</button><button class="tile" data-veh="plane"><b>✈️</b>Plane</button><button class="tile" data-veh="heli"><b>🚁</b>Helicopter</button><button class="tile" data-veh="boat"><b>🚤</b>Boat</button><button class="tile" id="gGarage"><b>🔧</b>Garage</button></div></section>
|
| 147 |
<section><h4>Go somewhere</h4><div class="tiles">
|
| 148 |
<button class="tile" id="gGpsBtn"><b>🧭</b>GPS</button><button class="tile" id="gAuto"><b>🤖</b>Autopilot</button><button class="tile" id="gFuelNav"><b>⛽</b>Nearest fuel</button><button class="tile" id="gTaxi"><b>🚕</b>Taxi job</button><button class="tile" id="gNetBtn"><b>🌐</b>Online</button></div></section>
|
| 149 |
<section><h4>World</h4><div class="tiles">
|
|
@@ -154,14 +156,19 @@
|
|
| 154 |
<label class="set">Look speed<input type="range" id="gSens" min="0.4" max="2.5" step="0.1"></label>
|
| 155 |
<label class="set">Auto gas<span class="seg" data-set="autogas"><button data-v="off">Off</button><button data-v="on">On</button></span></label>
|
| 156 |
<label class="set">Living city (day/night + weather change)<span class="seg" data-set="cycle"><button data-v="off">Off</button><button data-v="on">On</button></span></label>
|
|
|
|
|
|
|
|
|
|
| 157 |
<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>
|
|
|
|
| 158 |
<div class="set save-row">Your progress<span><button class="pill" id="gSaveOut">Back up</button><button class="pill" id="gSaveIn">Restore</button></span></div>
|
| 159 |
<p class="small">Progress lives in this browser. Back up copies a code you can paste on another device or browser.</p>
|
| 160 |
</section>
|
|
|
|
| 161 |
<section class="wide"><h4>Contracts</h4><div class="tiles con-tiles" id="gContracts"></div></section>
|
| 162 |
<section class="wide"><h4>Routes and ghosts</h4><div class="routes" id="gRoutes"></div></section>
|
| 163 |
<section class="wide"><h4>Challenges</h4><div class="goals" id="gGoals"></div></section>
|
| 164 |
-
<section class="wide"><h4>Controls</h4><p class="small"><b>Drive:</b> W A S D or arrows · Space handbrake/drift · Shift nitro · F fly · L land · C camera · R reset · H horn · T time · M GPS · O autopilot · Enter starts a race at a 🏁 marker ·
|
| 165 |
</div>
|
| 166 |
<div class="menu-foot"><button class="pill" id="gExit">Exit to home</button></div>
|
| 167 |
</div>
|
|
@@ -208,6 +215,6 @@
|
|
| 208 |
<div id="toast" role="status"></div>
|
| 209 |
<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>
|
| 210 |
<script src="/static/hub.js" data-app="void" defer></script>
|
| 211 |
-
<script type="module" src="/static/main.js?v=
|
| 212 |
</body>
|
| 213 |
</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=1793031179">
|
| 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": {
|
|
|
|
| 100 |
<!-- Play -->
|
| 101 |
<div id="playUI" class="mode">
|
| 102 |
<div class="hud-tl">
|
| 103 |
+
<div class="g-score"><span>Cash this run</span><b id="gScore">$0</b><em id="gCombo"></em></div>
|
| 104 |
<div class="g-bars"><div class="bar hp" id="gHp"><i></i><span>Health</span></div><div class="bar nos" id="gNos"><i></i><span>Nitro</span></div><div class="bar fuel" id="gFuel"><i></i><span>Fuel</span></div></div>
|
| 105 |
<div class="g-stars" id="gStars"></div>
|
| 106 |
</div>
|
|
|
|
| 116 |
<div class="g-job" id="gJob" hidden></div>
|
| 117 |
<div class="g-job g-con" id="gCon" hidden></div>
|
| 118 |
<div class="g-build" id="gBuild" hidden></div>
|
| 119 |
+
<div class="g-tut" id="gTut" hidden></div>
|
| 120 |
<div class="g-gps" id="gGps"></div>
|
| 121 |
<div class="g-hint" id="gHint"></div>
|
| 122 |
<div class="g-target"><i id="gArrow">⬆</i><span id="gTgt"></span></div>
|
|
|
|
| 126 |
<div class="g-count" id="gCount" hidden></div>
|
| 127 |
<div class="g-card g-raceres" id="gRaceRes" hidden></div>
|
| 128 |
<button class="pill solid g-racego" id="gRaceGo" hidden>Start race</button>
|
| 129 |
+
<button class="g-footbtn" id="gFootBtn" aria-label="Get out or get in">🚶</button>
|
| 130 |
<div class="g-card g-panel" id="gGpsPanel" hidden></div>
|
| 131 |
<div class="g-feed" id="gFeed"></div>
|
| 132 |
<div class="g-lookhint">Click the game to look around with the mouse · Esc opens the menu</div>
|
|
|
|
| 145 |
<div class="stats-row" id="gStatsRow"></div>
|
| 146 |
<div class="menu-grid">
|
| 147 |
<section><h4>Vehicle</h4><div class="tiles">
|
| 148 |
+
<button class="tile" data-veh="car"><b>🚗</b>Car</button><button class="tile" data-veh="plane"><b>✈️</b>Plane</button><button class="tile" data-veh="heli"><b>🚁</b>Helicopter</button><button class="tile" data-veh="boat"><b>🚤</b>Boat</button><button class="tile" id="gGarage"><b>🔧</b>Garage</button><button class="tile" id="gDealer"><b>🏪</b>VOID Motors</button></div></section>
|
| 149 |
<section><h4>Go somewhere</h4><div class="tiles">
|
| 150 |
<button class="tile" id="gGpsBtn"><b>🧭</b>GPS</button><button class="tile" id="gAuto"><b>🤖</b>Autopilot</button><button class="tile" id="gFuelNav"><b>⛽</b>Nearest fuel</button><button class="tile" id="gTaxi"><b>🚕</b>Taxi job</button><button class="tile" id="gNetBtn"><b>🌐</b>Online</button></div></section>
|
| 151 |
<section><h4>World</h4><div class="tiles">
|
|
|
|
| 156 |
<label class="set">Look speed<input type="range" id="gSens" min="0.4" max="2.5" step="0.1"></label>
|
| 157 |
<label class="set">Auto gas<span class="seg" data-set="autogas"><button data-v="off">Off</button><button data-v="on">On</button></span></label>
|
| 158 |
<label class="set">Living city (day/night + weather change)<span class="seg" data-set="cycle"><button data-v="off">Off</button><button data-v="on">On</button></span></label>
|
| 159 |
+
<label class="set">Handling<span class="seg" data-set="handling"><button data-v="arcade">Arcade</button><button data-v="realistic">Realistic</button></span></label>
|
| 160 |
+
<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>
|
| 161 |
+
<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>
|
| 162 |
<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>
|
| 163 |
+
<div class="set save-row">Secret code<span><input id="gCode" maxlength="16" placeholder="type a code" autocomplete="off"><button class="pill" id="gCodeGo">Enter</button></span></div>
|
| 164 |
<div class="set save-row">Your progress<span><button class="pill" id="gSaveOut">Back up</button><button class="pill" id="gSaveIn">Restore</button></span></div>
|
| 165 |
<p class="small">Progress lives in this browser. Back up copies a code you can paste on another device or browser.</p>
|
| 166 |
</section>
|
| 167 |
+
<section class="wide"><h4>Today's challenges</h4><div class="goals" id="gDaily"></div></section>
|
| 168 |
<section class="wide"><h4>Contracts</h4><div class="tiles con-tiles" id="gContracts"></div></section>
|
| 169 |
<section class="wide"><h4>Routes and ghosts</h4><div class="routes" id="gRoutes"></div></section>
|
| 170 |
<section class="wide"><h4>Challenges</h4><div class="goals" id="gGoals"></div></section>
|
| 171 |
+
<section class="wide"><h4>Controls</h4><p class="small"><b>Drive:</b> W A S D or arrows · Space handbrake/drift · on a jump: A/D spin, X (Drift) backflip · Shift nitro · F fly · L land · C camera · R reset · H horn · T time · M GPS · O autopilot · Enter starts a race at a 🏁 marker · B drops a gate in the route builder. <b>On foot:</b> stop and press E to get out · WASD walk · Shift run · Space jump · E next to any car to drive it (taking traffic cars brings the police). VOID Motors in the park sells 50 cars. <b>Look:</b> click the game, then move the mouse. <b>Phone:</b> joystick on the left, pedals on the right, drag anywhere to look. <b>Fuel:</b> about 10 min driving, 5 min flying; stop at ⛽ to fill up and repair, or drive through a green 🔧.</p></section>
|
| 172 |
</div>
|
| 173 |
<div class="menu-foot"><button class="pill" id="gExit">Exit to home</button></div>
|
| 174 |
</div>
|
|
|
|
| 215 |
<div id="toast" role="status"></div>
|
| 216 |
<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>
|
| 217 |
<script src="/static/hub.js" data-app="void" defer></script>
|
| 218 |
+
<script type="module" src="/static/main.js?v=1793031179"></script>
|
| 219 |
</body>
|
| 220 |
</html>
|
static/dealer.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// VOID Motors: a dealership on the boulevard with 50 cars for sale, from city runabouts to hover cars and hypercars.
|
| 2 |
+
// Walk or drive up to the showroom (or open it from the Menu once you have been there) and buy with your cash.
|
| 3 |
+
// Bought cars are yours for good: pick one in the showroom and it is delivered next to you, wherever you are.
|
| 4 |
+
import * as THREE from "three";
|
| 5 |
+
import { GRID } from "./city.js";
|
| 6 |
+
|
| 7 |
+
const BASE = { compact: 6000, hatch: 8000, sedan: 10000, van: 9000, wagon: 11000, pickup: 14000, suv: 16000, classic: 22000, coupe: 25000, limo: 40000, muscle: 45000, sports: 60000, hover: 80000, supercar: 110000 };
|
| 8 |
+
const CLASS = { void: "VOID car (flies, 330 km/h)", compact: "City car", hatch: "Hatchback", sedan: "Sedan", van: "Van", wagon: "Estate", pickup: "Pickup", suv: "SUV", classic: "Classic", coupe: "Coupe", limo: "Limousine", muscle: "Muscle", sports: "Sports", hover: "Hover car (flies)", supercar: "Hypercar" };
|
| 9 |
+
|
| 10 |
+
export function createDealer(W, game, life, nav, sound, store, toast, wallet) {
|
| 11 |
+
const api = game.api, $ = id => document.getElementById(id);
|
| 12 |
+
// 50 for sale: the best-kept models of every class (no rusty ones), more of the fast ones
|
| 13 |
+
const per = { sports: 5, supercar: 5, hover: 5, muscle: 4, coupe: 4 };
|
| 14 |
+
const STOCK = [];
|
| 15 |
+
for (const cls of Object.keys(BASE)) {
|
| 16 |
+
const ms = life.models.filter(m => m.kind.id === cls && !m.rust).sort((a, b) => b.top - a.top);
|
| 17 |
+
for (const m of ms.slice(0, per[cls] || 3)) STOCK.push({ m, price: Math.round(BASE[cls] * (m.top / ms[ms.length - 1].top) * (.9 + m.acc * .1) / 500) * 500 });
|
| 18 |
+
}
|
| 19 |
+
STOCK.length = Math.min(50, STOCK.length);
|
| 20 |
+
// and the one everybody wants: your VOID car, the fastest thing in the city and the only car with wings
|
| 21 |
+
const HERO = { id: "void", name: "VOID Hydra", kind: { id: "void", fly: true }, cols: [0x0a0a0c], top: 92, acc: 1.7, grip: 1.25, sl: 1, sh: 1, sw: 1 };
|
| 22 |
+
STOCK.unshift({ m: HERO, price: 250000, hero: true });
|
| 23 |
+
const owned = store.get("void.owned") || {}; // id -> colour
|
| 24 |
+
if (wallet.heroOwned()) owned.void = 0x0a0a0c;
|
| 25 |
+
// the showroom: a gold column, a sign, and three cars on turntables, on a quiet crossing near the start
|
| 26 |
+
// on the lawn at the edge of the park, off the streets, so traffic never drives through it
|
| 27 |
+
const at = (() => { const L = W.city.lake || { x: 1e9, z: 1e9 }; for (let j = 12; j < 18; j++) for (let i = 2; i < 5; i++) { const x = (GRID.ax(i) + GRID.ax(i + 1)) / 2, z = (GRID.sz(j) + GRID.sz(j + 1)) / 2; if (!api.hitBox(x, z, .5, 14) && !api.isWater(x, z) && Math.hypot(x - L.x, z - L.z) > 150) return { x, z }; } return { x: GRID.ax(4) + 40, z: GRID.sz(13) }; })();
|
| 28 |
+
const scene = W.scene, root = new THREE.Group(); root.position.set(at.x, 0, at.z); scene.add(root);
|
| 29 |
+
root.add(Object.assign(new THREE.Mesh(new THREE.CylinderGeometry(7, 7, 80, 32, 1, true).translate(0, 40, 0), new THREE.MeshBasicMaterial({ color: new THREE.Color(0xffd23d).multiplyScalar(1.5), transparent: true, opacity: .1, blending: THREE.AdditiveBlending, depthWrite: false, side: THREE.DoubleSide, toneMapped: false }))));
|
| 30 |
+
const sign = W.canvasTex(512, 128, (g, w, h) => { g.fillStyle = "#0b0b0e"; g.fillRect(0, 0, w, h); g.strokeStyle = "#ffd23d"; g.lineWidth = 6; g.strokeRect(4, 4, w - 8, h - 8); g.fillStyle = "#ffd23d"; g.font = "900 58px Unbounded, Impact, sans-serif"; g.textAlign = "center"; g.fillText("VOID MOTORS", w / 2, 84); });
|
| 31 |
+
const sp = new THREE.Sprite(new THREE.SpriteMaterial({ map: sign, toneMapped: false })); sp.scale.set(18, 4.5, 1); sp.position.y = 11; root.add(sp);
|
| 32 |
+
const plats = [];
|
| 33 |
+
[[-9, 0], [0, -9], [9, 0]].forEach(([x, z], k) => {
|
| 34 |
+
const pl = new THREE.Mesh(new THREE.CylinderGeometry(3.4, 3.6, .25, 32), new THREE.MeshStandardMaterial({ color: 0x15161a, metalness: .6, roughness: .3 })); pl.position.set(x, .12, z); root.add(pl);
|
| 35 |
+
const ring = new THREE.Mesh(new THREE.TorusGeometry(3.5, .06, 6, 48).rotateX(Math.PI / 2), new THREE.MeshBasicMaterial({ color: new THREE.Color(0xffd23d).multiplyScalar(2), toneMapped: false })); ring.position.set(x, .27, z); root.add(ring);
|
| 36 |
+
const s = STOCK[[STOCK.length - 1, STOCK.length - 6, STOCK.length - 11][k]] || STOCK[k + 1], geo = life.geoOf(s.m.kind);
|
| 37 |
+
const car = new THREE.Group(); car.add(new THREE.Mesh(geo.body, new THREE.MeshStandardMaterial({ vertexColors: true, color: s.m.cols[0], roughness: .25, metalness: .6 })), new THREE.Mesh(geo.lights, new THREE.MeshBasicMaterial({ vertexColors: true, toneMapped: false, color: new THREE.Color(2, 2, 2) })));
|
| 38 |
+
car.scale.set(s.m.sl, s.m.sh, s.m.sw); car.position.set(x, .25, z); root.add(car); plats.push(car);
|
| 39 |
+
});
|
| 40 |
+
|
| 41 |
+
let open = false, sel = null, filter = "all";
|
| 42 |
+
const panel = document.createElement("div"); panel.className = "g-deal"; panel.id = "gDeal"; panel.hidden = true; document.getElementById("playUI").appendChild(panel);
|
| 43 |
+
const kmh = m => Math.round(m.top * 3.6 * .82);
|
| 44 |
+
const bar = (v, max) => `<i style="--v:${Math.min(1, v / max)}"></i>`;
|
| 45 |
+
function render() {
|
| 46 |
+
const list = STOCK.filter(s => filter === "all" || (filter === "owned" ? owned[s.m.id] : s.m.kind.id === filter));
|
| 47 |
+
const s = sel || list[0]; sel = s;
|
| 48 |
+
const cls = ["all", "owned", ...Object.keys(BASE)];
|
| 49 |
+
panel.innerHTML = `<div class="deal-head"><h2>VOID Motors</h2><span class="deal-cash">$${wallet.cash().toLocaleString()}</span><button class="pill solid" id="gDealX">Close</button></div>
|
| 50 |
+
<div class="deal-filters">${cls.map(c => `<button class="${c === filter ? "on" : ""}" data-f="${c}">${c === "all" ? `All ${STOCK.length}` : c === "owned" ? `Yours (${Object.keys(owned).length})` : CLASS[c].replace(" (flies)", "")}</button>`).join("")}</div>
|
| 51 |
+
<div class="deal-body"><div class="deal-list">${list.map(x => `<button class="deal-car${x === s ? " on" : ""}${owned[x.m.id] ? " own" : ""}" data-id="${x.m.id}"><span style="background:#${new THREE.Color(owned[x.m.id] || x.m.cols[0]).getHexString()}"></span><b>${x.m.name}</b><small>${CLASS[x.m.kind.id]} · ${kmh(x.m)} km/h</small><em>${owned[x.m.id] ? "OWNED" : "$" + x.price.toLocaleString()}</em></button>`).join("") || `<p class="small">Nothing here yet.</p>`}</div>
|
| 52 |
+
${s ? `<div class="deal-info"><h3>${s.m.name}</h3><p class="small">${CLASS[s.m.kind.id]}${s.m.kind.fly ? " · flies with F" : ""}</p>
|
| 53 |
+
<div class="deal-stats"><label>Top speed<span>${kmh(s.m)} km/h</span>${bar(s.m.top, 92)}</label><label>Acceleration${bar(s.m.acc, 1.7)}</label><label>Grip${bar(s.m.grip, 1.25)}</label></div>
|
| 54 |
+
<div class="deal-cols">${s.m.cols.map(c => `<button data-col="${c}" style="background:#${new THREE.Color(c).getHexString()}" class="${(owned[s.m.id] || s.m.cols[0]) === c ? "on" : ""}"></button>`).join("")}</div>
|
| 55 |
+
${owned[s.m.id] ? `<button class="pill solid deal-go" id="gDealDrive">Drive it</button>` : `<button class="pill solid deal-go" id="gDealBuy" ${wallet.cash() < s.price ? "disabled" : ""}>Buy for $${s.price.toLocaleString()}</button>${wallet.cash() < s.price ? `<p class="small">You need $${(s.price - wallet.cash()).toLocaleString()} more. Points from driving, races and contracts are cash.</p>` : ""}`}
|
| 56 |
+
</div>` : ""}</div>`;
|
| 57 |
+
}
|
| 58 |
+
panel.addEventListener("click", e => {
|
| 59 |
+
const t = e.target.closest("button"); if (!t) return;
|
| 60 |
+
if (t.id === "gDealX") return show(false);
|
| 61 |
+
if (t.dataset.f) { filter = t.dataset.f; sel = null; return render(); }
|
| 62 |
+
if (t.dataset.id) { sel = STOCK.find(x => x.m.id === t.dataset.id); return render(); }
|
| 63 |
+
if (t.dataset.col && sel) { if (owned[sel.m.id]) { owned[sel.m.id] = +t.dataset.col; store.set("void.owned", owned); } else sel.pick = +t.dataset.col; for (const b of panel.querySelectorAll("[data-col]")) b.classList.toggle("on", b === t); return; }
|
| 64 |
+
if (t.id === "gDealBuy" && sel && wallet.spend(sel.price)) { if (sel.hero) { wallet.unlockHero(); owned.void = 0x0a0a0c; show(false); return; } owned[sel.m.id] = sel.pick || sel.m.cols[0]; store.set("void.owned", owned); sound.win(); toast(`You bought the ${sel.m.name}!`); render(); }
|
| 65 |
+
if (t.id === "gDealDrive" && sel) { show(false); if (sel.hero) api.useHero(); else api.deliver(sel.m, owned[sel.m.id]); }
|
| 66 |
+
});
|
| 67 |
+
function show(on) { open = on; panel.hidden = !on; game.pause(on); if (on) { render(); if (document.pointerLockElement) document.exitPointerLock(); store.set("void.dealerSeen", 1); } }
|
| 68 |
+
const goBtn = document.createElement("button"); goBtn.className = "pill solid g-racego g-dealgo"; goBtn.hidden = true; goBtn.textContent = "Enter VOID Motors (Enter)"; goBtn.onclick = () => show(true); document.getElementById("playUI").appendChild(goBtn);
|
| 69 |
+
addEventListener("keydown", e => { if (e.code === "Enter" && !open && api.active() && near()) show(true); });
|
| 70 |
+
const near = () => { const p = api.state; return p && p.started && Math.hypot(p.x - at.x, p.z - at.z) < 16 && p.y < 3; };
|
| 71 |
+
return {
|
| 72 |
+
at, STOCK, owned, show, get open() { return open; },
|
| 73 |
+
// from the Menu: open the showroom if you have visited it, otherwise set the GPS there
|
| 74 |
+
menu() { if (store.get("void.dealerSeen")) show(true); else { nav.setDest({ name: "🏪 VOID Motors", x: at.x, z: at.z }); toast("GPS set to VOID Motors. Visit once and you can shop from the Menu"); } },
|
| 75 |
+
update(dt, time) { plats.forEach((c, k) => c.rotation.y = time * .4 + k * 2); goBtn.hidden = open || !near(); },
|
| 76 |
+
};
|
| 77 |
+
}
|
static/game.js
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
static/goals.js
CHANGED
|
@@ -18,7 +18,7 @@ export const GOALS = [
|
|
| 18 |
{ id: "brooklyn", icon: "🌉", name: "Bridge and tunnel", text: "Cross the bridge to Brooklyn", stat: "brooklyn", need: 1 },
|
| 19 |
];
|
| 20 |
export function createGoals(onDone) {
|
| 21 |
-
const S = Object.assign({ top: 0, smashed: 0, drift: 0, air: 0, pads: 0, butter: 0, wins: 0, escapes: 0, rings: 0, km: 0, refuels: 0, brooklyn: 0, best: 0, coins: 0, fares: 0, upg: 0 }, store.get("void.stats") || {});
|
| 22 |
const done = new Set(store.get("void.goals") || []);
|
| 23 |
let saveT = 0, lastSmash = null, lastRings = null, lastFuel = null, pads = new Set();
|
| 24 |
function check() { for (const g of GOALS) if (!done.has(g.id) && S[g.stat] >= g.need) { done.add(g.id); store.set("void.goals", [...done]); onDone(g); } }
|
|
|
|
| 18 |
{ id: "brooklyn", icon: "🌉", name: "Bridge and tunnel", text: "Cross the bridge to Brooklyn", stat: "brooklyn", need: 1 },
|
| 19 |
];
|
| 20 |
export function createGoals(onDone) {
|
| 21 |
+
const S = Object.assign({ top: 0, smashed: 0, drift: 0, air: 0, pads: 0, butter: 0, wins: 0, escapes: 0, rings: 0, km: 0, refuels: 0, brooklyn: 0, best: 0, coins: 0, fares: 0, upg: 0, steals: 0, meds: 0 }, store.get("void.stats") || {});
|
| 22 |
const done = new Set(store.get("void.goals") || []);
|
| 23 |
let saveT = 0, lastSmash = null, lastRings = null, lastFuel = null, pads = new Set();
|
| 24 |
function check() { for (const g of GOALS) if (!done.has(g.id) && S[g.stat] >= g.need) { done.add(g.id); store.set("void.goals", [...done]); onDone(g); } }
|
static/main.js
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
static/npc.js
CHANGED
|
@@ -17,14 +17,58 @@ export const KINDS = [
|
|
| 17 |
{ id: "police", L: 5.0, W: 1.95, share: 5, cols: [0xf4f6fa] },
|
| 18 |
{ id: "bus", L: 12, W: 2.6, share: 5, cols: [0x2f6fd8] },
|
| 19 |
{ id: "truck", L: 8.5, W: 2.5, share: 7, cols: [0xf2f2f0, 0xc8202a, 0x2e5aa8, 0xf7c414] },
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
const tint = (g, c) => { const n = g.attributes.position.count, a = new Float32Array(n * 3); for (let i = 0; i < n; i++) a.set(c, i * 3); g.setAttribute("color", new THREE.BufferAttribute(a, 3)); return g; };
|
| 22 |
const B = (w, h, d, x, y, z, c) => tint(new THREE.BoxGeometry(w, h, d).translate(x, y, z).toNonIndexed(), c);
|
| 23 |
const BODY = [1, 1, 1], GLASS = [.03, .035, .045], TYRE = [.03, .03, .03], DARK = [.18, .18, .19], CHROME = [.55, .55, .58];
|
| 24 |
function wheelsOf(L, W, r = .36) { const out = [], ax = L / 2 - .95; for (const x of [ax, -ax]) for (const z of [W / 2 - .2, -W / 2 + .2]) out.push(tint(new THREE.CylinderGeometry(r, r, .28, 12).rotateX(Math.PI / 2).translate(x, r, z).toNonIndexed(), TYRE)); return out; }
|
| 25 |
// side profiles extruded across the car with rounded edges: a real hood, windscreen rake, roof and boot
|
| 26 |
const V2 = (x, y) => new THREE.Vector2(x, y);
|
| 27 |
-
const ext = (pts, width, c, bevel = .07) => tint(new THREE.ExtrudeGeometry(new THREE.Shape(pts.map(([x, y]) => V2(x, y))), { depth: Math.max(.05, width - 2 * bevel), bevelEnabled: true, bevelThickness: bevel, bevelSize: bevel, bevelSegments: 2, curveSegments: 4 }).translate(0, 0, -(width - 2 * bevel) / 2)
|
| 28 |
const CHR = [.62, .63, .66], RIM = [.5, .52, .56];
|
| 29 |
function wheels4(L, W, r = .34, inset = .95) {
|
| 30 |
const out = [], ax = L / 2 - inset;
|
|
@@ -35,11 +79,29 @@ function wheels4(L, W, r = .34, inset = .95) {
|
|
| 35 |
return out;
|
| 36 |
}
|
| 37 |
const PROFILES = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
// [body outline], [glass outline], [roof outline] (x from the back, metres; y up)
|
| 39 |
sedan: (L) => [[[-L / 2, .3], [L / 2, .3], [L / 2, .62], [L / 2 - .32, .78], [L / 2 - 1.25, .88], [-L / 2 + .95, .92], [-L / 2 + .15, .86], [-L / 2, .7]],
|
| 40 |
[[L / 2 - 1.3, .87], [L / 2 - 2.05, 1.38], [-L / 2 + 1.55, 1.4], [-L / 2 + .9, .9]], [[L / 2 - 2.0, 1.36], [-L / 2 + 1.6, 1.38], [-L / 2 + 1.62, 1.45], [L / 2 - 2.02, 1.43]]],
|
| 41 |
suv: (L) => [[[-L / 2, .38], [L / 2, .38], [L / 2, .82], [L / 2 - .3, 1.0], [L / 2 - 1.15, 1.08], [-L / 2 + .1, 1.12], [-L / 2, .95]],
|
| 42 |
[[L / 2 - 1.2, 1.07], [L / 2 - 1.75, 1.78], [-L / 2 + .25, 1.8], [-L / 2 + .12, 1.1]], [[L / 2 - 1.72, 1.76], [-L / 2 + .2, 1.78], [-L / 2 + .2, 1.86], [L / 2 - 1.75, 1.84]]],
|
|
|
|
|
|
|
| 43 |
sports: (L) => [[[-L / 2, .26], [L / 2, .26], [L / 2, .45], [L / 2 - .5, .66], [L / 2 - 1.5, .76], [-L / 2 + .7, .84], [-L / 2, .74]],
|
| 44 |
[[L / 2 - 1.55, .75], [L / 2 - 2.2, 1.12], [-L / 2 + 1.5, 1.12], [-L / 2 + .75, .83]], [[L / 2 - 2.15, 1.1], [-L / 2 + 1.55, 1.1], [-L / 2 + 1.55, 1.16], [L / 2 - 2.15, 1.16]]],
|
| 45 |
van: (L) => [[[-L / 2, .35], [L / 2, .35], [L / 2, .95], [L / 2 - .5, 1.25], [-L / 2, 1.25]],
|
|
@@ -69,16 +131,21 @@ export function vehicle(k) {
|
|
| 69 |
p.push(ext(body, W, BODY, .1), ext(glass, W * .9, GLASS, .06), ext(roof, W * .86, BODY, .05));
|
| 70 |
p.push(B(.2, .22, W * .96, L / 2 - .02, .42, 0, DARK), B(.2, .22, W * .96, -L / 2 + .02, .42, 0, DARK)); // bumpers
|
| 71 |
p.push(B(.03, .14, W * .5, L / 2 + .02, .58, 0, [.12, .12, .13])); // grille
|
| 72 |
-
const wr = kind === "suv" || kind === "pickup" ? .4 : kind === "sports" ? .33 : .34;
|
| 73 |
-
p.push(...
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
if (k.id === "taxi") { p.push(B(.55, .26, .9, -.1, 1.6, 0, BODY)); lt.push(B(.57, .16, .92, -.1, 1.62, 0, [1, .92, .6])); for (const s of [1, -1]) p.push(B(L * .7, .1, .02, 0, .66, s * (W / 2 + .01), [.03, .03, .03])); }
|
| 76 |
if (k.id === "police") { for (const s of [1, -1]) p.push(B(L * .8, .24, .02, 0, .65, s * (W / 2 + .01), [.12, .25, .75])); lt.push(B(.3, .14, .5, -.15, 1.52, .32, [.2, .35, 1]), B(.3, .14, .5, -.15, 1.52, -.32, [1, .15, .15])); }
|
| 77 |
-
lamps(kind === "sports" ? .5 : kind === "suv" || kind === "pickup" ? .85 : kind === "van" ? .85 : .66);
|
| 78 |
return { body: mergeGeometries(p), lights: mergeGeometries(lt) };
|
| 79 |
}
|
| 80 |
// people: torso and arms (shirt colour), each leg on its own so they can swing, a head and hair
|
| 81 |
-
function personGeo() {
|
| 82 |
const s = [1, 1, 1];
|
| 83 |
return {
|
| 84 |
body: mergeGeometries([B(.44, .6, .26, 0, 1.2, 0, s), B(.4, .12, .24, 0, .9, 0, [.25, .25, .28]), B(.11, .56, .12, 0, 1.16, .29, s), B(.11, .56, .12, 0, 1.16, -.29, s),
|
|
@@ -103,10 +170,18 @@ export function createLife(scene, roads, walkways, COARSE) {
|
|
| 103 |
|
| 104 |
// ---- vehicles ----
|
| 105 |
const carMat = new THREE.MeshStandardMaterial({ vertexColors: true, roughness: .32, metalness: .55, envMapIntensity: 1.1 });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
const lightMat = new THREE.MeshBasicMaterial({ vertexColors: true, toneMapped: false, color: new THREE.Color(2.2, 2.2, 2.2) });
|
| 107 |
const total = KINDS.reduce((s, k) => s + k.share, 0), counts = KINDS.map(k => Math.round(N * k.share / total));
|
| 108 |
const meshes = KINDS.map((k, ti) => {
|
| 109 |
-
const g = vehicle(k), body = new THREE.InstancedMesh(g.body, carMat, counts[ti]), lights = new THREE.InstancedMesh(g.lights, lightMat, counts[ti]);
|
|
|
|
| 110 |
body.castShadow = true; body.frustumCulled = lights.frustumCulled = false; scene.add(body, lights); return { body, lights };
|
| 111 |
});
|
| 112 |
const cars = [], col = new THREE.Color();
|
|
@@ -116,15 +191,16 @@ export function createLife(scene, roads, walkways, COARSE) {
|
|
| 116 |
if (!segOK(axis, road, k)) continue;
|
| 117 |
const a = cross(axis, k) + crossW(axis, k) / 2 + 6, b = cross(axis, k + 1) - crossW(axis, k + 1) / 2 - 6;
|
| 118 |
const lo = laneOffs(axis, road);
|
| 119 |
-
Object.assign(c, { axis, road, dir: rnd() < .5 ? 1 : -1, lane: lo[Math.floor(rnd() * lo.length)], s: a + rnd() * (b - a), turn: null, lastK: -99, wreck: null, gone: 0 });
|
| 120 |
c.v = c.want * .6; return;
|
| 121 |
}
|
| 122 |
}
|
| 123 |
KINDS.forEach((k, ti) => {
|
| 124 |
for (let n = 0; n < counts[ti]; n++) {
|
| 125 |
-
const
|
|
|
|
| 126 |
place(c); cars.push(c);
|
| 127 |
-
c.color =
|
| 128 |
}
|
| 129 |
meshes[ti].body.setColorAt(0, col.setHex(0xffffff));
|
| 130 |
});
|
|
@@ -209,7 +285,11 @@ export function createLife(scene, roads, walkways, COARSE) {
|
|
| 209 |
const api = { detail: 1 };
|
| 210 |
return {
|
| 211 |
get detail() { return api.detail; }, set detail(v) { api.detail = v; },
|
| 212 |
-
cars, meshes, smash, near,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
honk(x, z) { for (const p of people) if (Math.abs(p.x - x) < 25 && Math.abs(p.z - z) < 25) p.scare = 2; },
|
| 214 |
// cars near a point (for the player's collision tests)
|
| 215 |
nearCars(x, z, r) { const out = []; for (let cx = Math.floor((x - r) / CELL); cx <= Math.floor((x + r) / CELL); cx++) for (let cz = Math.floor((z - r) / CELL); cz <= Math.floor((z + r) / CELL); cz++) for (const c of cell.get(cx * 7919 + cz) || []) out.push(c); return out; },
|
|
@@ -221,13 +301,14 @@ export function createLife(scene, roads, walkways, COARSE) {
|
|
| 221 |
const D = api.detail || 1, RC = Math.min(900, ((COARSE ? 260 : 380) + alt * 2) * D), RC2 = RC * RC, RP = Math.min(260, ((COARSE ? 90 : 140) + alt * .4) * D), RP2 = RP * RP;
|
| 222 |
for (const m of meshes) m.k = 0;
|
| 223 |
if (relead) for (const c of cars) {
|
| 224 |
-
if (c.wreck || c.turn) continue;
|
| 225 |
const key = c.axis * 100000 + c.road * 1000 + (c.dir > 0 ? 500 : 0) + Math.round(c.lane * 10);
|
| 226 |
let b = buckets.get(key); if (!b) buckets.set(key, b = []); b.push(c);
|
| 227 |
}
|
| 228 |
if (relead) for (const b of buckets.values()) if (b.length) { b.sort((a, d) => a.s * a.dir - d.s * d.dir); for (let k = 0; k < b.length; k++) b[k].lead = b[k + 1] || null; }
|
| 229 |
for (const c of cars) {
|
| 230 |
const far = (Math.abs(c.x - px) > 450 || Math.abs(c.z - pz) > 450) && (Math.abs(c.x - cx0) > RC || Math.abs(c.z - cz0) > RC);
|
|
|
|
| 231 |
const vfar = far && (Math.abs(c.x - px) > 1200 || Math.abs(c.z - pz) > 1200), every = vfar ? 15 : 6; // far away nobody sees them: move them in big, rare steps
|
| 232 |
if (far && (frameN + c.n) % every) { continue; }
|
| 233 |
const h = far ? dt * every : dt;
|
|
@@ -237,6 +318,8 @@ export function createLife(scene, roads, walkways, COARSE) {
|
|
| 237 |
c.x += w.vx * h; c.z += w.vz * h; const f = Math.pow(c.y > 0 ? .9 : .2, h); w.vx *= f; w.vz *= f;
|
| 238 |
if (c.y === 0) { c.rx = Math.round(c.rx / Math.PI) * Math.PI * .02 + c.rx * .98; c.rz = Math.round(c.rz / Math.PI) * Math.PI * .02 + c.rz * .98; }
|
| 239 |
if (w.t > 24) { place(c); posOf(c); c.rx = c.rz = c.y = 0; c.col.setHex(c.color); c.crush = 0; c.dent = 0; }
|
|
|
|
|
|
|
| 240 |
} else if (c.turn) {
|
| 241 |
const T = c.turn; T.t += c.v * h / T.len; const t = Math.min(1, T.t), a = 1 - t;
|
| 242 |
const nx = a * a * T.p0.x + 2 * a * t * T.p1.x + t * t * T.p2.x, nz = a * a * T.p0.z + 2 * a * t * T.p1.z + t * t * T.p2.z;
|
|
@@ -251,13 +334,14 @@ export function createLife(scene, roads, walkways, COARSE) {
|
|
| 251 |
if (k >= 0 && k < lim) {
|
| 252 |
const cc = cross(c.axis, k), stop = cc - c.dir * (crossW(c.axis, k) / 2 + 2.5), dist = (stop - c.s) * c.dir;
|
| 253 |
const ph = c.axis === 0 ? phase(k, c.road, time) : phase(c.road, k, time), green = c.axis === 0 ? ph === 0 : ph === 2, amber = c.axis === 0 ? ph === 1 : ph === 3;
|
| 254 |
-
if (!green && dist > (amber ? 10 : -.5) && dist < 40) want = Math.min(want, Math.sqrt(Math.max(0, 2 * 4.5 * (dist - .5))));
|
| 255 |
if (dist < 0 && c.lastK !== k) { c.lastK = k; decide(c, k); }
|
| 256 |
} else if ((c.dir > 0 && c.s > cross(c.axis, lim - 1) - 12) || (c.dir < 0 && c.s < cross(c.axis, 0) + 12)) c.dir *= -1;
|
| 257 |
// keep a gap to the car in front
|
| 258 |
if (c.lead) { const gap = (c.lead.s - c.s) * c.dir - (c.kind.L + c.lead.kind.L) / 2; want = Math.min(want, gap < 2.5 ? 0 : Math.max(0, (gap - 2.5) * 1.1), c.lead.v + gap * .4); }
|
| 259 |
// and to the player
|
| 260 |
-
if (
|
|
|
|
| 261 |
if (c.stop > 0) { c.stop -= h; want = 0; } // dented: it stops a while before driving on
|
| 262 |
c.v += Math.max(-9 * h, Math.min(3 * h, want - c.v));
|
| 263 |
if (!c.turn) { c.s += c.v * c.dir * h; posOf(c); }
|
|
@@ -265,10 +349,10 @@ export function createLife(scene, roads, walkways, COARSE) {
|
|
| 265 |
if (!far) { const key = Math.floor(c.x / CELL) * 7919 + Math.floor(c.z / CELL); let b = cell.get(key); if (!b) cell.set(key, b = []); b.push(c); }
|
| 266 |
const ddx = c.x - cx0, ddz = c.z - cz0;
|
| 267 |
if (ddx * ddx + ddz * ddz < RC2) {
|
| 268 |
-
e.set(c.rx, -c.yaw, c.rz + (c.turn ? .05 : 0), "YXZ"); q.setFromEuler(e); m4.compose(pos.set(c.x, c.y, c.z), q, squash.set(
|
| 269 |
-
const M = meshes[c.ti], k = M.k++; M.body.setMatrixAt(k, m4); M.body.setColorAt(k, c.col); M.lights.setMatrixAt(k, c.wreck ? m4.compose(pos, q, zero) : m4); }
|
| 270 |
}
|
| 271 |
-
for (const m of meshes) { m.body.count = m.lights.count = m.k; m.body.instanceMatrix.needsUpdate = m.lights.instanceMatrix.needsUpdate = true; if (m.body.instanceColor) m.body.instanceColor.needsUpdate = true; }
|
| 272 |
// debris (skipped once every piece has landed and been hidden)
|
| 273 |
if (debLive) { debLive = 0; for (let k = 0; k < DEB; k++) {
|
| 274 |
const d = deb[k];
|
|
@@ -288,27 +372,40 @@ export function createLife(scene, roads, walkways, COARSE) {
|
|
| 288 |
around(p, p.d, tmp);
|
| 289 |
let x = tmp.x, z = tmp.z;
|
| 290 |
if (player && player.y < 4 && !(p.down > 0)) { // a car coming: leap out of the way, or get knocked over if it is too quick
|
| 291 |
-
const dx = x + p.push.x - player.x, dz = z + p.push.y - player.z, d = Math.hypot(dx, dz) || .01, r = 3 + Math.min(3, (player.speed || 0) * .04);
|
| 292 |
-
if (d < 2.2 && (player.speed || 0) > 6
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
else if (d < r) { const k = (r - d) * Math.min(1, h * 10); p.push.x += dx / d * k; p.push.y += dz / d * k; p.scare = 2; }
|
| 294 |
}
|
| 295 |
if (p.down > 0) {
|
| 296 |
const f = p.fall; p.down -= h;
|
| 297 |
-
if (f.
|
| 298 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
}
|
| 300 |
if (p.scare > 0) p.scare -= h;
|
| 301 |
-
p.push.multiplyScalar(1 - Math.min(1, h * .25));
|
| 302 |
x += p.push.x; z += p.push.y; p.x = x; p.z = z;
|
| 303 |
const walk = time * 7 * p.v * (p.scare > 0 ? 2 : 1) + p.ph, bob = p.wait > 0 ? 0 : Math.abs(Math.sin(walk)) * .05;
|
| 304 |
const ex = x - cx0, ez = z - cz0;
|
| 305 |
if (ex * ex + ez * ez < RP2) {
|
| 306 |
const yaw = -(tmp.yaw + (p.dir < 0 ? Math.PI : 0));
|
| 307 |
let lie = 0, y = bob + .06;
|
| 308 |
-
|
| 309 |
-
|
|
|
|
| 310 |
bodies.setMatrixAt(pk, m4); heads.setMatrixAt(pk, m4); hair.setMatrixAt(pk, m4);
|
| 311 |
-
const swing = p.
|
| 312 |
LEGS[0][0] = legL; LEGS[1][0] = legR; for (const [leg, sgn, off] of LEGS) { legQ.setFromAxisAngle(Z, swing * sgn); legM.compose(pos.set(0, .86, off), legQ, one); leg.setMatrixAt(pk, legT.copy(m4).multiply(legM)); }
|
| 313 |
bodies.setColorAt(pk, p.shirt); heads.setColorAt(pk, p.skin); legL.setColorAt(pk, p.pants); legR.setColorAt(pk, p.pants); hair.setColorAt(pk, p.hairC); pk++;
|
| 314 |
}
|
|
|
|
| 17 |
{ id: "police", L: 5.0, W: 1.95, share: 5, cols: [0xf4f6fa] },
|
| 18 |
{ id: "bus", L: 12, W: 2.6, share: 5, cols: [0x2f6fd8] },
|
| 19 |
{ id: "truck", L: 8.5, W: 2.5, share: 7, cols: [0xf2f2f0, 0xc8202a, 0x2e5aa8, 0xf7c414] },
|
| 20 |
+
{ id: "compact", L: 3.8, W: 1.72, share: 8, cols: [0xe8e6e0, 0x9fd3e8, 0xf2a0b4, 0x2b2d32, 0xc0d84a, 0xd84a3a] },
|
| 21 |
+
{ id: "hatch", L: 4.2, W: 1.8, share: 8, cols: [0xf2f2f0, 0x3a6ea5, 0x8a8f96, 0xb5261e, 0x1b1c20] },
|
| 22 |
+
{ id: "wagon", L: 4.9, W: 1.85, share: 4, cols: [0x6b5a44, 0x2f4a3a, 0xc8ccd2, 0x24364f] },
|
| 23 |
+
{ id: "coupe", L: 4.5, W: 1.85, share: 5, cols: [0xd8cfb8, 0x1b1c20, 0x6b1a1a, 0x123a8c, 0xf2f2f0] },
|
| 24 |
+
{ id: "muscle", L: 4.9, W: 1.95, share: 4, cols: [0xff6a00, 0x111111, 0x2d6a1e, 0xd21f2b, 0xf7c414] },
|
| 25 |
+
{ id: "supercar", L: 4.6, W: 2.0, share: 3, cols: [0xd21f2b, 0x8acb1a, 0xf7c414, 0x9b5cff, 0xf2f2f0, 0x111111] },
|
| 26 |
+
{ id: "classic", L: 5.0, W: 1.9, share: 4, cols: [0x7fb5a8, 0xe8d3a8, 0x8a2a2a, 0x3a4a6a, 0xd9c27a] },
|
| 27 |
+
{ id: "limo", L: 6.8, W: 1.95, share: 2, cols: [0x0a0a0c, 0xf2f2f0] },
|
| 28 |
+
{ id: "hover", L: 4.6, W: 2.0, share: 5, fly: true, cols: [0xe9e9ea, 0x3df5ff, 0x9b5cff, 0x111114, 0xff4fd8] },
|
| 29 |
];
|
| 30 |
+
// the catalogue: 200 named models. Each is a body type with its own proportions, paint range, condition and handling.
|
| 31 |
+
const BRANDS = ["Aurex", "Kova", "Bellmont", "Zentri", "Harlow", "Vanta", "Ostrel", "Mirage", "Dunmore", "Caldera", "Nexo", "Ryden", "Solace", "Torvik"];
|
| 32 |
+
const WORDS = {
|
| 33 |
+
sedan: ["Meridian", "Civic Line", "Corsa", "Accordo", "Regent", "Avenue", "Sable", "Statesman", "Primus", "Lumen", "Arbor", "Concord", "Vista", "Monarch"],
|
| 34 |
+
suv: ["Ridge", "Summit", "Outback", "Tundra X", "Yukon", "Atlas", "Granite", "Sierra", "Trailhawk", "Bison", "Glacier", "Canyon", "Forge", "Basecamp"],
|
| 35 |
+
van: ["Courier", "Transit", "Hauler", "Shuttle", "Cargo", "Nomad", "Porter", "Caravan", "Express", "Metro Van", "Workhorse", "Voyager", "Camper", "Sprinter"],
|
| 36 |
+
pickup: ["Ranch", "Stallion", "Rambler", "Boulder", "Trapper", "Longhorn", "Dune", "Hauler HD", "Maverick", "Gravel", "Wrangle", "Prairie", "Ironside", "Coyote"],
|
| 37 |
+
sports: ["GT", "Spyder", "Velo", "Apex", "Strada", "Corsa R", "Viper", "Zephyr", "Kestrel", "Arrow", "Nitro", "Rally S", "Blitz", "Comet"],
|
| 38 |
+
compact: ["Pip", "Mini", "Spark", "Dot", "Pebble", "City", "Zip", "Bean", "Cricket", "Hop", "Button", "Clover", "Juno", "Mote"],
|
| 39 |
+
hatch: ["Swift", "Polo", "Golfo", "Fiesta", "Rio", "Note", "Jazz", "Spritz", "Fox", "Ion", "Quill", "Pulse", "Echo", "Brisk"],
|
| 40 |
+
wagon: ["Estate", "Touring", "Allroad", "Avant", "Family", "Country", "Homestead", "Liner", "Grand Tour", "Ranger", "Woodie", "Voyage", "Harbor", "Lodge"],
|
| 41 |
+
coupe: ["Duo", "Coupe S", "Sterling", "Velvet", "Riviera", "Monaco", "Elan", "Crest", "Cadence", "Noble", "Saber", "Portofino", "Ember", "Vale"],
|
| 42 |
+
muscle: ["Charger", "Hellhound", "Thunder", "Mustang", "Bandit", "Torque", "Outlaw", "Brute", "Hammer", "Fury", "Rumble", "Big Block", "Thrasher", "Diablo"],
|
| 43 |
+
supercar: ["Hyperion", "Vortex", "Specter", "Zenith", "Aero 9", "Venom", "Phantom", "Inferno", "Halo", "Eclipse", "Raptor", "Nova X", "Mirage RS", "Oblivion"],
|
| 44 |
+
classic: ["Bel Air", "Starliner", "Fleetwood", "Coronet", "Ambassador", "Imperial", "Fairlane", "Biscayne", "Galaxie", "Roadmaster", "Skylark", "Deville", "Nomad 57", "Tudor"],
|
| 45 |
+
limo: ["Presidential", "Stretch", "Royale", "Phantom L", "Gala", "Majesty", "Diplomat", "Embassy", "Premier", "Grandeur", "Sovereign", "Ballroom", "Chairman", "Crown"],
|
| 46 |
+
hover: ["Skylane", "Aerodyne", "Hoverline", "Levitas", "Zephyrus", "Glide", "Nimbus", "Halcyon", "Strato", "Drift-X", "Airwave", "Cirrus", "Orbit", "Lumina"],
|
| 47 |
+
};
|
| 48 |
+
const PERF = { sedan: [52, 1, 1], suv: [48, .9, .9], van: [42, .8, .85], pickup: [47, .9, .9], sports: [80, 1.35, 1.15], compact: [44, .85, 1.05], hatch: [50, .95, 1.05], wagon: [50, .95, 1], coupe: [62, 1.1, 1.05], muscle: [74, 1.4, .85], supercar: [92, 1.6, 1.2], classic: [46, .85, .8], limo: [48, .8, .8], hover: [70, 1.2, 1.1], taxi: [52, 1, 1], police: [70, 1.25, 1.05], bus: [30, .55, .7], truck: [34, .6, .7] };
|
| 49 |
+
export const CAR_MODELS = (() => {
|
| 50 |
+
let sd = 4242; const rr = () => (sd = (sd * 16807) % 2147483647) / 2147483647;
|
| 51 |
+
const out = [];
|
| 52 |
+
for (const k of KINDS) {
|
| 53 |
+
const words = WORDS[k.id], perf = PERF[k.id];
|
| 54 |
+
if (!words) { out.push({ id: k.id, name: { taxi: "VOID Cab", police: "Interceptor", bus: "City Bus", truck: "Box Truck" }[k.id], kind: k, sl: 1, sh: 1, sw: 1, cols: k.cols, rust: 0, top: perf[0], acc: perf[1], grip: perf[2] }); continue; }
|
| 55 |
+
words.forEach((w, i) => {
|
| 56 |
+
const beater = k.id !== "hover" && k.id !== "supercar" && (i % 5 === 3 || k.id === "classic" && i % 3 === 0);
|
| 57 |
+
const brand = BRANDS[(i * 5 + k.id.length) % BRANDS.length];
|
| 58 |
+
out.push({ id: `${k.id}-${i}`, name: `${brand} ${w}${beater ? " (rusty)" : ""}`, kind: k, sl: .9 + rr() * .2, sh: .93 + rr() * .14, sw: .95 + rr() * .1,
|
| 59 |
+
cols: beater ? [0x6b5a44, 0x7a3a2a, 0x5a6a5a, 0x8a7a5a] : k.cols.slice(0, 2 + Math.floor(rr() * (k.cols.length - 1))), rust: beater ? .55 + rr() * .45 : rr() < .15 ? rr() * .3 : 0,
|
| 60 |
+
top: perf[0] * (beater ? .85 : .92 + rr() * .16), acc: perf[1] * (beater ? .85 : .95 + rr() * .1), grip: perf[2] * (beater ? .85 : 1) });
|
| 61 |
+
});
|
| 62 |
+
}
|
| 63 |
+
return out;
|
| 64 |
+
})();
|
| 65 |
const tint = (g, c) => { const n = g.attributes.position.count, a = new Float32Array(n * 3); for (let i = 0; i < n; i++) a.set(c, i * 3); g.setAttribute("color", new THREE.BufferAttribute(a, 3)); return g; };
|
| 66 |
const B = (w, h, d, x, y, z, c) => tint(new THREE.BoxGeometry(w, h, d).translate(x, y, z).toNonIndexed(), c);
|
| 67 |
const BODY = [1, 1, 1], GLASS = [.03, .035, .045], TYRE = [.03, .03, .03], DARK = [.18, .18, .19], CHROME = [.55, .55, .58];
|
| 68 |
function wheelsOf(L, W, r = .36) { const out = [], ax = L / 2 - .95; for (const x of [ax, -ax]) for (const z of [W / 2 - .2, -W / 2 + .2]) out.push(tint(new THREE.CylinderGeometry(r, r, .28, 12).rotateX(Math.PI / 2).translate(x, r, z).toNonIndexed(), TYRE)); return out; }
|
| 69 |
// side profiles extruded across the car with rounded edges: a real hood, windscreen rake, roof and boot
|
| 70 |
const V2 = (x, y) => new THREE.Vector2(x, y);
|
| 71 |
+
const ext = (pts, width, c, bevel = .07) => tint(new THREE.ExtrudeGeometry(new THREE.Shape(pts.map(([x, y]) => V2(x, y))), { depth: Math.max(.05, width - 2 * bevel), bevelEnabled: true, bevelThickness: bevel, bevelSize: bevel, bevelSegments: 2, curveSegments: 4 }).translate(0, 0, -(width - 2 * bevel) / 2), c);
|
| 72 |
const CHR = [.62, .63, .66], RIM = [.5, .52, .56];
|
| 73 |
function wheels4(L, W, r = .34, inset = .95) {
|
| 74 |
const out = [], ax = L / 2 - inset;
|
|
|
|
| 79 |
return out;
|
| 80 |
}
|
| 81 |
const PROFILES = {
|
| 82 |
+
compact: (L) => [[[-L / 2, .3], [L / 2, .3], [L / 2, .66], [L / 2 - .3, .82], [L / 2 - .8, .9], [-L / 2 + .05, .95], [-L / 2, .8]],
|
| 83 |
+
[[L / 2 - .85, .89], [L / 2 - 1.45, 1.42], [-L / 2 + .25, 1.45], [-L / 2 + .1, .94]], [[L / 2 - 1.4, 1.4], [-L / 2 + .25, 1.43], [-L / 2 + .25, 1.5], [L / 2 - 1.42, 1.48]]],
|
| 84 |
+
hatch: (L) => [[[-L / 2, .3], [L / 2, .3], [L / 2, .62], [L / 2 - .3, .78], [L / 2 - 1.0, .88], [-L / 2 + .1, .94], [-L / 2, .78]],
|
| 85 |
+
[[L / 2 - 1.05, .87], [L / 2 - 1.75, 1.38], [-L / 2 + .45, 1.4], [-L / 2 + .12, .95]], [[L / 2 - 1.7, 1.36], [-L / 2 + .45, 1.38], [-L / 2 + .42, 1.45], [L / 2 - 1.72, 1.43]]],
|
| 86 |
+
wagon: (L) => [[[-L / 2, .32], [L / 2, .32], [L / 2, .64], [L / 2 - .32, .8], [L / 2 - 1.2, .9], [-L / 2 + .1, .94], [-L / 2, .8]],
|
| 87 |
+
[[L / 2 - 1.25, .89], [L / 2 - 1.95, 1.4], [-L / 2 + .2, 1.42], [-L / 2 + .12, .95]], [[L / 2 - 1.9, 1.38], [-L / 2 + .18, 1.4], [-L / 2 + .18, 1.47], [L / 2 - 1.92, 1.45]]],
|
| 88 |
+
coupe: (L) => [[[-L / 2, .28], [L / 2, .28], [L / 2, .55], [L / 2 - .4, .72], [L / 2 - 1.4, .82], [-L / 2 + .8, .88], [-L / 2, .74]],
|
| 89 |
+
[[L / 2 - 1.45, .81], [L / 2 - 2.15, 1.25], [-L / 2 + 1.3, 1.26], [-L / 2 + .6, .87]], [[L / 2 - 2.1, 1.23], [-L / 2 + 1.35, 1.24], [-L / 2 + 1.35, 1.3], [L / 2 - 2.1, 1.3]]],
|
| 90 |
+
muscle: (L) => [[[-L / 2, .3], [L / 2, .3], [L / 2, .7], [L / 2 - .2, .84], [L / 2 - 1.8, .92], [-L / 2 + .9, .95], [-L / 2 + .1, .92], [-L / 2, .78]],
|
| 91 |
+
[[L / 2 - 1.85, .91], [L / 2 - 2.35, 1.3], [-L / 2 + 1.5, 1.32], [-L / 2 + .85, .94]], [[L / 2 - 2.3, 1.28], [-L / 2 + 1.55, 1.3], [-L / 2 + 1.55, 1.36], [L / 2 - 2.3, 1.36]]],
|
| 92 |
+
supercar: (L) => [[[-L / 2, .22], [L / 2, .22], [L / 2, .38], [L / 2 - .9, .6], [L / 2 - 1.6, .7], [-L / 2 + .5, .82], [-L / 2, .7]],
|
| 93 |
+
[[L / 2 - 1.5, .68], [L / 2 - 2.3, 1.05], [-L / 2 + 1.7, 1.04], [-L / 2 + .9, .8]], [[L / 2 - 2.25, 1.03], [-L / 2 + 1.75, 1.03], [-L / 2 + 1.75, 1.08], [L / 2 - 2.25, 1.08]]],
|
| 94 |
+
classic: (L) => [[[-L / 2, .34], [L / 2, .34], [L / 2, .74], [L / 2 - .25, .86], [L / 2 - 1.5, .92], [-L / 2 + 1.1, .94], [-L / 2 + .1, .9], [-L / 2, .8]],
|
| 95 |
+
[[L / 2 - 1.55, .91], [L / 2 - 1.95, 1.4], [-L / 2 + 1.7, 1.42], [-L / 2 + 1.25, .93]], [[L / 2 - 1.9, 1.38], [-L / 2 + 1.75, 1.4], [-L / 2 + 1.75, 1.48], [L / 2 - 1.9, 1.46]]],
|
| 96 |
+
limo: (L) => [[[-L / 2, .3], [L / 2, .3], [L / 2, .62], [L / 2 - .32, .78], [L / 2 - 1.25, .88], [-L / 2 + .95, .92], [-L / 2 + .15, .86], [-L / 2, .7]],
|
| 97 |
+
[[L / 2 - 1.3, .87], [L / 2 - 2.05, 1.38], [-L / 2 + 1.55, 1.4], [-L / 2 + .9, .9]], [[L / 2 - 2.0, 1.36], [-L / 2 + 1.6, 1.38], [-L / 2 + 1.62, 1.45], [L / 2 - 2.02, 1.43]]],
|
| 98 |
// [body outline], [glass outline], [roof outline] (x from the back, metres; y up)
|
| 99 |
sedan: (L) => [[[-L / 2, .3], [L / 2, .3], [L / 2, .62], [L / 2 - .32, .78], [L / 2 - 1.25, .88], [-L / 2 + .95, .92], [-L / 2 + .15, .86], [-L / 2, .7]],
|
| 100 |
[[L / 2 - 1.3, .87], [L / 2 - 2.05, 1.38], [-L / 2 + 1.55, 1.4], [-L / 2 + .9, .9]], [[L / 2 - 2.0, 1.36], [-L / 2 + 1.6, 1.38], [-L / 2 + 1.62, 1.45], [L / 2 - 2.02, 1.43]]],
|
| 101 |
suv: (L) => [[[-L / 2, .38], [L / 2, .38], [L / 2, .82], [L / 2 - .3, 1.0], [L / 2 - 1.15, 1.08], [-L / 2 + .1, 1.12], [-L / 2, .95]],
|
| 102 |
[[L / 2 - 1.2, 1.07], [L / 2 - 1.75, 1.78], [-L / 2 + .25, 1.8], [-L / 2 + .12, 1.1]], [[L / 2 - 1.72, 1.76], [-L / 2 + .2, 1.78], [-L / 2 + .2, 1.86], [L / 2 - 1.75, 1.84]]],
|
| 103 |
+
hover: (L) => [[[-L / 2, .35], [L / 2, .35], [L / 2, .5], [L / 2 - .7, .72], [L / 2 - 1.5, .8], [-L / 2 + .6, .85], [-L / 2, .7]],
|
| 104 |
+
[[L / 2 - 1.5, .78], [L / 2 - 2.3, 1.18], [-L / 2 + 1.3, 1.2], [-L / 2 + .65, .84]], [[L / 2 - 2.25, 1.16], [-L / 2 + 1.35, 1.17], [-L / 2 + 1.35, 1.24], [L / 2 - 2.25, 1.24]]],
|
| 105 |
sports: (L) => [[[-L / 2, .26], [L / 2, .26], [L / 2, .45], [L / 2 - .5, .66], [L / 2 - 1.5, .76], [-L / 2 + .7, .84], [-L / 2, .74]],
|
| 106 |
[[L / 2 - 1.55, .75], [L / 2 - 2.2, 1.12], [-L / 2 + 1.5, 1.12], [-L / 2 + .75, .83]], [[L / 2 - 2.15, 1.1], [-L / 2 + 1.55, 1.1], [-L / 2 + 1.55, 1.16], [L / 2 - 2.15, 1.16]]],
|
| 107 |
van: (L) => [[[-L / 2, .35], [L / 2, .35], [L / 2, .95], [L / 2 - .5, 1.25], [-L / 2, 1.25]],
|
|
|
|
| 131 |
p.push(ext(body, W, BODY, .1), ext(glass, W * .9, GLASS, .06), ext(roof, W * .86, BODY, .05));
|
| 132 |
p.push(B(.2, .22, W * .96, L / 2 - .02, .42, 0, DARK), B(.2, .22, W * .96, -L / 2 + .02, .42, 0, DARK)); // bumpers
|
| 133 |
p.push(B(.03, .14, W * .5, L / 2 + .02, .58, 0, [.12, .12, .13])); // grille
|
| 134 |
+
const wr = kind === "suv" || kind === "pickup" ? .4 : kind === "sports" || kind === "supercar" || kind === "coupe" ? .33 : kind === "classic" ? .36 : .34;
|
| 135 |
+
if (kind === "hover") { for (const x of [L / 2 - .9, -L / 2 + .9]) for (const s of [1, -1]) { p.push(B(.8, .3, .4, x, .3, s * (W / 2 - .1), DARK)); lt.push(B(.7, .06, .32, x, .14, s * (W / 2 - .1), [.3, 1.6, 2])); } lt.push(B(L * .7, .04, W * .5, 0, .22, 0, [.4, 1.2, 1.8])); }
|
| 136 |
+
else p.push(...wheels4(L, W, wr, kind === "sports" || kind === "supercar" ? .85 : kind === "limo" ? 1.1 : .95));
|
| 137 |
+
if (kind === "muscle") { p.push(B(.7, .12, .5, L / 2 - 1, .98, 0, DARK)); for (const s of [1, -1]) p.push(B(L * .8, .05, .25, 0, .96, s * .3, [.95, .95, .95])); }
|
| 138 |
+
if (kind === "supercar") { p.push(B(.25, .06, W * .9, -L / 2 + .2, 1.0, 0, DARK)); for (const s of [1, -1]) p.push(B(.08, .25, .08, -L / 2 + .25, .85, s * W * .35, DARK)); }
|
| 139 |
+
if (kind === "classic") { p.push(B(.12, .1, W * 1.02, L / 2 + .02, .45, 0, CHR), B(.12, .1, W * 1.02, -L / 2 - .02, .45, 0, CHR)); for (const s of [1, -1]) p.push(B(.6, .2, .05, -L / 2 + .4, .95, s * W * .4, BODY)); }
|
| 140 |
+
if (kind === "wagon") { for (const s of [1, -1]) p.push(B(L * .55, .04, .06, -.3, 1.5, s * W * .38, CHR)); }
|
| 141 |
+
mirrors(L / 2 - (kind === "compact" ? .95 : kind === "hatch" ? 1.1 : 1.35), kind === "suv" || kind === "pickup" ? 1.15 : kind === "van" ? 1.35 : .95);
|
| 142 |
if (k.id === "taxi") { p.push(B(.55, .26, .9, -.1, 1.6, 0, BODY)); lt.push(B(.57, .16, .92, -.1, 1.62, 0, [1, .92, .6])); for (const s of [1, -1]) p.push(B(L * .7, .1, .02, 0, .66, s * (W / 2 + .01), [.03, .03, .03])); }
|
| 143 |
if (k.id === "police") { for (const s of [1, -1]) p.push(B(L * .8, .24, .02, 0, .65, s * (W / 2 + .01), [.12, .25, .75])); lt.push(B(.3, .14, .5, -.15, 1.52, .32, [.2, .35, 1]), B(.3, .14, .5, -.15, 1.52, -.32, [1, .15, .15])); }
|
| 144 |
+
lamps(kind === "sports" || kind === "supercar" ? .5 : kind === "hover" ? .55 : kind === "suv" || kind === "pickup" ? .85 : kind === "van" ? .85 : .66);
|
| 145 |
return { body: mergeGeometries(p), lights: mergeGeometries(lt) };
|
| 146 |
}
|
| 147 |
// people: torso and arms (shirt colour), each leg on its own so they can swing, a head and hair
|
| 148 |
+
export function personGeo() {
|
| 149 |
const s = [1, 1, 1];
|
| 150 |
return {
|
| 151 |
body: mergeGeometries([B(.44, .6, .26, 0, 1.2, 0, s), B(.4, .12, .24, 0, .9, 0, [.25, .25, .28]), B(.11, .56, .12, 0, 1.16, .29, s), B(.11, .56, .12, 0, 1.16, -.29, s),
|
|
|
|
| 170 |
|
| 171 |
// ---- vehicles ----
|
| 172 |
const carMat = new THREE.MeshStandardMaterial({ vertexColors: true, roughness: .32, metalness: .55, envMapIntensity: 1.1 });
|
| 173 |
+
carMat.onBeforeCompile = sh => {
|
| 174 |
+
sh.vertexShader = sh.vertexShader.replace("#include <common>", "#include <common>\nattribute float aRust; varying float vRust; varying vec3 vOP;").replace("#include <begin_vertex>", "#include <begin_vertex>\nvRust = aRust; vOP = position;");
|
| 175 |
+
sh.fragmentShader = sh.fragmentShader.replace("#include <common>", "#include <common>\nvarying float vRust; varying vec3 vOP;")
|
| 176 |
+
.replace("#include <color_fragment>", "#include <color_fragment>\nfloat rn = fract(sin(dot(floor(vOP * 7.0), vec3(12.9898, 78.233, 37.719))) * 43758.5453) * .6 + fract(sin(dot(floor(vOP * 23.0), vec3(39.3, 11.7, 83.1))) * 9631.7) * .4; float rr = vRust * smoothstep(.55 - vRust * .35, .9, rn) * step(.12, diffuseColor.r + diffuseColor.g + diffuseColor.b); diffuseColor.rgb = mix(diffuseColor.rgb * (1.0 - vRust * .35), vec3(.36, .17, .07) * (.6 + .7 * rn), rr);")
|
| 177 |
+
.replace("#include <roughnessmap_fragment>", "#include <roughnessmap_fragment>\nroughnessFactor = mix(roughnessFactor, .95, vRust * .85);")
|
| 178 |
+
.replace("#include <metalnessmap_fragment>", "#include <metalnessmap_fragment>\nmetalnessFactor *= 1.0 - vRust * .85;");
|
| 179 |
+
};
|
| 180 |
const lightMat = new THREE.MeshBasicMaterial({ vertexColors: true, toneMapped: false, color: new THREE.Color(2.2, 2.2, 2.2) });
|
| 181 |
const total = KINDS.reduce((s, k) => s + k.share, 0), counts = KINDS.map(k => Math.round(N * k.share / total));
|
| 182 |
const meshes = KINDS.map((k, ti) => {
|
| 183 |
+
const g = vehicle(k), body = new THREE.InstancedMesh(g.body, carMat, Math.max(1, counts[ti])), lights = new THREE.InstancedMesh(g.lights, lightMat, Math.max(1, counts[ti]));
|
| 184 |
+
const rust = new Float32Array(Math.max(1, counts[ti])); body.geometry.setAttribute("aRust", new THREE.InstancedBufferAttribute(rust, 1)); body.rust = rust;
|
| 185 |
body.castShadow = true; body.frustumCulled = lights.frustumCulled = false; scene.add(body, lights); return { body, lights };
|
| 186 |
});
|
| 187 |
const cars = [], col = new THREE.Color();
|
|
|
|
| 191 |
if (!segOK(axis, road, k)) continue;
|
| 192 |
const a = cross(axis, k) + crossW(axis, k) / 2 + 6, b = cross(axis, k + 1) - crossW(axis, k + 1) / 2 - 6;
|
| 193 |
const lo = laneOffs(axis, road);
|
| 194 |
+
Object.assign(c, { axis, road, dir: rnd() < .5 ? 1 : -1, lane: lo[Math.floor(rnd() * lo.length)], s: a + rnd() * (b - a), turn: null, lastK: -99, wreck: null, gone: 0, parked: false });
|
| 195 |
c.v = c.want * .6; return;
|
| 196 |
}
|
| 197 |
}
|
| 198 |
KINDS.forEach((k, ti) => {
|
| 199 |
for (let n = 0; n < counts[ti]; n++) {
|
| 200 |
+
const ms = CAR_MODELS.filter(m => m.kind === k), model = ms[Math.floor(rnd() * ms.length)];
|
| 201 |
+
const c = { kind: k, ti, n, model, v: 0, want: (k.id === "bus" || k.id === "truck" ? 8 : k.id === "sports" || k.id === "supercar" ? 13 : k.fly ? 18 : 10) + rnd() * 4, x: 0, z: 0, yaw: 0, y: 0, rx: 0, rz: 0, bump: 0, near: false, rust: model.rust ? Math.min(1, model.rust * (.8 + rnd() * .4)) : 0, fly: k.fly ? 20 + rnd() * 34 : 0 };
|
| 202 |
place(c); cars.push(c);
|
| 203 |
+
c.color = model.cols[Math.floor(rnd() * model.cols.length)]; c.col = new THREE.Color(c.color);
|
| 204 |
}
|
| 205 |
meshes[ti].body.setColorAt(0, col.setHex(0xffffff));
|
| 206 |
});
|
|
|
|
| 285 |
const api = { detail: 1 };
|
| 286 |
return {
|
| 287 |
get detail() { return api.detail; }, set detail(v) { api.detail = v; },
|
| 288 |
+
cars, people, meshes, smash, near, models: CAR_MODELS,
|
| 289 |
+
set onHit(f) { api.onHit = f; }, set collide(f) { api.collide = f; },
|
| 290 |
+
take(c) { c.taken = true; c.parked = false; c.turn = null; },
|
| 291 |
+
park(c, x, z, yaw) { Object.assign(c, { taken: false, parked: true, x, z, yaw, v: 0, y: 0, wreck: null, turn: null, crush: 0 }); },
|
| 292 |
+
geoOf(k) { const m = meshes[KINDS.indexOf(k)]; return m ? { body: m.body.geometry, lights: m.lights.geometry } : null; },
|
| 293 |
honk(x, z) { for (const p of people) if (Math.abs(p.x - x) < 25 && Math.abs(p.z - z) < 25) p.scare = 2; },
|
| 294 |
// cars near a point (for the player's collision tests)
|
| 295 |
nearCars(x, z, r) { const out = []; for (let cx = Math.floor((x - r) / CELL); cx <= Math.floor((x + r) / CELL); cx++) for (let cz = Math.floor((z - r) / CELL); cz <= Math.floor((z + r) / CELL); cz++) for (const c of cell.get(cx * 7919 + cz) || []) out.push(c); return out; },
|
|
|
|
| 301 |
const D = api.detail || 1, RC = Math.min(900, ((COARSE ? 260 : 380) + alt * 2) * D), RC2 = RC * RC, RP = Math.min(260, ((COARSE ? 90 : 140) + alt * .4) * D), RP2 = RP * RP;
|
| 302 |
for (const m of meshes) m.k = 0;
|
| 303 |
if (relead) for (const c of cars) {
|
| 304 |
+
if (c.wreck || c.turn || c.taken || c.parked) continue;
|
| 305 |
const key = c.axis * 100000 + c.road * 1000 + (c.dir > 0 ? 500 : 0) + Math.round(c.lane * 10);
|
| 306 |
let b = buckets.get(key); if (!b) buckets.set(key, b = []); b.push(c);
|
| 307 |
}
|
| 308 |
if (relead) for (const b of buckets.values()) if (b.length) { b.sort((a, d) => a.s * a.dir - d.s * d.dir); for (let k = 0; k < b.length; k++) b[k].lead = b[k + 1] || null; }
|
| 309 |
for (const c of cars) {
|
| 310 |
const far = (Math.abs(c.x - px) > 450 || Math.abs(c.z - pz) > 450) && (Math.abs(c.x - cx0) > RC || Math.abs(c.z - cz0) > RC);
|
| 311 |
+
if (c.taken) continue;
|
| 312 |
const vfar = far && (Math.abs(c.x - px) > 1200 || Math.abs(c.z - pz) > 1200), every = vfar ? 15 : 6; // far away nobody sees them: move them in big, rare steps
|
| 313 |
if (far && (frameN + c.n) % every) { continue; }
|
| 314 |
const h = far ? dt * every : dt;
|
|
|
|
| 318 |
c.x += w.vx * h; c.z += w.vz * h; const f = Math.pow(c.y > 0 ? .9 : .2, h); w.vx *= f; w.vz *= f;
|
| 319 |
if (c.y === 0) { c.rx = Math.round(c.rx / Math.PI) * Math.PI * .02 + c.rx * .98; c.rz = Math.round(c.rz / Math.PI) * Math.PI * .02 + c.rz * .98; }
|
| 320 |
if (w.t > 24) { place(c); posOf(c); c.rx = c.rz = c.y = 0; c.col.setHex(c.color); c.crush = 0; c.dent = 0; }
|
| 321 |
+
} else if (c.parked) {
|
| 322 |
+
c.v = 0;
|
| 323 |
} else if (c.turn) {
|
| 324 |
const T = c.turn; T.t += c.v * h / T.len; const t = Math.min(1, T.t), a = 1 - t;
|
| 325 |
const nx = a * a * T.p0.x + 2 * a * t * T.p1.x + t * t * T.p2.x, nz = a * a * T.p0.z + 2 * a * t * T.p1.z + t * t * T.p2.z;
|
|
|
|
| 334 |
if (k >= 0 && k < lim) {
|
| 335 |
const cc = cross(c.axis, k), stop = cc - c.dir * (crossW(c.axis, k) / 2 + 2.5), dist = (stop - c.s) * c.dir;
|
| 336 |
const ph = c.axis === 0 ? phase(k, c.road, time) : phase(c.road, k, time), green = c.axis === 0 ? ph === 0 : ph === 2, amber = c.axis === 0 ? ph === 1 : ph === 3;
|
| 337 |
+
if (!green && !c.fly && dist > (amber ? 10 : -.5) && dist < 40) want = Math.min(want, Math.sqrt(Math.max(0, 2 * 4.5 * (dist - .5))));
|
| 338 |
if (dist < 0 && c.lastK !== k) { c.lastK = k; decide(c, k); }
|
| 339 |
} else if ((c.dir > 0 && c.s > cross(c.axis, lim - 1) - 12) || (c.dir < 0 && c.s < cross(c.axis, 0) + 12)) c.dir *= -1;
|
| 340 |
// keep a gap to the car in front
|
| 341 |
if (c.lead) { const gap = (c.lead.s - c.s) * c.dir - (c.kind.L + c.lead.kind.L) / 2; want = Math.min(want, gap < 2.5 ? 0 : Math.max(0, (gap - 2.5) * 1.1), c.lead.v + gap * .4); }
|
| 342 |
// and to the player
|
| 343 |
+
if (c.fly) c.y = c.fly + Math.sin(time * 1.3 + c.n) * .6;
|
| 344 |
+
if (player && player.y < 3 && !c.fly) { const dx = player.x - c.x, dz = player.z - c.z, ah = dx * Math.cos(c.yaw) + dz * Math.sin(c.yaw), sd = -dx * Math.sin(c.yaw) + dz * Math.cos(c.yaw); if (ah > 0 && ah < 22 && Math.abs(sd) < 2.6) want = Math.min(want, Math.max(0, (ah - 6) * .8)); }
|
| 345 |
if (c.stop > 0) { c.stop -= h; want = 0; } // dented: it stops a while before driving on
|
| 346 |
c.v += Math.max(-9 * h, Math.min(3 * h, want - c.v));
|
| 347 |
if (!c.turn) { c.s += c.v * c.dir * h; posOf(c); }
|
|
|
|
| 349 |
if (!far) { const key = Math.floor(c.x / CELL) * 7919 + Math.floor(c.z / CELL); let b = cell.get(key); if (!b) cell.set(key, b = []); b.push(c); }
|
| 350 |
const ddx = c.x - cx0, ddz = c.z - cz0;
|
| 351 |
if (ddx * ddx + ddz * ddz < RC2) {
|
| 352 |
+
e.set(c.rx, -c.yaw, c.rz + (c.turn ? (c.fly ? .25 : .05) : 0), "YXZ"); q.setFromEuler(e); const md = c.model; m4.compose(pos.set(c.x, c.y, c.z), q, squash.set(md.sl, (c.crush || 1 - (c.dent || 0) * .07) * md.sh, md.sw));
|
| 353 |
+
const M = meshes[c.ti], k = M.k++; M.body.setMatrixAt(k, m4); M.body.setColorAt(k, c.col); M.body.rust[k] = c.rust; M.lights.setMatrixAt(k, c.wreck ? m4.compose(pos, q, zero) : m4); }
|
| 354 |
}
|
| 355 |
+
for (const m of meshes) { m.body.count = m.lights.count = m.k; m.body.visible = m.lights.visible = m.k > 0; m.body.instanceMatrix.needsUpdate = m.lights.instanceMatrix.needsUpdate = true; if (m.body.instanceColor) m.body.instanceColor.needsUpdate = true; m.body.geometry.attributes.aRust.needsUpdate = true; }
|
| 356 |
// debris (skipped once every piece has landed and been hidden)
|
| 357 |
if (debLive) { debLive = 0; for (let k = 0; k < DEB; k++) {
|
| 358 |
const d = deb[k];
|
|
|
|
| 372 |
around(p, p.d, tmp);
|
| 373 |
let x = tmp.x, z = tmp.z;
|
| 374 |
if (player && player.y < 4 && !(p.down > 0)) { // a car coming: leap out of the way, or get knocked over if it is too quick
|
| 375 |
+
const dx = x + p.push.x - player.x, dz = z + p.push.y - player.z, d = Math.hypot(dx, dz) || .01, r = player.foot ? 1.1 : 3 + Math.min(3, (player.speed || 0) * .04);
|
| 376 |
+
if (d < 2.2 && (player.speed || 0) > 6 && !player.foot) {
|
| 377 |
+
const sp = player.speed, k = Math.min(1, sp / 30), vx = player.vx || 0, vz = player.vz || 0, ux = dx / d, uz = dz / d, side = rnd() < .5 ? -1 : 1;
|
| 378 |
+
p.down = 3 + Math.min(6, sp * .12); p.downT = p.down;
|
| 379 |
+
p.fall = { vx: vx * (.55 + .3 * k) + ux * (1.5 + rnd() * 2.5), vz: vz * (.55 + .3 * k) + uz * (1.5 + rnd() * 2.5), y: .2, vy: Math.min(11, 1.8 + sp * (.16 + .1 * k)), rx: 0, rz: 0, wx: side * (2 + sp * .35), wz: (rnd() - .5) * (2 + sp * .2), spin: (rnd() - .5) * (3 + sp * .15), rot: 0, air: true };
|
| 380 |
+
p.scare = 4; if (api.onHit) api.onHit(sp, x, z);
|
| 381 |
+
}
|
| 382 |
else if (d < r) { const k = (r - d) * Math.min(1, h * 10); p.push.x += dx / d * k; p.push.y += dz / d * k; p.scare = 2; }
|
| 383 |
}
|
| 384 |
if (p.down > 0) {
|
| 385 |
const f = p.fall; p.down -= h;
|
| 386 |
+
if (f.air) { // flying: gravity, tumbling, a little air drag
|
| 387 |
+
f.vy -= 9.81 * h; f.y += f.vy * h; f.rx += f.wx * h; f.rz += f.wz * h; f.rot += f.spin * h; const dr = Math.pow(.92, h); f.vx *= dr; f.vz *= dr;
|
| 388 |
+
if (f.y <= 0) { // hits the ground: bounce if it was hard, else stay down and slide
|
| 389 |
+
f.y = 0; if (f.vy < -3.5) { f.vy = -f.vy * .28; f.wx *= .45; f.wz *= .45; f.spin *= .5; } else { f.vy = 0; f.air = false; }
|
| 390 |
+
}
|
| 391 |
+
} else { const fr = Math.pow(.04, h); f.vx *= fr; f.vz *= fr; const lay = Math.round(f.rx / Math.PI - .5) * Math.PI + Math.PI / 2; f.rx += (lay - f.rx) * Math.min(1, h * 8); f.rz += (0 - f.rz) * Math.min(1, h * 8); }
|
| 392 |
+
let nx = p.push.x + f.vx * h, nz = p.push.y + f.vz * h;
|
| 393 |
+
if (api.collide && api.collide(x + nx, z + nz)) { f.vx *= -.3; f.vz *= -.3; nx = p.push.x; nz = p.push.y; } // walls stop them
|
| 394 |
+
p.push.x = nx; p.push.y = nz;
|
| 395 |
}
|
| 396 |
if (p.scare > 0) p.scare -= h;
|
| 397 |
+
if (!(p.down > 0)) p.push.multiplyScalar(1 - Math.min(1, h * (p.fall ? .5 : .25))); // once back up they walk back to the pavement
|
| 398 |
x += p.push.x; z += p.push.y; p.x = x; p.z = z;
|
| 399 |
const walk = time * 7 * p.v * (p.scare > 0 ? 2 : 1) + p.ph, bob = p.wait > 0 ? 0 : Math.abs(Math.sin(walk)) * .05;
|
| 400 |
const ex = x - cx0, ez = z - cz0;
|
| 401 |
if (ex * ex + ez * ez < RP2) {
|
| 402 |
const yaw = -(tmp.yaw + (p.dir < 0 ? Math.PI : 0));
|
| 403 |
let lie = 0, y = bob + .06;
|
| 404 |
+
let rx = 0, rz = 0;
|
| 405 |
+
if (p.down > 0) { const f = p.fall; lie = p.down > 1.2 ? 1 : p.down / 1.2; rx = f.rx * lie; rz = f.rz * lie; y = f.y + .06 + Math.abs(Math.sin(rx)) * .14; } // thrown, down, then back on their feet
|
| 406 |
+
e.set(rx, yaw + (p.fall ? p.fall.rot : 0), rz, "YXZ"); q.setFromEuler(e); m4.compose(pos.set(x, y, z), q, one);
|
| 407 |
bodies.setMatrixAt(pk, m4); heads.setMatrixAt(pk, m4); hair.setMatrixAt(pk, m4);
|
| 408 |
+
const swing = p.down > 0 ? (p.fall.air ? Math.sin(time * 19 + n) * .9 : .15) : p.wait > 0 ? 0 : Math.sin(walk) * .5;
|
| 409 |
LEGS[0][0] = legL; LEGS[1][0] = legR; for (const [leg, sgn, off] of LEGS) { legQ.setFromAxisAngle(Z, swing * sgn); legM.compose(pos.set(0, .86, off), legQ, one); leg.setMatrixAt(pk, legT.copy(m4).multiply(legM)); }
|
| 410 |
bodies.setColorAt(pk, p.shirt); heads.setColorAt(pk, p.skin); legL.setColorAt(pk, p.pants); legR.setColorAt(pk, p.pants); hair.setColorAt(pk, p.hairC); pk++;
|
| 411 |
}
|
static/police.js
CHANGED
|
@@ -95,6 +95,8 @@ export function createPolice(scene, api, sound, life) {
|
|
| 95 |
event(kind, extra) { // things the game tells us about
|
| 96 |
if (kind === "smash") addHeat(extra === "police" ? 3 : 1, extra === "police" ? "You smashed a police car!" : "Wanted for smashing cars");
|
| 97 |
if (kind === "cop") addHeat(3, "Police officer down");
|
|
|
|
|
|
|
| 98 |
},
|
| 99 |
update(dt, time) {
|
| 100 |
const p = api.state; if (!p || !p.started) return;
|
|
|
|
| 95 |
event(kind, extra) { // things the game tells us about
|
| 96 |
if (kind === "smash") addHeat(extra === "police" ? 3 : 1, extra === "police" ? "You smashed a police car!" : "Wanted for smashing cars");
|
| 97 |
if (kind === "cop") addHeat(3, "Police officer down");
|
| 98 |
+
if (kind === "steal") addHeat(1.2, "Car theft reported");
|
| 99 |
+
if (kind === "ped") addHeat(.6, "Hit and run");
|
| 100 |
},
|
| 101 |
update(dt, time) {
|
| 102 |
const p = api.state; if (!p || !p.started) return;
|
static/race.js
CHANGED
|
@@ -1,253 +1,253 @@
|
|
| 1 |
-
// Street races round VOID City: drive into a start marker and press Enter (or tap Race). Gates on the real streets,
|
| 2 |
-
// three AI rivals in sports cars (solo), or everybody in your online room. A countdown, a timer, live positions,
|
| 3 |
-
// best times kept on this device. The Sky Rings race is a flying time trial through gates in the air.
|
| 4 |
-
// Ghosts: your best run on every route is recorded and races beside you next time, with a split at every gate.
|
| 5 |
-
// Your own routes: drop gates while you drive or fly, save them, race them against your ghost, share them as a link.
|
| 6 |
-
import * as THREE from "three";
|
| 7 |
-
import { GRID } from "./city.js";
|
| 8 |
-
import { KINDS, vehicle } from "./npc.js";
|
| 9 |
-
import { makeDriver } from "./police.js";
|
| 10 |
-
|
| 11 |
-
const G = (i, j) => [GRID.ax(i), 0, GRID.sz(j)];
|
| 12 |
-
export const ROUTES = [
|
| 13 |
-
{ id: "midtown", name: "Midtown Sprint", pts: [G(7, 8), G(7, 4), G(9, 4), G(9, 1), G(5, 1), G(5, 6), G(8, 6)] },
|
| 14 |
-
{ id: "park", name: "Park Loop ×2", pts: [G(2, 12), G(5, 12), G(5, 18), G(2, 18), G(2, 12), G(5, 12), G(5, 18), G(2, 18), G(2, 13)] },
|
| 15 |
-
{ id: "bridge", name: "Bridge Run", pts: [G(7, 10), G(12, 10), [1400, 0, 0], [1690, 0, 0]] },
|
| 16 |
-
{ id: "downtown", name: "Downtown Dash", pts: [G(8, 13), G(12, 13), G(12, 19), G(9, 19), G(9, 15), G(11, 15)] },
|
| 17 |
-
{ id: "drag", name: "Runway Drag", pts: [[-2600, 0, 0], [-2350, 0, 0], [-2100, 0, 0], [-1960, 0, 0]] },
|
| 18 |
-
{ id: "sky", name: "Sky Rings", air: true, pts: [[420, 0, -40], [420, 90, -160], [520, 120, -300], [430, 150, -450], [250, 170, -520], [60, 150, -460], [-60, 120, -330], [-120, 90, -180], [-260, 70, 60], [-400, 60, 250]] },
|
| 19 |
-
];
|
| 20 |
-
const RIVALS = [["VIPER", 0xd21f2b], ["BLAZE", 0xff6a00], ["NOVA", 0x1f6fe0]];
|
| 21 |
-
const fmt = t => `${Math.floor(t / 60)}:${(t % 60).toFixed(1).padStart(4, "0")}`;
|
| 22 |
-
const store = { get(k) { try { return JSON.parse(localStorage.getItem(k)); } catch { return null; } }, set(k, v) { try { localStorage.setItem(k, JSON.stringify(v)); } catch { } } };
|
| 23 |
-
|
| 24 |
-
export function createRaces(scene, api, sound, canvasTex, net) {
|
| 25 |
-
const $ = id => document.getElementById(id);
|
| 26 |
-
const D = makeDriver(api);
|
| 27 |
-
const label = (text, col) => { const t = canvasTex(512, 128, (g, w, h) => { g.fillStyle = "rgba(0,0,0,.6)"; g.fillRect(0, 0, w, h); g.strokeStyle = col; g.lineWidth = 6; g.strokeRect(4, 4, w - 8, h - 8); g.fillStyle = col; g.font = "900 54px Unbounded, Impact, sans-serif"; g.textAlign = "center"; g.fillText(text, w / 2, 84); }); return new THREE.Sprite(new THREE.SpriteMaterial({ map: t, toneMapped: false, depthTest: true })); };
|
| 28 |
-
// start markers: a column of light and a floating name
|
| 29 |
-
const custom = (store.get("void.routes") || []).filter(r => r && Array.isArray(r.pts) && r.pts.length >= 3);
|
| 30 |
-
const routes = [...ROUTES, ...custom];
|
| 31 |
-
const makeStart = r => {
|
| 32 |
-
const [x, , z] = r.pts[0], [nx, , nz] = r.pts[1], back = Math.atan2(nz - z, nx - x);
|
| 33 |
-
const sx = x - Math.cos(back) * 35, sz = z - Math.sin(back) * 35;
|
| 34 |
-
const col = r.custom ? "#c07dff" : r.air ? "#3dfcff" : "#ffd23d";
|
| 35 |
-
const pillar = new THREE.Mesh(new THREE.CylinderGeometry(6, 6, 60, 32, 1, true).translate(0, 30, 0), new THREE.MeshBasicMaterial({ color: new THREE.Color(col).multiplyScalar(1.6), transparent: true, opacity: .12, blending: THREE.AdditiveBlending, depthWrite: false, side: THREE.DoubleSide, toneMapped: false }));
|
| 36 |
-
pillar.position.set(sx, 0, sz); scene.add(pillar);
|
| 37 |
-
const ring = new THREE.Mesh(new THREE.RingGeometry(5.4, 6, 48).rotateX(-Math.PI / 2), new THREE.MeshBasicMaterial({ color: new THREE.Color(col).multiplyScalar(2), toneMapped: false })); ring.position.set(sx, .15, sz); scene.add(ring);
|
| 38 |
-
const s = label("🏁 " + r.name.toUpperCase(), col); s.scale.set(16, 4, 1); s.position.set(sx, 9, sz); scene.add(s);
|
| 39 |
-
return { r, x: sx, z: sz, yaw: back, pillar, ring, s };
|
| 40 |
-
};
|
| 41 |
-
const starts = routes.map(makeStart);
|
| 42 |
-
// gates: two posts and a banner across the road
|
| 43 |
-
const gateMat = new THREE.MeshBasicMaterial({ color: new THREE.Color(0xffd23d).multiplyScalar(2), toneMapped: false }), dimMat = new THREE.MeshBasicMaterial({ color: 0x555555 });
|
| 44 |
-
const checker = canvasTex(256, 32, (g, w, h) => { for (let x = 0; x < w; x += 16) for (let y = 0; y < h; y += 16) { g.fillStyle = ((x + y) / 16) % 2 ? "#111" : "#fff"; g.fillRect(x, y, 16, 16); } });
|
| 45 |
-
const gates = [];
|
| 46 |
-
function buildGates(r) {
|
| 47 |
-
clearGates();
|
| 48 |
-
r.pts.forEach((p, k) => {
|
| 49 |
-
const q = r.pts[Math.max(0, k - 1)], o = r.pts[Math.min(r.pts.length - 1, k + 1)], dir = k ? Math.atan2(p[2] - q[2], p[0] - q[0]) : Math.atan2(o[2] - p[2], o[0] - p[0]);
|
| 50 |
-
const g = new THREE.Group(); g.position.set(p[0], p[1], p[2]); g.rotation.y = -dir;
|
| 51 |
-
const fin = k === r.pts.length - 1;
|
| 52 |
-
if (r.air) { const ring = new THREE.Mesh(new THREE.TorusGeometry(10, .7, 10, 40), gateMat.clone()); ring.rotation.y = Math.PI / 2; g.add(ring); g.userData.parts = [ring]; }
|
| 53 |
-
else {
|
| 54 |
-
const W = 11, parts = [];
|
| 55 |
-
for (const s of [-1, 1]) { const post = new THREE.Mesh(new THREE.BoxGeometry(.6, 7, .6).translate(0, 3.5, 0), gateMat.clone()); post.position.z = s * W; g.add(post); parts.push(post); }
|
| 56 |
-
const banner = new THREE.Mesh(new THREE.BoxGeometry(.3, 1.4, W * 2 + .6), fin ? new THREE.MeshBasicMaterial({ map: checker, toneMapped: false }) : gateMat.clone()); banner.position.y = 7; g.add(banner); if (!fin) parts.push(banner);
|
| 57 |
-
g.userData.parts = parts;
|
| 58 |
-
}
|
| 59 |
-
scene.add(g); gates.push(g);
|
| 60 |
-
});
|
| 61 |
-
}
|
| 62 |
-
function clearGates() { for (const g of gates) scene.remove(g); gates.length = 0; }
|
| 63 |
-
// rivals: sports cars with their own colours
|
| 64 |
-
const sport = KINDS.find(k => k.id === "sports"), sg = vehicle(sport);
|
| 65 |
-
const rivals = RIVALS.map(([name, col]) => {
|
| 66 |
-
const g = new THREE.Group(); g.add(new THREE.Mesh(sg.body, new THREE.MeshStandardMaterial({ vertexColors: true, color: col, roughness: .25, metalness: .6 })), new THREE.Mesh(sg.lights, new THREE.MeshBasicMaterial({ vertexColors: true, toneMapped: false, color: new THREE.Color(2, 2, 2) })));
|
| 67 |
-
const tag = label(name, "#" + col.toString(16).padStart(6, "0")); tag.scale.set(5, 1.25, 1); tag.position.y = 3.4; g.add(tag);
|
| 68 |
-
g.visible = false; scene.add(g); return { name, g, x: 0, z: 0, yaw: 0, v: 0, cp: 1, done: 0, skill: 1 };
|
| 69 |
-
});
|
| 70 |
-
|
| 71 |
-
// the ghost: a see-through copy of your best run on this route
|
| 72 |
-
const ghost = new THREE.Group(); ghost.add(new THREE.Mesh(sg.body, new THREE.MeshBasicMaterial({ color: new THREE.Color(.3, 1.6, 2), transparent: true, opacity: .28, depthWrite: false, toneMapped: false })));
|
| 73 |
-
{ const gt = label("GHOST", "#3dfcff"); gt.scale.set(5, 1.25, 1); gt.position.y = 3.4; ghost.add(gt); } ghost.visible = false; scene.add(ghost);
|
| 74 |
-
const ghostOf = id => { const gh = store.get("void.ghost." + id); return gh && Array.isArray(gh.p) && gh.p.length >= 8 ? gh : null; };
|
| 75 |
-
let R = null; // the race in progress
|
| 76 |
-
function begin(route, opts = {}) {
|
| 77 |
-
const p = api.state; if (!p) return;
|
| 78 |
-
api.policeClear && api.policeClear();
|
| 79 |
-
const st = starts.find(s => s.r === route);
|
| 80 |
-
buildGates(route);
|
| 81 |
-
const slot = opts.slot ?? 0, side = [0, 1, -1, 2, -2, 3, -3][slot % 7] * 4.2, row = Math.floor(slot / 7) * 8;
|
| 82 |
-
const fx = Math.cos(st.yaw), fz = Math.sin(st.yaw);
|
| 83 |
-
api.teleport(st.x + fx * (20 - row) - fz * side, st.z + fz * (20 - row) + fx * side, st.yaw);
|
| 84 |
-
api.freeze(opts.wait ?? 3.2);
|
| 85 |
-
// bots: in an online race every player runs the same bots from the same seed on a fixed clock, so they match
|
| 86 |
-
let sd = (opts.seed || Math.floor(Math.random() * 1e9)) % 2147483647 || 7; const rng = () => (sd = (sd * 16807) % 2147483647) / 2147483647;
|
| 87 |
-
R = { route, t: -(opts.wait ?? 3.2), cp: 1, done: 0, online: !!opts.online, results: [], lastBeep: 4, rng, acc: 0, bt: 0, rec: [], recT: 0, splits: [], ghost: ghostOf(route.id) };
|
| 88 |
-
ghost.visible = !!R.ghost;
|
| 89 |
-
rivals.forEach((r, k) => {
|
| 90 |
-
r.g.visible = !route.air && !route.custom && (!R.online || opts.bots !== false);
|
| 91 |
-
const sd = [-1, 1, 2][k] * 4.2, rw = k === 2 ? 8 : 0;
|
| 92 |
-
Object.assign(r, { x: st.x + fx * (20 - rw) - fz * sd, z: st.z + fz * (20 - rw) + fx * sd, yaw: st.yaw, v: 0, cp: 1, done: 0, skill: .9 + rng() * .16, accel: 13, stuck: 0 });
|
| 93 |
-
});
|
| 94 |
-
$("gRace").hidden = false; $("gRaceRes").hidden = true;
|
| 95 |
-
}
|
| 96 |
-
function finish(msg) {
|
| 97 |
-
const p = api.state, best = store.get("void.race." + R.route.id);
|
| 98 |
-
const me = R.done, rank = standings().findIndex(s => s.me) + 1;
|
| 99 |
-
if (me && (!best || me < best)) { store.set("void.race." + R.route.id, me); if (R.rec.length >= 8) store.set("void.ghost." + R.route.id, { t: me, s: R.splits, p: R.rec.slice(0, 12000) }); }
|
| 100 |
-
const gdiff = me && R.ghost ? me - R.ghost.t : null;
|
| 101 |
-
const reward = me ? [0, 1500, 800, 400, 200][Math.min(4, rank)] || 100 : 0;
|
| 102 |
-
if (reward) api.addScore(reward, `${R.route.name}: ${["", "1st", "2nd", "3rd"][rank] || rank + "th"}`);
|
| 103 |
-
$("gRaceRes").innerHTML = `<h3>${R.route.name}</h3><p>${msg || (me ? `You finished ${["", "1st", "2nd", "3rd"][rank] || rank + "th"} in ${fmt(me)}` : "Race over")}</p><p class="small">${best ? `Best: ${fmt(Math.min(best, me || 1e9))}` : me ? "New best time!" : ""}${gdiff != null ? ` · vs ghost ${gdiff <= 0 ? "−" : "+"}${Math.abs(gdiff).toFixed(2)} s${gdiff < 0 ? " (new ghost saved)" : ""}` : me && !R.ghost ? " · ghost saved" : ""}</p><ol>${standings().map(s => `<li${s.me ? ' class="me"' : ""}>${s.name}<span>${s.done ? fmt(s.done) : "DNF"}</span></li>`).join("")}</ol><button class="pill solid" id="gRaceOk">OK</button>`;
|
| 104 |
-
$("gRaceRes").hidden = false; $("gRaceOk").onclick = () => { $("gRaceRes").hidden = true; };
|
| 105 |
-
$("gRace").hidden = true; clearGates(); rivals.forEach(r => r.g.visible = false); ghost.visible = false; R = null; void p;
|
| 106 |
-
}
|
| 107 |
-
// everyone's progress: checkpoints passed plus how far along to the next
|
| 108 |
-
const progOf = (x, y, z, cp, route) => { const a = route.pts[cp - 1], b = route.pts[Math.min(route.pts.length - 1, cp)]; const L = Math.hypot(b[0] - a[0], b[2] - a[2]) || 1, d = Math.hypot(b[0] - x, b[2] - z); return cp + Math.max(0, 1 - d / L); };
|
| 109 |
-
function standings() {
|
| 110 |
-
if (!R) return [];
|
| 111 |
-
const p = api.state, out = [{ name: "YOU", me: true, prog: R.done ? 1e6 - R.done : progOf(p.x, p.y, p.z, R.cp, R.route), done: R.done }];
|
| 112 |
-
for (const r of rivals) if (r.g.visible) out.push({ name: r.name, prog: r.done ? 1e6 - r.done : progOf(r.x, 0, r.z, r.cp, R.route), done: r.done });
|
| 113 |
-
if (R.online && net) for (const q of net.players()) if (q.race === R.route.id) out.push({ name: q.name, prog: q.rf ? 1e6 - q.rf : q.rp || 0, done: q.rf || 0 });
|
| 114 |
-
return out.sort((a, b) => b.prog - a.prog);
|
| 115 |
-
}
|
| 116 |
-
function nearStart() { const p = api.state; if (!p || R || B) return null; return starts.find(s => Math.hypot(p.x - s.x, p.z - s.z) < 14) || null; }
|
| 117 |
-
addEventListener("keydown", e => { if (e.code === "Enter" && api.active()) { const s = nearStart(); if (s) start(s.r); } });
|
| 118 |
-
$("gRaceGo").onclick = () => { const s = nearStart(); if (s) start(s.r); };
|
| 119 |
-
function start(route) {
|
| 120 |
-
if (net && net.online() && !route.custom) { const seed = Math.floor(Math.random() * 1e9); net.send({ t: "race", id: route.id, wait: 6, seed }); begin(route, { online: true, wait: 6, slot: 0, seed }); }
|
| 121 |
-
else begin(route);
|
| 122 |
-
}
|
| 123 |
-
// ---- the route builder ----
|
| 124 |
-
let B = null;
|
| 125 |
-
const saveCustom = () => store.set("void.routes", custom.map(({ id, name, air, pts }) => ({ id, name, air, pts, custom: true })));
|
| 126 |
-
function addRoute(r) { r.custom = true; custom.push(r); routes.push(r); starts.push(makeStart(r)); while (custom.length > 12) removeRoute(custom[0].id); saveCustom(); return r; }
|
| 127 |
-
function removeRoute(id) {
|
| 128 |
-
const k = custom.findIndex(r => r.id === id); if (k < 0) return; const r = custom[k]; custom.splice(k, 1); routes.splice(routes.indexOf(r), 1);
|
| 129 |
-
const s = starts.findIndex(st => st.r === r); if (s >= 0) { const st = starts[s]; scene.remove(st.pillar, st.ring, st.s); starts.splice(s, 1); }
|
| 130 |
-
try { localStorage.removeItem("void.ghost." + id); localStorage.removeItem("void.race." + id); } catch { } saveCustom();
|
| 131 |
-
}
|
| 132 |
-
const buildMarks = [];
|
| 133 |
-
function builder(on) {
|
| 134 |
-
if (on && R) return api.say("Finish the race first", 2);
|
| 135 |
-
for (const m of buildMarks) scene.remove(m); buildMarks.length = 0;
|
| 136 |
-
B = on ? { pts: [] } : null; $("gBuild").hidden = !on; if (on) { dropGate(); api.say("Route builder: drive or fly your route, press
|
| 137 |
-
renderBuild();
|
| 138 |
-
}
|
| 139 |
-
function dropGate() {
|
| 140 |
-
const p = api.state; if (!B || !p) return;
|
| 141 |
-
const pt = [Math.round(p.x), p.mode === "air" && p.y > 6 ? Math.round(p.y) : 0, Math.round(p.z)], last = B.pts[B.pts.length - 1];
|
| 142 |
-
if (last && Math.hypot(pt[0] - last[0], pt[1] - last[1], pt[2] - last[2]) < 30) return api.say("Too close to the last gate (30 m)", 1.5);
|
| 143 |
-
B.pts.push(pt); sound.pop();
|
| 144 |
-
const m = new THREE.Mesh(new THREE.TorusGeometry(pt[1] ? 10 : 6, .5, 8, 32), new THREE.MeshBasicMaterial({ color: new THREE.Color(0xc07dff).multiplyScalar(2), toneMapped: false })); m.position.set(pt[0], pt[1] || 6, pt[2]); m.rotation.y = -p.yaw + Math.PI / 2; scene.add(m); buildMarks.push(m);
|
| 145 |
-
renderBuild();
|
| 146 |
-
}
|
| 147 |
-
function saveBuild() {
|
| 148 |
-
if (!B || B.pts.length < 3) return api.say("A route needs a start and at least 2 more gates", 2);
|
| 149 |
-
const r = addRoute({ id: "c" + Date.now().toString(36), name: "My route " + (custom.length + 1), air: B.pts.some(q => q[1] > 0), pts: B.pts });
|
| 150 |
-
builder(false); api.say(`Saved "${r.name}" and copied its link. The purple marker is its start`, 3.5); renderList(); share(r, true);
|
| 151 |
-
}
|
| 152 |
-
function renderBuild() { if (!B) return; $("gBuild").innerHTML = `<b>🛠 Route builder</b><span>${B.pts.length === 1 ? "Start set" : `${B.pts.length - 1} gate${B.pts.length > 2 ? "s" : ""}`}</span><button class="pill" id="gBDrop">Drop gate (
|
| 153 |
-
addEventListener("keydown", e => { if (e.code === "
|
| 154 |
-
// share a route as a link: everything is in the address, nothing is uploaded
|
| 155 |
-
const b64 = o => btoa(unescape(encodeURIComponent(JSON.stringify(o)))).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
| 156 |
-
async function share(r, quiet) {
|
| 157 |
-
const url = location.origin + location.pathname + "#r=" + b64({ n: r.name, p: r.pts });
|
| 158 |
-
try { await navigator.clipboard.writeText(url); if (!quiet) api.say("Route link copied: send it to a friend", 2.5); } catch { if (!quiet) prompt("Copy this route link", url); }
|
| 159 |
-
}
|
| 160 |
-
try {
|
| 161 |
-
const h = new URLSearchParams(location.hash.slice(1)).get("r");
|
| 162 |
-
if (h) {
|
| 163 |
-
const o = JSON.parse(decodeURIComponent(escape(atob(h.replace(/-/g, "+").replace(/_/g, "/")))));
|
| 164 |
-
const pts = (o.p || []).slice(0, 40).map(q => [+q[0] || 0, Math.max(0, Math.min(400, +q[1] || 0)), +q[2] || 0]).filter(q => q.every(Number.isFinite) && Math.abs(q[0]) < 4000 && Math.abs(q[2]) < 4000);
|
| 165 |
-
if (pts.length >= 3 && !custom.some(r => JSON.stringify(r.pts) === JSON.stringify(pts))) addRoute({ id: "c" + Date.now().toString(36), name: String(o.n || "Shared route").replace(/[<>&"]/g, "").slice(0, 28), air: pts.some(q => q[1] > 0), pts });
|
| 166 |
-
history.replaceState(null, "", location.pathname + location.search);
|
| 167 |
-
}
|
| 168 |
-
} catch { }
|
| 169 |
-
// the Routes list in the Menu: best time, ghost, go there, share, delete
|
| 170 |
-
function renderList() {
|
| 171 |
-
const el = $("gRoutes"); if (!el) return;
|
| 172 |
-
el.innerHTML = routes.map(r => { const b = store.get("void.race." + r.id), gh = !!ghostOf(r.id); return `<div class="route${r.custom ? " mine" : ""}"><div><span>${r.air ? "✈️" : "🏁"} ${r.name}</span><small>${b ? `Best ${fmt(b)}` : "No time yet"}${gh ? " · 👻 ghost" : ""}${r.custom ? " · yours" : ""}</small></div><button class="pill" data-rgo="${r.id}">Go</button>${r.custom ? `<button class="pill" data-share="${r.id}">Share</button><button class="pill" data-del="${r.id}">✕</button>` : ""}</div>`; }).join("") + `<button class="tile wide-tile" id="gBuildBtn"><b>🛠</b>Make your own route</button>`;
|
| 173 |
-
}
|
| 174 |
-
document.addEventListener("click", e => {
|
| 175 |
-
const t = e.target.closest("[data-rgo],[data-share],[data-del],#gBuildBtn"); if (!t || !t.closest("#gRoutes")) return;
|
| 176 |
-
if (t.id === "gBuildBtn") return builder(true);
|
| 177 |
-
const r = routes.find(x => x.id === (t.dataset.rgo || t.dataset.share || t.dataset.del)); if (!r) return;
|
| 178 |
-
if (t.dataset.rgo) { const st = starts.find(s => s.r === r); if (st && !R) { api.teleport(st.x, st.z, st.yaw); api.say(`${r.name}: press Enter (or Start) at the marker`, 3); } }
|
| 179 |
-
if (t.dataset.share) share(r);
|
| 180 |
-
if (t.dataset.del && confirm(`Delete "${r.name}" and its ghost?`)) { removeRoute(r.id); renderList(); }
|
| 181 |
-
});
|
| 182 |
-
if (net) net.on("race", (m, from) => { const r = ROUTES.find(x => x.id === m.id); if (r && !R && !B) { begin(r, { online: true, wait: Math.max(2, Math.min(10, +m.wait || 6)) - .3, seed: Math.abs(m.seed | 0) || 7, slot: 1 + (net.players().findIndex(q => q.id === from) + 1) % 6 }); api.say(`${net.nameOf(from)} started ${r.name}. Get ready!`, 3); } });
|
| 183 |
-
|
| 184 |
-
return {
|
| 185 |
-
get race() { return R; }, rivals, renderList, get building() { return !!B; },
|
| 186 |
-
progress() { return R ? { race: R.route.id, rp: R.done ? 0 : progOf(api.state.x, api.state.y, api.state.z, R.cp, R.route), rf: R.done } : null; },
|
| 187 |
-
starts,
|
| 188 |
-
update(dt, time) {
|
| 189 |
-
const p = api.state;
|
| 190 |
-
for (const s of starts) { s.pillar.material.opacity = .1 + .05 * Math.sin(time * 3); s.s.visible = s.ring.visible = s.pillar.visible = !R; }
|
| 191 |
-
const ns = nearStart(); $("gRaceGo").hidden = !ns; if (ns) $("gRaceGo").textContent = `Start ${ns.r.name} (Enter)`;
|
| 192 |
-
if (!R || !p) return;
|
| 193 |
-
R.t += dt;
|
| 194 |
-
// countdown
|
| 195 |
-
if (R.t < 0) { const n = Math.min(3, Math.ceil(-R.t)); $("gCount").textContent = n; $("gCount").hidden = false; if (n < R.lastBeep) { R.lastBeep = n; sound.pop(); } }
|
| 196 |
-
else if (R.t < .8) { $("gCount").textContent = "GO!"; $("gCount").hidden = false; if (R.lastBeep > 0) { R.lastBeep = 0; sound.win(); } }
|
| 197 |
-
else $("gCount").hidden = true;
|
| 198 |
-
const route = R.route, N = route.pts.length;
|
| 199 |
-
// gates: the next one bright, the one after dim, the rest hidden
|
| 200 |
-
gates.forEach((g, k) => { g.visible = k >= R.cp && k <= R.cp + 1; for (const m of g.userData.parts || []) m.material.color.set(k === R.cp ? 0xffd23d : 0x666666).multiplyScalar(k === R.cp ? 2 : 1); });
|
| 201 |
-
if (R.t > 0 && !R.done) {
|
| 202 |
-
const c = route.pts[R.cp], d = route.air ? Math.hypot(p.x - c[0], p.y - c[1], p.z - c[2]) : Math.hypot(p.x - c[0], p.z - c[2]);
|
| 203 |
-
if (d < (route.air ? (route.custom ? 14 : 11) : 13)) { R.splits[R.cp] = +R.t.toFixed(2); const gs = R.ghost && R.ghost.s && R.ghost.s[R.cp]; if (gs && R.cp < N - 1) { const df = R.t - gs; api.say(`Gate ${R.cp} ${df <= 0 ? "−" : "+"}${Math.abs(df).toFixed(2)} s vs ghost`, 1.6); } R.cp++; sound.pop(); if (R.cp >= N) { R.done = R.t; sound.win(); api.say(`Finished in ${fmt(R.t)}!`, 3); R.endAt = R.t + (R.online ? 15 : 25); } }
|
| 204 |
-
}
|
| 205 |
-
if (R.t > 0 && !R.done && (R.recT += dt) >= .1) { R.recT -= .1; R.rec.push(Math.round(p.x * 10) / 10, Math.round(p.y * 10) / 10, Math.round(p.z * 10) / 10, Math.round(p.yaw * 100) / 100); }
|
| 206 |
-
if (R.ghost) {
|
| 207 |
-
const gp = R.ghost.p, n = gp.length / 4, f = Math.max(0, R.t) * 10, i = Math.min(n - 2, Math.floor(f)), k = Math.min(1, f - i), a = i * 4, b = a + 4;
|
| 208 |
-
if (f >= n - 1) ghost.visible = false;
|
| 209 |
-
else { let dy = gp[b + 3] - gp[a + 3]; if (dy > Math.PI) dy -= 2 * Math.PI; if (dy < -Math.PI) dy += 2 * Math.PI; ghost.position.set(gp[a] + (gp[b] - gp[a]) * k, gp[a + 1] + (gp[b + 1] - gp[a + 1]) * k, gp[a + 2] + (gp[b + 2] - gp[a + 2]) * k); ghost.rotation.y = -(gp[a + 3] + dy * k); }
|
| 210 |
-
}
|
| 211 |
-
// rivals: stepped on a fixed 1/60 s clock from the green light (the same on every player's screen when online)
|
| 212 |
-
if (!route.air && !route.custom && R.t > 0) {
|
| 213 |
-
R.acc += dt;
|
| 214 |
-
while (R.acc >= 1 / 60 && R.bt < R.t) {
|
| 215 |
-
R.acc -= 1 / 60; R.bt += 1 / 60;
|
| 216 |
-
for (const r of rivals) {
|
| 217 |
-
if (!r.g.visible || r.done) continue;
|
| 218 |
-
const c = route.pts[Math.min(N - 1, r.cp)];
|
| 219 |
-
let band = 1;
|
| 220 |
-
if (!R.online) { const mine = progOf(p.x, 0, p.z, R.cp, route), theirs = progOf(r.x, 0, r.z, r.cp, route); band = mine - theirs > .8 ? 1.12 : theirs - mine > .8 ? .9 : 1; }
|
| 221 |
-
// brake for the corner at the next gate, and get back on the road if it ever ends up stuck
|
| 222 |
-
const prev = route.pts[Math.max(0, r.cp - 1)], nxt = route.pts[Math.min(N - 1, r.cp + 1)];
|
| 223 |
-
const a1 = Math.atan2(c[2] - prev[2], c[0] - prev[0]), a2 = Math.atan2(nxt[2] - c[2], nxt[0] - c[0]);
|
| 224 |
-
let turnA = Math.abs(a2 - a1); if (turnA > Math.PI) turnA = 2 * Math.PI - turnA;
|
| 225 |
-
const dc = Math.hypot(c[0] - r.x, c[2] - r.z), top = 62 * r.skill * band;
|
| 226 |
-
const want = turnA > .5 && r.cp < N - 1 ? Math.min(top, 13 + Math.max(0, dc - 8) * .9) : top;
|
| 227 |
-
D.step(r, c[0], c[2], want, 1 / 60, R.rng);
|
| 228 |
-
if (r.stuck > 1.2) {
|
| 229 |
-
const dx = c[0] - prev[0], dz = c[2] - prev[2], L2 = dx * dx + dz * dz || 1, t = Math.max(0, Math.min(1, ((r.x - prev[0]) * dx + (r.z - prev[2]) * dz) / L2));
|
| 230 |
-
r.x = prev[0] + dx * t; r.z = prev[2] + dz * t; r.yaw = Math.atan2(c[2] - r.z, c[0] - r.x); r.v = 6; r.stuck = 0;
|
| 231 |
-
}
|
| 232 |
-
if (Math.hypot(r.x - c[0], r.z - c[2]) < 12) { r.cp++; if (r.cp >= N) r.done = R.bt; }
|
| 233 |
-
}
|
| 234 |
-
}
|
| 235 |
-
}
|
| 236 |
-
for (const r of rivals) {
|
| 237 |
-
if (!r.g.visible) continue;
|
| 238 |
-
r.g.position.set(r.x, 0, r.z); r.g.rotation.y = -r.yaw;
|
| 239 |
-
// they can't drive through you (you get pushed; they keep their line so every screen agrees)
|
| 240 |
-
const dx = p.x - r.x, dz = p.z - r.z, dd = Math.hypot(dx, dz);
|
| 241 |
-
if (dd < 3.4 && dd > .01 && p.y < 2) { p.x += dx / dd * (3.4 - dd); p.z += dz / dd * (3.4 - dd); if (!R.online) r.v *= .9; }
|
| 242 |
-
}
|
| 243 |
-
const st = standings(), pos = st.findIndex(s => s.me) + 1;
|
| 244 |
-
// (the banner is built once per race and only its text changes, so the Quit button stays clickable)
|
| 245 |
-
if (!$("gRaceT")) { $("gRace").innerHTML = `<b id="gRaceN"></b><span id="gRaceT"></span><span id="gRaceG"></span><span id="gRaceP"></span><button class="pill" id="gRaceQuit2">Quit</button>`; $("gRaceQuit2").onclick = () => R && finish("You left the race"); }
|
| 246 |
-
$("gRaceN").textContent = route.name; $("gRaceT").textContent = R.t < 0 ? "Get ready" : fmt(Math.max(0, R.done || R.t)); $("gRaceG").textContent = `Gate ${Math.min(R.cp, N - 1)}/${N - 1}`; $("gRaceP").textContent = `${pos}/${st.length}`;
|
| 247 |
-
const next = route.pts[Math.min(N - 1, R.cp)]; R.next = next;
|
| 248 |
-
if (R.done && (R.t > R.endAt || (!R.online && rivals.every(r => r.done || !r.g.visible)))) { finish(); return; }
|
| 249 |
-
if (R && R.t > 600) finish("Out of time");
|
| 250 |
-
},
|
| 251 |
-
cancel() { if (R) finish("Race cancelled"); },
|
| 252 |
-
};
|
| 253 |
-
}
|
|
|
|
| 1 |
+
// Street races round VOID City: drive into a start marker and press Enter (or tap Race). Gates on the real streets,
|
| 2 |
+
// three AI rivals in sports cars (solo), or everybody in your online room. A countdown, a timer, live positions,
|
| 3 |
+
// best times kept on this device. The Sky Rings race is a flying time trial through gates in the air.
|
| 4 |
+
// Ghosts: your best run on every route is recorded and races beside you next time, with a split at every gate.
|
| 5 |
+
// Your own routes: drop gates while you drive or fly, save them, race them against your ghost, share them as a link.
|
| 6 |
+
import * as THREE from "three";
|
| 7 |
+
import { GRID } from "./city.js";
|
| 8 |
+
import { KINDS, vehicle } from "./npc.js";
|
| 9 |
+
import { makeDriver } from "./police.js";
|
| 10 |
+
|
| 11 |
+
const G = (i, j) => [GRID.ax(i), 0, GRID.sz(j)];
|
| 12 |
+
export const ROUTES = [
|
| 13 |
+
{ id: "midtown", name: "Midtown Sprint", pts: [G(7, 8), G(7, 4), G(9, 4), G(9, 1), G(5, 1), G(5, 6), G(8, 6)] },
|
| 14 |
+
{ id: "park", name: "Park Loop ×2", pts: [G(2, 12), G(5, 12), G(5, 18), G(2, 18), G(2, 12), G(5, 12), G(5, 18), G(2, 18), G(2, 13)] },
|
| 15 |
+
{ id: "bridge", name: "Bridge Run", pts: [G(7, 10), G(12, 10), [1400, 0, 0], [1690, 0, 0]] },
|
| 16 |
+
{ id: "downtown", name: "Downtown Dash", pts: [G(8, 13), G(12, 13), G(12, 19), G(9, 19), G(9, 15), G(11, 15)] },
|
| 17 |
+
{ id: "drag", name: "Runway Drag", pts: [[-2600, 0, 0], [-2350, 0, 0], [-2100, 0, 0], [-1960, 0, 0]] },
|
| 18 |
+
{ id: "sky", name: "Sky Rings", air: true, pts: [[420, 0, -40], [420, 90, -160], [520, 120, -300], [430, 150, -450], [250, 170, -520], [60, 150, -460], [-60, 120, -330], [-120, 90, -180], [-260, 70, 60], [-400, 60, 250]] },
|
| 19 |
+
];
|
| 20 |
+
const RIVALS = [["VIPER", 0xd21f2b], ["BLAZE", 0xff6a00], ["NOVA", 0x1f6fe0]];
|
| 21 |
+
const fmt = t => `${Math.floor(t / 60)}:${(t % 60).toFixed(1).padStart(4, "0")}`;
|
| 22 |
+
const store = { get(k) { try { return JSON.parse(localStorage.getItem(k)); } catch { return null; } }, set(k, v) { try { localStorage.setItem(k, JSON.stringify(v)); } catch { } } };
|
| 23 |
+
|
| 24 |
+
export function createRaces(scene, api, sound, canvasTex, net) {
|
| 25 |
+
const $ = id => document.getElementById(id);
|
| 26 |
+
const D = makeDriver(api);
|
| 27 |
+
const label = (text, col) => { const t = canvasTex(512, 128, (g, w, h) => { g.fillStyle = "rgba(0,0,0,.6)"; g.fillRect(0, 0, w, h); g.strokeStyle = col; g.lineWidth = 6; g.strokeRect(4, 4, w - 8, h - 8); g.fillStyle = col; g.font = "900 54px Unbounded, Impact, sans-serif"; g.textAlign = "center"; g.fillText(text, w / 2, 84); }); return new THREE.Sprite(new THREE.SpriteMaterial({ map: t, toneMapped: false, depthTest: true })); };
|
| 28 |
+
// start markers: a column of light and a floating name
|
| 29 |
+
const custom = (store.get("void.routes") || []).filter(r => r && Array.isArray(r.pts) && r.pts.length >= 3);
|
| 30 |
+
const routes = [...ROUTES, ...custom];
|
| 31 |
+
const makeStart = r => {
|
| 32 |
+
const [x, , z] = r.pts[0], [nx, , nz] = r.pts[1], back = Math.atan2(nz - z, nx - x);
|
| 33 |
+
const sx = x - Math.cos(back) * 35, sz = z - Math.sin(back) * 35;
|
| 34 |
+
const col = r.custom ? "#c07dff" : r.air ? "#3dfcff" : "#ffd23d";
|
| 35 |
+
const pillar = new THREE.Mesh(new THREE.CylinderGeometry(6, 6, 60, 32, 1, true).translate(0, 30, 0), new THREE.MeshBasicMaterial({ color: new THREE.Color(col).multiplyScalar(1.6), transparent: true, opacity: .12, blending: THREE.AdditiveBlending, depthWrite: false, side: THREE.DoubleSide, toneMapped: false }));
|
| 36 |
+
pillar.position.set(sx, 0, sz); scene.add(pillar);
|
| 37 |
+
const ring = new THREE.Mesh(new THREE.RingGeometry(5.4, 6, 48).rotateX(-Math.PI / 2), new THREE.MeshBasicMaterial({ color: new THREE.Color(col).multiplyScalar(2), toneMapped: false })); ring.position.set(sx, .15, sz); scene.add(ring);
|
| 38 |
+
const s = label("🏁 " + r.name.toUpperCase(), col); s.scale.set(16, 4, 1); s.position.set(sx, 9, sz); scene.add(s);
|
| 39 |
+
return { r, x: sx, z: sz, yaw: back, pillar, ring, s };
|
| 40 |
+
};
|
| 41 |
+
const starts = routes.map(makeStart);
|
| 42 |
+
// gates: two posts and a banner across the road
|
| 43 |
+
const gateMat = new THREE.MeshBasicMaterial({ color: new THREE.Color(0xffd23d).multiplyScalar(2), toneMapped: false }), dimMat = new THREE.MeshBasicMaterial({ color: 0x555555 });
|
| 44 |
+
const checker = canvasTex(256, 32, (g, w, h) => { for (let x = 0; x < w; x += 16) for (let y = 0; y < h; y += 16) { g.fillStyle = ((x + y) / 16) % 2 ? "#111" : "#fff"; g.fillRect(x, y, 16, 16); } });
|
| 45 |
+
const gates = [];
|
| 46 |
+
function buildGates(r) {
|
| 47 |
+
clearGates();
|
| 48 |
+
r.pts.forEach((p, k) => {
|
| 49 |
+
const q = r.pts[Math.max(0, k - 1)], o = r.pts[Math.min(r.pts.length - 1, k + 1)], dir = k ? Math.atan2(p[2] - q[2], p[0] - q[0]) : Math.atan2(o[2] - p[2], o[0] - p[0]);
|
| 50 |
+
const g = new THREE.Group(); g.position.set(p[0], p[1], p[2]); g.rotation.y = -dir;
|
| 51 |
+
const fin = k === r.pts.length - 1;
|
| 52 |
+
if (r.air) { const ring = new THREE.Mesh(new THREE.TorusGeometry(10, .7, 10, 40), gateMat.clone()); ring.rotation.y = Math.PI / 2; g.add(ring); g.userData.parts = [ring]; }
|
| 53 |
+
else {
|
| 54 |
+
const W = 11, parts = [];
|
| 55 |
+
for (const s of [-1, 1]) { const post = new THREE.Mesh(new THREE.BoxGeometry(.6, 7, .6).translate(0, 3.5, 0), gateMat.clone()); post.position.z = s * W; g.add(post); parts.push(post); }
|
| 56 |
+
const banner = new THREE.Mesh(new THREE.BoxGeometry(.3, 1.4, W * 2 + .6), fin ? new THREE.MeshBasicMaterial({ map: checker, toneMapped: false }) : gateMat.clone()); banner.position.y = 7; g.add(banner); if (!fin) parts.push(banner);
|
| 57 |
+
g.userData.parts = parts;
|
| 58 |
+
}
|
| 59 |
+
scene.add(g); gates.push(g);
|
| 60 |
+
});
|
| 61 |
+
}
|
| 62 |
+
function clearGates() { for (const g of gates) scene.remove(g); gates.length = 0; }
|
| 63 |
+
// rivals: sports cars with their own colours
|
| 64 |
+
const sport = KINDS.find(k => k.id === "sports"), sg = vehicle(sport);
|
| 65 |
+
const rivals = RIVALS.map(([name, col]) => {
|
| 66 |
+
const g = new THREE.Group(); g.add(new THREE.Mesh(sg.body, new THREE.MeshStandardMaterial({ vertexColors: true, color: col, roughness: .25, metalness: .6 })), new THREE.Mesh(sg.lights, new THREE.MeshBasicMaterial({ vertexColors: true, toneMapped: false, color: new THREE.Color(2, 2, 2) })));
|
| 67 |
+
const tag = label(name, "#" + col.toString(16).padStart(6, "0")); tag.scale.set(5, 1.25, 1); tag.position.y = 3.4; g.add(tag);
|
| 68 |
+
g.visible = false; scene.add(g); return { name, g, x: 0, z: 0, yaw: 0, v: 0, cp: 1, done: 0, skill: 1 };
|
| 69 |
+
});
|
| 70 |
+
|
| 71 |
+
// the ghost: a see-through copy of your best run on this route
|
| 72 |
+
const ghost = new THREE.Group(); ghost.add(new THREE.Mesh(sg.body, new THREE.MeshBasicMaterial({ color: new THREE.Color(.3, 1.6, 2), transparent: true, opacity: .28, depthWrite: false, toneMapped: false })));
|
| 73 |
+
{ const gt = label("GHOST", "#3dfcff"); gt.scale.set(5, 1.25, 1); gt.position.y = 3.4; ghost.add(gt); } ghost.visible = false; scene.add(ghost);
|
| 74 |
+
const ghostOf = id => { const gh = store.get("void.ghost." + id); return gh && Array.isArray(gh.p) && gh.p.length >= 8 ? gh : null; };
|
| 75 |
+
let R = null; // the race in progress
|
| 76 |
+
function begin(route, opts = {}) {
|
| 77 |
+
const p = api.state; if (!p) return;
|
| 78 |
+
api.policeClear && api.policeClear();
|
| 79 |
+
const st = starts.find(s => s.r === route);
|
| 80 |
+
buildGates(route);
|
| 81 |
+
const slot = opts.slot ?? 0, side = [0, 1, -1, 2, -2, 3, -3][slot % 7] * 4.2, row = Math.floor(slot / 7) * 8;
|
| 82 |
+
const fx = Math.cos(st.yaw), fz = Math.sin(st.yaw);
|
| 83 |
+
api.teleport(st.x + fx * (20 - row) - fz * side, st.z + fz * (20 - row) + fx * side, st.yaw);
|
| 84 |
+
api.freeze(opts.wait ?? 3.2);
|
| 85 |
+
// bots: in an online race every player runs the same bots from the same seed on a fixed clock, so they match
|
| 86 |
+
let sd = (opts.seed || Math.floor(Math.random() * 1e9)) % 2147483647 || 7; const rng = () => (sd = (sd * 16807) % 2147483647) / 2147483647;
|
| 87 |
+
R = { route, t: -(opts.wait ?? 3.2), cp: 1, done: 0, online: !!opts.online, results: [], lastBeep: 4, rng, acc: 0, bt: 0, rec: [], recT: 0, splits: [], ghost: ghostOf(route.id) };
|
| 88 |
+
ghost.visible = !!R.ghost;
|
| 89 |
+
rivals.forEach((r, k) => {
|
| 90 |
+
r.g.visible = !route.air && !route.custom && (!R.online || opts.bots !== false);
|
| 91 |
+
const sd = [-1, 1, 2][k] * 4.2, rw = k === 2 ? 8 : 0;
|
| 92 |
+
Object.assign(r, { x: st.x + fx * (20 - rw) - fz * sd, z: st.z + fz * (20 - rw) + fx * sd, yaw: st.yaw, v: 0, cp: 1, done: 0, skill: .9 + rng() * .16, accel: 13, stuck: 0 });
|
| 93 |
+
});
|
| 94 |
+
$("gRace").hidden = false; $("gRaceRes").hidden = true;
|
| 95 |
+
}
|
| 96 |
+
function finish(msg) {
|
| 97 |
+
const p = api.state, best = store.get("void.race." + R.route.id);
|
| 98 |
+
const me = R.done, rank = standings().findIndex(s => s.me) + 1;
|
| 99 |
+
if (me && (!best || me < best)) { store.set("void.race." + R.route.id, me); if (R.rec.length >= 8) store.set("void.ghost." + R.route.id, { t: me, s: R.splits, p: R.rec.slice(0, 12000) }); }
|
| 100 |
+
const gdiff = me && R.ghost ? me - R.ghost.t : null;
|
| 101 |
+
const reward = me ? [0, 1500, 800, 400, 200][Math.min(4, rank)] || 100 : 0;
|
| 102 |
+
if (reward) api.addScore(reward, `${R.route.name}: ${["", "1st", "2nd", "3rd"][rank] || rank + "th"}`);
|
| 103 |
+
$("gRaceRes").innerHTML = `<h3>${R.route.name}</h3><p>${msg || (me ? `You finished ${["", "1st", "2nd", "3rd"][rank] || rank + "th"} in ${fmt(me)}` : "Race over")}</p><p class="small">${best ? `Best: ${fmt(Math.min(best, me || 1e9))}` : me ? "New best time!" : ""}${gdiff != null ? ` · vs ghost ${gdiff <= 0 ? "−" : "+"}${Math.abs(gdiff).toFixed(2)} s${gdiff < 0 ? " (new ghost saved)" : ""}` : me && !R.ghost ? " · ghost saved" : ""}</p><ol>${standings().map(s => `<li${s.me ? ' class="me"' : ""}>${s.name}<span>${s.done ? fmt(s.done) : "DNF"}</span></li>`).join("")}</ol><button class="pill solid" id="gRaceOk">OK</button>`;
|
| 104 |
+
$("gRaceRes").hidden = false; $("gRaceOk").onclick = () => { $("gRaceRes").hidden = true; };
|
| 105 |
+
$("gRace").hidden = true; clearGates(); rivals.forEach(r => r.g.visible = false); ghost.visible = false; R = null; void p;
|
| 106 |
+
}
|
| 107 |
+
// everyone's progress: checkpoints passed plus how far along to the next
|
| 108 |
+
const progOf = (x, y, z, cp, route) => { const a = route.pts[cp - 1], b = route.pts[Math.min(route.pts.length - 1, cp)]; const L = Math.hypot(b[0] - a[0], b[2] - a[2]) || 1, d = Math.hypot(b[0] - x, b[2] - z); return cp + Math.max(0, 1 - d / L); };
|
| 109 |
+
function standings() {
|
| 110 |
+
if (!R) return [];
|
| 111 |
+
const p = api.state, out = [{ name: "YOU", me: true, prog: R.done ? 1e6 - R.done : progOf(p.x, p.y, p.z, R.cp, R.route), done: R.done }];
|
| 112 |
+
for (const r of rivals) if (r.g.visible) out.push({ name: r.name, prog: r.done ? 1e6 - r.done : progOf(r.x, 0, r.z, r.cp, R.route), done: r.done });
|
| 113 |
+
if (R.online && net) for (const q of net.players()) if (q.race === R.route.id) out.push({ name: q.name, prog: q.rf ? 1e6 - q.rf : q.rp || 0, done: q.rf || 0 });
|
| 114 |
+
return out.sort((a, b) => b.prog - a.prog);
|
| 115 |
+
}
|
| 116 |
+
function nearStart() { const p = api.state; if (!p || R || B) return null; return starts.find(s => Math.hypot(p.x - s.x, p.z - s.z) < 14) || null; }
|
| 117 |
+
addEventListener("keydown", e => { if (e.code === "Enter" && api.active()) { const s = nearStart(); if (s) start(s.r); } });
|
| 118 |
+
$("gRaceGo").onclick = () => { const s = nearStart(); if (s) start(s.r); };
|
| 119 |
+
function start(route) {
|
| 120 |
+
if (net && net.online() && !route.custom) { const seed = Math.floor(Math.random() * 1e9); net.send({ t: "race", id: route.id, wait: 6, seed }); begin(route, { online: true, wait: 6, slot: 0, seed }); }
|
| 121 |
+
else begin(route);
|
| 122 |
+
}
|
| 123 |
+
// ---- the route builder ----
|
| 124 |
+
let B = null;
|
| 125 |
+
const saveCustom = () => store.set("void.routes", custom.map(({ id, name, air, pts }) => ({ id, name, air, pts, custom: true })));
|
| 126 |
+
function addRoute(r) { r.custom = true; custom.push(r); routes.push(r); starts.push(makeStart(r)); while (custom.length > 12) removeRoute(custom[0].id); saveCustom(); return r; }
|
| 127 |
+
function removeRoute(id) {
|
| 128 |
+
const k = custom.findIndex(r => r.id === id); if (k < 0) return; const r = custom[k]; custom.splice(k, 1); routes.splice(routes.indexOf(r), 1);
|
| 129 |
+
const s = starts.findIndex(st => st.r === r); if (s >= 0) { const st = starts[s]; scene.remove(st.pillar, st.ring, st.s); starts.splice(s, 1); }
|
| 130 |
+
try { localStorage.removeItem("void.ghost." + id); localStorage.removeItem("void.race." + id); } catch { } saveCustom();
|
| 131 |
+
}
|
| 132 |
+
const buildMarks = [];
|
| 133 |
+
function builder(on) {
|
| 134 |
+
if (on && R) return api.say("Finish the race first", 2);
|
| 135 |
+
for (const m of buildMarks) scene.remove(m); buildMarks.length = 0;
|
| 136 |
+
B = on ? { pts: [] } : null; $("gBuild").hidden = !on; if (on) { dropGate(); api.say("Route builder: drive or fly your route, press B (or the button) to drop each gate", 4); }
|
| 137 |
+
renderBuild();
|
| 138 |
+
}
|
| 139 |
+
function dropGate() {
|
| 140 |
+
const p = api.state; if (!B || !p) return;
|
| 141 |
+
const pt = [Math.round(p.x), p.mode === "air" && p.y > 6 ? Math.round(p.y) : 0, Math.round(p.z)], last = B.pts[B.pts.length - 1];
|
| 142 |
+
if (last && Math.hypot(pt[0] - last[0], pt[1] - last[1], pt[2] - last[2]) < 30) return api.say("Too close to the last gate (30 m)", 1.5);
|
| 143 |
+
B.pts.push(pt); sound.pop();
|
| 144 |
+
const m = new THREE.Mesh(new THREE.TorusGeometry(pt[1] ? 10 : 6, .5, 8, 32), new THREE.MeshBasicMaterial({ color: new THREE.Color(0xc07dff).multiplyScalar(2), toneMapped: false })); m.position.set(pt[0], pt[1] || 6, pt[2]); m.rotation.y = -p.yaw + Math.PI / 2; scene.add(m); buildMarks.push(m);
|
| 145 |
+
renderBuild();
|
| 146 |
+
}
|
| 147 |
+
function saveBuild() {
|
| 148 |
+
if (!B || B.pts.length < 3) return api.say("A route needs a start and at least 2 more gates", 2);
|
| 149 |
+
const r = addRoute({ id: "c" + Date.now().toString(36), name: "My route " + (custom.length + 1), air: B.pts.some(q => q[1] > 0), pts: B.pts });
|
| 150 |
+
builder(false); api.say(`Saved "${r.name}" and copied its link. The purple marker is its start`, 3.5); renderList(); share(r, true);
|
| 151 |
+
}
|
| 152 |
+
function renderBuild() { if (!B) return; $("gBuild").innerHTML = `<b>🛠 Route builder</b><span>${B.pts.length === 1 ? "Start set" : `${B.pts.length - 1} gate${B.pts.length > 2 ? "s" : ""}`}</span><button class="pill" id="gBDrop">Drop gate (B)</button><button class="pill solid" id="gBSave">Save</button><button class="pill" id="gBCancel">Cancel</button>`; $("gBDrop").onclick = dropGate; $("gBSave").onclick = saveBuild; $("gBCancel").onclick = () => builder(false); }
|
| 153 |
+
addEventListener("keydown", e => { if (e.code === "KeyB" && B && api.active() && !e.repeat) dropGate(); });
|
| 154 |
+
// share a route as a link: everything is in the address, nothing is uploaded
|
| 155 |
+
const b64 = o => btoa(unescape(encodeURIComponent(JSON.stringify(o)))).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
| 156 |
+
async function share(r, quiet) {
|
| 157 |
+
const url = location.origin + location.pathname + "#r=" + b64({ n: r.name, p: r.pts });
|
| 158 |
+
try { await navigator.clipboard.writeText(url); if (!quiet) api.say("Route link copied: send it to a friend", 2.5); } catch { if (!quiet) prompt("Copy this route link", url); }
|
| 159 |
+
}
|
| 160 |
+
try {
|
| 161 |
+
const h = new URLSearchParams(location.hash.slice(1)).get("r");
|
| 162 |
+
if (h) {
|
| 163 |
+
const o = JSON.parse(decodeURIComponent(escape(atob(h.replace(/-/g, "+").replace(/_/g, "/")))));
|
| 164 |
+
const pts = (o.p || []).slice(0, 40).map(q => [+q[0] || 0, Math.max(0, Math.min(400, +q[1] || 0)), +q[2] || 0]).filter(q => q.every(Number.isFinite) && Math.abs(q[0]) < 4000 && Math.abs(q[2]) < 4000);
|
| 165 |
+
if (pts.length >= 3 && !custom.some(r => JSON.stringify(r.pts) === JSON.stringify(pts))) addRoute({ id: "c" + Date.now().toString(36), name: String(o.n || "Shared route").replace(/[<>&"]/g, "").slice(0, 28), air: pts.some(q => q[1] > 0), pts });
|
| 166 |
+
history.replaceState(null, "", location.pathname + location.search);
|
| 167 |
+
}
|
| 168 |
+
} catch { }
|
| 169 |
+
// the Routes list in the Menu: best time, ghost, go there, share, delete
|
| 170 |
+
function renderList() {
|
| 171 |
+
const el = $("gRoutes"); if (!el) return;
|
| 172 |
+
el.innerHTML = routes.map(r => { const b = store.get("void.race." + r.id), gh = !!ghostOf(r.id); return `<div class="route${r.custom ? " mine" : ""}"><div><span>${r.air ? "✈️" : "🏁"} ${r.name}</span><small>${b ? `Best ${fmt(b)}` : "No time yet"}${gh ? " · 👻 ghost" : ""}${r.custom ? " · yours" : ""}</small></div><button class="pill" data-rgo="${r.id}">Go</button>${r.custom ? `<button class="pill" data-share="${r.id}">Share</button><button class="pill" data-del="${r.id}">✕</button>` : ""}</div>`; }).join("") + `<button class="tile wide-tile" id="gBuildBtn"><b>🛠</b>Make your own route</button>`;
|
| 173 |
+
}
|
| 174 |
+
document.addEventListener("click", e => {
|
| 175 |
+
const t = e.target.closest("[data-rgo],[data-share],[data-del],#gBuildBtn"); if (!t || !t.closest("#gRoutes")) return;
|
| 176 |
+
if (t.id === "gBuildBtn") return builder(true);
|
| 177 |
+
const r = routes.find(x => x.id === (t.dataset.rgo || t.dataset.share || t.dataset.del)); if (!r) return;
|
| 178 |
+
if (t.dataset.rgo) { const st = starts.find(s => s.r === r); if (st && !R) { api.teleport(st.x, st.z, st.yaw); api.say(`${r.name}: press Enter (or Start) at the marker`, 3); } }
|
| 179 |
+
if (t.dataset.share) share(r);
|
| 180 |
+
if (t.dataset.del && confirm(`Delete "${r.name}" and its ghost?`)) { removeRoute(r.id); renderList(); }
|
| 181 |
+
});
|
| 182 |
+
if (net) net.on("race", (m, from) => { const r = ROUTES.find(x => x.id === m.id); if (r && !R && !B) { begin(r, { online: true, wait: Math.max(2, Math.min(10, +m.wait || 6)) - .3, seed: Math.abs(m.seed | 0) || 7, slot: 1 + (net.players().findIndex(q => q.id === from) + 1) % 6 }); api.say(`${net.nameOf(from)} started ${r.name}. Get ready!`, 3); } });
|
| 183 |
+
|
| 184 |
+
return {
|
| 185 |
+
get race() { return R; }, rivals, renderList, get building() { return !!B; },
|
| 186 |
+
progress() { return R ? { race: R.route.id, rp: R.done ? 0 : progOf(api.state.x, api.state.y, api.state.z, R.cp, R.route), rf: R.done } : null; },
|
| 187 |
+
starts,
|
| 188 |
+
update(dt, time) {
|
| 189 |
+
const p = api.state;
|
| 190 |
+
for (const s of starts) { s.pillar.material.opacity = .1 + .05 * Math.sin(time * 3); s.s.visible = s.ring.visible = s.pillar.visible = !R; }
|
| 191 |
+
const ns = nearStart(); $("gRaceGo").hidden = !ns; if (ns) $("gRaceGo").textContent = `Start ${ns.r.name} (Enter)`;
|
| 192 |
+
if (!R || !p) return;
|
| 193 |
+
R.t += dt;
|
| 194 |
+
// countdown
|
| 195 |
+
if (R.t < 0) { const n = Math.min(3, Math.ceil(-R.t)); $("gCount").textContent = n; $("gCount").hidden = false; if (n < R.lastBeep) { R.lastBeep = n; sound.pop(); } }
|
| 196 |
+
else if (R.t < .8) { $("gCount").textContent = "GO!"; $("gCount").hidden = false; if (R.lastBeep > 0) { R.lastBeep = 0; sound.win(); } }
|
| 197 |
+
else $("gCount").hidden = true;
|
| 198 |
+
const route = R.route, N = route.pts.length;
|
| 199 |
+
// gates: the next one bright, the one after dim, the rest hidden
|
| 200 |
+
gates.forEach((g, k) => { g.visible = k >= R.cp && k <= R.cp + 1; for (const m of g.userData.parts || []) m.material.color.set(k === R.cp ? 0xffd23d : 0x666666).multiplyScalar(k === R.cp ? 2 : 1); });
|
| 201 |
+
if (R.t > 0 && !R.done) {
|
| 202 |
+
const c = route.pts[R.cp], d = route.air ? Math.hypot(p.x - c[0], p.y - c[1], p.z - c[2]) : Math.hypot(p.x - c[0], p.z - c[2]);
|
| 203 |
+
if (d < (route.air ? (route.custom ? 14 : 11) : 13)) { R.splits[R.cp] = +R.t.toFixed(2); const gs = R.ghost && R.ghost.s && R.ghost.s[R.cp]; if (gs && R.cp < N - 1) { const df = R.t - gs; api.say(`Gate ${R.cp} ${df <= 0 ? "−" : "+"}${Math.abs(df).toFixed(2)} s vs ghost`, 1.6); } R.cp++; sound.pop(); if (R.cp >= N) { R.done = R.t; sound.win(); api.say(`Finished in ${fmt(R.t)}!`, 3); R.endAt = R.t + (R.online ? 15 : 25); } }
|
| 204 |
+
}
|
| 205 |
+
if (R.t > 0 && !R.done && (R.recT += dt) >= .1) { R.recT -= .1; R.rec.push(Math.round(p.x * 10) / 10, Math.round(p.y * 10) / 10, Math.round(p.z * 10) / 10, Math.round(p.yaw * 100) / 100); }
|
| 206 |
+
if (R.ghost) {
|
| 207 |
+
const gp = R.ghost.p, n = gp.length / 4, f = Math.max(0, R.t) * 10, i = Math.min(n - 2, Math.floor(f)), k = Math.min(1, f - i), a = i * 4, b = a + 4;
|
| 208 |
+
if (f >= n - 1) ghost.visible = false;
|
| 209 |
+
else { let dy = gp[b + 3] - gp[a + 3]; if (dy > Math.PI) dy -= 2 * Math.PI; if (dy < -Math.PI) dy += 2 * Math.PI; ghost.position.set(gp[a] + (gp[b] - gp[a]) * k, gp[a + 1] + (gp[b + 1] - gp[a + 1]) * k, gp[a + 2] + (gp[b + 2] - gp[a + 2]) * k); ghost.rotation.y = -(gp[a + 3] + dy * k); }
|
| 210 |
+
}
|
| 211 |
+
// rivals: stepped on a fixed 1/60 s clock from the green light (the same on every player's screen when online)
|
| 212 |
+
if (!route.air && !route.custom && R.t > 0) {
|
| 213 |
+
R.acc += dt;
|
| 214 |
+
while (R.acc >= 1 / 60 && R.bt < R.t) {
|
| 215 |
+
R.acc -= 1 / 60; R.bt += 1 / 60;
|
| 216 |
+
for (const r of rivals) {
|
| 217 |
+
if (!r.g.visible || r.done) continue;
|
| 218 |
+
const c = route.pts[Math.min(N - 1, r.cp)];
|
| 219 |
+
let band = 1;
|
| 220 |
+
if (!R.online) { const mine = progOf(p.x, 0, p.z, R.cp, route), theirs = progOf(r.x, 0, r.z, r.cp, route); band = mine - theirs > .8 ? 1.12 : theirs - mine > .8 ? .9 : 1; }
|
| 221 |
+
// brake for the corner at the next gate, and get back on the road if it ever ends up stuck
|
| 222 |
+
const prev = route.pts[Math.max(0, r.cp - 1)], nxt = route.pts[Math.min(N - 1, r.cp + 1)];
|
| 223 |
+
const a1 = Math.atan2(c[2] - prev[2], c[0] - prev[0]), a2 = Math.atan2(nxt[2] - c[2], nxt[0] - c[0]);
|
| 224 |
+
let turnA = Math.abs(a2 - a1); if (turnA > Math.PI) turnA = 2 * Math.PI - turnA;
|
| 225 |
+
const dc = Math.hypot(c[0] - r.x, c[2] - r.z), top = 62 * r.skill * band;
|
| 226 |
+
const want = turnA > .5 && r.cp < N - 1 ? Math.min(top, 13 + Math.max(0, dc - 8) * .9) : top;
|
| 227 |
+
D.step(r, c[0], c[2], want, 1 / 60, R.rng);
|
| 228 |
+
if (r.stuck > 1.2) {
|
| 229 |
+
const dx = c[0] - prev[0], dz = c[2] - prev[2], L2 = dx * dx + dz * dz || 1, t = Math.max(0, Math.min(1, ((r.x - prev[0]) * dx + (r.z - prev[2]) * dz) / L2));
|
| 230 |
+
r.x = prev[0] + dx * t; r.z = prev[2] + dz * t; r.yaw = Math.atan2(c[2] - r.z, c[0] - r.x); r.v = 6; r.stuck = 0;
|
| 231 |
+
}
|
| 232 |
+
if (Math.hypot(r.x - c[0], r.z - c[2]) < 12) { r.cp++; if (r.cp >= N) r.done = R.bt; }
|
| 233 |
+
}
|
| 234 |
+
}
|
| 235 |
+
}
|
| 236 |
+
for (const r of rivals) {
|
| 237 |
+
if (!r.g.visible) continue;
|
| 238 |
+
r.g.position.set(r.x, 0, r.z); r.g.rotation.y = -r.yaw;
|
| 239 |
+
// they can't drive through you (you get pushed; they keep their line so every screen agrees)
|
| 240 |
+
const dx = p.x - r.x, dz = p.z - r.z, dd = Math.hypot(dx, dz);
|
| 241 |
+
if (dd < 3.4 && dd > .01 && p.y < 2) { p.x += dx / dd * (3.4 - dd); p.z += dz / dd * (3.4 - dd); if (!R.online) r.v *= .9; }
|
| 242 |
+
}
|
| 243 |
+
const st = standings(), pos = st.findIndex(s => s.me) + 1;
|
| 244 |
+
// (the banner is built once per race and only its text changes, so the Quit button stays clickable)
|
| 245 |
+
if (!$("gRaceT")) { $("gRace").innerHTML = `<b id="gRaceN"></b><span id="gRaceT"></span><span id="gRaceG"></span><span id="gRaceP"></span><button class="pill" id="gRaceQuit2">Quit</button>`; $("gRaceQuit2").onclick = () => R && finish("You left the race"); }
|
| 246 |
+
$("gRaceN").textContent = route.name; $("gRaceT").textContent = R.t < 0 ? "Get ready" : fmt(Math.max(0, R.done || R.t)); $("gRaceG").textContent = `Gate ${Math.min(R.cp, N - 1)}/${N - 1}`; $("gRaceP").textContent = `${pos}/${st.length}`;
|
| 247 |
+
const next = route.pts[Math.min(N - 1, R.cp)]; R.next = next;
|
| 248 |
+
if (R.done && (R.t > R.endAt || (!R.online && rivals.every(r => r.done || !r.g.visible)))) { finish(); return; }
|
| 249 |
+
if (R && R.t > 600) finish("Out of time");
|
| 250 |
+
},
|
| 251 |
+
cancel() { if (R) finish("Race cancelled"); },
|
| 252 |
+
};
|
| 253 |
+
}
|
static/style.css
CHANGED
|
@@ -422,3 +422,30 @@ body[data-mode="play"] .hub-chip { display: none !important; }
|
|
| 422 |
.g-build[hidden] { display: none; } .g-build .pill { padding: 6px 12px; font-size: 12px; }
|
| 423 |
.save-row { display: flex; justify-content: space-between; align-items: center; } .save-row span { display: flex; gap: 6px; }
|
| 424 |
@media (max-width: 820px) { .routes { grid-template-columns: minmax(0, 1fr); } }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 422 |
.g-build[hidden] { display: none; } .g-build .pill { padding: 6px 12px; font-size: 12px; }
|
| 423 |
.save-row { display: flex; justify-content: space-between; align-items: center; } .save-row span { display: flex; gap: 6px; }
|
| 424 |
@media (max-width: 820px) { .routes { grid-template-columns: minmax(0, 1fr); } }
|
| 425 |
+
|
| 426 |
+
/* on foot, the dealership, the tutorial, bigger text */
|
| 427 |
+
.g-footbtn { position: absolute; right: 16px; bottom: calc(230px + var(--bot, 0px)); width: 52px; height: 52px; border-radius: 50%; border: 1px solid rgba(255,255,255,.3); background: rgba(0,0,0,.55); color: #fff; font-size: 24px; display: none; pointer-events: auto; }
|
| 428 |
+
@media (pointer: coarse) { .g-footbtn { display: block; } }
|
| 429 |
+
.g-tut { display: flex; align-items: center; gap: 10px; padding: 7px 8px 7px 14px; border-radius: 14px; background: rgba(10,40,20,.85); border: 1px solid rgba(110,255,160,.55); color: #c9ffd9; font: 600 14px var(--body); pointer-events: auto; }
|
| 430 |
+
.g-tut[hidden] { display: none; } .g-tut b { color: #6dff9e; } .g-tut .pill { padding: 5px 10px; font-size: 11px; }
|
| 431 |
+
.g-dealgo { bottom: calc(150px + var(--bot, 0px)); }
|
| 432 |
+
.g-deal { position: absolute; inset: calc(56px + var(--top, 0px)) 16px 16px; z-index: 30; display: flex; flex-direction: column; border-radius: 22px; background: rgba(12,12,15,.94); border: 1px solid rgba(255,210,61,.35); color: #fff; pointer-events: auto; overflow: hidden; backdrop-filter: blur(10px); }
|
| 433 |
+
.g-deal[hidden] { display: none; }
|
| 434 |
+
.deal-head { display: flex; align-items: center; gap: 14px; padding: 16px 20px 8px; } .deal-head h2 { margin: 0; font-size: 22px; flex: 1; } .deal-cash { color: #9dffb0; font: 800 16px var(--body); }
|
| 435 |
+
.deal-filters { display: flex; gap: 6px; padding: 4px 20px 10px; overflow-x: auto; scrollbar-width: thin; }
|
| 436 |
+
.deal-filters button { flex: none; padding: 6px 12px; border-radius: 999px; border: 1px solid rgba(255,255,255,.15); background: transparent; color: var(--mute, #aaa); font: 600 12px var(--body); cursor: pointer; }
|
| 437 |
+
.deal-filters button.on { background: #ffd23d; color: #111; border-color: #ffd23d; }
|
| 438 |
+
.deal-body { flex: 1; display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(260px, 1fr); gap: 14px; padding: 0 20px 18px; min-height: 0; }
|
| 439 |
+
.deal-list { overflow-y: auto; display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 8px; align-content: start; scrollbar-width: thin; }
|
| 440 |
+
.deal-car { display: grid; grid-template-columns: 18px 1fr; gap: 2px 10px; text-align: left; padding: 10px 12px; border-radius: 14px; border: 1px solid rgba(255,255,255,.1); background: rgba(255,255,255,.04); color: #fff; cursor: pointer; }
|
| 441 |
+
.deal-car span { grid-row: span 3; width: 18px; height: 18px; border-radius: 50%; align-self: center; border: 1px solid rgba(255,255,255,.3); }
|
| 442 |
+
.deal-car b { font: 700 13px var(--body); } .deal-car small { color: var(--mute, #aaa); font-size: 11px; } .deal-car em { font: 800 12px var(--body); font-style: normal; color: #ffd23d; }
|
| 443 |
+
.deal-car.on { border-color: #ffd23d; background: rgba(255,210,61,.1); } .deal-car.own em { color: #9dffb0; }
|
| 444 |
+
.deal-info { overflow-y: auto; padding: 14px 16px; border-radius: 16px; background: rgba(255,255,255,.04); border: 1px solid rgba(255,255,255,.08); display: grid; gap: 12px; align-content: start; }
|
| 445 |
+
.deal-info h3 { margin: 0; font-size: 20px; } .deal-info .small { margin: 0; }
|
| 446 |
+
.deal-stats { display: grid; gap: 8px; } .deal-stats label { display: grid; grid-template-columns: 1fr auto; gap: 4px; font: 600 12px var(--body); color: var(--mute, #aaa); }
|
| 447 |
+
.deal-stats span { color: #fff; } .deal-stats i { grid-column: 1 / -1; height: 6px; border-radius: 3px; background: linear-gradient(90deg, #ffd23d calc(var(--v) * 100%), rgba(255,255,255,.12) 0); }
|
| 448 |
+
.deal-cols { display: flex; gap: 8px; flex-wrap: wrap; } .deal-cols button { width: 28px; height: 28px; border-radius: 50%; border: 2px solid rgba(255,255,255,.2); cursor: pointer; } .deal-cols button.on { border-color: #fff; box-shadow: 0 0 0 2px #ffd23d; }
|
| 449 |
+
.deal-go { justify-self: start; } .deal-go:disabled { opacity: .45; cursor: not-allowed; }
|
| 450 |
+
@media (max-width: 820px) { .deal-body { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr) auto; } .deal-info { max-height: 42vh; } .g-deal { inset: 8px; } }
|
| 451 |
+
body.big-ui .g-hint, body.big-ui .g-job, body.big-ui .g-tut, body.big-ui .g-gps { font-size: 17px; } body.big-ui .g-menu { font-size: 16px; } body.big-ui .tile { font-size: 14px; } body.big-ui .goal small, body.big-ui .small { font-size: 14px; }
|