/* * 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 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) function drawGlyph(ctx, type, level, cx, cy, S, ang, t, alpha, rot) { 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(); 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; default: break; } // level pips ctx.fillStyle = '#ffffff'; for (let i = 0; i < level; i++) { ctx.beginPath(); ctx.arc((i - (level - 1) / 2) * S * 0.13, h - S * 0.08, S * 0.035, 0, TAU); ctx.fill(); } ctx.restore(); } // ------------------------------------------------------------------ monsters function drawMonster(ctx, m, ang, t, flash, rot) { const def = TD.ENEMIES[m.kind], r = def.r * C, col = def.color; 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; } 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, m.kind === 'ghost' ? -7 : 0, r * 1.05, 0, TAU); ctx.fill(); ctx.globalAlpha = 1; } if (m.slow > 0) { ctx.lineWidth = 1.6; ctx.strokeStyle = 'rgba(77,227,255,.85)'; ctx.beginPath(); ctx.arc(0, m.kind === 'ghost' ? -7 : 0, r + 3.5, 0, TAU); ctx.stroke(); } ctx.restore(); // health bar if (m.hp < m.maxhp) { const w = Math.max(15, r * 2.3), y = -r - (m.kind === 'ghost' ? 16 : 9), x = -w / 2; const f = clamp(m.hp / m.maxhp, 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, 5); ctx.fillStyle = f > 0.5 ? '#35e0a1' : f > 0.25 ? '#ffd24d' : '#ff5c7a'; ctx.fillRect(x, y, w * f, 3); 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.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.aim.clear(); this.trk.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, (this.H - pad * 2) / d.h); 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); } 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 }); } _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 : 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 '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; 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]); 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; drawGlyph(ctx, tw.type, tw.level, cx, cy, C, ang, t, 1, rot); if (coop && tw.type !== 'wall') { ctx.fillStyle = SLOT_COLORS[tw.owner] || '#fff'; ctx.beginPath(); ctx.moveTo(cx - C * 0.43, cy - C * 0.43); ctx.lineTo(cx - C * 0.43 + 8, cy - C * 0.43); ctx.lineTo(cx - C * 0.43, cy - C * 0.43 + 8); ctx.closePath(); ctx.fill(); } } 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 ctx.font = '600 ' + (11 / sc) + 'px system-ui, sans-serif'; ctx.textBaseline = 'middle'; 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); const nm = (ui.names && ui.names[cu.slot]) || 'Partner'; const tw = ctx.measureText(nm).width + 10 / sc; ctx.save(); ctx.translate((gx + 0.5) * C, (gy + 0.5) * C); if (rot) ctx.rotate(-rot); ctx.fillStyle = col; ctx.fillRect(-tw / 2, -C / 2 - 17 / sc, tw, 15 / sc); ctx.fillStyle = '#04101a'; ctx.textAlign = 'center'; ctx.fillText(nm, 0, -C / 2 - 9.5 / sc); ctx.restore(); } ctx.textAlign = 'start'; 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(); } } _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; for (const m of view.monsters) { if (!!m.air !== air) continue; let k = trk.get(m.id); if (!k) { k = { x: m.x, y: m.y, ang: 0, hp: m.hp, flash: 0 }; trk.set(m.id, k); } 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.x = m.x; k.y = m.y; if (m.hp < k.hp) k.flash = 0.1; k.hp = m.hp; k.flash = Math.max(0, k.flash - dt); if (air) { const er = TD.ENEMIES[m.kind].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, m, k.ang, t, k.flash, rot); } 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); } } _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.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'; } } } // 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.SLOT_COLORS = SLOT_COLORS; NW.CELL = C; })(typeof self !== 'undefined' ? self : this);