Files
Robot-Clash/game/render3d.js

810 lines
48 KiB
JavaScript
Raw Normal View History

2026-11-13 00:41:54 +01:00
/* Robot Clash - 3D view with three.js (vendor/three.min.js, r149, classic script). Needs parts.js,
robot.js, combat.js (RC.ARENA).
const view = RC.Render(canvas, 960, 540);
view.resize(scale) after Kit.fit (CSS scale of #app)
view.showGarage(build, paint) workshop scene with the robot on a turntable
view.showArena(state, builds, paints) arena scene for a fight (state from RC.Combat.create)
view.events(list) combat events -> sparks, explosions, camera shake
view.frame(dt, now) animate + render the active scene
view.project(x, y, z) world -> design pixels { x, y } (for floating texts)
All meshes are procedural (no image files needed). */
(function (root) {
'use strict';
const RC = root.RC, T = root.THREE;
const clamp = (v, a, b) => (v < a ? a : v > b ? b : v);
const lerp = (a, b, k) => a + (b - a) * k;
// ---------------------------------------------------------------- textures (canvas)
function canvasTex(w, h, draw, repeat) {
const c = document.createElement('canvas'); c.width = w; c.height = h;
draw(c.getContext('2d'), w, h);
const t = new T.CanvasTexture(c);
t.encoding = T.sRGBEncoding;
t.anisotropy = 4;
if (repeat) { t.wrapS = t.wrapT = T.RepeatWrapping; t.repeat.set(repeat[0], repeat[1]); }
return t;
}
function stripes(ctx, x, y, w, h, size = 16) {
ctx.save(); ctx.beginPath(); ctx.rect(x, y, w, h); ctx.clip();
ctx.fillStyle = '#f2b51c'; ctx.fillRect(x, y, w, h);
ctx.fillStyle = '#16181c';
for (let k = -h; k < w + h; k += size * 2) {
ctx.beginPath(); ctx.moveTo(x + k, y); ctx.lineTo(x + k + size, y); ctx.lineTo(x + k + size - h, y + h); ctx.lineTo(x + k - h, y + h); ctx.fill();
}
ctx.restore();
}
const stripeTex = () => canvasTex(128, 32, (c, w, h) => stripes(c, 0, 0, w, h, 12));
const dotTex = () => canvasTex(64, 64, (c) => {
const g = c.createRadialGradient(32, 32, 0, 32, 32, 32);
g.addColorStop(0, 'rgba(255,255,255,1)'); g.addColorStop(0.35, 'rgba(255,255,255,.75)'); g.addColorStop(1, 'rgba(255,255,255,0)');
c.fillStyle = g; c.fillRect(0, 0, 64, 64);
});
function floorTex() {
const A = RC.ARENA, S = 64, N = A.half * 2 * S; // 64 px per metre
const P = (x, z) => [(x + A.half) * S, (z + A.half) * S];
2026-11-13 01:02:55 +01:00
let plate = null; // tex_floor.jpg from ComfyGen (make_tileable)
const tex = canvasTex(N, N, draw);
const im = new Image();
im.onload = () => {
// under file:// Chrome taints the canvas with a disk image and WebGL refuses it -> keep the drawn plates
// (the release HTML embeds images as data: URIs, there it works)
const probe = document.createElement('canvas').getContext('2d');
probe.drawImage(im, 0, 0, 1, 1);
try { probe.getImageData(0, 0, 1, 1); } catch (e) { return; }
plate = im; draw(tex.image.getContext('2d')); tex.needsUpdate = true;
};
im.src = Kit.asset('assets/Sprites/tex_floor.jpg');
return tex;
function draw(c) {
if (plate) { // 4 m per texture tile, slightly darkened
for (let x = 0; x < N; x += 256) for (let y = 0; y < N; y += 256) c.drawImage(plate, x, y, 256, 256);
c.fillStyle = 'rgba(10,12,16,.35)'; c.fillRect(0, 0, N, N);
c.strokeStyle = 'rgba(0,0,0,.55)'; c.lineWidth = 3;
for (let k = 0; k <= N; k += 128) { c.beginPath(); c.moveTo(k, 0); c.lineTo(k, N); c.moveTo(0, k); c.lineTo(N, k); c.stroke(); }
} else drawPlates(c);
drawMarks(c);
}
function drawPlates(c) {
2026-11-13 00:41:54 +01:00
// steel plates 2 m
for (let i = 0; i < A.half; i++) for (let j = 0; j < A.half; j++) {
const v = 52 + ((i * 7 + j * 13) % 5) * 4;
c.fillStyle = `rgb(${v},${v + 3},${v + 8})`; c.fillRect(i * 128, j * 128, 128, 128);
c.strokeStyle = '#1b1d22'; c.lineWidth = 3; c.strokeRect(i * 128 + 1.5, j * 128 + 1.5, 125, 125);
c.strokeStyle = 'rgba(255,255,255,.07)'; c.lineWidth = 1; c.strokeRect(i * 128 + 4, j * 128 + 4, 120, 120);
c.fillStyle = '#25272c';
for (const [rx, ry] of [[10, 10], [118, 10], [10, 118], [118, 118]]) { c.beginPath(); c.arc(i * 128 + rx, j * 128 + ry, 3, 0, 7); c.fill(); }
}
2026-11-13 01:02:55 +01:00
}
function drawMarks(c) {
2026-11-13 00:41:54 +01:00
// scratches + oil
let seed = 5; const r = () => { seed = (seed * 16807) % 2147483647; return seed / 2147483647; };
for (let k = 0; k < 340; k++) {
c.strokeStyle = `rgba(${r() < 0.5 ? '255,255,255' : '0,0,0'},${0.04 + r() * 0.07})`; c.lineWidth = 0.5 + r() * 1.5;
const x = r() * N, y = r() * N, a = r() * 6.3, l = 10 + r() * 60;
c.beginPath(); c.moveTo(x, y); c.lineTo(x + Math.cos(a) * l, y + Math.sin(a) * l); c.stroke();
}
for (let k = 0; k < 18; k++) { c.fillStyle = `rgba(10,8,6,${0.12 + r() * 0.15})`; c.beginPath(); c.ellipse(r() * N, r() * N, 10 + r() * 40, 8 + r() * 26, r() * 3, 0, 7); c.fill(); }
// guardian corners + pit: hazard stripes
for (const z of [A.press, A.crane, A.pit]) {
const [x0, y0] = P(z.x - z.hw, z.z - z.hd), w = z.hw * 2 * S, h = z.hd * 2 * S, b = 14;
stripes(c, x0, y0, w, b); stripes(c, x0, y0 + h - b, w, b); stripes(c, x0, y0, b, h); stripes(c, x0 + w - b, y0, b, h);
if (z === A.pit) { c.fillStyle = '#030303'; c.fillRect(x0 + b, y0 + b, w - 2 * b, h - 2 * b); }
else { c.fillStyle = 'rgba(0,0,0,.25)'; c.fillRect(x0 + b, y0 + b, w - 2 * b, h - 2 * b); }
}
// saw slots
for (const h of A.saws) {
const [x, y] = P(h.x, h.z);
c.fillStyle = '#121316'; c.beginPath(); c.arc(x, y, h.r * S * 0.95, 0, 7); c.fill();
c.strokeStyle = '#d33'; c.lineWidth = 4; c.setLineDash([12, 10]); c.beginPath(); c.arc(x, y, h.r * S + 8, 0, 7); c.stroke(); c.setLineDash([]);
}
// centre mark
const [cx, cy] = P(0, 0);
c.strokeStyle = 'rgba(255,122,26,.35)'; c.lineWidth = 5; c.beginPath(); c.arc(cx, cy, 1.4 * S, 0, 7); c.stroke();
2026-11-13 01:02:55 +01:00
}
2026-11-13 00:41:54 +01:00
}
// ---------------------------------------------------------------- particles
function Particles(scene, max, size, additive, tex) {
const pos = new Float32Array(max * 3), col = new Float32Array(max * 4);
const vel = new Float32Array(max * 3), life = new Float32Array(max), life0 = new Float32Array(max), base = new Float32Array(max * 3);
const geo = new T.BufferGeometry();
geo.setAttribute('position', new T.BufferAttribute(pos, 3));
geo.setAttribute('color', new T.BufferAttribute(col, 4));
const mat = new T.PointsMaterial({ size, map: tex, vertexColors: true, transparent: true, depthWrite: false,
blending: additive ? T.AdditiveBlending : T.NormalBlending, sizeAttenuation: true });
const pts = new T.Points(geo, mat);
pts.frustumCulled = false;
scene.add(pts);
let next = 0;
for (let i = 0; i < max; i++) pos[i * 3 + 1] = -100;
return {
pts,
spawn(x, y, z, vx, vy, vz, l, r, g, b) {
const i = next; next = (next + 1) % max;
pos[i * 3] = x; pos[i * 3 + 1] = y; pos[i * 3 + 2] = z;
vel[i * 3] = vx; vel[i * 3 + 1] = vy; vel[i * 3 + 2] = vz;
life[i] = life0[i] = l; base[i * 3] = r; base[i * 3 + 1] = g; base[i * 3 + 2] = b;
},
update(dt, grav, drag) {
for (let i = 0; i < max; i++) {
if (life[i] <= 0) continue;
life[i] -= dt;
const k = Math.max(0, life[i] / life0[i]);
if (life[i] <= 0) { pos[i * 3 + 1] = -100; col[i * 4 + 3] = 0; continue; }
vel[i * 3 + 1] -= grav * dt;
const d = Math.max(0, 1 - drag * dt);
vel[i * 3] *= d; vel[i * 3 + 1] *= d; vel[i * 3 + 2] *= d;
pos[i * 3] += vel[i * 3] * dt; pos[i * 3 + 1] += vel[i * 3 + 1] * dt; pos[i * 3 + 2] += vel[i * 3 + 2] * dt;
if (pos[i * 3 + 1] < 0.02 && grav > 0) { pos[i * 3 + 1] = 0.02; vel[i * 3 + 1] *= -0.35; vel[i * 3] *= 0.6; vel[i * 3 + 2] *= 0.6; }
col[i * 4] = base[i * 3]; col[i * 4 + 1] = base[i * 3 + 1] * (additive ? k : 1); col[i * 4 + 2] = base[i * 3 + 2] * (additive ? k * k : 1);
col[i * 4 + 3] = additive ? k : k * 0.55;
}
geo.attributes.position.needsUpdate = true; geo.attributes.color.needsUpdate = true;
},
clear() { for (let i = 0; i < max; i++) { life[i] = 0; pos[i * 3 + 1] = -100; col[i * 4 + 3] = 0; } },
};
}
// ---------------------------------------------------------------- robot model
const M = {};
function mats() {
if (M.dark) return M;
M.dark = new T.MeshStandardMaterial({ color: 0x2a2d33, metalness: 0.6, roughness: 0.5 });
M.steel = new T.MeshStandardMaterial({ color: 0xb8bec7, metalness: 0.85, roughness: 0.3 });
M.rubber = new T.MeshStandardMaterial({ color: 0x15161a, metalness: 0, roughness: 0.92 });
M.hub = new T.MeshStandardMaterial({ color: 0x9aa1aa, metalness: 0.8, roughness: 0.35 });
M.glowW = new T.MeshStandardMaterial({ color: 0xffffff, emissive: 0xfff2c0, emissiveIntensity: 1.5 });
2026-11-13 01:02:55 +01:00
M.lamp = new T.MeshStandardMaterial({ color: 0xffffff, emissive: 0xfff2c0, emissiveIntensity: 1.5 }); // floodlights (intro switches them on)
2026-11-13 00:41:54 +01:00
M.stripe = new T.MeshStandardMaterial({ map: stripeTex(), metalness: 0.3, roughness: 0.6 });
M.flame = new T.MeshBasicMaterial({ color: 0xff3a08, transparent: true, opacity: 0.42, blending: T.AdditiveBlending, depthWrite: false, side: T.DoubleSide });
M.trackTex = canvasTex(64, 16, (c, w, h) => { c.fillStyle = '#17181b'; c.fillRect(0, 0, w, h); c.fillStyle = '#2c2e33'; for (let x = 0; x < w; x += 8) c.fillRect(x, 0, 4, h); }, [6, 1]);
M.track = new T.MeshStandardMaterial({ map: M.trackTex, roughness: 0.85, metalness: 0.2 });
return M;
}
const box = (w, h, d, m) => { const o = new T.Mesh(new T.BoxGeometry(w, h, d), m); o.castShadow = true; o.receiveShadow = true; return o; };
const cyl = (r1, r2, h, m, seg = 20) => { const o = new T.Mesh(new T.CylinderGeometry(r1, r2, h, seg), m); o.castShadow = true; return o; };
2026-11-13 01:02:55 +01:00
// ---- skin: paint colour + the player's own drawing (paint.layer, 512x256 canvas:
// left half = top view, front to the right; right half = side view, front to the right)
const css = n => '#' + (n >>> 0).toString(16).padStart(6, '0');
function makeSkin(paint) {
const cv = document.createElement('canvas'); cv.width = 512; cv.height = 256;
const tex = new T.CanvasTexture(cv);
tex.encoding = T.sRGBEncoding; tex.anisotropy = 4;
const mat = new T.MeshStandardMaterial({ map: tex, metalness: 0.4, roughness: 0.45 });
function redraw(p = paint) {
paint = p;
const c = cv.getContext('2d');
c.fillStyle = css(p.body); c.fillRect(0, 0, 512, 256);
c.strokeStyle = 'rgba(0,0,0,.2)'; c.lineWidth = 3; c.strokeRect(5, 5, 246, 246); c.strokeRect(261, 5, 246, 246);
if (p.layer) c.drawImage(p.layer, 0, 0, 512, 256);
tex.needsUpdate = true;
}
redraw();
return { mat, redraw };
}
// planar UVs by face direction: up/down faces -> top half, sides -> side half (readable on both sides)
function skinUV(geo, L, Wd, H, y0) {
geo = geo.index ? geo.toNonIndexed() : geo;
geo.computeVertexNormals();
const p = geo.attributes.position, n = geo.attributes.normal, uv = new Float32Array(p.count * 2);
const cl = (v, a, b) => Math.min(b, Math.max(a, v));
for (let i = 0; i < p.count; i++) {
const fx = cl((p.getX(i) + L / 2) / L, 0, 1), fz = cl((p.getZ(i) + Wd / 2) / Wd, 0, 1), fy = cl((p.getY(i) - y0) / H, 0, 1);
const ax = Math.abs(n.getX(i)), ay = Math.abs(n.getY(i)), az = Math.abs(n.getZ(i));
let u, v;
if (ay >= ax && ay >= az) { u = 0.004 + 0.492 * fx; v = 1 - fz; }
else if (az >= ax) { u = 0.504 + 0.492 * (n.getZ(i) > 0 ? fx : 1 - fx); v = fy; }
else { u = 0.504 + 0.492 * (n.getX(i) > 0 ? 1 - fz : fz); v = fy; }
uv[i * 2] = u; uv[i * 2 + 1] = v;
}
geo.setAttribute('uv', new T.BufferAttribute(uv, 2));
return geo;
}
2026-11-13 00:41:54 +01:00
function buildRobot(build, paint) {
const m = mats(), st = RC.stats(build), P = RC.PARTS;
const ch = P[build.chassis], wh = P[build.wheels], ar = build.armour ? P[build.armour] : null;
const L = st.len, Wd = st.wid, H = st.h;
2026-11-13 01:02:55 +01:00
const skin = makeSkin(paint), body = skin.mat;
const drawn = !!(paint.layer && paint.drawn); // own artwork: no stock stripes, painted armour
2026-11-13 00:41:54 +01:00
const accent = new T.MeshStandardMaterial({ color: paint.accent, metalness: 0.3, roughness: 0.45 });
2026-11-13 01:02:55 +01:00
const armour = ar ? (drawn ? body : new T.MeshStandardMaterial({ color: ar.tint, metalness: 0.9, roughness: 0.28 })) : null;
2026-11-13 00:41:54 +01:00
const root = new T.Group(), tilt = new T.Group();
root.add(tilt);
2026-11-13 01:02:55 +01:00
const out = { root, tilt, body, skin, st, build, roll: 0, flash: 0, spinA: 0, sawA: 0, gunA: 0, smokeT: 0 };
const sk = (geo, w, h, d, y0) => { const o = new T.Mesh(skinUV(geo, w, d, h, y0), body); o.castShadow = o.receiveShadow = true; return o; };
2026-11-13 00:41:54 +01:00
const big = wh && wh.kind === 'big';
const c = big ? 0.12 : 0.1; // ground clearance
out.c = c;
// chassis
let top = c + H;
if (ch.shape === 'wedge') {
const s = new T.Shape();
s.moveTo(-L / 2, 0); s.lineTo(L / 2, 0); s.lineTo(L / 2, H * 0.22); s.lineTo(-L / 2 + L * 0.38, H); s.lineTo(-L / 2, H); s.closePath();
const g = new T.ExtrudeGeometry(s, { depth: Wd, bevelEnabled: true, bevelSize: 0.015, bevelThickness: 0.015, bevelSegments: 1 });
g.translate(0, 0, -Wd / 2);
2026-11-13 01:02:55 +01:00
const o = sk(g, L, H, Wd, 0); o.position.y = c; tilt.add(o);
if (!drawn) { const st1 = box(L * 0.32, 0.02, Wd * 0.8, m.stripe); st1.position.set(-L / 2 + L * 0.19, top + 0.012, 0); tilt.add(st1); }
2026-11-13 00:41:54 +01:00
} else if (ch.shape === 'dome') {
2026-11-13 01:02:55 +01:00
const o = sk(new T.BoxGeometry(L, H * 0.5, Wd), L, H * 0.5, Wd, -H * 0.25); o.position.y = c + H * 0.25; tilt.add(o);
const d = sk(new T.SphereGeometry(1, 28, 12, 0, Math.PI * 2, 0, Math.PI / 2), 2, 1, 2, 0);
d.scale.set(L * 0.5, H * 0.75, Wd * 0.5); d.position.y = c + H * 0.5; tilt.add(d);
2026-11-13 00:41:54 +01:00
const ring = new T.Mesh(new T.TorusGeometry(1, 0.035, 8, 32), accent);
ring.rotation.x = Math.PI / 2; ring.scale.set(L * 0.5, Wd * 0.5, 1); ring.position.y = c + H * 0.5; tilt.add(ring);
top = c + H * 1.2;
} else {
2026-11-13 01:02:55 +01:00
const o = sk(new T.BoxGeometry(L, H, Wd), L, H, Wd, -H / 2); o.position.y = c + H / 2; tilt.add(o);
if (!drawn) {
const s1 = box(L * 0.86, 0.02, Wd * 0.22, accent); s1.position.set(0, top + 0.01, 0); tilt.add(s1);
const s2 = box(0.06, H * 0.6, Wd * 1.002, m.stripe); s2.position.set(-L / 2 + 0.08, c + H / 2, 0); tilt.add(s2);
}
2026-11-13 00:41:54 +01:00
}
out.top = top;
// armour plates (sides + rear), bolts
if (armour) {
for (const sz of [-1, 1]) {
2026-11-13 01:02:55 +01:00
const p = drawn ? sk(new T.BoxGeometry(L * 0.9, H * 0.78, 0.05), L * 0.9, H * 0.78, 0.05, -H * 0.39) : box(L * 0.9, H * 0.78, 0.05, armour);
p.position.set(0, c + H * 0.42, sz * (Wd / 2 + 0.028)); tilt.add(p);
2026-11-13 00:41:54 +01:00
for (const bx of [-0.36, 0, 0.36]) { const b = cyl(0.025, 0.025, 0.03, m.dark, 8); b.rotation.x = Math.PI / 2; b.position.set(bx * L, c + H * 0.55, sz * (Wd / 2 + 0.06)); tilt.add(b); }
}
const r = box(0.05, H * 0.78, Wd * 0.95, armour); r.position.set(-L / 2 - 0.028, c + H * 0.42, 0); tilt.add(r);
}
// wheels
out.wheels = [];
const side = Wd / 2 + (armour ? 0.13 : 0.08);
if (wh.kind === 'tracks') {
for (const sz of [-1, 1]) {
const t = box(L * 1.02, 0.3, 0.2, m.track); t.position.set(0, 0.15, sz * side); tilt.add(t);
for (const bx of [-0.42, 0.42]) { const w = cyl(0.15, 0.15, 0.21, m.hub, 14); w.rotation.x = Math.PI / 2; w.position.set(bx * L, 0.15, sz * side); tilt.add(w); }
}
} else if (big) {
const r = (c + H) / 2 + 0.09;
for (const sz of [-1, 1]) {
const w = new T.Group(); w.position.set(-L * 0.12, (c + H) / 2, sz * (side + 0.04));
const tyre = cyl(r, r, 0.2, m.rubber, 22); tyre.rotation.x = Math.PI / 2; w.add(tyre);
const hub = cyl(r * 0.45, r * 0.45, 0.22, m.hub, 12); hub.rotation.x = Math.PI / 2; w.add(hub);
tilt.add(w); out.wheels.push(w);
}
out.wheelR = r;
} else {
for (const sx of [-1, 1]) for (const sz of [-1, 1]) {
const w = new T.Group(); w.position.set(sx * L * 0.32, 0.16, sz * side);
const tyre = cyl(0.16, 0.16, 0.12, m.rubber, 16); tyre.rotation.x = Math.PI / 2; w.add(tyre);
const hub = cyl(0.07, 0.07, 0.13, m.hub, 8); hub.rotation.x = Math.PI / 2; w.add(hub);
tilt.add(w); out.wheels.push(w);
}
out.wheelR = 0.16;
}
// headlights + antenna
for (const sz of [-0.3, 0.3]) { const l = box(0.03, 0.05, 0.1, m.glowW); l.position.set(L / 2 + 0.005, c + H * 0.62, sz * Wd); tilt.add(l); }
const ant = cyl(0.01, 0.01, 0.45, m.dark, 6); ant.position.set(-L / 2 + 0.12, top + 0.22, Wd / 2 - 0.12); tilt.add(ant);
const tip = new T.Mesh(new T.SphereGeometry(0.035, 8, 6), new T.MeshStandardMaterial({ color: paint.accent, emissive: paint.accent, emissiveIntensity: 0.9 }));
tip.position.set(-L / 2 + 0.12, top + 0.46, Wd / 2 - 0.12); tilt.add(tip);
// main weapon (front = +x)
const w = st.weapon;
if (w) {
if (w.type === 'wedge') {
const p = box(0.55, 0.04, Wd * 1.05, m.steel); p.position.set(L / 2 + 0.22, c * 0.5 + 0.08, 0); p.rotation.z = -0.42; tilt.add(p);
} else if (w.type === 'flipper') {
const piv = new T.Group(); piv.position.set(L / 2 - 0.08, c + H * 0.3, 0); tilt.add(piv);
const p = box(0.62, 0.05, Wd * 0.92, m.steel); p.position.x = 0.31; piv.add(p);
const s = box(0.6, 0.012, Wd * 0.3, accent); s.position.set(0.31, 0.03, 0); piv.add(s);
out.flipper = piv;
} else if (w.type === 'hammer') {
const piv = new T.Group(); piv.position.set(-L * 0.08, top + 0.06, 0); tilt.add(piv);
const post = box(0.12, 0.14, 0.18, m.dark); post.position.set(-L * 0.08, top + 0.03, 0); tilt.add(post);
const arm = box(L * 0.72, 0.07, 0.07, m.dark); arm.position.x = L * 0.36; piv.add(arm);
const head = box(0.1, 0.34, 0.06, m.steel); head.position.set(L * 0.72, -0.1, 0); piv.add(head);
const back = box(0.14, 0.12, 0.12, accent); back.position.set(L * 0.72, 0.08, 0); piv.add(back);
out.hammer = piv;
} else if (w.type === 'saw') {
const arm = box(0.35, 0.08, 0.08, m.dark); arm.position.set(L / 2 + 0.05, c + H * 0.55, 0); tilt.add(arm);
const g = new T.Group(); g.position.set(L / 2 + 0.2, c + H * 0.55, 0); tilt.add(g);
const d = cyl(0.3, 0.3, 0.025, m.steel, 28); d.rotation.x = Math.PI / 2; g.add(d);
for (let k = 0; k < 14; k++) { const t = box(0.07, 0.04, 0.026, m.steel); const a = k / 14 * Math.PI * 2; t.position.set(Math.cos(a) * 0.31, Math.sin(a) * 0.31, 0); t.rotation.z = a + 0.6; g.add(t); }
const hub = cyl(0.06, 0.06, 0.04, accent, 10); hub.rotation.x = Math.PI / 2; g.add(hub);
out.saw = g;
} else if (w.type === 'spinner') {
const post = cyl(0.07, 0.07, 0.25, m.dark, 10); post.position.set(L / 2 + 0.02, c + H * 0.35, 0); tilt.add(post);
const g = new T.Group(); g.position.set(L / 2 + 0.02, c + H * 0.32, 0); tilt.add(g);
const bar = box(1.25, 0.07, 0.16, m.steel); g.add(bar);
for (const sx of [-1, 1]) { const t = box(0.1, 0.09, 0.2, accent); t.position.x = sx * 0.6; g.add(t); }
out.spinner = g;
} else if (w.type === 'crusher') {
out.jaws = [];
for (const sz of [-1, 1]) {
const piv = new T.Group(); piv.position.set(L / 2 - 0.02, c + H * 0.45, sz * Wd * 0.34); tilt.add(piv);
const j = box(0.58, 0.14, 0.08, m.steel); j.position.x = 0.29; piv.add(j);
const tooth = box(0.08, 0.14, 0.14, accent); tooth.position.set(0.56, 0, -sz * 0.05); piv.add(tooth);
piv.userData.side = sz; out.jaws.push(piv);
}
}
}
// tower on top
const tw = st.tower;
if (tw) {
const g = new T.Group(); g.position.set(-L * 0.05, top, 0); tilt.add(g);
const base = cyl(0.2, 0.24, 0.12, m.dark, 16); base.position.y = 0.06; g.add(base);
if (tw.type === 'flamer') {
const tank = cyl(0.08, 0.08, 0.26, new T.MeshStandardMaterial({ color: 0xc0392b, metalness: 0.4, roughness: 0.4 }), 12); tank.position.set(-0.08, 0.25, 0); g.add(tank);
const n = cyl(0.045, 0.06, 0.42, m.steel, 10); n.rotation.z = -Math.PI / 2; n.position.set(0.24, 0.18, 0); g.add(n);
const f = new T.Mesh(new T.ConeGeometry(0.42, 2.6, 14, 1, true), M.flame);
f.rotation.z = Math.PI / 2; f.position.set(0.45 + 1.3, 0.18, 0); f.visible = false; g.add(f);
out.flame = f;
} else if (tw.type === 'minigun') {
const hous = box(0.22, 0.16, 0.2, accent); hous.position.y = 0.2; g.add(hous);
const brl = new T.Group(); brl.position.set(0.1, 0.2, 0); g.add(brl);
for (let k = 0; k < 4; k++) { const b = cyl(0.022, 0.022, 0.5, m.steel, 6); b.rotation.z = -Math.PI / 2; const a = k / 4 * Math.PI * 2; b.position.set(0.25, Math.cos(a) * 0.045, Math.sin(a) * 0.045); brl.add(b); }
out.gun = brl;
const fl = new T.Mesh(new T.SphereGeometry(0.09, 8, 6), new T.MeshBasicMaterial({ color: 0xffd060, transparent: true, blending: T.AdditiveBlending, depthWrite: false }));
fl.position.set(0.62, 0.2, 0); fl.visible = false; g.add(fl); out.muzzle = fl;
} else if (tw.type === 'rockets') {
const pod = box(0.38, 0.26, 0.36, accent); pod.position.set(0.02, 0.27, 0); g.add(pod);
for (const [y, z] of [[0.2, -0.09], [0.2, 0.09], [0.34, -0.09], [0.34, 0.09]]) { const t = cyl(0.05, 0.05, 0.06, m.dark, 10); t.rotation.z = Math.PI / 2; t.position.set(0.22, y, z); g.add(t); }
}
out.turret = g;
}
root.traverse(o => { if (o.isMesh && !o.material.transparent) { o.castShadow = true; } });
return out;
}
// per-frame robot animation from the combat state
function poseRobot(R, r, dt) {
const w = R.st.weapon;
R.root.position.set(r.x, r.y, r.z);
R.root.rotation.y = -r.a;
// upside down when flipped
const want = r.flipped > 0 ? Math.PI : 0;
if (r.y > 0.05 && !r.lifted) R.roll += dt * 9; else R.roll = lerp(R.roll, want, Math.min(1, dt * 10));
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;
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);
if (R.flipper) R.flipper.rotation.z = -0.32 + (r.flipAnim > 0 ? Math.sin((1 - r.flipAnim / 0.35) * Math.PI) * 1.5 : 0);
if (R.hammer) {
let ang = 1.9;
if (r.swing > 0) { const k = 1 - r.swing / 0.28; ang = lerp(1.9, -0.25, k * k); }
else if (w && r.cd > w.cd - 0.28 - 0.15) ang = -0.25;
else if (w && r.cd > 0) ang = lerp(-0.25, 1.9, clamp((w.cd - 0.43 - r.cd) / 0.5, 0, 1));
R.hammer.rotation.z = ang;
}
if (R.saw) { if (r.sawOn) R.sawA += dt * 40; R.saw.rotation.z = -R.sawA; }
if (R.spinner) { R.spinA += dt * r.spin * 38; R.spinner.rotation.y = R.spinA; }
if (R.jaws) for (const j of R.jaws) j.rotation.y = j.userData.side * (r.grab > 0 ? -0.18 : 0.45);
if (R.turret) R.turret.rotation.y = -(r.turretA - r.a);
if (R.flame) { R.flame.visible = r.flameOn; if (r.flameOn) { const f = 0.85 + Math.random() * 0.3; R.flame.scale.set(f, 1, f); } }
if (R.gun) { if (r.gunOn) R.gunA += dt * 30; R.gun.rotation.x = R.gunA; R.muzzle.visible = r.gunOn && Math.random() < 0.6; }
R.flash = Math.max(0, R.flash - dt * 4);
R.body.emissive.setRGB(R.flash * 0.9, R.flash * 0.15, 0);
R.root.visible = r.y > -1.6;
}
// ---------------------------------------------------------------- arena
function buildArena(scene) {
const A = RC.ARENA, m = mats(), half = A.half;
const out = {};
scene.background = new T.Color(0x07090d);
scene.fog = new T.Fog(0x07090d, 26, 60);
// lights: one shadow-casting key light + floodlight colours
scene.add(new T.HemisphereLight(0x8fa6c8, 0x1a140f, 0.55));
const key = new T.DirectionalLight(0xfff1dc, 1.25);
key.position.set(6, 16, 9); key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
Object.assign(key.shadow.camera, { left: -11, right: 11, top: 11, bottom: -11, near: 1, far: 40 });
key.shadow.bias = -0.0008;
scene.add(key);
const rimA = new T.PointLight(0xff7a1a, 1.1, 22); rimA.position.set(-9, 5, 9); scene.add(rimA);
const rimB = new T.PointLight(0x4fa8ff, 0.9, 22); rimB.position.set(9, 5, -9); scene.add(rimB);
2026-11-13 01:02:55 +01:00
out.lights = { key, rimA, rimB, hemi: scene.children.find(o => o.isHemisphereLight) };
2026-11-13 00:41:54 +01:00
out.flashLight = new T.PointLight(0xffaa55, 0, 9); scene.add(out.flashLight);
// floor
const floor = new T.Mesh(new T.PlaneGeometry(half * 2, half * 2), new T.MeshStandardMaterial({ map: floorTex(), metalness: 0.55, roughness: 0.62 }));
floor.rotation.x = -Math.PI / 2; floor.receiveShadow = true; scene.add(floor);
const outer = new T.Mesh(new T.PlaneGeometry(80, 80), new T.MeshStandardMaterial({ color: 0x0d0f13, roughness: 1 }));
outer.rotation.x = -Math.PI / 2; outer.position.y = -0.02; outer.receiveShadow = true; scene.add(outer);
// spike walls (instanced spikes)
const wallMat = new T.MeshStandardMaterial({ color: 0x30343b, metalness: 0.7, roughness: 0.45 });
const n = Math.round(half * 2 / 0.5);
const spikes = new T.InstancedMesh(new T.ConeGeometry(0.07, 0.28, 8), m.steel, n * 4);
const dm = new T.Object3D(); let si = 0;
for (const [nx, nz] of [[1, 0], [-1, 0], [0, 1], [0, -1]]) {
const wall = box(nz ? half * 2 + 0.5 : 0.25, 0.55, nz ? 0.25 : half * 2 + 0.5, wallMat);
wall.position.set(nx * (half + 0.12), 0.275, nz * (half + 0.12)); scene.add(wall);
const strip = box(nz ? half * 2 + 0.5 : 0.26, 0.08, nz ? 0.26 : half * 2 + 0.5, m.stripe);
strip.position.set(nx * (half + 0.12), 0.58, nz * (half + 0.12)); scene.add(strip);
for (let k = 0; k < n; k++) {
const t = -half + 0.25 + k * 0.5;
dm.position.set(nx ? nx * (half - 0.02) : t, 0.3, nz ? nz * (half - 0.02) : t);
dm.rotation.set(0, 0, 0);
if (nx) dm.rotation.z = nx > 0 ? Math.PI / 2 : -Math.PI / 2; else dm.rotation.x = nz > 0 ? -Math.PI / 2 : Math.PI / 2;
dm.updateMatrix(); spikes.setMatrixAt(si++, dm.matrix);
}
}
spikes.castShadow = true; scene.add(spikes);
// glass + posts + floodlight rigs
const glass = new T.MeshStandardMaterial({ color: 0x9fc3ff, transparent: true, opacity: 0.07, metalness: 0.1, roughness: 0.05, depthWrite: false, side: T.DoubleSide });
for (const [nx, nz] of [[-1, 0], [0, -1]]) {
const g = new T.Mesh(new T.PlaneGeometry(half * 2 + 0.6, 2.6), glass);
g.position.set(nx * (half + 0.3), 1.9, nz * (half + 0.3)); g.rotation.y = nx ? Math.PI / 2 : 0; scene.add(g);
}
for (const [px, pz] of [[-1, -1], [1, -1], [-1, 1]]) {
const post = box(0.22, 3.4, 0.22, wallMat); post.position.set(px * (half + 0.35), 1.7, pz * (half + 0.35)); scene.add(post);
2026-11-13 01:02:55 +01:00
const lamp = box(0.5, 0.3, 0.3, m.lamp); lamp.position.set(px * (half + 0.2), 3.5, pz * (half + 0.2)); scene.add(lamp);
2026-11-13 00:41:54 +01:00
}
// audience stands behind the back walls
const seats = new T.InstancedMesh(new T.BoxGeometry(0.32, 0.5, 0.32), new T.MeshStandardMaterial({ roughness: 0.9 }), 900);
const col = new T.Color(); let ci = 0; let sd = 11;
const rnd = () => { sd = (sd * 16807) % 2147483647; return sd / 2147483647; };
for (const [nx, nz] of [[-1, 0], [0, -1]]) for (let row = 0; row < 5; row++) {
const step = box(nz ? half * 2 + 6 : 1, 0.6, nz ? 1 : half * 2 + 6, new T.MeshStandardMaterial({ color: 0x14161b, roughness: 1 }));
const d = half + 1.6 + row * 1.0;
step.position.set(nx * d, 0.3 + row * 0.6, nz * d); scene.add(step);
for (let k = 0; k < 44 && ci < 900; k++) {
if (rnd() < 0.3) continue;
const t = -half - 2 + k * 0.45 + rnd() * 0.1;
dm.position.set(nx ? nx * d : t, 0.85 + row * 0.6, nz ? nz * d : t); dm.rotation.set(0, rnd(), 0);
dm.scale.set(1, 0.8 + rnd() * 0.5, 1); dm.updateMatrix(); seats.setMatrixAt(ci, dm.matrix);
col.setHSL(rnd(), 0.3, 0.07 + rnd() * 0.08); seats.setColorAt(ci, col); ci++;
}
}
seats.count = ci; dm.scale.set(1, 1, 1); scene.add(seats);
// floor saws
out.saws = A.saws.map(h => {
const g = new T.Group(); g.position.set(h.x, 0.005, h.z); scene.add(g);
const d = cyl(h.r * 0.85, h.r * 0.85, 0.035, m.steel, 32); g.add(d);
for (let k = 0; k < 18; k++) { const t = box(0.14, 0.035, 0.07, m.steel); const a = k / 18 * Math.PI * 2; t.position.set(Math.cos(a) * h.r * 0.88, 0, Math.sin(a) * h.r * 0.88); t.rotation.y = -a + 0.6; g.add(t); }
const hub = cyl(0.12, 0.12, 0.06, m.dark, 12); g.add(hub);
return { h, g, a: 0 };
});
// flame grilles
const grille = canvasTex(64, 64, (c, w) => { c.clearRect(0, 0, w, w); c.strokeStyle = '#3a3e45'; c.lineWidth = 5; for (let x = 4; x < w; x += 12) { c.beginPath(); c.moveTo(x, 0); c.lineTo(x, w); c.stroke(); c.beginPath(); c.moveTo(0, x); c.lineTo(w, x); c.stroke(); } }, [3, 3]);
out.flames = A.flames.map(h => {
const base = new T.Mesh(new T.PlaneGeometry(h.hw * 2, h.hd * 2), new T.MeshStandardMaterial({ color: 0x1a0e08, emissive: 0x000000 }));
base.rotation.x = -Math.PI / 2; base.position.set(h.x, 0.006, h.z); base.receiveShadow = true; scene.add(base);
const gr = new T.Mesh(new T.PlaneGeometry(h.hw * 2, h.hd * 2), new T.MeshStandardMaterial({ map: grille, transparent: true, metalness: 0.6, roughness: 0.5 }));
gr.rotation.x = -Math.PI / 2; gr.position.set(h.x, 0.012, h.z); scene.add(gr);
const cones = [];
for (let i = 0; i < 3; i++) for (let j = 0; j < 3; j++) {
const f = new T.Mesh(new T.ConeGeometry(0.22, 1.2, 10, 1, true), M.flame);
f.position.set(h.x + (i - 1) * h.hw * 0.62, 0.6, h.z + (j - 1) * h.hd * 0.62); f.visible = false; scene.add(f); cones.push(f);
}
return { h, base, cones };
});
// guardian 1: forge press
const P = A.press, frameMat = new T.MeshStandardMaterial({ color: 0x3b3f47, metalness: 0.7, roughness: 0.4 });
for (const [sx, sz] of [[-1, -1], [1, -1], [-1, 1], [1, 1]]) {
const post = box(0.16, 3.4, 0.16, frameMat); post.position.set(P.x + sx * (P.hw + 0.05), 1.7, P.z + sz * (P.hd + 0.05)); scene.add(post);
}
const beam = box(P.hw * 2 + 0.4, 0.25, P.hd * 2 + 0.4, frameMat); beam.position.set(P.x, 3.45, P.z); scene.add(beam);
const head = new T.Group(); scene.add(head);
const hb = box(P.hw * 1.8, 0.5, P.hd * 1.8, new T.MeshStandardMaterial({ color: 0x4a4f58, metalness: 0.85, roughness: 0.3 })); head.add(hb);
const hs = box(P.hw * 1.82, 0.12, P.hd * 1.82, m.stripe); hs.position.y = 0.2; head.add(hs);
const ram = cyl(0.16, 0.16, 3, m.steel, 12); ram.position.y = 1.7; head.add(ram);
out.press = head;
out.pressLamp = new T.Mesh(new T.SphereGeometry(0.16, 12, 8), new T.MeshStandardMaterial({ color: 0x550000, emissive: 0xff2020, emissiveIntensity: 0 }));
out.pressLamp.position.set(P.x, 3.7, P.z); scene.add(out.pressLamp);
// guardian 2: magnet crane (post outside the corner, boom, trolley, magnet)
const C = A.crane;
const cpost = box(0.35, 4.6, 0.35, frameMat); cpost.position.set(C.x - 2.4, 2.3, C.z + 2.4); scene.add(cpost);
const boom = box(1, 0.22, 0.22, new T.MeshStandardMaterial({ color: 0xe0a020, metalness: 0.5, roughness: 0.5 })); boom.position.y = 4.4; scene.add(boom);
const cable = box(0.03, 1, 0.03, m.dark); scene.add(cable);
const mag = new T.Group(); scene.add(mag);
const md = cyl(0.42, 0.42, 0.18, new T.MeshStandardMaterial({ color: 0x2b2e34, metalness: 0.8, roughness: 0.35 }), 20); mag.add(md);
const mr = cyl(0.43, 0.43, 0.06, m.stripe, 20); mr.position.y = 0.08; mag.add(mr);
out.crane = { post: cpost, boom, cable, mag, px: C.x - 2.4, pz: C.z + 2.4 };
// pit doors + warning lights
const pit = A.pit;
out.pitDoors = [-1, 1].map(sx => {
const d = box(pit.hw, 0.05, pit.hd * 2, new T.MeshStandardMaterial({ color: 0x3d4148, metalness: 0.8, roughness: 0.4 }));
d.position.set(pit.x + sx * pit.hw / 2, 0.025, pit.z); d.userData.sx = sx; scene.add(d); return d;
});
out.pitLamps = [[-1, -1], [1, -1], [-1, 1], [1, 1]].map(([sx, sz]) => {
const l = new T.Mesh(new T.SphereGeometry(0.09, 10, 6), new T.MeshStandardMaterial({ color: 0x400000, emissive: 0xff1a1a, emissiveIntensity: 0 }));
l.position.set(pit.x + sx * (pit.hw + 0.12), 0.08, pit.z + sz * (pit.hd + 0.12)); scene.add(l); return l;
});
// rockets
out.rocketGeo = new T.CylinderGeometry(0.05, 0.05, 0.32, 8); out.rocketGeo.rotateZ(Math.PI / 2);
out.rocketMat = new T.MeshStandardMaterial({ color: 0xdddddd, emissive: 0x331100 });
out.rocketMeshes = [];
return out;
}
// ---------------------------------------------------------------- garage
2026-11-13 01:02:55 +01:00
// transparent: the painted workshop (bg_workshop.jpg, CSS) shows through; only a shadow catcher
2026-11-13 00:41:54 +01:00
function buildGarage(scene) {
2026-11-13 01:02:55 +01:00
scene.background = null;
2026-11-13 00:41:54 +01:00
scene.add(new T.HemisphereLight(0x9fb4d8, 0x1c160f, 0.6));
const spot = new T.SpotLight(0xfff0d8, 2.2, 14, 0.62, 0.45);
spot.position.set(1.6, 5, 2.6); spot.target.position.set(0, 0, 0); spot.castShadow = true; spot.shadow.mapSize.set(1024, 1024);
scene.add(spot); scene.add(spot.target);
const rim = new T.PointLight(0xff7a1a, 1.2, 8); rim.position.set(-2.5, 1.2, -1.5); scene.add(rim);
const rim2 = new T.PointLight(0x4fa8ff, 0.8, 8); rim2.position.set(2.5, 1.0, -2); scene.add(rim2);
2026-11-13 01:02:55 +01:00
const floor = new T.Mesh(new T.PlaneGeometry(30, 30), new T.ShadowMaterial({ opacity: 0.55 }));
2026-11-13 00:41:54 +01:00
floor.rotation.x = -Math.PI / 2; floor.receiveShadow = true; scene.add(floor);
const turntable = new T.Group(); scene.add(turntable);
const disc = cyl(1.5, 1.6, 0.12, new T.MeshStandardMaterial({ color: 0x3a3e46, metalness: 0.8, roughness: 0.35 }), 48);
disc.position.y = 0.06; disc.receiveShadow = true; turntable.add(disc);
const ring = new T.Mesh(new T.TorusGeometry(1.56, 0.03, 8, 64), new T.MeshStandardMaterial({ color: 0xff7a1a, emissive: 0xff7a1a, emissiveIntensity: 0.6 }));
ring.rotation.x = Math.PI / 2; ring.position.y = 0.12; scene.add(ring);
2026-11-13 01:02:55 +01:00
return { turntable, spot };
2026-11-13 00:41:54 +01:00
}
// ---------------------------------------------------------------- view
RC.Render = function (canvas, W, H) {
2026-11-13 01:02:55 +01:00
const renderer = new T.WebGLRenderer({ canvas, antialias: true, alpha: true });
renderer.setClearColor(0x000000, 0);
2026-11-13 00:41:54 +01:00
renderer.outputEncoding = T.sRGBEncoding;
renderer.toneMapping = T.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = T.PCFSoftShadowMap;
const arenaScene = new T.Scene(), garageScene = new T.Scene();
const arena = buildArena(arenaScene), garage = buildGarage(garageScene);
const tex = dotTex();
// orthographic camera: point sizes are screen pixels (x pixel ratio), not metres
const sparks = Particles(arenaScene, 900, 6, true, tex);
const smoke = Particles(arenaScene, 300, 34, false, tex);
const fire = Particles(arenaScene, 300, 18, true, tex);
const VIEW = 15.4, asp = W / H;
const cam = new T.OrthographicCamera(-VIEW * asp / 2, VIEW * asp / 2, VIEW / 2, -VIEW / 2, 0.1, 120);
const camBase = new T.Vector3(15.2, 15.5, 15.2), camLook = new T.Vector3(-1.25, 0, -1.25);
cam.position.copy(camBase); cam.lookAt(camLook);
const gcam = new T.PerspectiveCamera(34, asp, 0.1, 60);
2026-11-13 01:02:55 +01:00
gcam.position.set(3.7, 2.5, 4.4); gcam.lookAt(0, 0.3, 0);
gcam.setViewOffset(W, H, 300, 40, W, H); // robot sits in the left part of the shop
// intro / title: perspective camera flying over the arena
const pcam = new T.PerspectiveCamera(40, asp, 0.1, 120);
2026-11-13 00:41:54 +01:00
2026-11-13 01:02:55 +01:00
let mode = 'garage', gRobot = null, robots = [], state = null, shake = 0, flashT = 0, introT = 0, scale = 1, hold = 0;
let quality = 'high', fxMul = 1;
const PX = [6, 34, 18], WORLD = [0.07, 0.7, 0.32]; // particle sizes: ortho px / perspective m
const pointMode = persp => [sparks, smoke, fire].forEach((p, i) => { p.pts.material.size = persp ? WORLD[i] : PX[i]; });
2026-11-13 00:41:54 +01:00
const api = {
get mode() { return mode; },
2026-11-13 01:02:55 +01:00
get introTime() { return introT; },
resize(sc) {
scale = sc;
const cap = quality === 'low' ? 1 : quality === 'medium' ? 1.5 : 3;
renderer.setPixelRatio(Math.min(cap, sc * (root.devicePixelRatio || 1)));
2026-11-13 00:41:54 +01:00
renderer.setSize(W, H);
},
2026-11-13 01:02:55 +01:00
// 'low' | 'medium' | 'high': shadows, resolution, particle amount
setQuality(q) {
quality = q; fxMul = q === 'low' ? 0.35 : q === 'medium' ? 0.7 : 1;
const sh = q !== 'low';
renderer.shadowMap.enabled = sh;
renderer.shadowMap.type = q === 'high' ? T.PCFSoftShadowMap : T.PCFShadowMap;
for (const sc of [arenaScene, garageScene]) sc.traverse(o => { if (o.material) [].concat(o.material).forEach(m => { m.needsUpdate = true; }); });
api.resize(scale);
},
// intro fly-over, then a slow orbit behind the title screen; skip() jumps to the orbit
showIntro(builds, paints) {
api.showArena(RC.Combat.create({ robots: builds.map(b => RC.stats(b)), seed: 3 }), builds, paints);
mode = 'intro'; introT = 0; pointMode(true);
},
skipIntro() { if (mode === 'intro' && introT < INTRO_END) introT = INTRO_END; },
2026-11-13 00:41:54 +01:00
showGarage(build, paint) {
mode = 'garage';
if (gRobot) garage.turntable.remove(gRobot.root);
gRobot = buildRobot(build, paint);
gRobot.root.position.y = 0.12;
garage.turntable.add(gRobot.root);
},
2026-11-13 01:02:55 +01:00
// live update while painting (colour or drawing changed, no rebuild)
setPaint(paint) { if (gRobot) gRobot.skin.redraw(paint); },
// camera for the paint booth: closer, robot centred in the left part
// drag to turn the robot on the turntable (auto-rotation pauses for a moment)
turn(dx) { garage.turntable.rotation.y += dx * 0.012; hold = 2.5; },
// view a side straight on: 'top' | 'left' | 'right' | 'front' | null (free)
face(side) {
const a = { right: 0, front: -Math.PI / 2, left: Math.PI, top: 0 }[side];
if (a == null) { hold = 0; return; }
// turntable angle that points the robot's side (+z = right) at the camera
garage.turntable.rotation.y = Math.atan2(gcam.position.x, gcam.position.z) + a; hold = 1e9;
},
paintCam(on) { gcam.position.set(on ? 2.6 : 3.7, on ? 2.2 : 2.5, on ? 3.1 : 4.4); gcam.lookAt(0, 0.3, 0); },
2026-11-13 00:41:54 +01:00
showArena(s, builds, paints) {
2026-11-13 01:02:55 +01:00
mode = 'arena'; state = s; pointMode(false); lightsOn(1);
2026-11-13 00:41:54 +01:00
for (const R of robots) arenaScene.remove(R.root);
robots = builds.map((b, i) => { const R = buildRobot(b, paints[i]); arenaScene.add(R.root); return R; });
sparks.clear(); smoke.clear(); fire.clear(); shake = 0;
for (const m of arena.rocketMeshes) arenaScene.remove(m);
arena.rocketMeshes = [];
},
events(list) {
for (const e of list) {
const y = e.y || 0.35;
switch (e.type) {
case 'sparks': burst(e.x, y, e.z, e.n || 6, 4, [1, 0.85, 0.4]); break;
case 'dmg':
if (!e.tick && robots[e.who]) robots[e.who].flash = Math.min(1, 0.4 + e.amount / 25);
if (!e.tick && e.amount > 4) { burst(e.x, 0.4, e.z, 10 + e.amount, 5, [1, 0.8, 0.35]); shake = Math.max(shake, Math.min(0.35, e.amount / 60)); }
break;
case 'wall': burst(e.x, 0.3, e.z, 14, 4, [1, 0.8, 0.4]); shake = Math.max(shake, 0.12); break;
case 'bump': burst(e.x, 0.3, e.z, Math.round(e.speed * 3), 3, [1, 0.9, 0.6]); break;
case 'smash': burst(e.x, 0.1, e.z, 18, 4, [1, 0.85, 0.5]); puff(e.x, e.z, 4); shake = Math.max(shake, 0.2); break;
case 'flip': puff(e.x, e.z, 3); break;
case 'land': puff(e.x, e.z, 6); shake = Math.max(shake, 0.15); break;
case 'spinhit': burst(e.x, 0.3, e.z, 30, 7, [1, 0.9, 0.5]); shake = Math.max(shake, 0.3 * e.k); break;
case 'boom': explode(e.x, e.y || 0.4, e.z, 0.6); break;
case 'press': puff(e.x, e.z, 14); burst(e.x, 0.1, e.z, 24, 5, [1, 0.8, 0.4]); shake = Math.max(shake, 0.45); break;
case 'drop': puff(e.x, e.z, 8); shake = Math.max(shake, 0.2); break;
case 'ko': explode(e.x, 0.5, e.z, 1.2); break;
case 'boost': for (let k = 0; k < 6; k++) fire.spawn(e.x, 0.3, e.z, (Math.random() - 0.5), 0.6, (Math.random() - 0.5), 0.4, 1, 0.6, 0.2); break;
}
}
},
project(x, y, z) {
const v = new T.Vector3(x, y, z).project(cam);
return { x: (v.x + 1) / 2 * W, y: (1 - v.y) / 2 * H };
},
frame(dt, now) {
if (mode === 'garage') {
2026-11-13 01:02:55 +01:00
if (hold > 0) hold -= dt; else garage.turntable.rotation.y += dt * 0.45;
2026-11-13 00:41:54 +01:00
renderer.render(garageScene, gcam);
return;
}
2026-11-13 01:02:55 +01:00
if (mode === 'intro') {
introT += dt;
introScript(dt, now);
animateArena(dt, now);
sparks.update(dt, 9, 1.2); smoke.update(dt, -0.6, 1.4); fire.update(dt, -2.5, 2);
renderer.render(arenaScene, pcam);
return;
}
2026-11-13 00:41:54 +01:00
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);
const sx = (Math.random() - 0.5) * shake, sz = (Math.random() - 0.5) * shake;
cam.position.set(camBase.x + sx, camBase.y, camBase.z + sz);
cam.lookAt(camLook.x + sx, camLook.y, camLook.z + sz);
flashT = Math.max(0, flashT - dt * 3);
arena.flashLight.intensity = flashT * 4;
renderer.render(arenaScene, cam);
},
};
2026-11-13 01:02:55 +01:00
// ---- intro: dark arena -> floodlights flicker on -> camera sweeps up -> two robots are lowered in
const INTRO_END = RC.INTRO_END = 7.5;
const ease = u => (u < 0.5 ? 2 * u * u : 1 - Math.pow(-2 * u + 2, 2) / 2);
function lightsOn(k) {
const L = arena.lights;
L.key.intensity = 1.25 * k; L.rimA.intensity = 1.1 * k; L.rimB.intensity = 0.9 * k;
if (L.hemi) L.hemi.intensity = 0.55 * (0.12 + 0.88 * k);
M.lamp.emissiveIntensity = 1.5 * k;
}
function introScript(dt) {
const t = introT, s = state;
// floodlights: off, flicker, on
lightsOn(t < 1.2 ? 0 : t < 1.32 ? 1 : t < 1.5 ? 0.08 : t < 1.62 ? 0.8 : t < 1.85 ? 0.25 : 1);
// camera path, then a slow orbit
let ang, r, h, lx, lz;
if (t < INTRO_END) {
const e = ease(Math.min(1, t / INTRO_END));
ang = lerp(-2.4, Math.PI / 4, e); r = lerp(5, 20.5, e); h = lerp(0.8, 13.5, Math.pow(e, 1.4));
lx = lerp(RC.ARENA.pit.x, -0.6, e); lz = lerp(RC.ARENA.pit.z, -0.6, e);
} else {
ang = Math.PI / 4 + (t - INTRO_END) * 0.05; r = 20.5; h = 13.5; lx = lz = 0;
}
pcam.position.set(Math.cos(ang) * r, h, Math.sin(ang) * r);
pcam.lookAt(lx, 0.3, lz);
// robots lowered in from above, landing with sparks
s.t = t + 2; // trap timing: flames ignite once the lights are on
s.robots.forEach((rb, i) => {
const k = clamp((t - 2.6 - i * 0.35) / 1.6, 0, 1);
const was = rb.y;
rb.y = 7 * Math.pow(1 - k, 2); rb.vx = rb.vz = 0; rb.lifted = rb.y > 0 ? 1 : 0; // lifted: no tumbling
if (was > 0 && rb.y === 0) { burst(rb.x, 0.2, rb.z, Math.round(40 * fxMul), 5, [1, 0.8, 0.4]); puff(rb.x, rb.z, 10); }
rb.spin = t > 5.4 ? Math.min(1, (t - 5.4) * 0.8) : 0; // spinners rev up
rb.sawOn = t > 5.6;
rb.flameOn = t > 5.8 && t < 6.6 || (t > INTRO_END && Math.sin(t * 0.9 + i * 2) > 0.97);
rb.turretA = rb.a + Math.sin(t * 0.7 + i) * 0.4;
});
}
2026-11-13 00:41:54 +01:00
function burst(x, y, z, n, sp, c) {
2026-11-13 01:02:55 +01:00
n = Math.max(1, Math.round(n * fxMul));
2026-11-13 00:41:54 +01:00
for (let k = 0; k < n; k++) {
const a = Math.random() * Math.PI * 2, v = sp * (0.3 + Math.random());
sparks.spawn(x, y, z, Math.cos(a) * v, 1 + Math.random() * sp, Math.sin(a) * v, 0.25 + Math.random() * 0.45, c[0], c[1], c[2]);
}
}
function puff(x, z, n) {
for (let k = 0; k < n; k++) { const a = Math.random() * 6.28; smoke.spawn(x + Math.cos(a) * 0.3, 0.15, z + Math.sin(a) * 0.3, Math.cos(a) * 1.2, 0.4, Math.sin(a) * 1.2, 0.9 + Math.random() * 0.6, 0.55, 0.52, 0.48); }
}
function explode(x, y, z, k) {
burst(x, y, z, Math.round(40 * k), 6 * k, [1, 0.7, 0.25]);
for (let i = 0; i < 20 * k; i++) { const a = Math.random() * 6.28, v = Math.random() * 2 * k; fire.spawn(x, y, z, Math.cos(a) * v, 1 + Math.random() * 2, Math.sin(a) * v, 0.5 + Math.random() * 0.4, 1, 0.55, 0.15); }
puff(x, z, Math.round(10 * k));
arena.flashLight.position.set(x, 1.2, z); flashT = 1; shake = Math.max(shake, 0.3 * k);
}
function animateArena(dt, now) {
const s = state; if (!s) return;
const A = RC.ARENA;
robots.forEach((R, i) => {
const r = s.robots[i];
poseRobot(R, r, dt);
// damaged robots smoke, dead ones burn
const hp = r.hp / r.maxHp;
R.smokeT -= dt;
if (!r.out && hp < 0.45 && R.smokeT <= 0) {
R.smokeT = r.dead ? 0.05 : 0.08 + hp * 0.4;
const y = r.y + R.top + 0.1;
smoke.spawn(r.x, y, r.z, (Math.random() - 0.5) * 0.3, 0.9, (Math.random() - 0.5) * 0.3, 1.6, r.dead ? 0.12 : 0.35, r.dead ? 0.12 : 0.34, r.dead ? 0.12 : 0.33);
if (r.dead || hp < 0.2) fire.spawn(r.x, y, r.z, (Math.random() - 0.5) * 0.4, 0.8, (Math.random() - 0.5) * 0.4, 0.4, 1, 0.5, 0.1);
}
if (r.flameOn && Math.random() < 0.7) {
const a = r.turretA + (Math.random() - 0.5) * 0.5, v = 5 + Math.random() * 2;
fire.spawn(r.x + Math.cos(r.turretA) * 0.6, r.y + R.top + 0.18, r.z + Math.sin(r.turretA) * 0.6, Math.cos(a) * v, 0.3, Math.sin(a) * v, 0.45, 1, 0.55, 0.15);
}
});
// saws
for (const S of arena.saws) {
const on = RC.hazardOn(S.h, s.t), warn = RC.hazardOn(S.h, s.t + 0.6);
S.g.position.y = lerp(S.g.position.y, on ? 0.09 : warn ? 0.04 : 0.005, Math.min(1, dt * 12));
if (on) S.a += dt * 25; S.g.rotation.y = S.a;
}
for (const F of arena.flames) {
const on = RC.hazardOn(F.h, s.t), warn = RC.hazardOn(F.h, s.t + 0.7);
F.base.material.emissive.setRGB(on ? 1 : warn ? 0.35 : 0, on ? 0.3 : warn ? 0.08 : 0, 0);
for (const c of F.cones) { c.visible = on; if (on) { const f = 0.7 + Math.random() * 0.6; c.scale.set(f, 0.7 + Math.random() * 0.7, f); c.position.y = c.scale.y * 0.6; } }
if (on && Math.random() < 0.5) fire.spawn(F.h.x + (Math.random() - 0.5) * F.h.hw * 2, 0.2, F.h.z + (Math.random() - 0.5) * F.h.hd * 2, 0, 3, 0, 0.5, 1, 0.5, 0.12);
}
// press
const p = s.press, ph = arena.press;
ph.position.set(A.press.x + (p.st === 'warn' ? (Math.random() - 0.5) * 0.04 : 0), 0.25 + p.y * 2.65, A.press.z);
arena.pressLamp.material.emissiveIntensity = p.st === 'warn' || p.st === 'slam' ? (Math.sin(now / 60) > 0 ? 3 : 0.3) : 0.15;
// crane
const c = s.crane, C = arena.crane;
const dx = c.x - C.px, dz = c.z - C.pz, len = Math.hypot(dx, dz);
C.boom.scale.x = len + 0.4; C.boom.position.set(C.px + dx / 2, 4.4, C.pz + dz / 2); C.boom.rotation.y = -Math.atan2(dz, dx);
let my = 2.6;
if (c.who >= 0) { const r = s.robots[c.who]; my = r.y + robots[c.who].top + 0.12; }
C.mag.position.set(c.x, my, c.z);
const cl = 4.3 - my; C.cable.scale.y = Math.max(0.05, cl); C.cable.position.set(c.x, my + cl / 2, c.z);
// pit
const o = s.pitOpen;
for (const d of arena.pitDoors) { d.position.x = A.pit.x + d.userData.sx * (A.pit.hw / 2 + o * A.pit.hw * 1.02); d.position.y = 0.025 + o * 0.01; }
for (const l of arena.pitLamps) l.material.emissiveIntensity = s.pitAlarm ? (Math.sin(now / 150) > 0 ? 2.5 : 0.4) : 0;
// rockets
while (arena.rocketMeshes.length < s.rockets.length) { const m = new T.Mesh(arena.rocketGeo, arena.rocketMat); arenaScene.add(m); arena.rocketMeshes.push(m); }
arena.rocketMeshes.forEach((m, i) => {
const k = s.rockets[i];
m.visible = !!k;
if (k) {
m.position.set(k.x, k.y, k.z); m.rotation.y = -k.a;
fire.spawn(k.x - Math.cos(k.a) * 0.2, k.y, k.z - Math.sin(k.a) * 0.2, 0, 0.2, 0, 0.3, 1, 0.6, 0.2);
if (Math.random() < 0.5) smoke.spawn(k.x, k.y, k.z, 0, 0.2, 0, 0.8, 0.5, 0.5, 0.5);
}
});
}
api.resize(1);
return api;
};
})(window);