Grosse Erweiterung ueber die erste spielbare Version hinaus: - Online (Firebase ueber REST, kein SDK): Konto per E-Mail, globale Lobby mit Laender-Raeumen, Herausfordern, Live-PvP (Host rechnet, Gast interpoliert, KI-Uebernahme bei Lag). Gast kann anonym beitreten. net.js / lobby.js / netfight.js, Regeln in docs/firebase. - 3 Arenen in verschiedenen Groessen (RC.ARENAS, half 8/12/16), Kamera + Licht + Boden skalieren; Liga skaliert die Arena mit, Setting + online aus Seed. Harte Rams/Wandtreffer schleudern Roboter auf den Rucken. - Intro (3D-Kamerafahrt), Titel, Einstellungen (Kamera, Schwierigkeit, Steuerung, Grafik, Arena), Lackiererei (painter.js, selbst bemalen). - Shop mit ComfyGen-Sprites (aus den 3D-Teilen erzeugt), Liga-Gegner sehen aus wie ihre Portraits (looks.js), kraeftigere Waffen-Modelle. - Oekonomie: Startgeld 800, Schadensbonus, Schaukaempfe, Online-Preisgeld. - Verschleierung: build.ps1 -Obfuscate (terser), tablet-taugliche Touch-UI. - Tests: tests/sim.test.js, balance.js, online.test.js. Dev-Tools in tools/. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
971 lines
56 KiB
JavaScript
971 lines
56 KiB
JavaScript
/* 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.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;
|
|
}
|
|
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;
|
|
}
|
|
|
|
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';
|
|
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 {
|
|
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 plates (sides + rear), bolts
|
|
if (armour) {
|
|
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); }
|
|
}
|
|
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 === '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') {
|
|
// 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 piv = new T.Group(); piv.position.set(L / 2 - 0.1, c + H * 0.22, 0); tilt.add(piv);
|
|
const p = box(0.78, 0.09, Wd * 0.95, WS); p.position.x = 0.39; piv.add(p);
|
|
const s = box(0.78, 0.03, Wd * 0.4, accent); s.position.set(0.39, 0.06, 0); piv.add(s);
|
|
const edge = box(0.12, 0.14, Wd * 0.98, HZ); edge.position.set(0.78, 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);
|
|
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);
|
|
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') {
|
|
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 === '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.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;
|
|
}
|
|
mark('tower');
|
|
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
|
|
// 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(2048, 2048);
|
|
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: 0xdddddd, emissive: 0x331100 });
|
|
out.rocketMeshes = [];
|
|
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); }
|
|
}
|
|
|
|
function buildArenaBody(scene, out, A, m, half, th) {
|
|
// floor
|
|
const floor = new T.Mesh(new T.PlaneGeometry(half * 2, half * 2), 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 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, panelMat);
|
|
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);
|
|
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);
|
|
}
|
|
// 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;
|
|
});
|
|
}
|
|
|
|
// ---------------------------------------------------------------- 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 });
|
|
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);
|
|
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);
|
|
|
|
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;
|
|
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;
|
|
const cap = quality === 'low' ? 1 : quality === 'medium' ? 1.5 : 3;
|
|
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;
|
|
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);
|
|
},
|
|
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, 300, 40, W, H); api.paintCam(false);
|
|
return url;
|
|
},
|
|
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); },
|
|
// 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(); 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;
|
|
}
|
|
}
|
|
},
|
|
// 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;
|
|
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 = 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;
|
|
});
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
function animateArena(dt, now) {
|
|
const s = state; if (!s) return;
|
|
const A = state.arena || 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);
|