/* 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(A) { const S = 64, N = A.half * 2 * S; // 64 px per metre const P = (x, z) => [(x + A.half) * S, (z + A.half) * S]; 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) { // 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(); } } } function drawMarks(c) { // 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(); } } // image from assets/Sprites as a WebGL texture; skipped quietly where file:// taints it (release embeds data: URIs) function loadTex(file, onReady) { const im = new Image(); im.onload = () => { const probe = document.createElement('canvas').getContext('2d'); probe.drawImage(im, 0, 0, 1, 1); try { probe.getImageData(0, 0, 1, 1); } catch (e) { return; } const tex = new T.Texture(im); tex.encoding = T.sRGBEncoding; tex.anisotropy = 4; tex.needsUpdate = true; onReady(tex); }; im.src = Kit.asset('assets/Sprites/' + file); } // ---------------------------------------------------------------- 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 }); M.lamp = new T.MeshStandardMaterial({ color: 0xffffff, emissive: 0xfff2c0, emissiveIntensity: 1.5 }); // floodlights (intro switches them on) 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.flameCore = new T.MeshBasicMaterial({ color: 0xffd060, transparent: true, opacity: 0.55, blending: T.AdditiveBlending, depthWrite: false, side: T.DoubleSide }); M.arc = new T.MeshBasicMaterial({ color: 0x6ec8ff, transparent: true, opacity: 0.5, 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 }); 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 }); return M; } // shop: the robot is framed in the left column, not in the middle of the stage const GARAGE_OFF_X = 455, GARAGE_OFF_Y = 16; // shop zoom: 0.6 = furthest out (the default, whole robot in frame), 1.9 = closest const GZ_MIN = 0.6, GZ_MAX = 1.9, GZ_DEF = GZ_MIN; 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; }; // ---- 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; } // body from a top-view outline, extruded upwards (shape plane XY -> XZ, extrusion -> +Y) function prism(pts, h) { const sh = new T.Shape(); sh.moveTo(pts[0][0], pts[0][1]); for (let i = 1; i < pts.length; i++) sh.lineTo(pts[i][0], pts[i][1]); sh.closePath(); const g = new T.ExtrudeGeometry(sh, { depth: h, bevelEnabled: true, bevelSize: 0.015, bevelThickness: 0.015, bevelSegments: 1 }); g.rotateX(-Math.PI / 2); return g; } 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; const skin = makeSkin(paint), body = skin.mat; const drawn = !!(paint.layer && paint.drawn); // own artwork: no stock stripes, painted armour const accent = new T.MeshStandardMaterial({ color: paint.accent, metalness: 0.3, roughness: 0.45 }); const armour = ar ? (drawn ? body : new T.MeshStandardMaterial({ color: ar.tint, metalness: 0.9, roughness: 0.28 })) : null; const root = new T.Group(), tilt = new T.Group(); root.add(tilt); 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; }; const look = paint.look || {}; // opponent look (looks.js), cosmetic only const big = wh && (wh.kind === 'big' || wh.kind === 'stud'); const ws = look.wheelScale || 1; const c = big ? 0.12 : 0.1 * ws; // ground clearance out.c = c; // every piece gets userData.part = slot (lets tools/partshot.html render one part alone) let from = 0; const mark = part => { for (let i = from; i < tilt.children.length; i++) tilt.children[i].userData.part = part; from = tilt.children.length; }; // 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); 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); } } else if (ch.shape === 'dome') { 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); 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 if (ch.shape === 'hex' || ch.shape === 'arrow') { const hx = L / 2, hz = Wd / 2; const pts = ch.shape === 'hex' ? [[-hx * 0.55, -hz], [hx * 0.55, -hz], [hx, 0], [hx * 0.55, hz], [-hx * 0.55, hz], [-hx, 0]] : [[hx, 0], [hx * 0.2, -hz], [-hx, -hz * 0.78], [-hx, hz * 0.78], [hx * 0.2, hz]]; const o = sk(prism(pts, H), L, H, Wd, 0); o.position.y = c; tilt.add(o); if (!drawn) { const st1 = box(L * 0.28, 0.02, Wd * 0.5, m.stripe); st1.position.set(-L * 0.22, top + 0.012, 0); tilt.add(st1); } } else if (ch.shape === 'barrel') { const rr = Math.min(Wd, H * 1.9) / 2; const g = new T.CylinderGeometry(rr, rr, L, 18); g.rotateZ(Math.PI / 2); const o = sk(g, L, H, Wd, 0); o.position.y = c + rr; tilt.add(o); for (const sx of [-1, 1]) { const cap = cyl(rr * 0.5, rr * 0.5, 0.07, m.hub, 14); cap.rotation.z = Math.PI / 2; cap.position.set(sx * L / 2, c + rr, 0); tilt.add(cap); } for (const bx of [-0.22, 0.22]) { const band = cyl(rr * 1.03, rr * 1.03, 0.05, m.dark, 18); band.rotation.z = Math.PI / 2; band.position.set(bx * L, c + rr, 0); tilt.add(band); } top = c + rr * 2; } else if (ch.shape === 'saucer') { const o = sk(new T.CylinderGeometry(1, 1, 1, 22), L, H, Wd, 0); o.scale.set(L / 2, H, Wd / 2); o.position.y = c + H / 2; tilt.add(o); const ring = new T.Mesh(new T.TorusGeometry(1, 0.035, 8, 30), accent); ring.rotation.x = Math.PI / 2; ring.scale.set(L / 2 * 1.02, Wd / 2 * 1.02, 1); ring.position.y = c + H * 0.72; tilt.add(ring); top = c + H; } else if (ch.shape === 'plow') { const bl = L * 0.84, bw = Wd * 0.9; const o = sk(new T.BoxGeometry(bl, H, bw), bl, H, bw, -H / 2); o.position.set(-L * 0.08, c + H / 2, 0); tilt.add(o); const plate = box(0.09, H * 1.35, Wd * 1.1, m.steel); plate.position.set(L / 2 - 0.04, c + H * 0.62, 0); plate.rotation.z = -0.32; tilt.add(plate); const edge = box(0.12, 0.1, Wd * 1.12, m.stripe); edge.position.set(L / 2 + 0.16, c * 0.4, 0); tilt.add(edge); top = c + H; } else if (ch.shape === 'frame') { const bm = 0.07; for (const sx of [-1, 1]) for (const sz of [-1, 1]) { const post = box(bm, H, bm, m.dark); post.position.set(sx * (L / 2 - bm / 2), c + H / 2, sz * (Wd / 2 - bm / 2)); tilt.add(post); } for (const y of [c + bm / 2, c + H - bm / 2]) { for (const sz of [-1, 1]) { const rail = box(L, bm, bm, m.dark); rail.position.set(0, y, sz * (Wd / 2 - bm / 2)); tilt.add(rail); } for (const sx of [-1, 1]) { const rail = box(bm, bm, Wd, m.dark); rail.position.set(sx * (L / 2 - bm / 2), y, 0); tilt.add(rail); } } const pan = sk(new T.BoxGeometry(L * 0.9, 0.09, Wd * 0.86), L * 0.9, 0.09, Wd * 0.86, -0.045); pan.position.y = c + 0.07; tilt.add(pan); top = c + H; } else { 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); } } // look extras: raised cab with window slit, eye bumps (like the portraits) if (look.cab) { const cw = L * 0.4, chh = H * 0.62, cd = Wd * 0.64; const cab = sk(new T.BoxGeometry(cw, chh, cd), cw, chh, cd, -chh / 2); cab.position.set(-L * 0.16, top + chh / 2, 0); tilt.add(cab); const win = box(0.02, chh * 0.25, cd * 0.8, m.dark); win.position.set(-L * 0.16 + cw / 2 + 0.005, top + chh * 0.62, 0); tilt.add(win); top += chh; } if (look.bumps) { for (const sz of [-1, 1]) { const b = sk(new T.SphereGeometry(1, 16, 8, 0, Math.PI * 2, 0, Math.PI / 2), 2, 1, 2, 0); b.scale.set(0.13, 0.11, 0.13); b.position.set(L * 0.18, top - (ch.shape === 'wedge' ? H * 0.45 : 0), sz * Wd * 0.26); tilt.add(b); } } out.top = top; mark('chassis'); // armour: every kind looks clearly different, so swapping it shows in the workshop preview if (armour) { const asty = (ar && ar.style) || 'plates'; const sideX = Wd / 2 + 0.028; if (asty === 'mesh') { // open wire grille for (const sz of [-1, 1]) { for (const y of [c + H * 0.1, c + H * 0.74]) { const rail = box(L * 0.9, 0.05, 0.04, armour); rail.position.set(0, y, sz * sideX); tilt.add(rail); } for (let k = 0; k < 6; k++) { const bar = box(0.045, H * 0.64, 0.04, armour); bar.position.set((k / 5 - 0.5) * L * 0.86, c + H * 0.42, sz * sideX); tilt.add(bar); } } } else if (asty === 'bumper') { // fat rubber buffers around the body for (const sx of [-1, 1]) for (const sz of [-1, 1]) { const b = new T.Mesh(new T.SphereGeometry(0.14, 12, 9), armour); b.scale.set(1, 0.78, 1); b.castShadow = true; b.position.set(sx * L * 0.4, c + H * 0.42, sz * (Wd / 2 + 0.055)); tilt.add(b); } for (const sx of [-1, 1]) { const b = box(0.11, H * 0.52, Wd * 0.78, armour); b.position.set(sx * (L / 2 + 0.055), c + H * 0.42, 0); tilt.add(b); } } else if (asty === 'tiles') { // ceramic tiles with gaps for (const sz of [-1, 1]) for (let kx = 0; kx < 4; kx++) for (let ky = 0; ky < 2; ky++) { const tl = box(L * 0.19, H * 0.32, 0.05, armour); tl.position.set((kx / 3 - 0.5) * L * 0.74, c + H * (0.26 + ky * 0.36), sz * sideX); tilt.add(tl); } const rr = box(0.05, H * 0.72, Wd * 0.9, armour); rr.position.set(-L / 2 - 0.028, c + H * 0.42, 0); tilt.add(rr); } else { // solid plates (steel / titanium / hardox / spiked) for (const sz of [-1, 1]) { 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); 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); } if (asty === 'spike') for (let k = 0; k < 4; k++) { const sp = cyl(0.001, 0.055, 0.18, m.stripe, 8); sp.rotation.x = sz * Math.PI / 2; sp.position.set((k / 3 - 0.5) * L * 0.62, c + H * 0.5, sz * (Wd / 2 + 0.12)); tilt.add(sp); } } 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); } } mark('armour'); // wheels out.wheels = []; const side = Wd / 2 + (armour ? 0.13 : 0.08); if (wh.kind === 'hover') { // air skirt: glowing ring, no wheels const skirt = new T.Mesh(new T.CylinderGeometry(Wd * 0.6, Wd * 0.72, 0.14, 22), new T.MeshStandardMaterial({ color: 0x223040, metalness: 0.5, roughness: 0.5, emissive: paint.accent, emissiveIntensity: 0.3 })); skirt.scale.x = L / Wd * 0.92; skirt.position.set(-L * 0.04, 0.09, 0); skirt.castShadow = true; tilt.add(skirt); const glow = new T.Mesh(new T.CylinderGeometry(Wd * 0.5, Wd * 0.5, 0.04, 22), new T.MeshBasicMaterial({ color: paint.accent, transparent: true, opacity: 0.5, blending: T.AdditiveBlending, depthWrite: false })); glow.scale.x = L / Wd * 0.92; glow.position.set(-L * 0.04, 0.02, 0); tilt.add(glow); out.wheelR = 0.16; } else if (wh.kind === 'drum') { // one wide roller drum + two small rear casters const R = (c + H) / 2 + 0.08; const dr = cyl(R, R, Wd * 0.92, m.rubber, 20); dr.rotation.x = Math.PI / 2; dr.position.set(-L * 0.04, R, 0); tilt.add(dr); out.wheels.push(dr); const hub = cyl(R * 0.4, R * 0.4, Wd * 0.94, m.hub, 12); hub.rotation.x = Math.PI / 2; hub.position.set(-L * 0.04, R, 0); tilt.add(hub); for (const sz of [-1, 1]) { const w = new T.Group(); w.position.set(-L * 0.42, 0.13, sz * side); const t = cyl(0.13, 0.13, 0.14, m.hub, 10); t.rotation.x = Math.PI / 2; w.add(t); tilt.add(w); out.wheels.push(w); } out.wheelR = R; } else if (wh.kind === 'tracks' || look.tracksLook) { 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 wr = 0.16 * ws; const w = new T.Group(); w.position.set(sx * L * 0.32, wr, sz * (side + (ws - 1) * 0.08)); const tyre = cyl(wr, wr, 0.12 * ws, m.rubber, 16); tyre.rotation.x = Math.PI / 2; w.add(tyre); const hub = cyl(wr * 0.44, wr * 0.44, 0.13 * ws, m.hub, 8); hub.rotation.x = Math.PI / 2; w.add(hub); tilt.add(w); out.wheels.push(w); } out.wheelR = 0.16 * ws; } mark('wheels'); // headlights, or glowing eyes for opponents with a look if (look.eyes) { const eyeMat = new T.MeshStandardMaterial({ color: look.eyes, emissive: look.eyes, emissiveIntensity: 2.2 }); for (const sz of [-1, 1]) { const e = new T.Mesh(new T.SphereGeometry(0.055, 12, 8), eyeMat); e.position.set(L / 2 + 0.01, c + H * (ch.shape === 'wedge' ? 0.3 : 0.7), sz * Wd * 0.24); tilt.add(e); } } else 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); mark('lights'); // main weapon (front = +x) const w = st.weapon; if (w) { const WS = m.wsteel, ED = m.wedge, HZ = m.stripe; // weapon steel, edge, hazard stripe if (w.type === 'wedge') { if (w.id === 'lance') { // long spike lance const shaft = cyl(0.05, 0.05, 0.85, m.dark, 8); shaft.rotation.z = Math.PI / 2; shaft.position.set(L / 2 + 0.42, c + H * 0.4, 0); tilt.add(shaft); const tip = cyl(0.001, 0.13, 0.42, HZ, 12); tip.rotation.z = -Math.PI / 2; tip.position.set(L / 2 + 0.95, c + H * 0.4, 0); tilt.add(tip); } else { // short ramp plate at the front (as before - stays close to the body so it does not poke through) const p = box(0.5, 0.05, Wd * 1.05, ED); p.position.set(L / 2 + 0.18, c * 0.5 + 0.08, 0); p.rotation.z = -0.42; tilt.add(p); const edge = box(0.12, 0.16, Wd * 1.06, HZ); edge.position.set(L / 2 + 0.34, 0.02, 0); tilt.add(edge); } } else if (w.type === 'flipper') { const lf = w.id === 'launcher' ? 1.3 : 1, wf = w.id === 'launcher' ? 1.12 : 1; const piv = new T.Group(); piv.position.set(L / 2 - 0.1, c + H * 0.22, 0); tilt.add(piv); const p = box(0.78 * lf, 0.09, Wd * 0.95 * wf, WS); p.position.x = 0.39 * lf; piv.add(p); const s = box(0.78 * lf, 0.03, Wd * 0.4, accent); s.position.set(0.39 * lf, 0.06, 0); piv.add(s); const edge = box(0.12, 0.14, Wd * 0.98 * wf, HZ); edge.position.set(0.78 * lf, 0, 0); piv.add(edge); const pist = cyl(0.05, 0.05, 0.4, m.hub, 8); pist.rotation.z = 1.0; pist.position.set(0.2, -0.16, 0); tilt.add(pist); // ram out.flipper = piv; } else if (w.type === 'hammer') { const post = box(0.16, 0.26, 0.24, m.dark); post.position.set(-L * 0.1, top + 0.11, 0); tilt.add(post); const piv = new T.Group(); piv.position.set(-L * 0.1, top + 0.16, 0); tilt.add(piv); const arm = box(L * 0.82, 0.1, 0.1, m.dark); arm.position.x = L * 0.41; piv.add(arm); if (w.id === 'mace') { const ball = new T.Mesh(new T.SphereGeometry(0.24, 14, 10), WS); ball.castShadow = true; ball.position.set(L * 0.82, -0.05, 0); piv.add(ball); for (let k = 0; k < 6; k++) { const a = k / 6 * Math.PI * 2; const sp = box(0.16, 0.07, 0.07, HZ); sp.position.set(L * 0.82 + Math.cos(a) * 0.26, -0.05, Math.sin(a) * 0.26); sp.rotation.y = -a; piv.add(sp); } } else { const head = box(0.26, 0.42, 0.26, WS); head.position.set(L * 0.82, -0.12, 0); piv.add(head); // big mallet head const face = box(0.1, 0.44, 0.28, HZ); face.position.set(L * 0.82 + 0.17, -0.12, 0); piv.add(face); const spike = box(0.18, 0.12, 0.12, accent); spike.position.set(L * 0.82, 0.12, 0); piv.add(spike); } out.hammer = piv; } else if (w.type === 'saw') { const arm = box(0.4, 0.12, 0.12, m.dark); arm.position.set(L / 2 + 0.06, c + H * 0.5, 0); tilt.add(arm); if (w.id === 'drill') { // conical auger drill, spins on its forward (x) axis const g = new T.Group(); g.position.set(L / 2 + 0.42, c + H * 0.5, 0); tilt.add(g); const cone = cyl(0.001, 0.2, 0.72, WS, 14); cone.rotation.z = -Math.PI / 2; cone.castShadow = true; g.add(cone); for (let k = 0; k < 3; k++) { const fl = box(0.6, 0.03, 0.06, HZ); fl.position.x = -0.1; fl.rotation.x = k / 3 * Math.PI * 2; g.add(fl); } out.saw = g; out.sawAxis = 'x'; } else { const guard = new T.Mesh(new T.TorusGeometry(0.42, 0.05, 8, 20, Math.PI), accent); // orange guard over the top guard.position.set(L / 2 + 0.28, c + H * 0.5, 0); tilt.add(guard); const g = new T.Group(); g.position.set(L / 2 + 0.28, c + H * 0.5, 0); tilt.add(g); const d = cyl(0.38, 0.38, 0.04, WS, 32); d.rotation.x = Math.PI / 2; g.add(d); for (let k = 0; k < 16; k++) { const t = box(0.11, 0.06, 0.05, WS); const a = k / 16 * Math.PI * 2; t.position.set(Math.cos(a) * 0.4, Math.sin(a) * 0.4, 0); t.rotation.z = a + 0.6; g.add(t); } const hub = cyl(0.09, 0.09, 0.07, accent, 12); hub.rotation.x = Math.PI / 2; g.add(hub); out.saw = g; } } else if (w.type === 'spinner') { if (w.id === 'drum') { const post = cyl(0.1, 0.12, 0.3, m.dark, 12); post.position.set(L / 2 + 0.02, c + H * 0.32, 0); tilt.add(post); const g = new T.Group(); g.position.set(L / 2 + 0.12, c + H * 0.34, 0); tilt.add(g); const drum = cyl(0.3, 0.3, 0.52, WS, 20); drum.castShadow = true; g.add(drum); for (const sx of [-1, 1]) { const tb = box(0.14, 0.54, 0.12, HZ); tb.position.set(sx * 0.3, 0, 0); g.add(tb); } out.spinner = g; } else { const post = cyl(0.1, 0.12, 0.3, m.dark, 12); post.position.set(L / 2 + 0.04, c + H * 0.32, 0); tilt.add(post); const g = new T.Group(); g.position.set(L / 2 + 0.04, c + H * 0.3, 0); tilt.add(g); const bar = box(1.5, 0.12, 0.22, WS); g.add(bar); const stripe = box(1.5, 0.13, 0.08, HZ); g.add(stripe); for (const sx of [-1, 1]) { const t = box(0.18, 0.16, 0.32, accent); t.position.x = sx * 0.72; g.add(t); } out.spinner = g; } } else if (w.type === 'lifter') { const piv = new T.Group(); piv.position.set(L / 2 - 0.08, c + H * 0.16, 0); tilt.add(piv); const bar = box(0.12, 0.09, Wd * 0.95, m.dark); bar.position.set(0.06, 0, 0); piv.add(bar); for (const sz of [-1, 1]) { const prong = box(0.85, 0.07, Wd * 0.26, WS); prong.position.set(0.46, 0, sz * Wd * 0.3); piv.add(prong); const tip = box(0.16, 0.1, Wd * 0.28, HZ); tip.position.set(0.9, 0.015, sz * Wd * 0.3); piv.add(tip); } out.flipper = piv; } else if (w.type === 'vspinner') { const arm = box(0.3, 0.14, 0.14, m.dark); arm.position.set(L / 2 + 0.02, c + H * 0.45, 0); tilt.add(arm); const g = new T.Group(); g.position.set(L / 2 + 0.32, c + H * 0.42, 0); tilt.add(g); const d = cyl(0.45, 0.45, 0.11, WS, 26); d.rotation.x = Math.PI / 2; g.add(d); for (let k = 0; k < 4; k++) { const tth = box(0.22, 0.17, 0.14, HZ); const a = k / 4 * Math.PI * 2; tth.position.set(Math.cos(a) * 0.45, Math.sin(a) * 0.45, 0); tth.rotation.z = a; g.add(tth); } const hub = cyl(0.1, 0.1, 0.14, accent, 12); hub.rotation.x = Math.PI / 2; g.add(hub); out.vspin = 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.32); tilt.add(piv); const j = box(0.68, 0.2, 0.14, WS); j.position.x = 0.34; piv.add(j); for (let k = 0; k < 3; k++) { const tooth = box(0.1, 0.18, 0.14, accent); tooth.position.set(0.3 + k * 0.18, -sz * 0.02, -sz * 0.07); tooth.rotation.z = -sz * 0.25; piv.add(tooth); } piv.userData.side = sz; out.jaws.push(piv); } } } mark('weapon'); // 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.Group(); f.visible = false; g.add(f); const outer = new T.Mesh(new T.ConeGeometry(0.44, 2.7, 16, 1, true), M.flame); outer.rotation.z = Math.PI / 2; outer.position.set(0.45 + 1.35, 0.18, 0); f.add(outer); const core = new T.Mesh(new T.ConeGeometry(0.2, 1.5, 12, 1, true), M.flameCore); core.rotation.z = Math.PI / 2; core.position.set(0.45 + 0.75, 0.18, 0); f.add(core); out.flame = f; out.flameParts = [outer, core]; } 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 === 'zapper') { const coil = cyl(0.1, 0.13, 0.22, m.dark, 12); coil.position.y = 0.2; g.add(coil); for (let k = 0; k < 3; k++) { const ring = new T.Mesh(new T.TorusGeometry(0.1, 0.018, 6, 14), accent); ring.rotation.x = Math.PI / 2; ring.position.y = 0.14 + k * 0.07; g.add(ring); } const rod = cyl(0.022, 0.022, 0.34, m.steel, 8); rod.rotation.z = -Math.PI / 2; rod.position.set(0.2, 0.3, 0); g.add(rod); const ball = new T.Mesh(new T.SphereGeometry(0.06, 10, 8), new T.MeshStandardMaterial({ color: 0x9fd8ff, emissive: 0x3aa0ff, emissiveIntensity: 1.6 })); ball.position.set(0.38, 0.3, 0); g.add(ball); const arc = new T.Mesh(new T.ConeGeometry(0.13, 1.5, 8, 1, true), M.arc); arc.rotation.z = -Math.PI / 2; arc.position.set(1.1, 0.3, 0); arc.visible = false; g.add(arc); out.gun = coil; out.muzzle = ball; out.flame = arc; // reuse the beam slots } else if (tw.type === 'mortar') { const base2 = cyl(0.14, 0.16, 0.1, m.dark, 12); base2.position.y = 0.14; g.add(base2); const tube = cyl(0.11, 0.13, 0.52, m.steel, 14); tube.position.set(-0.05, 0.36, 0); tube.rotation.z = 0.5; g.add(tube); const mouth = cyl(0.13, 0.13, 0.05, accent, 14); mouth.position.set(0.1, 0.58, 0); mouth.rotation.z = 0.5; g.add(mouth); for (const sz of [-1, 1]) { const leg = cyl(0.02, 0.02, 0.3, m.dark, 6); leg.position.set(0.12, 0.17, sz * 0.1); leg.rotation.z = -0.5; g.add(leg); } } else if (tw.type === 'swarm') { const pod = box(0.42, 0.3, 0.42, accent); pod.position.set(0.02, 0.28, 0); g.add(pod); for (const y of [0.19, 0.3, 0.41]) for (const sz of [-0.12, 0, 0.12]) { const tube = cyl(0.04, 0.04, 0.07, m.dark, 8); tube.rotation.z = Math.PI / 2; tube.position.set(0.23, y, sz); g.add(tube); } } 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; } mark('tower'); // gadget: small pod on the rear deck, colour-coded by type const gd = st.gadget; if (gd) { const gc = { righter: 0x40c0ff, turbo: 0xff5020, shield: 0x40e0c0, repair: 0x50e060, emp: 0xc060ff }[gd.type] || 0xffffff; const pod = box(0.24, 0.16, 0.3, m.dark); pod.position.set(-L * 0.33, top + 0.02, 0); tilt.add(pod); const led = new T.Mesh(new T.SphereGeometry(0.05, 8, 6), new T.MeshStandardMaterial({ color: gc, emissive: gc, emissiveIntensity: 0.8, roughness: 0.4 })); led.position.set(-L * 0.33, top + 0.12, 0); tilt.add(led); } mark('gadget'); 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[R.sawAxis || 'z'] = -R.sawA; } if (R.spinner) { R.spinA += dt * r.spin * 38; R.spinner.rotation.y = R.spinA; } if (R.vspin) { R.spinA += dt * r.spin * 40; R.vspin.rotation.z = -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) { const tw2 = R.st.tower, beam = r.flameOn || (tw2 && tw2.type === 'zapper' && r.gunOn); R.flame.visible = !!beam; if (beam) { const f = 0.85 + Math.random() * 0.3; R.flame.scale.set(f, 1, f); if (R.flameParts) for (const fp of R.flameParts) { const n = 0.78 + Math.random() * 0.5; fp.scale.set(n, 0.9 + Math.random() * 0.35, n); } } } 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 // theme colours per arena (combat.js arena.theme) const THEME = { steel: { bg: 0x07090d, fog: 0x07090d, rimA: 0xff7a1a, rimB: 0x4fa8ff, hazard: 0xd33 }, lava: { bg: 0x0d0604, fog: 0x1a0a06, rimA: 0xff5a1a, rimB: 0xff2a10, hazard: 0xff6020 }, blue: { bg: 0x05080f, fog: 0x070d1a, rimA: 0x3aa0ff, rimB: 0x9a60ff, hazard: 0x40c0ff }, }; // persistent lights (reused across arena rebuilds) function buildArena(scene) { const out = {}; 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(1024, 1024); // 2048 was a big per-frame shadow pass; 1024 is plenty at this scale Object.assign(key.shadow.camera, { left: -11, right: 11, top: 11, bottom: -11, near: 1, far: 50 }); key.shadow.bias = -0.0008; scene.add(key); const rimA = new T.PointLight(0xff7a1a, 1.1, 22); scene.add(rimA); const rimB = new T.PointLight(0x4fa8ff, 0.9, 22); scene.add(rimB); out.lights = { key, rimA, rimB, hemi: scene.children.find(o => o.isHemisphereLight) }; out.flashLight = new T.PointLight(0xffaa55, 0, 9); scene.add(out.flashLight); out.scene = scene; out.group = null; out.id = null; out.rocketGeo = new T.CylinderGeometry(0.05, 0.05, 0.32, 8); out.rocketGeo.rotateZ(Math.PI / 2); out.rocketMat = new T.MeshStandardMaterial({ color: 0xe8e8e8, emissive: 0xff5a10, emissiveIntensity: 0.75 }); out.rocketMeshes = []; out.mineMeshes = []; buildArenaGeo(out, RC.ARENA); return out; } function disposeGroup(g) { g.traverse(o => { if (o.geometry) o.geometry.dispose(); if (o.material) [].concat(o.material).forEach(mm => { if (mm.map && mm.map.dispose) mm.map.dispose(); mm.dispose(); }); }); } // (re)build all arena geometry for arena def A into a fresh group function buildArenaGeo(out, A) { const m = mats(), half = A.half, th = THEME[A.theme] || THEME.steel; const scene = out.scene; if (out.group) { scene.remove(out.group); disposeGroup(out.group); } const group = new T.Group(); scene.add(group); out.group = group; out.id = A.id; const scene_add = o => group.add(o); scene.background = new T.Color(th.bg); scene.fog = new T.Fog(th.fog, half * 3, half * 5.5); const { key, rimA, rimB, flashLight } = out.lights; const sc = Math.max(11, half + 3); Object.assign(key.shadow.camera, { left: -sc, right: sc, top: sc, bottom: -sc }); key.shadow.camera.updateProjectionMatrix(); rimA.color.setHex(th.rimA); rimA.position.set(-half - 1, 5, half + 1); rimA.distance = half * 2.6; rimB.color.setHex(th.rimB); rimB.position.set(half + 1, 5, -half - 1); rimB.distance = half * 2.6; out.hazardCol = th.hazard; { const scene = { add: scene_add }; buildArenaBody(scene, out, A, m, half, th); } } // arena outline (same maths as wallPush in combat.js): square, octagon or a many-sided ring function outline(A) { const h = A.half; if (A.shape === 'round') return Array.from({ length: 28 }, (_, i) => { const a = i / 28 * Math.PI * 2; return [Math.cos(a) * h, Math.sin(a) * h]; }); if (A.shape === 'octagon') { const d = h * 0.67; return [[-d, -h], [d, -h], [h, -d], [h, d], [d, h], [-d, h], [-h, d], [-h, -d]]; } return [[-h, -h], [h, -h], [h, h], [-h, h]]; } function floorGeo(A) { const h = A.half; if (A.shape === 'square') return new T.PlaneGeometry(h * 2, h * 2); const pts = outline(A), sh = new T.Shape(); sh.moveTo(pts[0][0], pts[0][1]); for (let i = 1; i < pts.length; i++) sh.lineTo(pts[i][0], pts[i][1]); sh.closePath(); const g = new T.ShapeGeometry(sh), pos = g.attributes.position, uv = new Float32Array(pos.count * 2); for (let i = 0; i < pos.count; i++) { uv[i * 2] = pos.getX(i) / (h * 2) + 0.5; uv[i * 2 + 1] = pos.getY(i) / (h * 2) + 0.5; } g.setAttribute('uv', new T.BufferAttribute(uv, 2)); return g; } function buildArenaBody(scene, out, A, m, half, th) { // floor const floor = new T.Mesh(floorGeo(A), new T.MeshStandardMaterial({ map: floorTex(A), 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 }); // ComfyGen wall panels (tex_wall.jpg) on the arena walls, if the browser lets the image into WebGL const panelMat = new T.MeshStandardMaterial({ color: 0x30343b, metalness: 0.6, roughness: 0.5 }); loadTex('tex_wall.jpg', tex => { tex.wrapS = T.RepeatWrapping; tex.repeat.set(5, 1); panelMat.map = tex; panelMat.color.set(0xb0b4bc); panelMat.needsUpdate = true; }); const pts = outline(A); let perim = 0; for (let i = 0; i < pts.length; i++) { const a2 = pts[i], b2 = pts[(i + 1) % pts.length]; perim += Math.hypot(b2[0] - a2[0], b2[1] - a2[1]); } const spikes = new T.InstancedMesh(new T.ConeGeometry(0.07, 0.28, 8), m.steel, Math.ceil(perim / 0.5) + pts.length + 4); const dm = new T.Object3D(); let si = 0; for (let i = 0; i < pts.length; i++) { const [x1, z1] = pts[i], [x2, z2] = pts[(i + 1) % pts.length]; const mx = (x1 + x2) / 2, mz = (z1 + z2) / 2, len = Math.hypot(x2 - x1, z2 - z1), ang = Math.atan2(z2 - z1, x2 - x1); const d = Math.hypot(mx, mz) || 1, ox = mx / d, oz = mz / d; // outwards from the middle const wall = box(len + 0.3, 0.55, 0.25, panelMat); wall.position.set(mx + ox * 0.12, 0.275, mz + oz * 0.12); wall.rotation.y = -ang; scene.add(wall); const strip = box(len + 0.3, 0.08, 0.26, m.stripe); strip.position.set(mx + ox * 0.12, 0.58, mz + oz * 0.12); strip.rotation.y = -ang; scene.add(strip); const steps = Math.max(1, Math.round(len / 0.5)); for (let k = 0; k < steps; k++) { const f = (k + 0.5) / steps; dm.position.set(x1 + (x2 - x1) * f - ox * 0.02, 0.3, z1 + (z2 - z1) * f - oz * 0.02); dm.rotation.set(0, 0, 0); dm.rotation.z = Math.PI / 2; dm.rotation.y = -Math.atan2(oz, ox); // point inwards dm.updateMatrix(); if (si < spikes.count) spikes.setMatrixAt(si++, dm.matrix); } } spikes.count = si; 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); const rig = new T.Group(); rig.position.set(px * (half + 0.2), 3.5, pz * (half + 0.2)); scene.add(rig); const hous = box(0.46, 0.36, 0.34, new T.MeshStandardMaterial({ color: 0x23272e, metalness: 0.6, roughness: 0.5 })); hous.castShadow = true; rig.add(hous); const lens = box(0.3, 0.2, 0.3, m.lamp); lens.position.set(-px * 0.13, -0.12, -pz * 0.13); rig.add(lens); const arm = cyl(0.035, 0.035, 0.36, m.dark, 6); arm.position.set(px * 0.12, 0.26, pz * 0.12); rig.add(arm); } // audience stands behind the back walls const seats = new T.InstancedMesh(new T.BoxGeometry(0.32, 0.5, 0.32), new T.MeshStandardMaterial({ color: 0x1b1d24, roughness: 0.95 }), 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); seats.instanceMatrix.needsUpdate = true; if (seats.instanceColor) seats.instanceColor.needsUpdate = true; scene.add(seats); // floor saws // --- cover and floor traps (static, built once per arena) --- const BMAT = { pillar: new T.MeshStandardMaterial({ color: 0x4a515c, metalness: 0.5, roughness: 0.6 }), crate: new T.MeshStandardMaterial({ color: 0x6b5634, metalness: 0.2, roughness: 0.85 }), scrap: new T.MeshStandardMaterial({ color: 0x5a5048, metalness: 0.6, roughness: 0.8 }), barrier: new T.MeshStandardMaterial({ color: 0x9aa0a8, metalness: 0.6, roughness: 0.5 }), }; let bseed = 7; const brnd = () => { bseed = (bseed * 16807) % 2147483647; return bseed / 2147483647; }; for (const b of A.blocks) { const g = new T.Group(); g.position.set(b.x, 0, b.z); scene.add(g); const mat = BMAT[b.kind] || BMAT.crate; if (b.kind === 'pillar') { const rr = Math.min(b.hw, b.hd); const col = cyl(rr, rr * 1.12, b.h, mat, 14); col.position.y = b.h / 2; col.castShadow = true; g.add(col); const cap = cyl(rr * 1.18, rr * 1.18, 0.12, m.dark, 14); cap.position.y = b.h; g.add(cap); const band = cyl(rr * 1.06, rr * 1.06, 0.18, m.stripe, 14); band.position.y = b.h * 0.42; g.add(band); } else if (b.kind === 'scrap') { for (let k = 0; k < 5; k++) { const w2 = b.hw * (0.5 + brnd() * 0.75), h2 = b.h * (0.35 + brnd() * 0.65), d2 = b.hd * (0.5 + brnd() * 0.75); const p2 = box(w2, h2, d2, mat); p2.position.set((brnd() - 0.5) * b.hw, h2 / 2, (brnd() - 0.5) * b.hd); p2.rotation.y = brnd() * 3; p2.castShadow = true; g.add(p2); } } else if (b.kind === 'barrier') { const bar = box(b.hw * 2, b.h, b.hd * 2, mat); bar.position.y = b.h / 2; bar.castShadow = true; g.add(bar); const top = box(b.hw * 2, 0.1, b.hd * 2.1, m.stripe); top.position.y = b.h; g.add(top); } else { const bx = box(b.hw * 2, b.h, b.hd * 2, mat); bx.position.y = b.h / 2; bx.castShadow = true; g.add(bx); for (const sy of [0.28, 0.74]) { const band = box(b.hw * 2.05, 0.06, b.hd * 2.05, m.dark); band.position.y = b.h * sy; g.add(band); } } } for (const b of A.belts) { const g = new T.Group(); g.position.set(b.x, 0.012, b.z); scene.add(g); const pad = new T.Mesh(new T.PlaneGeometry(b.hw * 2, b.hd * 2), new T.MeshStandardMaterial({ color: 0x191d24, roughness: 0.92 })); pad.rotation.x = -Math.PI / 2; pad.receiveShadow = true; g.add(pad); const along = Math.abs(b.ax) > Math.abs(b.az); // slats across the running direction for (let k = -3; k <= 3; k++) { const slat = box(along ? 0.14 : b.hw * 1.9, 0.05, along ? b.hd * 1.9 : 0.14, m.stripe); slat.position.set(along ? k * b.hw / 3.4 : 0, 0.03, along ? 0 : k * b.hd / 3.4); g.add(slat); } } for (const o of A.oil) { const sl = new T.Mesh(new T.CircleGeometry(o.r, 22), new T.MeshStandardMaterial({ color: 0x06070b, roughness: 0.2, metalness: 0.75 })); sl.rotation.x = -Math.PI / 2; sl.position.set(o.x, 0.014, o.z); scene.add(sl); } out.rams = A.rams.map(d => { const g = new T.Group(); g.position.set(d.x, 0, d.z); scene.add(g); const head = box(0.5, 0.72, d.hd * 1.8, m.steel); head.position.y = 0.4; head.castShadow = true; g.add(head); const face = box(0.12, 0.74, d.hd * 1.85, m.stripe); face.position.set(d.nx * 0.3, 0.4, d.nz * 0.3); g.add(face); return { d, g, x0: d.x, z0: d.z }; }); 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; }); } // ---------------------------------------------------------------- garage // transparent: the painted workshop (bg_workshop.jpg, CSS) shows through; only a shadow catcher function buildGarage(scene) { scene.background = null; 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); const floor = new T.Mesh(new T.PlaneGeometry(30, 30), new T.ShadowMaterial({ opacity: 0.55 })); 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); return { turntable, spot }; } // ---------------------------------------------------------------- view RC.buildRobotModel = buildRobot; RC.Render = function (canvas, W, H) { const renderer = new T.WebGLRenderer({ canvas, antialias: true, alpha: true, powerPreference: 'high-performance' }); renderer.setClearColor(0x000000, 0); 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 asp = W / H; const cam = new T.OrthographicCamera(-1, 1, 1, -1, 0.1, 200); const camBase = new T.Vector3(15.2, 15.5, 15.2), camLook = new T.Vector3(-1.25, 0, -1.25); // fit the orthographic camera to an arena of half-size h function fitCam(h) { const VIEW = h * 1.95; cam.left = -VIEW * asp / 2; cam.right = VIEW * asp / 2; cam.top = VIEW / 2; cam.bottom = -VIEW / 2; cam.updateProjectionMatrix(); const d = h * 1.9; camBase.set(d, h * 1.95, d); camLook.set(-h * 0.16, 0, -h * 0.16); } fitCam(RC.ARENA.half); cam.position.copy(camBase); cam.lookAt(camLook); const gcam = new T.PerspectiveCamera(34, asp, 0.1, 60); // put the shop camera at the current zoom (base distance / zoom, aimed at the turntable) function placeGarageCam() { const look = new T.Vector3(0, 0.3, 0), base = new T.Vector3(3.7, 2.5, 4.4); gcam.position.copy(look).add(base.clone().sub(look).multiplyScalar(1 / gcamZoom)); gcam.lookAt(look); } gcam.setViewOffset(W, H, GARAGE_OFF_X, GARAGE_OFF_Y, 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); let mode = 'garage', gRobot = null, robots = [], state = null, shake = 0, flashT = 0, introT = 0, scale = 1, hold = 0; 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 let demoT = 0; function demoGarage(dt) { const R = gRobot; if (!R) return; demoT += dt; const c = demoT % 3; // one signature move every 3 s if (R.saw) { R.sawA += dt * 26; R.saw.rotation[R.sawAxis || 'z'] = -R.sawA; } if (R.spinner) { R.spinA += dt * 17; R.spinner.rotation.y = R.spinA; } if (R.vspin) { R.spinA += dt * 19; R.vspin.rotation.z = -R.spinA; } if (R.hammer) { const k = c < 0.35 ? c / 0.35 : c < 1.2 ? 1 - (c - 0.35) / 0.85 : 0; R.hammer.rotation.z = lerp(1.9, -0.25, k); } if (R.flipper) R.flipper.rotation.z = -0.32 + (c < 0.3 ? Math.sin(c / 0.3 * Math.PI) * 1.5 : 0); if (R.jaws) { const k = c < 1.2 ? Math.abs(Math.sin(c / 1.2 * Math.PI)) : 0; for (const j of R.jaws) j.rotation.y = j.userData.side * (0.45 - k * 0.63); } if (R.gun) { R.gunA += dt * 16; R.gun.rotation.x = R.gunA; } const shooting = c > 1.5 && c < 2.3; if (R.flame) R.flame.visible = shooting; if (R.muzzle) R.muzzle.visible = shooting && Math.random() < 0.5; // launchers have no moving part: show a short recoil kick instead const tw2 = R.st.tower; if (R.turret && tw2 && (tw2.type === 'mortar' || tw2.type === 'swarm')) { const kick = c > 1.5 && c < 1.75 ? Math.sin((c - 1.5) / 0.25 * Math.PI) : 0; R.turret.rotation.z = -0.14 * kick; } } 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]; }); const api = { get mode() { return mode; }, get introTime() { return introT; }, resize(sc) { scale = sc; // the stage is scaled up by CSS; rendering at full devicePixelRatio on top of that is the main CPU/GPU hog const cap = quality === 'low' ? 1 : quality === 'medium' ? 1.25 : 1.5; renderer.setPixelRatio(Math.min(cap, sc * (root.devicePixelRatio || 1))); renderer.setSize(W, H); }, // '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; // soft shadows only on high 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, arena: 'workshop' }), builds, paints); mode = 'intro'; introT = 0; pointMode(true); // compile every shader up front, otherwise the first seconds stutter while they are built try { renderer.compile(arenaScene, pcam); } catch (e) { /* older three: just carry on */ } }, skipIntro() { if (mode === 'intro' && introT < INTRO_END) introT = INTRO_END; }, 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); }, // 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; }, portrait(build, paint) { api.showGarage(build, paint); garage.turntable.rotation.y = 0.55; gcam.clearViewOffset(); gcam.position.set(1.75, 1.3, 2.1); gcam.lookAt(0, 0.4, 0); renderer.render(garageScene, gcam); let url = ''; try { url = canvas.toDataURL('image/png'); } catch (e) { /* tainted canvas: no picture */ } gcam.setViewOffset(W, H, GARAGE_OFF_X, GARAGE_OFF_Y, W, H); api.paintCam(false); return url; }, paintCam(on) { gcamZoom = GZ_DEF; if (on) { gcam.position.set(2.6, 2.2, 3.1); gcam.lookAt(0, 0.3, 0); } else placeGarageCam(); }, // garage preview zoom (shop "- Zoom +"): move the camera closer / further zoomGarage(mult) { gcamZoom = Kit.clamp(gcamZoom * mult, GZ_MIN, GZ_MAX); placeGarageCam(); }, resetGarageZoom() { gcamZoom = GZ_DEF; placeGarageCam(); }, // rebuild the arena scene if the fight uses a different one; rescale the camera setArena(A) { if (!A || arena.id === A.id) { if (A) fitCam(A.half); return; } RC.setArena(A.id); robots.forEach(R => arenaScene.remove(R.root)); robots = []; buildArenaGeo(arena, A); fitCam(A.half); }, 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); 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); arena.rocketMeshes = []; for (const m of arena.mineMeshes) arenaScene.remove(m); arena.mineMeshes = []; }, 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 'mine_drop': burst(e.x, 0.12, e.z, 6, 2, [1, 0.8, 0.3]); break; case 'broke': breakPart(robots[e.who], e.part); burst(e.x, 0.45, e.z, 22, 5, [1, 0.8, 0.4]); puff(e.x, e.z, 6); shake = Math.max(shake, 0.28); 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; } } }, // fight camera: 'iso' (whole arena) | 'chase' (behind robot 0) | 'fpv' (cockpit of robot 0) get camMode() { return camMode; }, setCam(m) { camMode = m; chaseInit = false; pcam.fov = m === 'fpv' ? 72 : 55; pcam.updateProjectionMatrix(); if (mode === 'arena') pointMode(m !== 'iso'); if (robots[0] && robots[0].turret) robots[0].turret.visible = m !== 'fpv'; }, project(x, y, z) { const v = new T.Vector3(x, y, z).project(mode === 'arena' && camMode !== 'iso' ? pcam : cam); // 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 }; }, frame(dt, now) { if (mode === 'garage') { if (hold > 0) hold -= dt; else garage.turntable.rotation.y += dt * 0.45; demoGarage(dt); renderer.render(garageScene, gcam); return; } 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; } 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; flashT = Math.max(0, flashT - dt * 3); arena.flashLight.intensity = flashT * 4; if (camMode !== 'iso' && state && robots[0]) { followCam(dt, sx, sz); renderer.render(arenaScene, pcam); return; } cam.position.set(camBase.x + sx, camBase.y, camBase.z + sz); cam.lookAt(camLook.x + sx, camLook.y, camLook.z + sz); renderer.render(arenaScene, cam); }, }; // 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(); 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); let px, py, pz, lx, ly, lz; if (camMode === 'fpv') { const k = R.st.len * 0.08; px = r.x + fx * k; pz = r.z + fz * k; py = Math.max(0.25, r.y) + R.top + 0.55; lx = px + fx * 6; lz = pz + fz * 6; ly = py - 1.25; cpos.set(px + sx * 0.3, py, pz + sz * 0.3); clook.set(lx, ly, lz); } 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); } pcam.position.copy(cpos); pcam.lookAt(clook); } // ---- intro: dark arena -> floodlights flicker on -> camera sweeps up -> two robots are lowered in const INTRO_END = RC.INTRO_END = 5.2; 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 < 0.25 ? 0.12 : t < 0.4 ? 1 : t < 0.55 ? 0.15 : t < 0.7 ? 0.9 : t < 0.85 ? 0.3 : 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(9, 20.5, e); h = lerp(2.4, 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 - 0.9 - i * 0.3) / 1.3, 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 > 3.1 ? Math.min(1, (t - 3.1) * 1.1) : 0; // spinners rev up rb.sawOn = t > 3.3; rb.flameOn = t > 3.5 && t < 4.3 || (t > INTRO_END && Math.sin(t * 0.9 + i * 2) > 0.97); rb.turretA = rb.a + Math.sin(t * 0.7 + i) * 0.4; }); } function burst(x, y, z, n, sp, c) { n = Math.max(1, Math.round(n * fxMul)); 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); } // parts torn off in a fight: the real mesh leaves the robot and tumbles over the floor const debris = []; function dropPiece(obj) { const wp = new T.Vector3(), wq = new T.Quaternion(); 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); } } function updateDebris(dt) { 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); } } } function clearDebris() { for (const d of debris) arenaScene.remove(d.o); 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; if (part === 'wheels') { const w = (R.wheels || []).find(o => o.parent === R.tilt); if (w) { R.wheels.splice(R.wheels.indexOf(w), 1); dropPiece(w); } return; } const kill = R.tilt.children.filter(o => o.userData.part === part && o.visible); if (!kill.length) return; const main = kill[0]; for (const o of kill) if (o !== main) o.visible = false; dropPiece(main); if (part === 'weapon') { R.saw = null; R.spinner = null; R.hammer = null; R.flipper = null; R.jaws = null; } if (part === 'tower') { R.turret = null; R.flame = null; R.gun = null; R.muzzle = null; } } function animateArena(dt, now) { const s = state; if (!s) return; const A = state.arena || RC.ARENA; updateDebris(dt); 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); } // minigun: glowing tracers flying out of the barrel so you can see where it shoots if (r.gunOn && R.st.tower && R.st.tower.type === 'minigun') { const ca = Math.cos(r.turretA), sa = Math.sin(r.turretA), y0 = r.y + R.top + 0.2; for (let q = 0; q < 2; q++) { const sp = 46 + Math.random() * 14, sprd = (Math.random() - 0.5) * 0.06; sparks.spawn(r.x + ca * 0.62, y0, r.z + sa * 0.62, (ca - sa * sprd) * sp, (Math.random() - 0.5) * 0.6, (sa + ca * sprd) * sp, 0.2, 1, 0.92, 0.55); } if (Math.random() < 0.5) smoke.spawn(r.x + ca * 0.6, y0, r.z + sa * 0.6, ca * 0.6, 0.5, sa * 0.6, 0.5, 0.42, 0.42, 0.42); } 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); } }); // wall rams punch out while they are armed for (const rm of arena.rams || []) { const on = RC.hazardOn(rm.d, s.t) ? 1 : 0, k = Math.min(1, dt * (on ? 20 : 5)); rm.g.position.x = lerp(rm.g.position.x, rm.x0 + rm.d.nx * on * 1.2, k); rm.g.position.z = lerp(rm.g.position.z, rm.z0 + rm.d.nz * on * 1.2, k); } // 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 // mines lying on the floor: dark puck, the lamp blinks fast once it is armed const mines = s.mines || []; while (arena.mineMeshes.length < mines.length) { const g = new T.Group(); const puck = new T.Mesh(new T.CylinderGeometry(0.3, 0.34, 0.11, 14), new T.MeshStandardMaterial({ color: 0x23262c, metalness: 0.6, roughness: 0.6 })); puck.position.y = 0.055; puck.castShadow = true; g.add(puck); const ring = new T.Mesh(new T.TorusGeometry(0.3, 0.03, 6, 18), new T.MeshStandardMaterial({ color: 0xf2b51c, roughness: 0.6 })); ring.rotation.x = Math.PI / 2; ring.position.y = 0.1; g.add(ring); const lamp = new T.Mesh(new T.SphereGeometry(0.06, 8, 6), new T.MeshStandardMaterial({ color: 0x661111, emissive: 0xff2010, emissiveIntensity: 1 })); lamp.position.y = 0.15; g.add(lamp); g.userData.lamp = lamp; arenaScene.add(g); arena.mineMeshes.push(g); } arena.mineMeshes.forEach((g, i) => { const mn = mines[i]; g.visible = !!mn; if (!mn) return; g.position.set(mn.x, 0.01, mn.z); const armed = mn.arm <= 0; const blink = armed ? (Math.sin(now / 90) > 0 ? 2.4 : 0.15) : 0.5; g.userData.lamp.material.emissiveIntensity = blink; }); while (arena.rocketMeshes.length < s.rockets.length) { const gr = new T.Group(); const body = new T.Mesh(arena.rocketGeo, arena.rocketMat); gr.add(body); const nose = new T.Mesh(new T.ConeGeometry(0.055, 0.12, 8), arena.rocketMat); nose.rotation.z = -Math.PI / 2; nose.position.x = 0.21; gr.add(nose); for (const sz of [-1, 1]) { const fin = new T.Mesh(new T.BoxGeometry(0.09, 0.012, 0.07), arena.rocketMat); fin.position.set(-0.13, 0, sz * 0.045); gr.add(fin); } arenaScene.add(gr); arena.rocketMeshes.push(gr); } 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; const bx = k.x - Math.cos(k.a) * 0.22, bz = k.z - Math.sin(k.a) * 0.22; for (let q = 0; q < 2; q++) fire.spawn(bx, k.y, bz, (Math.random() - 0.5) * 0.4, 0.25, (Math.random() - 0.5) * 0.4, 0.26, 1, 0.65, 0.22); smoke.spawn(bx, k.y, bz, (Math.random() - 0.5) * 0.3, 0.35, (Math.random() - 0.5) * 0.3, 1.1, 0.55, 0.55, 0.55); } }); } api.resize(1); return api; }; })(window);