onlinemodus fix failed

This commit is contained in:
2026-11-19 00:55:39 +01:00
parent 64c5b65543
commit 347701d66f
10 changed files with 414 additions and 53 deletions

View File

@@ -163,6 +163,18 @@
// ---------------------------------------------------------------- robot model
const M = {};
const sharedMat = new Set(); // the cached palette materials must never be disposed
// free a replaced robot's GPU resources (unique per build) so browsing parts / many fights does not leak
function disposeRobot(R) {
if (!R || !R.root) return;
const gs = new Set(), ms = new Set();
R.root.traverse(o => {
if (o.geometry && !gs.has(o.geometry)) { gs.add(o.geometry); o.geometry.dispose(); }
if (o.material) for (const m of [].concat(o.material)) {
if (m && !sharedMat.has(m) && !ms.has(m)) { ms.add(m); if (m.map && m.map.isTexture) m.map.dispose(); m.dispose(); }
}
});
}
function mats() {
if (M.dark) return M;
M.dark = new T.MeshStandardMaterial({ color: 0x2a2d33, metalness: 0.6, roughness: 0.5 });
@@ -179,6 +191,7 @@
M.track = new T.MeshStandardMaterial({ map: M.trackTex, roughness: 0.85, metalness: 0.2 });
M.wsteel = new T.MeshStandardMaterial({ color: 0xd2d7de, metalness: 0.95, roughness: 0.22 }); // bright weapon steel
M.wedge = new T.MeshStandardMaterial({ color: 0x9aa0a8, metalness: 0.85, roughness: 0.35 });
for (const k in M) if (M[k] && M[k].isMaterial) sharedMat.add(M[k]); // protect the shared palette from disposal
return M;
}
// shop: the robot is framed in the left column, not in the middle of the stage
@@ -641,6 +654,8 @@
}
mark('gadget');
root.traverse(o => { if (o.isMesh && !o.material.transparent) { o.castShadow = true; } });
// full height (incl. tower/antenna) so a flipped robot rests on its roof, never clips through the floor
const bb = new T.Box3().setFromObject(tilt); out.topY = Math.max(0.3, bb.max.y);
return out;
}
@@ -655,7 +670,7 @@
if (r.y <= 0.05 && Math.abs(R.roll - want) < 0.02) R.roll = want;
R.roll %= Math.PI * 2;
R.tilt.rotation.x = R.roll;
const h = R.c + R.st.h;
const h = R.topY || (R.c + R.st.h); // lift by the real top so nothing sinks under the floor when flipped
R.tilt.position.y = (1 - Math.cos(R.roll)) / 2 * h;
const sp = Math.hypot(r.vx, r.vz) * (Math.cos(r.a) * r.vx + Math.sin(r.a) * r.vz >= 0 ? 1 : -1);
for (const wl of R.wheels) wl.rotation.z -= sp * dt / (R.wheelR || 0.16);
@@ -1066,7 +1081,7 @@
// intro / title: perspective camera flying over the arena
const pcam = new T.PerspectiveCamera(40, asp, 0.1, 120);
let mode = 'garage', gRobot = null, robots = [], state = null, shake = 0, flashT = 0, introT = 0, scale = 1, hold = 0, editSel = 0, introLightK = 0, comfort = true;
let mode = 'garage', gRobot = null, robots = [], state = null, shake = 0, flashT = 0, introT = 0, scale = 1, hold = 0, editSel = 0, introLightK = 0, comfort = true, titleT = 0, garageSig = null;
let quality = 'high', fxMul = 1, camMode = 'iso', chaseInit = false, gcamZoom = GZ_DEF;
placeGarageCam(); // after gcamZoom exists: start fully zoomed out
// workshop: show what the fitted weapon / turret actually does, on a short loop
@@ -1133,7 +1148,10 @@
skipIntro() { if (mode === 'intro' && introT < INTRO_END) introT = INTRO_END; },
showGarage(build, paint) {
mode = 'garage';
if (gRobot) garage.turntable.remove(gRobot.root);
const sig = JSON.stringify(build);
if (gRobot && sig === garageSig) { gRobot.skin.redraw(paint); return; } // same robot -> just repaint, no rebuild (smoother)
garageSig = sig;
if (gRobot) { garage.turntable.remove(gRobot.root); disposeRobot(gRobot); }
gRobot = buildRobot(build, paint);
gRobot.root.position.y = 0.12;
garage.turntable.add(gRobot.root);
@@ -1211,7 +1229,7 @@
showArena(s, builds, paints) {
api.setArena(s.arena);
mode = 'arena'; state = s; pointMode(camMode !== 'iso'); lightsOn(1); chaseInit = false;
for (const R of robots) arenaScene.remove(R.root);
for (const R of robots) { arenaScene.remove(R.root); disposeRobot(R); }
robots = builds.map((b, i) => { const R = buildRobot(b, paints[i]); arenaScene.add(R.root); return R; });
sparks.clear(); smoke.clear(); fire.clear(); clearDebris(); shake = 0;
for (const m of arena.rocketMeshes) arenaScene.remove(m);
@@ -1259,7 +1277,7 @@
// comfort mode: smoother cockpit follow, narrower fov, weaker shake (reduces motion sickness)
setComfort(on) { comfort = !!on; pcam.fov = camMode === 'fpv' ? (comfort ? 62 : 70) : 55; pcam.updateProjectionMatrix(); },
project(x, y, z) {
const v = new T.Vector3(x, y, z).project(mode === 'arena' && camMode !== 'iso' ? pcam : cam);
const v = _pv.set(x, y, z).project(mode === 'arena' && camMode !== 'iso' ? pcam : cam); // reuse one vector (no per-call GC)
// behind the camera (chase / cockpit): off screen
if (v.z > 1) return { x: -999, y: -999 };
return { x: (v.x + 1) / 2 * W, y: (1 - v.y) / 2 * H };
@@ -1279,6 +1297,7 @@
renderer.render(arenaScene, pcam);
return;
}
if (mode === 'title') titleIdle(dt); // gently animate the two robots behind the title menu
animateArena(dt, now);
sparks.update(dt, 9, 1.2); smoke.update(dt, -0.6, 1.4); fire.update(dt, -2.5, 2);
shake = Math.max(0, shake - dt * 1.2);
@@ -1298,7 +1317,7 @@
};
// chase: smooth camera behind and above robot 0 · fpv: eyes at the front edge of its top
const cpos = new T.Vector3(), clook = new T.Vector3();
const cpos = new T.Vector3(), clook = new T.Vector3(), _ta = new T.Vector3(), _tb = new T.Vector3(), _pv = new T.Vector3();
function followCam(dt, sx, sz) {
const r = state.robots[0], R = robots[0], half = (state.arena || RC.ARENA).half;
const fx = Math.cos(r.a), fz = Math.sin(r.a);
@@ -1310,14 +1329,14 @@
// smooth the eye + aim instead of snapping 1:1 to the robot's yaw/bounce -> the main motion-sickness fix
const sm = chaseInit ? Math.min(1, dt * (comfort ? 9 : 16)) : 1;
chaseInit = true;
cpos.lerp(new T.Vector3(px + sx * 0.3, py, pz + sz * 0.3), sm);
clook.lerp(new T.Vector3(lx, ly, lz), sm);
cpos.lerp(_ta.set(px + sx * 0.3, py, pz + sz * 0.3), sm);
clook.lerp(_tb.set(lx, ly, lz), sm);
} else {
px = Kit.clamp(r.x - fx * 3.6, -half - 1.2, half + 1.2); pz = Kit.clamp(r.z - fz * 3.6, -half - 1.2, half + 1.2); py = 2.2 + r.y * 0.5;
lx = r.x + fx * 1.6; lz = r.z + fz * 1.6; ly = 0.3 + r.y * 0.5;
const k = chaseInit ? Math.min(1, dt * 5) : 1;
chaseInit = true;
cpos.lerp(new T.Vector3(px + sx, py, pz + sz), k); clook.lerp(new T.Vector3(lx, ly, lz), k);
cpos.lerp(_ta.set(px + sx, py, pz + sz), k); clook.lerp(_tb.set(lx, ly, lz), k);
}
pcam.position.copy(cpos); pcam.lookAt(clook);
}
@@ -1366,6 +1385,19 @@
});
}
// title screen: keep the two intro robots alive — spinners turning, turrets swaying, a soft hover bob
function titleIdle(dt) {
const s = state; if (!s || !s.robots) return;
titleT += dt;
s.robots.forEach((rb, i) => {
rb.lifted = 1; // no tumbling physics
rb.spin = 1; rb.sawOn = false;
rb.turretA = rb.a + Math.sin(titleT * 0.55 + i * 2.1) * 0.35;
rb.y = 0.02 + Math.sin(titleT * 1.0 + i * 1.7) * 0.025; // gentle hover
rb.flameOn = Math.sin(titleT * 0.8 + i * 2) > 0.985; // rare flame flick
});
}
function burst(x, y, z, n, sp, c) {
n = Math.max(1, Math.round(n * fxMul));
for (let k = 0; k < n; k++) {
@@ -1390,21 +1422,37 @@
obj.getWorldPosition(wp); obj.getWorldQuaternion(wq);
arenaScene.add(obj);
obj.position.copy(wp); obj.quaternion.copy(wq);
debris.push({ o: obj, vx: (Math.random() - 0.5) * 3.5, vy: 2.2 + Math.random() * 1.8, vz: (Math.random() - 0.5) * 3.5,
rx: (Math.random() - 0.5) * 9, rz: (Math.random() - 0.5) * 9, t: 9 });
while (debris.length > 10) { const d = debris.shift(); arenaScene.remove(d.o); }
const bb = new T.Box3().setFromObject(obj);
const half = Math.max(0.05, (bb.max.y - bb.min.y) / 2); // rest height so the piece sits ON the floor, not through it
debris.push({ o: obj, half, vx: (Math.random() - 0.5) * 3.5, vy: 2.2 + Math.random() * 1.8, vz: (Math.random() - 0.5) * 3.5,
rx: (Math.random() - 0.5) * 9, rz: (Math.random() - 0.5) * 9, settle: 0 });
while (debris.length > 12) { freeDebris(debris.shift()); } // keep the last few lying around
}
function updateDebris(dt) {
const ah = (state && state.arena ? state.arena.half : (RC.ARENA ? RC.ARENA.half : 10));
for (let i = debris.length - 1; i >= 0; i--) {
const d = debris[i];
d.t -= dt; d.vy -= 22 * dt;
d.o.position.x += d.vx * dt; d.o.position.y += d.vy * dt; d.o.position.z += d.vz * dt;
if (d.o.position.y < 0.07) { d.o.position.y = 0.07; d.vy = -d.vy * 0.3; d.vx *= 0.6; d.vz *= 0.6; d.rx *= 0.5; d.rz *= 0.5; }
d.o.rotation.x += d.rx * dt; d.o.rotation.z += d.rz * dt;
if (d.t <= 0) { arenaScene.remove(d.o); debris.splice(i, 1); }
const d = debris[i], o = d.o, rest = d.half;
d.vy -= 22 * dt;
o.position.x += d.vx * dt; o.position.y += d.vy * dt; o.position.z += d.vz * dt;
if (o.position.y <= rest) { // on the ground: bounce a little, then settle flat
o.position.y = rest;
if (d.vy < 0) d.vy = -d.vy * 0.25;
d.vx *= 0.7; d.vz *= 0.7; d.rx *= 0.4; d.rz *= 0.4;
if (Math.abs(d.vy) < 0.3 && Math.hypot(d.vx, d.vz) < 0.2) { d.vy = 0; d.settle = Math.min(1, d.settle + dt * 2); }
} else d.settle = 0;
o.position.x = Math.max(-ah + rest, Math.min(ah - rest, o.position.x)); // stay inside the arena
o.position.z = Math.max(-ah + rest, Math.min(ah - rest, o.position.z));
if (state && state.robots) for (const rb of state.robots) { // robots shove fallen parts aside
if (rb.out) continue;
const dx = o.position.x - rb.x, dz = o.position.z - rb.z, dd = Math.hypot(dx, dz), reach = (rb.st.rad || 0.8) + rest + 0.1;
if (dd < reach && dd > 0.001) { const push = reach - dd, nx = dx / dd, nz = dz / dd; o.position.x += nx * push; o.position.z += nz * push; d.vx += nx * 7; d.vz += nz * 7; d.settle = 0; }
}
o.rotation.x += d.rx * dt; o.rotation.z += d.rz * dt;
if (d.settle >= 1) { const k = 1 - Math.min(1, dt * 3); d.rx *= k; d.rz *= k; }
}
}
function clearDebris() { for (const d of debris) arenaScene.remove(d.o); debris.length = 0; }
const freeDebris = d => { arenaScene.remove(d.o); d.o.traverse(o => { if (o.geometry) o.geometry.dispose(); }); };
function clearDebris() { for (const d of debris) freeDebris(d); debris.length = 0; }
// combat.js says a part broke off -> take it off the model (one piece tumbles away)
function breakPart(R, part) {
if (!R) return;