Skip to content

gitflarePublicPartly agent-ready40

orbit-duel

Real-time two-player orbital combat game on Cloudflare Workers and Durable Objects.

Add the Apache-2.0 licence0413e56
JavaScript96 lines5.4 KB
const N = 600;const TWO_PI = Math.PI * 2;export function createFx() {  const px = new Float32Array(N), py = new Float32Array(N), vx = new Float32Array(N), vy = new Float32Array(N);  const life = new Float32Array(N), max = new Float32Array(N), hue = new Float32Array(N);  const alive = new Uint8Array(N);  let next = 0;  const fx = { shake: 0, hitStop: 0, flash: 0 };  const R = 24, rx = new Float32Array(R), ry = new Float32Array(R), rr = new Float32Array(R), rl = new Float32Array(R), rm = new Float32Array(R), rh = new Float32Array(R);  const D = 48, dx = new Float32Array(D), dy = new Float32Array(D), dvx = new Float32Array(D), dvy = new Float32Array(D), da = new Float32Array(D), dw = new Float32Array(D), dl = new Float32Array(D), dh = new Float32Array(D);  let rn = 0, dn = 0;  // pooled exhaust trail: T points ring buffer  const T = 12, tx = new Float32Array(T), ty = new Float32Array(T), tl = new Float32Array(T);  let tn = 0;  fx.trail = (x, y) => { const i = tn; tn = (tn + 1) % T; tx[i] = x; ty[i] = y; tl[i] = 0.4; };  // shield shimmer pool  const S = 8, sx = new Float32Array(S), sy = new Float32Array(S), sl = new Float32Array(S);  let sn = 0;  fx.shimmer = (x, y) => { const i = sn; sn = (sn + 1) % S; sx[i] = x; sy[i] = y; sl[i] = 0.3; };  fx.sparks = (x, y, shielded) => { fx.emit(x, y, 12, 220, shielded ? 200 : 40, 0.4); if (shielded) fx.shimmer(x, y); };  fx.pickup = (x, y) => { fx.emit(x, y, 25, 200, 150, 0.7); fx.ring(x, y, 50, 150, 0.4); };  fx.ring = (x, y, maxR, h, l) => { const i = rn; rn = (rn + 1) % R; rx[i] = x; ry[i] = y; rr[i] = maxR; rl[i] = rm[i] = l || 0.5; rh[i] = h; };  fx.debris = (x, y, count, speed, h) => { for (let k = 0; k < count; k++) { const i = dn; dn = (dn + 1) % D; const a = Math.random() * TWO_PI, s = speed * (0.3 + Math.random() * 0.7); dx[i] = x; dy[i] = y; dvx[i] = Math.cos(a) * s; dvy[i] = Math.sin(a) * s; da[i] = a; dw[i] = (Math.random() - 0.5) * 12; dl[i] = 1; dh[i] = h; } };  fx.explode = (x, y, h, local, distScale) => {    fx.emit(x, y, 60, 320, h, 1); fx.ring(x, y, 120, h, 0.6); fx.debris(x, y, 6, 160, h);    fx.addShake(14 * (distScale === undefined ? 1 : distScale));    if (local) { fx.hitStop = 2; fx.flash = 1; }  };  fx.emit = (x, y, count, speed, h, l) => {    for (let k = 0; k < count; k++) {      const i = next; next = (next + 1) % N;      const a = Math.random() * TWO_PI, s = speed * (0.2 + Math.random() * 0.8);      px[i] = x; py[i] = y; vx[i] = Math.cos(a) * s; vy[i] = Math.sin(a) * s;      life[i] = max[i] = l * (0.5 + Math.random() * 0.5); hue[i] = h; alive[i] = 1;    }  };  fx.addShake = (a) => { fx.shake = Math.min(24, fx.shake + a); };  fx.update = (dt) => {    fx.flash = Math.max(0, fx.flash - dt * 4);    for (let i = 0; i < T; i++) if (tl[i] > 0) tl[i] -= dt;    for (let i = 0; i < S; i++) if (sl[i] > 0) sl[i] -= dt;    for (let i = 0; i < R; i++) if (rl[i] > 0) rl[i] -= dt;    for (let i = 0; i < D; i++) if (dl[i] > 0) { dl[i] -= dt; dx[i] += dvx[i] * dt; dy[i] += dvy[i] * dt; da[i] += dw[i] * dt; }    fx.shake *= Math.exp(-6 * dt);    if (fx.shake < 0.05) fx.shake = 0;    const d = Math.exp(-1.5 * dt);    for (let i = 0; i < N; i++) {      if (!alive[i]) continue;      life[i] -= dt;      if (life[i] <= 0) { alive[i] = 0; continue; }      px[i] += vx[i] * dt; py[i] += vy[i] * dt; vx[i] *= d; vy[i] *= d;    }  };  // draws at world coords; caller has set the camera transform  fx.draw = (ctx, camX, camY, world) => {    ctx.globalCompositeOperation = 'lighter';    for (let i = 0; i < N; i++) {      if (!alive[i]) continue;      const t = life[i] / max[i];      let x = px[i], y = py[i];      x -= Math.round((x - camX) / world) * world;      y -= Math.round((y - camY) / world) * world;      ctx.globalAlpha = t;      ctx.fillStyle = 'hsl(' + (hue[i] | 0) + ',100%,60%)';      const s = 1 + 3 * t;      ctx.fillRect(x - s / 2, y - s / 2, s, s);    }    for (let i = 0; i < R; i++) {      if (rl[i] <= 0) continue;      const t = rl[i] / rm[i];      ctx.globalAlpha = t; ctx.strokeStyle = 'hsl(' + (rh[i] | 0) + ',100%,70%)';      ctx.beginPath(); ctx.arc(rx[i] - Math.round((rx[i] - camX) / world) * world, ry[i] - Math.round((ry[i] - camY) / world) * world, rr[i] * (1 - t), 0, TWO_PI); ctx.stroke();    }    for (let i = 0; i < D; i++) {      if (dl[i] <= 0) continue;      ctx.globalAlpha = dl[i]; ctx.strokeStyle = 'hsl(' + (dh[i] | 0) + ',100%,70%)';      const x = dx[i] - Math.round((dx[i] - camX) / world) * world, y = dy[i] - Math.round((dy[i] - camY) / world) * world;      ctx.beginPath(); ctx.moveTo(x - Math.cos(da[i]) * 6, y - Math.sin(da[i]) * 6); ctx.lineTo(x + Math.cos(da[i]) * 6, y + Math.sin(da[i]) * 6); ctx.stroke();    }    for (let i = 0; i < T; i++) {      if (tl[i] <= 0) continue;      const t = tl[i] / 0.4; ctx.globalAlpha = t * 0.6; ctx.fillStyle = '#fb4';      const r = 1 + 3 * t;      ctx.fillRect(tx[i] - Math.round((tx[i] - camX) / world) * world - r / 2, ty[i] - Math.round((ty[i] - camY) / world) * world - r / 2, r, r);    }    for (let i = 0; i < S; i++) {      if (sl[i] <= 0) continue;      const t = sl[i] / 0.3; ctx.globalAlpha = t; ctx.strokeStyle = '#8df';      ctx.beginPath(); ctx.arc(sx[i] - Math.round((sx[i] - camX) / world) * world, sy[i] - Math.round((sy[i] - camY) / world) * world, 22 + 8 * (1 - t), 0, TWO_PI); ctx.stroke();    }    ctx.globalAlpha = 1;    ctx.globalCompositeOperation = 'source-over';  };  return fx;}