/* * NETWALL renderer: canvas 2D, world units = 40 px per cell, camera (zoom + pan) maps world -> CSS px. * No shadowBlur / per-frame gradients so it stays smooth on phones. */ (function (root) { 'use strict'; const NW = root.NW = root.NW || {}; const TD = root.TD; const C = 40; // world px per cell const TAU = Math.PI * 2; const SLOT_COLORS = ['#4de3ff', '#ffd24d']; const BG = '#050810', BOARD = '#09101f'; const MARGIN = 10; // world px kept free on every side of the board so the gateway / server rings are never cut off const clamp = (v, a, b) => Math.min(b, Math.max(a, v)); const lerpAngle = (a, b, k) => { let d = ((b - a + Math.PI * 3) % TAU) - Math.PI; return a + d * k; }; function rr(ctx, x, y, w, h, r) { ctx.beginPath(); ctx.moveTo(x + r, y); ctx.arcTo(x + w, y, x + w, y + h, r); ctx.arcTo(x + w, y + h, x, y + h, r); ctx.arcTo(x, y + h, x, y, r); ctx.arcTo(x, y, x + w, y, r); ctx.closePath(); } function poly(ctx, n, r, rot) { ctx.beginPath(); for (let i = 0; i < n; i++) { const a = rot + (i / n) * TAU; i ? ctx.lineTo(Math.cos(a) * r, Math.sin(a) * r) : ctx.moveTo(Math.cos(a) * r, Math.sin(a) * r); } ctx.closePath(); } function rgba(hex, a) { const n = parseInt(hex.slice(1), 16); return 'rgba(' + (n >> 16) + ',' + ((n >> 8) & 255) + ',' + (n & 255) + ',' + a + ')'; } // ------------------------------------------------------------------ tower glyphs (also used for UI icons) // S = tile size, (cx,cy) = centre, ang = barrel angle (radians), t = seconds (animation), fx = 0..1 flash right after a Sandbox pulse function drawGlyph(ctx, type, level, cx, cy, S, ang, t, alpha, rot, fx) { const def = TD.TOWERS[type]; if (!def) return; const col = def.color; ctx.save(); ctx.globalAlpha *= alpha === undefined ? 1 : alpha; ctx.translate(cx, cy); if (rot) { ctx.rotate(-rot); ang = (ang || 0) + rot; } // board is drawn rotated: keep glyphs upright const h = S * 0.43; if (type === 'wall') { ctx.fillStyle = '#10305f'; rr(ctx, -h * 0.92, -h * 0.92, h * 1.84, h * 1.84, S * 0.09); ctx.fill(); ctx.lineWidth = S * 0.05; ctx.strokeStyle = col; ctx.stroke(); ctx.fillStyle = 'rgba(90,169,255,.28)'; ctx.fillRect(-h * 0.55, -h * 0.55, h * 1.1, h * 0.5); ctx.fillRect(-h * 0.55, h * 0.05, h * 0.5, h * 0.5); ctx.fillRect(h * 0.05, h * 0.05, h * 0.5, h * 0.5); ctx.restore(); return; } ctx.fillStyle = '#0c1426'; rr(ctx, -h, -h, h * 2, h * 2, S * 0.12); ctx.fill(); if (level >= 2) { ctx.fillStyle = rgba(col, level >= 3 ? 0.3 : 0.16); ctx.fill(); } // upgraded towers are tinted (level 3 clearly) ctx.lineWidth = S * (level >= 3 ? 0.075 : 0.05); ctx.strokeStyle = col; ctx.stroke(); ctx.lineCap = 'round'; ctx.lineJoin = 'round'; switch (type) { case 'filter': ctx.fillStyle = rgba(col, 0.22); ctx.beginPath(); ctx.arc(0, 0, S * 0.2, 0, TAU); ctx.fill(); ctx.lineWidth = S * 0.04; ctx.stroke(); ctx.save(); ctx.rotate(ang); ctx.fillStyle = col; ctx.fillRect(0, -S * 0.05, S * 0.34, S * 0.1); ctx.restore(); ctx.fillStyle = col; ctx.beginPath(); ctx.arc(0, 0, S * 0.07, 0, TAU); ctx.fill(); break; case 'ids': ctx.lineWidth = S * 0.035; ctx.beginPath(); ctx.arc(0, 0, S * 0.19, 0, TAU); ctx.stroke(); ctx.save(); ctx.rotate(ang); ctx.fillStyle = col; ctx.fillRect(0, -S * 0.03, S * 0.46, S * 0.06); ctx.fillRect(S * 0.36, -S * 0.07, S * 0.05, S * 0.14); ctx.restore(); ctx.beginPath(); ctx.moveTo(-S * 0.26, 0); ctx.lineTo(-S * 0.12, 0); ctx.moveTo(0, -S * 0.26); ctx.lineTo(0, -S * 0.12); ctx.moveTo(0, S * 0.26); ctx.lineTo(0, S * 0.12); ctx.stroke(); ctx.fillStyle = col; ctx.beginPath(); ctx.arc(0, 0, S * 0.05, 0, TAU); ctx.fill(); break; case 'mortar': ctx.fillStyle = rgba(col, 0.25); ctx.beginPath(); ctx.arc(0, 0, S * 0.22, 0, TAU); ctx.fill(); ctx.lineWidth = S * 0.04; ctx.stroke(); ctx.save(); ctx.rotate(ang); ctx.fillStyle = col; rr(ctx, -S * 0.02, -S * 0.1, S * 0.3, S * 0.2, S * 0.04); ctx.fill(); ctx.restore(); ctx.fillStyle = '#0c1426'; ctx.beginPath(); ctx.arc(0, 0, S * 0.08, 0, TAU); ctx.fill(); break; case 'throttle': { const pulse = ((t * 0.9) % 1); ctx.lineWidth = S * 0.04; for (let i = 0; i < 3; i++) { const r = S * (0.08 + 0.085 * i) + (i === 2 ? pulse * S * 0.05 : 0); ctx.strokeStyle = rgba(col, 1 - i * 0.28 - (i === 2 ? pulse * 0.3 : 0)); ctx.beginPath(); ctx.arc(0, 0, r, -2.3, -0.84); ctx.stroke(); ctx.beginPath(); ctx.arc(0, 0, r, 0.84, 2.3); ctx.stroke(); } ctx.fillStyle = col; ctx.beginPath(); ctx.arc(0, 0, S * 0.05, 0, TAU); ctx.fill(); ctx.strokeStyle = col; break; } case 'arc': ctx.fillStyle = col; ctx.beginPath(); ctx.moveTo(-S * 0.03, -S * 0.26); ctx.lineTo(-S * 0.14, S * 0.02); ctx.lineTo(-S * 0.01, S * 0.02); ctx.lineTo(-S * 0.07, S * 0.27); ctx.lineTo(S * 0.15, -S * 0.07); ctx.lineTo(S * 0.02, -S * 0.07); ctx.lineTo(S * 0.11, -S * 0.26); ctx.closePath(); ctx.fill(); break; case 'uplink': { // satellite dish with a feed horn and two pulsing signal arcs const pulse = (t * 0.8) % 1; ctx.lineWidth = S * 0.07; ctx.strokeStyle = col; ctx.beginPath(); ctx.arc(0, S * 0.07, S * 0.23, Math.PI * 0.12, Math.PI * 0.88); ctx.stroke(); ctx.lineWidth = S * 0.045; ctx.beginPath(); ctx.moveTo(0, S * 0.07); ctx.lineTo(0, -S * 0.15); ctx.stroke(); ctx.fillStyle = col; ctx.beginPath(); ctx.arc(0, -S * 0.17, S * 0.05, 0, TAU); ctx.fill(); for (let i = 0; i < 2; i++) { const k = (pulse + i * 0.5) % 1; ctx.strokeStyle = rgba(col, 1 - k); ctx.lineWidth = S * 0.035; ctx.beginPath(); ctx.arc(0, -S * 0.17, S * (0.1 + 0.2 * k), -Math.PI * 0.85, -Math.PI * 0.15); ctx.stroke(); } break; } case 'sandbox': { // containment cell: four corner brackets round a core that hums, held by thin tethers; it flashes when it pulses const g = fx || 0, p = 0.5 + 0.5 * Math.sin(t * 3), a = S * 0.22, k = S * 0.11; ctx.save(); ctx.translate(0, -S * 0.04); ctx.strokeStyle = rgba(col, 0.45); ctx.lineWidth = S * 0.03; ctx.beginPath(); for (const [sx, sy] of [[-1, -1], [1, -1], [1, 1], [-1, 1]]) { ctx.moveTo(sx * S * 0.09, sy * S * 0.09); ctx.lineTo(sx * (a - k * 0.35), sy * (a - k * 0.35)); } ctx.stroke(); ctx.strokeStyle = col; ctx.lineWidth = S * 0.06; ctx.beginPath(); for (const [sx, sy] of [[-1, -1], [1, -1], [1, 1], [-1, 1]]) { ctx.moveTo(sx * a, sy * (a - k)); ctx.lineTo(sx * a, sy * a); ctx.lineTo(sx * (a - k), sy * a); } ctx.stroke(); const c = S * (0.095 + 0.015 * p + 0.05 * g); ctx.beginPath(); ctx.moveTo(0, -c); ctx.lineTo(c, 0); ctx.lineTo(0, c); ctx.lineTo(-c, 0); ctx.closePath(); ctx.fillStyle = g > 0 ? '#fff3dd' : rgba(col, 0.55 + 0.3 * p); ctx.fill(); ctx.lineWidth = S * 0.035; ctx.stroke(); ctx.restore(); break; } case 'balancer': { // scales: post, a gently rocking beam and two hanging pans const dy = S * 0.27 * Math.sin(Math.sin(t * 1.4) * 0.07), ex = S * 0.27, top = -S * 0.21; ctx.save(); ctx.translate(0, -S * 0.02); ctx.lineWidth = S * 0.05; ctx.strokeStyle = col; ctx.beginPath(); ctx.moveTo(0, top); ctx.lineTo(0, S * 0.14); ctx.moveTo(-S * 0.12, S * 0.15); ctx.lineTo(S * 0.12, S * 0.15); ctx.stroke(); ctx.beginPath(); ctx.moveTo(-ex, top - dy); ctx.lineTo(ex, top + dy); ctx.stroke(); for (const sx of [-1, 1]) { const px = sx * ex, py = top + sx * dy, w = S * 0.1, d = S * 0.14; ctx.lineWidth = S * 0.03; ctx.beginPath(); ctx.moveTo(px - w, py + d); ctx.lineTo(px, py); ctx.lineTo(px + w, py + d); ctx.stroke(); ctx.fillStyle = rgba(col, 0.55); ctx.beginPath(); ctx.arc(px, py + d, w, 0, Math.PI); ctx.closePath(); ctx.fill(); ctx.lineWidth = S * 0.04; ctx.beginPath(); ctx.arc(px, py + d, w, 0, Math.PI); ctx.stroke(); } ctx.fillStyle = col; ctx.beginPath(); ctx.arc(0, top, S * 0.05, 0, TAU); ctx.fill(); ctx.restore(); break; } default: break; } // level pips: big enough to read at 14 px cells, with a dark halo so they show on any tile colour for (let i = 0; i < level; i++) { const px = (i - (level - 1) / 2) * S * 0.25, py = h - S * 0.11; ctx.fillStyle = '#04101a'; ctx.beginPath(); ctx.arc(px, py, S * 0.125, 0, TAU); ctx.fill(); ctx.fillStyle = '#ffffff'; ctx.beginPath(); ctx.arc(px, py, S * 0.085, 0, TAU); ctx.fill(); } ctx.restore(); } // ------------------------------------------------------------------ monsters // Hit points and shield of one enemy, from either kind of view object. Solo hands the sim's monsters over (hp and shield apart: // m.shield / m.maxShield); co-op gets hp + shield out of maxhp + maxShield from the wire, and the split follows the kind's table // (the shield soaks first, so what is above the hit point part of the total is shield). Result is a shared scratch object. const VIT = { hp: 0, mh: 1, sh: 0, ms: 0 }; function vitals(m, def) { if (!def.shield) { VIT.hp = m.hp; VIT.mh = m.maxhp; VIT.sh = 0; VIT.ms = 0; } else if (m.maxShield !== undefined) { VIT.hp = m.hp; VIT.mh = m.maxhp; VIT.sh = m.shield; VIT.ms = m.maxShield; } else { const mh = m.maxhp * def.hp / (def.hp + def.shield); VIT.mh = mh; VIT.ms = m.maxhp - mh; VIT.hp = Math.min(m.hp, mh); VIT.sh = Math.max(0, m.hp - mh); } return VIT; } const SWARM = { botnet: 1 }; // swarm units die in a hit or two: no bar, no ground shadow, few particles (there can be 100+) // stun: true while the enemy is frozen by a Sandbox (the caller knows: the sim's stunT in solo, "slow flag and not moving" in co-op) function drawMonster(ctx, m, ang, t, flash, rot, stun) { const def = TD.ENEMIES[m.kind], r = def.r * C, col = def.color; const v = vitals(m, def), sf = v.ms > 0 ? clamp(v.sh / v.ms, 0, 1) : 0; const lift = m.kind === 'ghost' ? -7 : 0; ctx.save(); ctx.translate(m.x * C, m.y * C); if (rot) { ctx.rotate(-rot); ang += rot; } ctx.lineJoin = 'round'; ctx.lineCap = 'round'; switch (m.kind) { case 'ping': ctx.fillStyle = '#2a0b16'; ctx.beginPath(); ctx.arc(0, 0, r, 0, TAU); ctx.fill(); ctx.lineWidth = 2.4; ctx.strokeStyle = col; ctx.stroke(); ctx.fillStyle = col; ctx.beginPath(); ctx.arc(Math.cos(ang) * r * 0.35, Math.sin(ang) * r * 0.35, r * 0.38, 0, TAU); ctx.fill(); break; case 'worm': { ctx.fillStyle = col; for (let i = 2; i >= 0; i--) { const k = i * r * 1.05, w = Math.sin(t * 11 + m.id + i * 1.3) * r * 0.35; const px = -Math.cos(ang) * k - Math.sin(ang) * w, py = -Math.sin(ang) * k + Math.cos(ang) * w; ctx.globalAlpha = 1 - i * 0.22; ctx.beginPath(); ctx.arc(px, py, r * (1 - i * 0.18), 0, TAU); ctx.fill(); } ctx.globalAlpha = 1; ctx.fillStyle = '#2a1204'; ctx.beginPath(); ctx.arc(Math.cos(ang) * r * 0.3, Math.sin(ang) * r * 0.3, r * 0.28, 0, TAU); ctx.fill(); break; } case 'trojan': poly(ctx, 6, r, ang); ctx.fillStyle = '#2a0e1a'; ctx.fill(); ctx.lineWidth = 3; ctx.strokeStyle = col; ctx.stroke(); ctx.fillStyle = col; rr(ctx, -r * 0.32, -r * 0.1, r * 0.64, r * 0.5, 2); ctx.fill(); ctx.lineWidth = 2.2; ctx.strokeStyle = col; ctx.beginPath(); ctx.arc(0, -r * 0.1, r * 0.22, Math.PI, 0); ctx.stroke(); break; case 'spam': ctx.save(); ctx.rotate(ang); ctx.beginPath(); ctx.moveTo(r * 1.25, 0); ctx.lineTo(-r, r * 0.9); ctx.lineTo(-r, -r * 0.9); ctx.closePath(); ctx.fillStyle = col; ctx.fill(); ctx.restore(); break; case 'ghost': { const bob = Math.sin(t * 5 + m.id) * 2; ctx.translate(0, -7 + bob); ctx.globalAlpha = 0.72 + Math.sin(t * 9 + m.id) * 0.18; poly(ctx, 4, r * 1.25, Math.PI / 4); ctx.fillStyle = rgba(col, 0.28); ctx.fill(); ctx.lineWidth = 2.2; ctx.strokeStyle = col; ctx.stroke(); ctx.beginPath(); ctx.arc(0, 0, r * 0.42, 0, TAU); ctx.fillStyle = col; ctx.fill(); ctx.globalAlpha = 1; break; } case 'rootkit': { ctx.save(); ctx.rotate(t * 0.6); ctx.fillStyle = col; for (let i = 0; i < 8; i++) { ctx.rotate(TAU / 8); ctx.beginPath(); ctx.moveTo(r * 0.8, -r * 0.18); ctx.lineTo(r * 1.28, 0); ctx.lineTo(r * 0.8, r * 0.18); ctx.closePath(); ctx.fill(); } ctx.restore(); poly(ctx, 8, r, 0.39); ctx.fillStyle = '#240609'; ctx.fill(); ctx.lineWidth = 3.2; ctx.strokeStyle = col; ctx.stroke(); ctx.fillStyle = rgba('#ff3b3b', 0.55 + Math.sin(t * 4) * 0.3); ctx.beginPath(); ctx.arc(0, 0, r * 0.45, 0, TAU); ctx.fill(); break; } case 'daemon': { // healer: an upright plus (a medic cross), its core breathing in time with the heal pulse const a = r * 1.3, w = r * 0.38, br = 0.5 + 0.5 * Math.sin(t * 5 + m.id); ctx.beginPath(); ctx.moveTo(-w, -a); ctx.lineTo(w, -a); ctx.lineTo(w, -w); ctx.lineTo(a, -w); ctx.lineTo(a, w); ctx.lineTo(w, w); ctx.lineTo(w, a); ctx.lineTo(-w, a); ctx.lineTo(-w, w); ctx.lineTo(-a, w); ctx.lineTo(-a, -w); ctx.lineTo(-w, -w); ctx.closePath(); ctx.fillStyle = '#0d2b17'; ctx.fill(); ctx.lineWidth = 2.8; ctx.strokeStyle = col; ctx.stroke(); ctx.fillStyle = rgba('#d6ffe0', 0.5 + 0.4 * br); ctx.fillRect(-w * 0.45, -a * 0.62, w * 0.9, a * 1.24); ctx.fillRect(-a * 0.62, -w * 0.45, a * 1.24, w * 0.9); break; } case 'proxy': { // shielded relay: a heater-shield badge (never a hexagon: the Trojan is one) inside a bubble that thins as the shield goes if (sf > 0.01) { ctx.fillStyle = rgba('#7ab8ff', 0.07 + 0.2 * sf); ctx.beginPath(); ctx.arc(0, 0, r * 1.55, 0, TAU); ctx.fill(); ctx.lineWidth = 0.9 + 2.3 * sf; ctx.strokeStyle = rgba('#b9dcff', 0.3 + 0.65 * sf); ctx.stroke(); } ctx.beginPath(); ctx.moveTo(-r * 0.92, -r * 0.8); ctx.lineTo(r * 0.92, -r * 0.8); ctx.lineTo(r * 0.92, r * 0.12); ctx.lineTo(0, r * 1.05); ctx.lineTo(-r * 0.92, r * 0.12); ctx.closePath(); ctx.fillStyle = '#0d1a3a'; ctx.fill(); ctx.lineWidth = 2.8; ctx.strokeStyle = col; ctx.stroke(); ctx.fillStyle = rgba(col, 0.9); ctx.beginPath(); ctx.arc(0, -r * 0.12, r * 0.3, 0, TAU); ctx.fill(); ctx.lineWidth = 2; ctx.beginPath(); ctx.moveTo(0, r * 0.2); ctx.lineTo(0, r * 0.72); ctx.stroke(); break; } case 'botnet': { // a drone of a swarm: spinning X of two struts with a rotor at each tip, wobbling about its path const a = Math.max(r * 1.7, 8), wb = 2.5; ctx.translate(Math.sin(t * 6 + m.id * 1.9) * wb, Math.cos(t * 5 + m.id * 2.3) * wb); ctx.rotate(t * 7 + m.id); ctx.lineWidth = 3; ctx.strokeStyle = col; ctx.beginPath(); ctx.moveTo(-a, -a); ctx.lineTo(a, a); ctx.moveTo(a, -a); ctx.lineTo(-a, a); ctx.stroke(); ctx.fillStyle = '#a6f2ea'; ctx.beginPath(); for (const [sx, sy] of [[-1, -1], [1, -1], [1, 1], [-1, 1]]) { ctx.moveTo(sx * a + 2.6, sy * a); ctx.arc(sx * a, sy * a, 2.6, 0, TAU); } ctx.fill(); ctx.fillStyle = '#e8fffd'; ctx.beginPath(); ctx.arc(0, 0, r * 0.6, 0, TAU); ctx.fill(); break; } default: ctx.fillStyle = col; ctx.beginPath(); ctx.arc(0, 0, r, 0, TAU); ctx.fill(); } if (flash > 0) { ctx.globalAlpha = Math.min(0.85, flash * 9); ctx.fillStyle = '#fff'; ctx.beginPath(); ctx.arc(0, lift, r * 1.05, 0, TAU); ctx.fill(); ctx.globalAlpha = 1; } if (stun) { // frozen by a Sandbox: an ice shell with crackling crystal spikes (the cyan ring below is only a slow) const swarm = SWARM[m.kind], s = swarm ? 7 : Math.max(r * 1.3, 7) + 2; ctx.fillStyle = 'rgba(200,240,255,.24)'; ctx.beginPath(); ctx.arc(0, lift, s, 0, TAU); ctx.fill(); ctx.lineWidth = swarm ? 1.4 : 2; ctx.strokeStyle = '#eefbff'; ctx.stroke(); if (!swarm) { ctx.lineWidth = 2.2; ctx.beginPath(); for (let i = 0; i < 6; i++) { const a = i * TAU / 6 + 0.5, len = s + (Math.sin(t * 31 + i * 2.1 + m.id) > 0 ? 6 : 3.5); ctx.moveTo(Math.cos(a) * s, lift + Math.sin(a) * s); ctx.lineTo(Math.cos(a) * len, lift + Math.sin(a) * len); } ctx.stroke(); } } else if (m.slow > 0) { ctx.lineWidth = 1.6; ctx.strokeStyle = 'rgba(77,227,255,.85)'; ctx.beginPath(); ctx.arc(0, lift, r + 3.5, 0, TAU); ctx.stroke(); } ctx.restore(); // health bar (a blue shield strip above it for shielded kinds) if (!SWARM[m.kind] && (v.hp < v.mh || v.sh < v.ms)) { const w = Math.max(15, r * 2.3), y = -r * (sf > 0.01 ? 1.55 : 1) - (m.kind === 'ghost' ? 16 : 9), x = -w / 2; const f = clamp(v.hp / v.mh, 0, 1); ctx.save(); ctx.translate(m.x * C, m.y * C); if (rot) ctx.rotate(-rot); ctx.fillStyle = 'rgba(0,0,0,.65)'; ctx.fillRect(x - 1, y - 1, w + 2, v.ms > 0 ? 8 : 5); ctx.fillStyle = f > 0.5 ? '#35e0a1' : f > 0.25 ? '#ffd24d' : '#ff5c7a'; ctx.fillRect(x, y + (v.ms > 0 ? 3 : 0), w * f, 3); if (v.ms > 0) { ctx.fillStyle = '#6fb4ff'; ctx.fillRect(x, y, w * sf, 2); } ctx.restore(); } } // ------------------------------------------------------------------ renderer class Renderer { constructor(canvas) { this.cv = canvas; this.ctx = canvas.getContext('2d', { alpha: false }); this.W = 300; this.H = 200; this.dpr = 1; this.cam = { zoom: 1, px: 0, py: 0 }; this.board = null; this.parts = []; this.rings = []; this.lines = []; this.texts = []; this.marks = []; this.pulses = []; // heal / stun pulses: their own small pool, so a swarm of daemons can never starve the leak and boom rings this.glow = new Map(); // Sandbox cell index -> seconds of flash left after its pulse this.aim = new Map(); this.trk = new Map(); this.wallGrid = null; this.shake = 0; this.flashCore = 0; this.lastNow = 0; this.rot = 0; // 0, or PI/2 when the board is drawn rotated for portrait screens this.fx = (root.matchMedia && root.matchMedia('(pointer: coarse)').matches) ? 0.55 : 1; // fewer particles on phones this.names = {}; } setBoard(board) { this.board = board; this.wallGrid = new Uint8Array(board.cols * board.rows); this.parts.length = 0; this.rings.length = 0; this.lines.length = 0; this.texts.length = 0; this.marks.length = 0; this.pulses.length = 0; this.aim.clear(); this.trk.clear(); this.glow.clear(); this.resetCamera(); } resize(w, h, dpr) { this.W = Math.max(50, w); this.H = Math.max(50, h); this.dpr = dpr || 1; this.cv.width = Math.round(this.W * this.dpr); this.cv.height = Math.round(this.H * this.dpr); this._layout(); } resetCamera() { this.cam.zoom = 1; this.cam.px = 0; this.cam.py = 0; this._layout(); } get zoom() { return this.cam.zoom; } setRotation(on) { const r = on ? Math.PI / 2 : 0; if (r === this.rot) return; this.rot = r; this.cam.zoom = 1; this.cam.px = 0; this.cam.py = 0; this._layout(); } _dims() { const b = this.board; return this.rot ? { w: b.rows * C, h: b.cols * C } : { w: b.cols * C, h: b.rows * C }; } _layout() { const b = this.board; if (!b) return; const d = this._dims(), pad = 4; this.fit = Math.min((this.W - pad * 2) / (d.w + MARGIN * 2), (this.H - pad * 2) / (d.h + MARGIN * 2)); this.scale = this.fit * this.cam.zoom; const sw = d.w * this.scale, sh = d.h * this.scale; const mx = sw > this.W ? (sw - this.W) / 2 + 24 : 0, my = sh > this.H ? (sh - this.H) / 2 + 24 : 0; this.cam.px = clamp(this.cam.px, -mx, mx); this.cam.py = clamp(this.cam.py, -my, my); this.ox = (this.W - sw) / 2 + this.cam.px; this.oy = (this.H - sh) / 2 + this.cam.py; } // CSS px <-> world px (world is always unrotated: 40 px per cell) _s2w(sx, sy) { const u = (sx - this.ox) / this.scale, v = (sy - this.oy) / this.scale; return this.rot ? { x: v, y: this.board.rows * C - u } : { x: u, y: v }; } _w2s(wx, wy) { return this.rot ? { x: this.ox + (this.board.rows * C - wy) * this.scale, y: this.oy + wx * this.scale } : { x: this.ox + wx * this.scale, y: this.oy + wy * this.scale }; } setZoom(z, ax, ay) { z = clamp(z, 1, 4); const w = this._s2w(ax, ay); this.cam.zoom = z; this._layout(); const s = this._w2s(w.x, w.y); this.cam.px += ax - s.x; this.cam.py += ay - s.y; this._layout(); } panBy(dx, dy) { this.cam.px += dx; this.cam.py += dy; this._layout(); } // CSS px (relative to the canvas) -> cell coordinates screenToCell(sx, sy) { const w = this._s2w(sx, sy), fx = w.x / C, fy = w.y / C; return { x: Math.floor(fx), y: Math.floor(fy), fx, fy }; } cellToScreen(cx, cy) { return this._w2s((cx + 0.5) * C, (cy + 0.5) * C); } // The cell one step away in a SCREEN direction (dx, dy each -1, 0 or 1). The board is drawn turned 90 degrees on tall screens, // so screen right is not world +x there: this is what the touch nudge buttons use. stepCell(cx, cy, dx, dy) { return this.rot ? { x: cx + dy, y: cy - dx } : { x: cx + dx, y: cy + dy }; } cellSizePx() { return C * this.scale; } // ---------------------------------------------------------------- effects _burst(x, y, col, n, speed, life) { n = Math.max(1, Math.round(n * this.fx)); if (this.parts.length > 260) return; for (let i = 0; i < n; i++) { const a = Math.random() * TAU, s = (0.4 + Math.random()) * speed; this.parts.push({ x, y, vx: Math.cos(a) * s, vy: Math.sin(a) * s, life: life * (0.6 + Math.random() * 0.6), max: life, s: 2 + Math.random() * 2.5, c: col }); } } _ring(x, y, r0, r1, col, life, w) { if (this.rings.length < 40) this.rings.push({ x, y, r0, r1, col, life, max: life, w: w || 2 }); } _pulse(x, y, r0, r1, col, life, w, fill) { if (this.pulses.length < 24) this.pulses.push({ x, y, r0, r1, col, life, max: life, w, fill }); } _text(x, y, txt, col) { if (this.texts.length < 24) this.texts.push({ x, y, txt, col, life: 0.9, max: 0.9 }); } addMark(x, y, slot) { this.marks.push({ x, y, slot: slot | 0, life: 1.6, max: 1.6 }); if (this.marks.length > 12) this.marks.shift(); } addEvents(evs) { for (const e of evs) { switch (e.e) { case 'kill': { const d = TD.ENEMIES[e.k]; this._burst(e.x * C, e.y * C, d ? d.color : '#fff', e.k === 'rootkit' ? 26 : SWARM[e.k] ? 2 : 6, 90, 0.5); if (e.rw >= 3) this._text(e.x * C, e.y * C - 8, '+' + e.rw, '#ffd24d'); break; } case 'leak': { const b = this.board; this.flashCore = 1; this.shake = Math.min(2, this.shake + 0.9); if (b) this._ring((b.core.x + 0.5) * C, (b.core.y + 0.5) * C, 10, 48, '#ff5c7a', 0.5, 3); break; } case 'boom': this._ring(e.x * C, e.y * C, 4, e.r * C, '#d4f04a', 0.32, 3); this._burst(e.x * C, e.y * C, '#d4f04a', 8, 110, 0.4); break; case 'shot': if (e.k === 'ids') this.lines.push({ pts: [[e.x * C, e.y * C], [e.tx * C, e.ty * C]], col: '#b78cff', w: 2.5, life: 0.16, max: 0.16, jit: 0 }); else if (e.k === 'filter') this._burst(e.x * C + Math.cos(e.a) * 14, e.y * C + Math.sin(e.a) * 14, '#35e0a1', 1, 30, 0.12); else if (e.k === 'mortar') this._burst(e.x * C + Math.cos(e.a) * 16, e.y * C + Math.sin(e.a) * 16, '#d4f04a', 3, 40, 0.25); break; case 'beam': { const x1 = e.x * C, y1 = e.y * C, x2 = e.tx * C, y2 = e.ty * C, wpx = (e.w || 0.5) * C; this.lines.push({ pts: [[x1, y1], [x2, y2]], col: 'rgba(255,226,122,.35)', w: wpx * 2.6, life: 0.42, max: 0.42, jit: 0 }); this.lines.push({ pts: [[x1, y1], [x2, y2]], col: '#ffe27a', w: wpx * 1.1, life: 0.34, max: 0.34, jit: 0 }); this.lines.push({ pts: [[x1, y1], [x2, y2]], col: '#ffffff', w: wpx * 0.45, life: 0.26, max: 0.26, jit: 0 }); this._ring(e.bx * C, e.by * C, 6, (e.br || 1.3) * C, '#ffe27a', 0.45, 4); this._burst(e.bx * C, e.by * C, '#ffe27a', 10, 130, 0.45); this._ring(x1, y1, 4, 30, '#ffffff', 0.25, 3); this.shake = Math.min(2, this.shake + 0.35); break; } case 'arc': this.lines.push({ pts: e.pts.map((p) => [p[0] * C, p[1] * C]), col: '#ff7ad9', w: 2.4, life: 0.18, max: 0.18, jit: 5 }); break; case 'place': { const d = TD.TOWERS[e.type]; this._ring((e.x + 0.5) * C, (e.y + 0.5) * C, 6, 34, d ? d.color : '#fff', 0.35, 2.5); this._burst((e.x + 0.5) * C, (e.y + 0.5) * C, d ? d.color : '#fff', 5, 60, 0.35); break; } case 'sell': this._burst((e.x + 0.5) * C, (e.y + 0.5) * C, '#9db4d8', 6, 70, 0.35); break; case 'up': this._ring((e.x + 0.5) * C, (e.y + 0.5) * C, 6, 38, '#ffffff', 0.4, 2.5); break; case 'stun': { // Sandbox pulse: an amber wave out to its range, a white one inside it, and the glyph flashes const d = TD.TOWERS.sandbox, col = d ? d.color : '#ffa447', b = this.board; const rg = (e.r || 3) * C; this._pulse(e.x * C, e.y * C, 10, rg, col, 0.55, 3.5, 0.13); this._pulse(e.x * C, e.y * C, 8, rg * 0.6, '#fff3dd', 0.35, 2, 0); if (b) this.glow.set(Math.floor(e.y) * b.cols + Math.floor(e.x), 0.4); break; } case 'heal': this._pulse(e.x * C, e.y * C, 10, (e.r || 2.2) * C, '#7ee08a', 0.7, 2.2, 0.07); break; // soft green wave out to the daemon's range default: break; } } } // ---------------------------------------------------------------- frame // ui: { hover:{x,y}|null, selType, selTower, preview, valid, coop, mySlot, names:{slot:name} } frame(now, view, ui) { const b = this.board; if (!b) return; const ctx = this.ctx, dpr = this.dpr; const t = now / 1000; const dt = clamp((now - (this.lastNow || now)) / 1000, 0, 0.1); this.lastNow = now; const sc = this.scale; ctx.setTransform(dpr, 0, 0, dpr, 0, 0); ctx.fillStyle = BG; ctx.fillRect(0, 0, this.W, this.H); let shx = 0, shy = 0; if (this.shake > 0.02) { shx = (Math.random() - 0.5) * this.shake * 5; shy = (Math.random() - 0.5) * this.shake * 5; this.shake *= Math.pow(0.02, dt); } const rot = this.rot; if (rot) ctx.setTransform(0, dpr * sc, -dpr * sc, 0, dpr * (this.ox + shx + b.rows * C * sc), dpr * (this.oy + shy)); else ctx.setTransform(dpr * sc, 0, 0, dpr * sc, dpr * (this.ox + shx), dpr * (this.oy + shy)); const cols = b.cols, rows = b.rows; // board plate ctx.fillStyle = BOARD; ctx.fillRect(0, 0, cols * C, rows * C); ctx.lineWidth = 1 / sc * 1.5; ctx.strokeStyle = 'rgba(77,227,255,.35)'; ctx.strokeRect(0, 0, cols * C, rows * C); // grid ctx.lineWidth = 1 / sc; ctx.strokeStyle = 'rgba(80,150,255,.10)'; ctx.beginPath(); for (let x = 1; x < cols; x++) { ctx.moveTo(x * C, 0); ctx.lineTo(x * C, rows * C); } for (let y = 1; y < rows; y++) { ctx.moveTo(0, y * C); ctx.lineTo(cols * C, y * C); } ctx.stroke(); ctx.fillStyle = 'rgba(120,200,255,.22)'; ctx.beginPath(); for (let y = 0; y <= rows; y++) for (let x = 0; x <= cols; x++) ctx.rect(x * C - 1.2, y * C - 1.2, 2.4, 2.4); ctx.fill(); // dead nodes for (let y = 0; y < rows; y++) for (let x = 0; x < cols; x++) { if (!b.obst[y * cols + x]) continue; const px = x * C, py = y * C; ctx.fillStyle = '#121828'; rr(ctx, px + 4, py + 4, C - 8, C - 8, 6); ctx.fill(); ctx.lineWidth = 1.5; ctx.strokeStyle = '#2b3556'; ctx.stroke(); ctx.strokeStyle = 'rgba(255,92,122,.32)'; ctx.lineWidth = 2; ctx.beginPath(); ctx.moveTo(px + 12, py + 12); ctx.lineTo(px + C - 12, py + C - 12); ctx.moveTo(px + C - 12, py + 12); ctx.lineTo(px + 12, py + C - 12); ctx.stroke(); } this._drawRoutes(ctx, b, ui, t); this._drawEndpoints(ctx, b, view, t, dt, rot); // walls (with trace links) then turrets const wg = this.wallGrid; wg.fill(0); const towers = view.towers; for (let i = 0; i < towers.length; i++) if (towers[i].type === 'wall') wg[towers[i].y * cols + towers[i].x] = 1; ctx.lineCap = 'round'; ctx.strokeStyle = 'rgba(90,169,255,.55)'; ctx.lineWidth = 3.2; ctx.beginPath(); for (let i = 0; i < towers.length; i++) { const w = towers[i]; if (w.type !== 'wall') continue; const cx = (w.x + 0.5) * C, cy = (w.y + 0.5) * C; if (w.x + 1 < cols && wg[w.y * cols + w.x + 1]) { ctx.moveTo(cx, cy); ctx.lineTo(cx + C, cy); } if (w.y + 1 < rows && wg[(w.y + 1) * cols + w.x]) { ctx.moveTo(cx, cy); ctx.lineTo(cx, cy + C); } } ctx.stroke(); ctx.strokeStyle = 'rgba(90,169,255,.32)'; ctx.lineWidth = 2; ctx.beginPath(); for (let i = 0; i < towers.length; i++) { const w = towers[i]; if (w.type !== 'wall') continue; const cx = (w.x + 0.5) * C, cy = (w.y + 0.5) * C; if (w.x + 1 < cols && w.y + 1 < rows && wg[(w.y + 1) * cols + w.x + 1]) { ctx.moveTo(cx, cy); ctx.lineTo(cx + C, cy + C); } if (w.x > 0 && w.y + 1 < rows && wg[(w.y + 1) * cols + w.x - 1]) { ctx.moveTo(cx, cy); ctx.lineTo(cx - C, cy + C); } } ctx.stroke(); // monster lookup for aiming const monById = new Map(); for (let i = 0; i < view.monsters.length; i++) monById.set(view.monsters[i].id, view.monsters[i]); if (this.glow.size) for (const [k, g] of this.glow) { if (g - dt <= 0) this.glow.delete(k); else this.glow.set(k, g - dt); } this._drawLinks(ctx, towers, ui, t); const coop = ui.coop; for (let i = 0; i < towers.length; i++) { const tw = towers[i]; let ang = this.aim.get(tw.id); if (ang === undefined) ang = -Math.PI / 2; const tg = tw.tgt ? monById.get(tw.tgt) : null; if (tg) { ang = lerpAngle(ang, Math.atan2(tg.y - (tw.y + 0.5), tg.x - (tw.x + 0.5)), 0.35); this.aim.set(tw.id, ang); } const cx = (tw.x + 0.5) * C, cy = (tw.y + 0.5) * C; const gl = tw.type === 'sandbox' && this.glow.size ? this.glow.get(tw.y * cols + tw.x) : 0; drawGlyph(ctx, tw.type, tw.level, cx, cy, C, ang, t, 1, rot, gl ? Math.min(1, gl / 0.25) : 0); if (coop && tw.type !== 'wall') { // owner corner flag: at least ~5 CSS px, dark outline so it shows on a tile of the same colour const k = Math.max(12, 5.5 / sc), x0 = cx - C * 0.46, y0 = cy - C * 0.46; ctx.beginPath(); ctx.moveTo(x0, y0); ctx.lineTo(x0 + k, y0); ctx.lineTo(x0, y0 + k); ctx.closePath(); ctx.fillStyle = SLOT_COLORS[tw.owner] || '#fff'; ctx.fill(); ctx.lineWidth = 1.5 / sc; ctx.strokeStyle = '#04101a'; ctx.stroke(); } } if (this.aim.size > towers.length + 40) { const live = new Set(towers.map((x) => x.id)); for (const k of this.aim.keys()) if (!live.has(k)) this.aim.delete(k); } // selection and ranges if (ui.selTower) { const st = towers.find((x) => x.id === ui.selTower); if (st) { const def = TD.TOWERS[st.type], lv = def.levels[st.level - 1]; const cx = (st.x + 0.5) * C, cy = (st.y + 0.5) * C; if (lv.range) this._rangeRing(ctx, cx, cy, lv.range * C, def.color); ctx.strokeStyle = '#fff'; ctx.lineWidth = 2; const k = 3 + Math.sin(t * 5) * 1.2, h = C / 2 + k; ctx.beginPath(); for (const [sx, sy] of [[-1, -1], [1, -1], [1, 1], [-1, 1]]) { ctx.moveTo(cx + sx * h, cy + sy * (h - 9)); ctx.lineTo(cx + sx * h, cy + sy * h); ctx.lineTo(cx + sx * (h - 9), cy + sy * h); } ctx.stroke(); } } // ground monsters, then flyers on top this._drawMonsters(ctx, view, t, dt, false, rot); this._drawMonsters(ctx, view, t, dt, true, rot); // bullets for (const bu of view.bullets) { if (bu.k === 'mortar') { ctx.fillStyle = 'rgba(0,0,0,.35)'; ctx.beginPath(); ctx.arc(bu.x * C, bu.y * C + 5, 4, 0, TAU); ctx.fill(); ctx.fillStyle = '#d4f04a'; ctx.beginPath(); ctx.arc(bu.x * C, bu.y * C - 2, 4.5, 0, TAU); ctx.fill(); } else { ctx.fillStyle = 'rgba(53,224,161,.3)'; ctx.beginPath(); ctx.arc(bu.x * C, bu.y * C, 6, 0, TAU); ctx.fill(); ctx.fillStyle = '#d6fff0'; ctx.beginPath(); ctx.arc(bu.x * C, bu.y * C, 2.6, 0, TAU); ctx.fill(); } } this._drawFx(ctx, dt, sc, rot); // placement ghost if (ui.hover && ui.selType && !ui.noGhost && b.inBounds(ui.hover.x, ui.hover.y)) { const def = TD.TOWERS[ui.selType], lv = def.levels[0]; const cx = (ui.hover.x + 0.5) * C, cy = (ui.hover.y + 0.5) * C; const ok = ui.valid !== false; if (lv.range) this._rangeRing(ctx, cx, cy, lv.range * C, ok ? def.color : '#ff5c7a'); drawGlyph(ctx, ui.selType, 1, cx, cy, C, -Math.PI / 2 - rot, t, ok ? 0.7 : 0.4, rot); if (!ok) { ctx.fillStyle = 'rgba(255,60,90,.28)'; ctx.fillRect(ui.hover.x * C + 2, ui.hover.y * C + 2, C - 4, C - 4); ctx.strokeStyle = '#ff5c7a'; ctx.lineWidth = 3; ctx.beginPath(); ctx.moveTo(cx - 9, cy - 9); ctx.lineTo(cx + 9, cy + 9); ctx.moveTo(cx + 9, cy - 9); ctx.lineTo(cx - 9, cy + 9); ctx.stroke(); } else { ctx.strokeStyle = rgba(def.color, 0.9); ctx.lineWidth = 2; rr(ctx, ui.hover.x * C + 2, ui.hover.y * C + 2, C - 4, C - 4, 6); ctx.stroke(); } } else if (ui.hover && (!ui.selType || ui.noGhost) && b.inBounds(ui.hover.x, ui.hover.y)) { ctx.strokeStyle = 'rgba(255,255,255,.22)'; ctx.lineWidth = 1.5; ctx.strokeRect(ui.hover.x * C + 1, ui.hover.y * C + 1, C - 2, C - 2); } // partner cursors and pings const labels = []; // the cell frame is drawn on the board, the name label later in screen space (clamped to the stage) for (const cu of view.cursors || []) { const col = SLOT_COLORS[cu.slot] || '#fff', gx = Math.floor(cu.x), gy = Math.floor(cu.y); ctx.strokeStyle = col; ctx.lineWidth = 2; ctx.strokeRect(gx * C + 1, gy * C + 1, C - 2, C - 2); labels.push({ nm: (ui.names && ui.names[cu.slot]) || 'Partner', col, gx, gy }); } for (let i = this.marks.length - 1; i >= 0; i--) { const m = this.marks[i]; m.life -= dt; if (m.life <= 0) { this.marks.splice(i, 1); continue; } const k = 1 - m.life / m.max, col = SLOT_COLORS[m.slot] || '#fff'; ctx.strokeStyle = col; ctx.lineWidth = 3; for (let j = 0; j < 3; j++) { const kk = k - j * 0.16; if (kk <= 0) continue; ctx.globalAlpha = (1 - kk) * 0.9; ctx.beginPath(); ctx.arc((m.x + 0.5) * C, (m.y + 0.5) * C, 6 + kk * 46, 0, TAU); ctx.stroke(); } ctx.globalAlpha = 1; ctx.fillStyle = col; ctx.beginPath(); ctx.arc((m.x + 0.5) * C, (m.y + 0.5) * C, 5, 0, TAU); ctx.fill(); } if (labels.length) this._cursorLabels(ctx, labels); } // Load Balancer links: dashed lines (flowing towards the turret) and an outline on every attack tower it boosts, only for the balancer that is // selected, hovered or about to be built, so a busy board stays calm. Same rule as the sim's updateAuras: attack role, centre to centre // within range. (The wire has no per-tower buff flag, so this is worked out here from the tower list.) _drawLinks(ctx, towers, ui, t) { let src = null; if (ui.selTower) { const st = towers.find((x) => x.id === ui.selTower); if (st) src = st; } if (!src && ui.hover) { for (let i = 0; i < towers.length; i++) if (towers[i].x === ui.hover.x && towers[i].y === ui.hover.y) { src = towers[i]; break; } if (!src && ui.selType && !ui.noGhost) src = { type: ui.selType, level: 1, x: ui.hover.x, y: ui.hover.y }; } const def = src && TD.TOWERS[src.type]; if (!def || def.role !== 'support') return; const lv = def.levels[src.level - 1] || def.levels[0], r2 = (lv.range || 0) * (lv.range || 0); const bx = (src.x + 0.5) * C, by = (src.y + 0.5) * C; let lines = null; for (let i = 0; i < towers.length; i++) { const tw = towers[i], d = TD.TOWERS[tw.type]; if (!d || d.role !== 'attack' || tw === src) continue; const dx = tw.x - src.x, dy = tw.y - src.y; if (dx * dx + dy * dy > r2) continue; (lines || (lines = [])).push(tw); } if (!lines) return; const u = Math.max(1, 0.55 / this.scale), lw = Math.max(2, 1.2 / this.scale); // keep the dashes and arrowheads readable when the cells are small ctx.save(); ctx.lineCap = 'round'; ctx.lineJoin = 'round'; ctx.strokeStyle = rgba(def.color, 0.75); ctx.lineWidth = lw; ctx.setLineDash([6 * u, 5 * u]); ctx.lineDashOffset = -t * 16; ctx.beginPath(); for (const tw of lines) { ctx.moveTo(bx, by); ctx.lineTo((tw.x + 0.5) * C, (tw.y + 0.5) * C); } ctx.stroke(); ctx.setLineDash([]); ctx.fillStyle = rgba(def.color, 0.95); ctx.lineWidth = lw; for (const tw of lines) { // an arrowhead just outside the tile, and a frame round it const tx = (tw.x + 0.5) * C, ty = (tw.y + 0.5) * C, a = Math.atan2(ty - by, tx - bx), d = C * 0.62 + 4 * u; const ax = tx - Math.cos(a) * d, ay = ty - Math.sin(a) * d, ca = Math.cos(a), sa = Math.sin(a); ctx.beginPath(); ctx.moveTo(ax + ca * 6 * u, ay + sa * 6 * u); ctx.lineTo(ax - ca * 3 * u - sa * 5 * u, ay - sa * 3 * u + ca * 5 * u); ctx.lineTo(ax - ca * 3 * u + sa * 5 * u, ay - sa * 3 * u - ca * 5 * u); ctx.closePath(); ctx.fill(); rr(ctx, tw.x * C + 1.5, tw.y * C + 1.5, C - 3, C - 3, 6); ctx.stroke(); } ctx.restore(); } _rangeRing(ctx, cx, cy, r, col) { ctx.fillStyle = rgba(col, 0.07); ctx.beginPath(); ctx.arc(cx, cy, r, 0, TAU); ctx.fill(); ctx.strokeStyle = rgba(col, 0.55); ctx.lineWidth = 1.5; ctx.stroke(); } _drawRoutes(ctx, b, ui, t) { const pv = ui && ui.preview && ui.preview.ok && ui.preview.routes; const dash = (col, w, speed) => { ctx.strokeStyle = col; ctx.lineWidth = w; ctx.setLineDash([9, 9]); ctx.lineDashOffset = -t * speed; }; ctx.lineJoin = 'round'; ctx.lineCap = 'round'; dash(pv ? 'rgba(77,227,255,.10)' : 'rgba(77,227,255,.34)', 3, 34); for (const r of b.routes) { ctx.beginPath(); for (let i = 0; i < r.length; i++) { const px = (r[i][0] + 0.5) * C, py = (r[i][1] + 0.5) * C; i ? ctx.lineTo(px, py) : ctx.moveTo(px, py); } ctx.stroke(); } if (pv) { dash('rgba(255,210,77,.85)', 3.6, 60); for (const r of ui.preview.routes) { ctx.beginPath(); for (let i = 0; i < r.length; i++) { const px = (r[i][0] + 0.5) * C, py = (r[i][1] + 0.5) * C; i ? ctx.lineTo(px, py) : ctx.moveTo(px, py); } ctx.stroke(); } } ctx.setLineDash([]); } _drawEndpoints(ctx, b, view, t, dt, rot) { // spawns: red gateways for (const sp of b.spawns) { const p = (t * 0.7) % 1; ctx.save(); ctx.translate((sp.x + 0.5) * C, (sp.y + 0.5) * C); if (rot) ctx.rotate(-rot); ctx.strokeStyle = rgba('#ff5c7a', 0.5 * (1 - p)); ctx.lineWidth = 2; ctx.beginPath(); ctx.arc(0, 0, 12 + p * 16, 0, TAU); ctx.stroke(); ctx.fillStyle = '#2a0c18'; ctx.beginPath(); ctx.arc(0, 0, 13, 0, TAU); ctx.fill(); ctx.strokeStyle = '#ff5c7a'; ctx.lineWidth = 2; ctx.stroke(); ctx.lineWidth = 1.3; ctx.beginPath(); ctx.ellipse(0, 0, 5.5, 13, 0, 0, TAU); ctx.moveTo(-13, 0); ctx.lineTo(13, 0); ctx.stroke(); ctx.restore(); } // core: server rack with an integrity ring const c = b.core; const frac = view.maxLives ? clamp(view.lives / view.maxLives, 0, 1) : 1; const col = frac > 0.5 ? '#35e0a1' : frac > 0.25 ? '#ffd24d' : '#ff5c7a'; this.flashCore = Math.max(0, this.flashCore - dt * 2.2); ctx.save(); ctx.translate((c.x + 0.5) * C, (c.y + 0.5) * C); if (rot) ctx.rotate(-rot); ctx.strokeStyle = 'rgba(255,255,255,.08)'; ctx.lineWidth = 4; ctx.beginPath(); ctx.arc(0, 0, 25, 0, TAU); ctx.stroke(); ctx.strokeStyle = col; ctx.lineWidth = 4; ctx.beginPath(); ctx.arc(0, 0, 25, -Math.PI / 2, -Math.PI / 2 + TAU * frac); ctx.stroke(); if (this.flashCore > 0) { ctx.fillStyle = 'rgba(255,60,90,' + (this.flashCore * 0.4) + ')'; ctx.beginPath(); ctx.arc(0, 0, 29, 0, TAU); ctx.fill(); } ctx.fillStyle = '#0d2038'; rr(ctx, -13, -16, 26, 32, 5); ctx.fill(); ctx.strokeStyle = '#4de3ff'; ctx.lineWidth = 2; ctx.stroke(); ctx.lineWidth = 1.5; ctx.beginPath(); for (let i = 0; i < 3; i++) { ctx.moveTo(-9, -9 + i * 8); ctx.lineTo(3, -9 + i * 8); } ctx.stroke(); for (let i = 0; i < 3; i++) { ctx.fillStyle = (Math.floor(t * 3 + i * 1.7) % 2) ? col : rgba(col, 0.35); ctx.beginPath(); ctx.arc(8, -9 + i * 8, 2, 0, TAU); ctx.fill(); } ctx.restore(); } _drawMonsters(ctx, view, t, dt, air, rot) { const trk = this.trk; if (!air) { // a Daemon's heal range, faint, all in one stroke let any = false; for (const m of view.monsters) { const d = TD.ENEMIES[m.kind]; if (!d.healRange) continue; if (!any) { any = true; ctx.beginPath(); } ctx.moveTo(m.x * C + d.healRange * C, m.y * C); ctx.arc(m.x * C, m.y * C, d.healRange * C, 0, TAU); } if (any) { ctx.strokeStyle = rgba(TD.ENEMIES.daemon.color, 0.15); ctx.lineWidth = 1.5; ctx.stroke(); } } for (const m of view.monsters) { if (!!m.air !== air) continue; const def = TD.ENEMIES[m.kind]; let k = trk.get(m.id); if (!k) { k = { x: m.x, y: m.y, ang: 0, hp: m.hp, flash: 0, still: 0, sv: null }; trk.set(m.id, k); } const dm = def.shield && m.maxShield === undefined ? this._shieldView(k, m, def, dt) : m; // co-op: tell hp and shield apart (see there) const v = vitals(dm, def), cur = v.hp + v.sh; const dx = m.x - k.x, dy = m.y - k.y; if (dx * dx + dy * dy > 1e-6) k.ang = lerpAngle(k.ang, Math.atan2(dy, dx), 0.35); k.still = dx * dx + dy * dy < 1e-8 ? k.still + dt : 0; k.x = m.x; k.y = m.y; if (cur < k.hp) k.flash = 0.1; k.hp = cur; k.flash = Math.max(0, k.flash - dt); // stunned: solo knows (stunT); in co-op the wire only says "slowed or stunned", and a slowed packet still moves, so a flagged packet that stands still is stunned const stun = m.stunT !== undefined ? m.stunT > 0 : m.slow > 0 && k.still > 0.2 && !view.paused; if (air && !SWARM[m.kind]) { const er = def.r * C; ctx.fillStyle = 'rgba(0,0,0,.28)'; ctx.save(); ctx.translate(m.x * C, m.y * C); if (rot) ctx.rotate(-rot); ctx.beginPath(); ctx.ellipse(0, 5, er, er * 0.45, 0, 0, TAU); ctx.fill(); ctx.restore(); } drawMonster(ctx, dm, k.ang, t, k.flash, rot, stun); } if (air && trk.size > view.monsters.length + 60) { // drop stale trackers const live = new Set(view.monsters.map((x) => x.id)); for (const id of trk.keys()) if (!live.has(id)) trk.delete(id); } } // Co-op sends a shielded enemy as hp + shield out of maxhp + maxShield. A shield soaks first, so most of the time the split is plain from the // total, but once the hp is hurt and the shield has regrown the total alone is ambiguous (hp 20 + shield 120 looks like hp 90 + shield 50). // So the split is followed over time: a drop in the total hits the shield first, a rise after shieldDelay seconds without a hit refills the // shield, any other rise is a heal. Returns a solo-style view object (hp and shield apart) kept in the tracker entry. _shieldView(k, m, def, dt) { const mh = m.maxhp * def.hp / (def.hp + def.shield), ms = m.maxhp - mh; let s = k.sv; if (!s) s = k.sv = { id: m.id, kind: m.kind, air: m.air, x: 0, y: 0, hp: 0, maxhp: mh, shield: clamp(m.hp - mh, 0, ms), maxShield: ms, slow: 0, tot: m.hp, idle: 0 }; else { const d = m.hp - s.tot; if (d < -0.5) { s.shield -= Math.min(s.shield, -d); s.idle = 0; } else if (d > 0.5 && s.idle >= def.shieldDelay - 0.25 && s.shield < ms) s.shield = Math.min(ms, s.shield + d); s.tot = m.hp; s.idle += dt; } s.maxhp = mh; s.maxShield = ms; if (m.hp - s.shield > mh) s.shield = clamp(m.hp - mh, 0, ms); // more than the hp part can hold: the rest is shield s.hp = clamp(m.hp - s.shield, 0, mh); s.x = m.x; s.y = m.y; s.slow = m.slow; s.air = m.air; return s; } _drawFx(ctx, dt, sc, rot) { // lines (tracers, arcs) ctx.lineCap = 'round'; ctx.lineJoin = 'round'; for (let i = this.lines.length - 1; i >= 0; i--) { const l = this.lines[i]; l.life -= dt; if (l.life <= 0) { this.lines.splice(i, 1); continue; } ctx.globalAlpha = l.life / l.max; ctx.strokeStyle = l.col; ctx.lineWidth = l.w; ctx.beginPath(); for (let j = 0; j < l.pts.length; j++) { const jx = l.jit && j > 0 ? (Math.random() - 0.5) * l.jit * 2 : 0, jy = l.jit && j > 0 ? (Math.random() - 0.5) * l.jit * 2 : 0; j ? ctx.lineTo(l.pts[j][0] + jx, l.pts[j][1] + jy) : ctx.moveTo(l.pts[j][0], l.pts[j][1]); } ctx.stroke(); } ctx.globalAlpha = 1; for (let i = this.rings.length - 1; i >= 0; i--) { const r = this.rings[i]; r.life -= dt; if (r.life <= 0) { this.rings.splice(i, 1); continue; } const k = 1 - r.life / r.max; ctx.globalAlpha = 1 - k; ctx.strokeStyle = r.col; ctx.lineWidth = r.w; ctx.beginPath(); ctx.arc(r.x, r.y, r.r0 + (r.r1 - r.r0) * k, 0, TAU); ctx.stroke(); } ctx.globalAlpha = 1; for (let i = this.pulses.length - 1; i >= 0; i--) { const r = this.pulses[i]; r.life -= dt; if (r.life <= 0) { this.pulses.splice(i, 1); continue; } const k = 1 - r.life / r.max; ctx.beginPath(); ctx.arc(r.x, r.y, r.r0 + (r.r1 - r.r0) * k, 0, TAU); if (r.fill) { ctx.globalAlpha = (1 - k) * r.fill; ctx.fillStyle = r.col; ctx.fill(); } ctx.globalAlpha = 1 - k; ctx.strokeStyle = r.col; ctx.lineWidth = r.w; ctx.stroke(); } ctx.globalAlpha = 1; for (let i = this.parts.length - 1; i >= 0; i--) { const p = this.parts[i]; p.life -= dt; if (p.life <= 0) { this.parts.splice(i, 1); continue; } p.x += p.vx * dt; p.y += p.vy * dt; p.vx *= 0.94; p.vy *= 0.94; ctx.globalAlpha = Math.min(1, p.life / p.max * 1.4); ctx.fillStyle = p.c; ctx.fillRect(p.x - p.s / 2, p.y - p.s / 2, p.s, p.s); } ctx.globalAlpha = 1; if (this.texts.length) { ctx.font = '700 ' + (12 / sc) + 'px ui-monospace, monospace'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; for (let i = this.texts.length - 1; i >= 0; i--) { const x = this.texts[i]; x.life -= dt; if (x.life <= 0) { this.texts.splice(i, 1); continue; } if (rot) x.x -= 22 * dt; else x.y -= 22 * dt; // drift towards the top of the screen ctx.globalAlpha = Math.min(1, x.life / x.max * 1.6); ctx.fillStyle = x.col; ctx.save(); ctx.translate(x.x, x.y); if (rot) ctx.rotate(-rot); ctx.fillText(x.txt, 0, 0); ctx.restore(); } ctx.globalAlpha = 1; ctx.textAlign = 'start'; } } // partner name labels: screen space, kept inside the stage, flipped below the cell when there is no room above it _cursorLabels(ctx, labels) { ctx.setTransform(this.dpr, 0, 0, this.dpr, 0, 0); ctx.font = '600 11px system-ui, sans-serif'; ctx.textBaseline = 'middle'; ctx.textAlign = 'center'; const half = C * this.scale / 2; for (const l of labels) { const p = this.cellToScreen(l.gx, l.gy), w = ctx.measureText(l.nm).width + 10; const x = clamp(p.x - w / 2, 2, Math.max(2, this.W - 2 - w)); let y = p.y - half - 17; if (y < 2) y = p.y + half + 2; y = clamp(y, 2, Math.max(2, this.H - 17)); ctx.fillStyle = l.col; ctx.fillRect(x, y, w, 15); ctx.fillStyle = '#04101a'; ctx.fillText(l.nm, x + w / 2, y + 7.5); } ctx.textAlign = 'start'; } } // Draw a tower icon into a small canvas (palette buttons, info panel) function drawIcon(canvas, type, level) { const dpr = Math.min(2, root.devicePixelRatio || 1), size = canvas.clientWidth || 34; canvas.width = canvas.height = Math.round(size * dpr); const ctx = canvas.getContext('2d'); ctx.setTransform(dpr, 0, 0, dpr, 0, 0); ctx.clearRect(0, 0, size, size); drawGlyph(ctx, type, level || 1, size / 2, size / 2, size * 1.1, -0.6, 0.3, 1); } NW.Renderer = Renderer; NW.drawIcon = drawIcon; NW.drawGlyph = drawGlyph; NW.drawMonster = drawMonster; NW.SLOT_COLORS = SLOT_COLORS; NW.CELL = C; NW.BOARD_MARGIN = MARGIN; })(typeof self !== 'undefined' ? self : this);