Skip to content

Black Friday

Annual Pro is $90 for the first year, normally $120. Or start monthly at $6 for two months, then $12.

Ends Dec 4, 11:59 PM PT.

Create an account
← Public packages

@kentcdodds/starsnake

Starsnake: steer a snake-like ship Asteroids style, grow a crystal tail, and spend crystals on towers to defend your core. Mobile friendly.

FollowForkOpen app

Files

client.ts

2 lines · 49.8 KB · TypeScript
// Generated by scripts/build.mjs from client/game.js. Do not edit by hand.
export const CLIENT_JS = "// Starsnake client. Plain browser JS, inlined into the page by app/page.ts (see scripts/build.mjs).\n;(() => {\n\t'use strict'\n\tconst $ = (id) => document.getElementById(id)\n\tconst canvas = $('c')\n\tconst ctx = canvas.getContext('2d', { alpha: false })\n\tconst KEYS = { hi: 'starsnake-hi', mute: 'starsnake-mute', seen: 'starsnake-seen' }\n\tconst store = {\n\t\tget(k) { try { return localStorage.getItem(k) } catch { return null } },\n\t\tset(k, v) { try { localStorage.setItem(k, v) } catch {} },\n\t}\n\tconst TAU = Math.PI * 2\n\tconst clamp = (v, a, b) => (v < a ? a : v > b ? b : v)\n\n\t// ---------- seeded random (use ?seed=N for reproducible runs) ----------\n\tconst params = new URLSearchParams(location.search)\n\tlet seed = Number(params.get('seed')) || (Math.random() * 4294967296) >>> 0\n\tfunction rand() {\n\t\tseed = (seed + 0x6d2b79f5) | 0\n\t\tlet t = Math.imul(seed ^ (seed >>> 15), 1 | seed)\n\t\tt = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t\n\t\treturn ((t ^ (t >>> 14)) >>> 0) / 4294967296\n\t}\n\tconst rr = (a, b) => a + (b - a) * rand()\n\n\t// ---------- tuning ----------\n\tconst SHORT_SIDE = 600 // world units along the shorter screen edge\n\tconst SPACING = 14\n\tconst SEG_R = 6\n\tconst HEAD_R = 8\n\tconst MAX_TAIL = 30\n\tconst GEMS_PER_SEG = 3\nconst TAIL_DMG = 2 // damage per tail hit\nconst TAIL_WHIP_SPEED = 180 // ship speed above this adds +1 damage (boosting whips harder)\nconst TAIL_BEAD_CD = 0.4 // seconds before the same bead can hit again\nconst TAIL_ROCK_CD = 0.15 // seconds before the same rock can be hit by the tail again\n\tconst TOWER = {\n\t\tpulse: { name: 'PULSE', color: '#5ef2ff', cost: 8, up: [10, 18], range: [125, 142, 160], rate: [2.2, 3.2, 4.2], dmg: [1, 1, 2] },\n\t\tcannon: { name: 'CANNON', color: '#ffb347', cost: 18, up: [20, 34], range: [165, 185, 205], rate: [0.55, 0.7, 0.9], dmg: [3, 4, 6], splash: [44, 52, 60] },\n\t\tfrost: { name: 'FROST', color: '#9bd1ff', cost: 12, up: [14, 24], range: [95, 115, 135], slow: [0.4, 0.55, 0.7] },\n\t}\n\tconst GUN_COSTS = [20, 40, 70]\n\tconst REPAIR_COST = 10\n\tconst REPAIR_HP = 20\n\tconst MAX_TOWERS = 14\n\tconst ROCK = { 1: { r: 12, hp: 1 }, 2: { r: 21, hp: 2 }, 3: { r: 34, hp: 3 } }\n\n\t// ---------- world / canvas ----------\n\tlet W = 600, H = 900, scale = 1, dpr = 1, vw = 0, vh = 0\n\tlet stars = null\n\tconst pointerCoarse = matchMedia('(pointer: coarse)').matches\n\tdocument.documentElement.classList.add(pointerCoarse ? 'touch' : 'mouse')\n\n\tfunction buildStars() {\n\t\tstars = document.createElement('canvas')\n\t\tstars.width = Math.max(1, Math.round(vw * dpr))\n\t\tstars.height = Math.max(1, Math.round(vh * dpr))\n\t\tconst s = stars.getContext('2d')\n\t\ts.fillStyle = '#060913'\n\t\ts.fillRect(0, 0, stars.width, stars.height)\n\t\tconst g = s.createRadialGradient(stars.width / 2, stars.height / 2, 0, stars.width / 2, stars.height / 2, Math.max(stars.width, stars.height) * 0.6)\n\t\tg.addColorStop(0, '#13204a')\n\t\tg.addColorStop(1, '#060913')\n\t\ts.fillStyle = g\n\t\ts.fillRect(0, 0, stars.width, stars.height)\n\t\tlet sd = 12345\n\t\tconst r = () => ((sd = (sd * 1664525 + 1013904223) >>> 0) / 4294967296)\n\t\tconst n = Math.round((vw * vh) / 2600)\n\t\tfor (let i = 0; i < n; i++) {\n\t\t\tconst x = r() * stars.width, y = r() * stars.height, b = r()\n\t\t\ts.fillStyle = `rgba(200,220,255,${0.15 + b * 0.55})`\n\t\t\tconst z = (b > 0.93 ? 2 : 1) * dpr\n\t\t\ts.fillRect(x, y, z, z)\n\t\t}\n\t}\n\n\tfunction resize() {\n\t\tvw = Math.max(1, window.innerWidth)\n\t\tvh = Math.max(1, window.innerHeight)\n\t\tdpr = Math.min(window.devicePixelRatio || 1, 2.5)\n\t\tcanvas.width = Math.round(vw * dpr)\n\t\tcanvas.height = Math.round(vh * dpr)\n\t\tcanvas.style.width = vw + 'px'\n\t\tcanvas.style.height = vh + 'px'\n\t\tconst sc = Math.min(vw, vh) / SHORT_SIDE\n\t\tconst nw = vw / sc, nh = vh / sc\n\t\tif (G) {\n\t\t\tconst fx = nw / W, fy = nh / H\n\t\t\tconst mv = (o) => { o.x *= fx; o.y *= fy }\n\t\t\t;[G.ship, G.core, ...G.rocks, ...G.gems, ...G.towers, ...G.trail, ...G.ambient].forEach(mv)\n\t\t}\n\t\tW = nw; H = nh; scale = sc\n\t\tbuildStars()\n\t}\n\n\t// ---------- game state ----------\n\tlet G = null\n\tfunction newGame() {\n\t\tconst g = {\n\t\t\tstate: 'title', t: 0, score: 0, wave: 0, crystals: 10, gemCount: 0, tailLen: 3, gunLvl: 1,\n\t\t\tcore: { x: W / 2, y: H / 2, r: 26, hp: 100, max: 100, cd: 0, flash: 0 },\n\t\t\tship: { x: W / 2, y: H / 2 + 140, vx: 0, vy: 0, a: Math.PI, inv: 0, cd: 0, thrust: 0 },\n\t\t\ttrail: [], segs: [], segCd: [], segHit: [], segFlash: [], bullets: [], rocks: [], gems: [], towers: [], parts: [], rings: [], ambient: [],\n\t\t\tqueue: [], spawnT: 0, waveState: 'break', breakT: 1.2, shake: 0, over: '', dyingT: 0, banner: { t: 0, text: '' },\n\t\t\ttoast: { t: 0, text: '', bad: false }, kills: 0, tailKills: 0, playT: 0, hintT: 0, tailBit: null,\n\t\t}\n\t\tfor (let i = 0; i < 160; i++) g.trail.push({ x: g.ship.x + i * 2.2, y: g.ship.y })\n\t\treturn g\n\t}\n\tG = null\n\n\t// ---------- input ----------\n\tconst input = { left: false, right: false, up: false, stick: null }\n\tconst pointers = new Map()\n\n\t// ---------- audio ----------\n\tlet ac = null\n\tlet muted = store.get(KEYS.mute) === '1'\n\tfunction audio() {\n\t\tif (!ac) { try { ac = new (window.AudioContext || window.webkitAudioContext)() } catch {} }\n\t\tif (ac && ac.state === 'suspended') ac.resume().catch(() => {})\n\t\treturn ac\n\t}\n\tfunction tone(f0, f1, dur, type, vol) {\n\t\tif (muted || !ac) return\n\t\tconst t = ac.currentTime\n\t\tconst o = ac.createOscillator(), g = ac.createGain()\n\t\to.type = type || 'square'\n\t\to.frequency.setValueAtTime(f0, t)\n\t\to.frequency.exponentialRampToValueAtTime(Math.max(20, f1), t + dur)\n\t\tg.gain.setValueAtTime(vol || 0.05, t)\n\t\tg.gain.exponentialRampToValueAtTime(0.0001, t + dur)\n\t\to.connect(g).connect(ac.destination)\n\t\to.start(t); o.stop(t + dur + 0.02)\n\t}\n\tlet noiseBuf = null\n\tfunction noise(dur, vol, hp) {\n\t\tif (muted || !ac) return\n\t\tif (!noiseBuf) {\n\t\t\tnoiseBuf = ac.createBuffer(1, ac.sampleRate, ac.sampleRate)\n\t\t\tconst d = noiseBuf.getChannelData(0)\n\t\t\tfor (let i = 0; i < d.length; i++) d[i] = Math.random() * 2 - 1\n\t\t}\n\t\tconst t = ac.currentTime\n\t\tconst s = ac.createBufferSource(), g = ac.createGain(), f = ac.createBiquadFilter()\n\t\ts.buffer = noiseBuf; f.type = 'lowpass'; f.frequency.value = hp || 900\n\t\tg.gain.setValueAtTime(vol, t); g.gain.exponentialRampToValueAtTime(0.0001, t + dur)\n\t\ts.connect(f).connect(g).connect(ac.destination)\n\t\ts.start(t, Math.random() * 0.5, dur)\n\t}\n\tconst SFX = {\n\t\tshoot: () => tone(880, 440, 0.07, 'square', 0.02),\n\t\ttower: () => tone(1200, 700, 0.05, 'triangle', 0.025),\n\t\tboom: (s) => noise(0.12 + s * 0.08, 0.1 + s * 0.04, 500 + s * 300),\n\t\tgem: () => tone(660 + Math.random() * 80, 1320, 0.09, 'sine', 0.05),\n\t\tbuild: () => { tone(300, 600, 0.12, 'square', 0.05); setTimeout(() => tone(600, 900, 0.1, 'square', 0.04), 90) },\n\t\tup: () => { tone(500, 1000, 0.14, 'triangle', 0.06); setTimeout(() => tone(900, 1500, 0.12, 'triangle', 0.05), 100) },\n\t\twhip: () => { tone(220, 90, 0.08, 'sawtooth', 0.05); noise(0.06, 0.08, 1400) },\n\t\tbad: () => tone(160, 110, 0.18, 'sawtooth', 0.05),\n\t\thurt: () => { noise(0.25, 0.2, 400); tone(140, 50, 0.3, 'sawtooth', 0.07) },\n\t\twave: () => { tone(440, 440, 0.1, 'square', 0.04); setTimeout(() => tone(660, 660, 0.14, 'square', 0.04), 120) },\n\t\tover: () => { tone(400, 60, 0.9, 'sawtooth', 0.08); noise(0.8, 0.2, 700) },\n\t}\n\tfunction updateMuteBtn() {\n\t\tconst b = $('muteBtn')\n\t\tb.textContent = muted ? 'MUTE' : 'SND'\n\t\tb.setAttribute('aria-pressed', String(!muted))\n\t\tb.setAttribute('aria-label', muted ? 'Sound is off. Turn on' : 'Sound is on. Turn off')\n\t}\n\tfunction toggleMute() {\n\t\tmuted = !muted\n\t\tstore.set(KEYS.mute, muted ? '1' : '0')\n\t\tif (!muted) audio()\n\t\tupdateMuteBtn()\n\t}\n\n\t// ---------- helpers ----------\n\tconst wrapX = (x) => (x < 0 ? x + W : x >= W ? x - W : x)\n\tconst wrapY = (y) => (y < 0 ? y + H : y >= H ? y - H : y)\n\tconst dW = (a, b, L) => { let d = b - a; if (d > L / 2) d -= L; else if (d < -L / 2) d += L; return d }\n\tconst angDiff = (a, b) => { let d = (b - a) % TAU; if (d > Math.PI) d -= TAU; if (d < -Math.PI) d += TAU; return d }\n\n\tfunction burst(x, y, color, n, speed, life, size) {\n\t\tif (G.parts.length > 500) return\n\t\tfor (let i = 0; i < n; i++) {\n\t\t\tconst a = rand() * TAU, s = speed * (0.3 + rand() * 0.7)\n\t\t\tG.parts.push({ x, y, vx: Math.cos(a) * s, vy: Math.sin(a) * s, life: life * (0.5 + rand() * 0.5), max: life, color, size: size || 2 })\n\t\t}\n\t}\n\tfunction ring(x, y, r0, r1, color, life) { G.rings.push({ x, y, r0, r1, color, life, max: life }) }\n\tfunction say(text, bad) { G.toast = { t: 1.8, text, bad: !!bad } }\n\tfunction banner(text) { G.banner = { t: 2.2, text } }\n\n\t// ---------- rocks ----------\n\tfunction makeShape(r) {\n\t\tconst n = 8 + Math.floor(rand() * 4), pts = []\n\t\tfor (let i = 0; i < n; i++) pts.push([Math.cos((i / n) * TAU) * r * rr(0.78, 1.12), Math.sin((i / n) * TAU) * r * rr(0.78, 1.12)])\n\t\treturn pts\n\t}\n\tfunction addRock(x, y, vx, vy, size, boss) {\n\t\tconst def = ROCK[size]\n\t\tconst r = boss ? 58 : def.r\n\t\tconst hp = boss ? 14 + G.wave * 2 : def.hp + (G.wave >= 8 && size === 3 ? 1 : 0)\n\t\tG.rocks.push({ x, y, vx, vy, r, hp, max: hp, size, boss: !!boss, rot: rand() * TAU, vr: rr(-1.2, 1.2), shape: makeShape(r), slow: 0, hit: 0, safe: 0, tailCd: 0 })\n\t}\n\tfunction rockSpeed(size) { return Math.min(26 + G.wave * 2.4, 72) * (size === 1 ? 1.35 : size === 2 ? 1.15 : 1) }\n\tfunction spawnRock(size, boss) {\n\t\tconst def = ROCK[size], r = boss ? 58 : def.r\n\t\tconst side = Math.floor(rand() * 4)\n\t\tlet x, y\n\t\tif (side === 0) { x = rr(0, W); y = -r - 8 }\n\t\telse if (side === 1) { x = W + r + 8; y = rr(0, H) }\n\t\telse if (side === 2) { x = rr(0, W); y = H + r + 8 }\n\t\telse { x = -r - 8; y = rr(0, H) }\n\t\tconst a = Math.atan2(G.core.y - y, G.core.x - x) + rr(-0.5, 0.5)\n\t\tconst sp = rockSpeed(size) * (boss ? 0.6 : 1) * rr(0.85, 1.15)\n\t\taddRock(x, y, Math.cos(a) * sp, Math.sin(a) * sp, size, boss)\n\t}\n\tfunction startWave() {\n\t\tG.wave++\n\t\tconst n = G.wave\n\t\tconst big = 3 + Math.floor(n * 1.3), med = Math.floor(n * 0.9), small = n > 3 ? Math.floor(n * 0.6) : 0\n\t\tconst q = []\n\t\tfor (let i = 0; i < big; i++) q.push(3)\n\t\tfor (let i = 0; i < med; i++) q.push(2)\n\t\tfor (let i = 0; i < small; i++) q.push(1)\n\t\tfor (let i = q.length - 1; i > 0; i--) { const j = Math.floor(rand() * (i + 1)); [q[i], q[j]] = [q[j], q[i]] }\n\t\tif (n % 5 === 0) q.push('boss')\n\t\tG.queue = q\n\t\tG.spawnT = 0.5\n\t\tG.waveState = 'fight'\n\t\tbanner(n % 5 === 0 ? `WAVE ${n}: BIG ROCK` : `WAVE ${n}`)\n\t\tSFX.wave()\n\t}\n\tfunction destroyRock(rk, byPlayer, byTail) {\n\t\tif (rk.dead) return\n\t\trk.dead = true\n\t\tif (byTail) G.tailKills++\n\t\tG.score += (rk.boss ? 300 : rk.size * 10) * (1 + Math.floor(G.wave / 4) * 0.25) | 0\n\t\tG.kills++\n\t\tburst(rk.x, rk.y, '#c8d6f5', 8 + rk.size * 4, 90 + rk.size * 30, 0.7, 2)\n\t\tring(rk.x, rk.y, rk.r * 0.5, rk.r * 1.6, '#8fb2ff', 0.3)\n\t\tG.shake = Math.max(G.shake, rk.boss ? 10 : rk.size * 1.2)\n\t\tSFX.boom(rk.boss ? 3 : rk.size)\n\t\tif (byPlayer !== false) {\n\t\t\tconst gems = rk.boss ? 10 : rk.size === 1 ? (rand() < 0.6 ? 1 : 0) : 1\n\t\t\tfor (let i = 0; i < gems; i++) G.gems.push({ x: rk.x + rr(-6, 6), y: rk.y + rr(-6, 6), vx: rk.vx * 0.3 + rr(-30, 30), vy: rk.vy * 0.3 + rr(-30, 30), life: 14, rot: rand() * TAU })\n\t\t}\n\t\tif (rk.boss) {\n\t\t\tfor (let i = 0; i < 4; i++) { const a = (i / 4) * TAU + rand(); addRock(rk.x, rk.y, rk.vx * 0.5 + Math.cos(a) * 55, rk.vy * 0.5 + Math.sin(a) * 55, 3); G.rocks[G.rocks.length - 1].safe = 0.6 }\n\t\t} else if (rk.size > 1) {\n\t\t\tfor (let i = 0; i < 2; i++) {\n\t\t\t\tconst a = rand() * TAU, sp = 40 + rand() * 40\n\t\t\t\taddRock(rk.x, rk.y, rk.vx * 0.6 + Math.cos(a) * sp, rk.vy * 0.6 + Math.sin(a) * sp, rk.size - 1)\n\t\t\t\tG.rocks[G.rocks.length - 1].safe = 0.6\n\t\t\t}\n\t\t}\n\t}\n\tfunction hitRock(rk, dmg, byTail) {\n\t\tif (rk.dead) return\n\t\trk.hp -= dmg\n\t\trk.hit = 0.08\n\t\tif (rk.hp <= 0) destroyRock(rk, true, byTail)\n\t}\n\n\t// ---------- bullets ----------\n\tfunction fire(x, y, ang, speed, life, dmg, color, extra) {\n\t\tG.bullets.push(Object.assign({ x, y, vx: Math.cos(ang) * speed, vy: Math.sin(ang) * speed, life, dmg, color, wrap: false }, extra))\n\t}\n\tfunction nearestRock(x, y, range, wrapAware) {\n\t\tlet best = null, bd = Infinity\n\t\tfor (const rk of G.rocks) {\n\t\t\tif (rk.dead) continue\n\t\t\tconst dx = wrapAware ? dW(x, rk.x, W) : rk.x - x, dy = wrapAware ? dW(y, rk.y, H) : rk.y - y\n\t\t\tconst d = Math.hypot(dx, dy) - rk.r\n\t\t\tif (d < range && d < bd) { bd = d; best = rk }\n\t\t}\n\t\treturn best\n\t}\n\tfunction aim(x, y, rk, speed) {\n\t\tconst dx = rk.x - x, dy = rk.y - y, t = Math.hypot(dx, dy) / speed\n\t\treturn Math.atan2(dy + rk.vy * t, dx + rk.vx * t)\n\t}\n\n\t// ---------- towers ----------\n\tfunction towerStat(t, key) { return TOWER[t.type][key][t.lvl - 1] }\n\tfunction upgradeCost(t) { return t.lvl >= 3 ? null : TOWER[t.type].up[t.lvl - 1] }\n\tfunction nearTower(x, y, d) {\n\t\tlet best = null, bd = d\n\t\tfor (const t of G.towers) { const k = Math.hypot(t.x - x, t.y - y); if (k < bd) { bd = k; best = t } }\n\t\treturn best\n\t}\n\tfunction tryUpgrade(t) {\n\t\tconst c = upgradeCost(t)\n\t\tif (c == null) return say('TOWER MAXED', true), SFX.bad(), false\n\t\tif (G.crystals < c) return say(`NEED ${c} CRYSTALS`, true), SFX.bad(), false\n\t\tG.crystals -= c; t.lvl++; t.maxhp = 60 + (t.lvl - 1) * 30; t.hp = t.maxhp\n\t\tring(t.x, t.y, 10, 50, TOWER[t.type].color, 0.5); burst(t.x, t.y, TOWER[t.type].color, 14, 120, 0.6, 2)\n\t\tsay(`${TOWER[t.type].name} LEVEL ${t.lvl}`); SFX.up()\n\t\treturn true\n\t}\n\tfunction build(type) {\n\t\tif (G.state !== 'play') return\n\t\tconst def = TOWER[type], s = G.ship\n\t\tconst under = nearTower(s.x, s.y, 30)\n\t\tif (under && under.type === type) return tryUpgrade(under)\n\t\tif (G.crystals < def.cost) return say(`NEED ${def.cost} CRYSTALS`, true), SFX.bad()\n\t\tif (G.towers.length >= MAX_TOWERS) return say('TOWER LIMIT', true), SFX.bad()\n\t\tif (Math.hypot(s.x - G.core.x, s.y - G.core.y) < G.core.r + 26) return say('TOO CLOSE TO CORE', true), SFX.bad()\n\t\tif (nearTower(s.x, s.y, 30)) return say('TOO CLOSE TO A TOWER', true), SFX.bad()\n\t\tG.crystals -= def.cost\n\t\tG.towers.push({ x: s.x, y: s.y, type, lvl: 1, cd: 0.4, hp: 60, maxhp: 60, ang: -Math.PI / 2, hit: 0, born: 0 })\n\t\tring(s.x, s.y, 8, 46, def.color, 0.45); burst(s.x, s.y, def.color, 12, 100, 0.5, 2)\n\t\tSFX.build()\n\t}\n\tfunction repair() {\n\t\tif (G.state !== 'play') return\n\t\tif (G.core.hp >= G.core.max) return say('CORE IS FULL', true), SFX.bad()\n\t\tif (G.crystals < REPAIR_COST) return say(`NEED ${REPAIR_COST} CRYSTALS`, true), SFX.bad()\n\t\tG.crystals -= REPAIR_COST\n\t\tG.core.hp = Math.min(G.core.max, G.core.hp + REPAIR_HP)\n\t\tring(G.core.x, G.core.y, 20, 80, '#7dff9a', 0.6); say('CORE REPAIRED'); SFX.up()\n\t}\n\tfunction gunUp() {\n\t\tif (G.state !== 'play') return\n\t\tif (G.gunLvl >= 4) return say('GUN MAXED', true), SFX.bad()\n\t\tconst c = GUN_COSTS[G.gunLvl - 1]\n\t\tif (G.crystals < c) return say(`NEED ${c} CRYSTALS`, true), SFX.bad()\n\t\tG.crystals -= c; G.gunLvl++\n\t\tring(G.ship.x, G.ship.y, 10, 60, '#ffffff', 0.5); say(`GUN LEVEL ${G.gunLvl}`); SFX.up()\n\t}\n\tfunction upgradeNearest() {\n\t\tif (G.state !== 'play') return\n\t\tconst t = nearTower(G.ship.x, G.ship.y, 70)\n\t\tif (!t) return say('FLY NEAR A TOWER', true), SFX.bad()\n\t\ttryUpgrade(t)\n\t}\n\n\t// ---------- simulation ----------\n\tfunction updateSegs() {\n\t\tconst n = G.tailLen, segs = G.segs\n\t\tsegs.length = 0\n\t\tlet need = SPACING, acc = 0, px = G.ship.x, py = G.ship.y\n\t\tfor (let k = 0; k < G.trail.length && segs.length < n; k++) {\n\t\t\tconst t = G.trail[k], dx = dW(px, t.x, W), dy = dW(py, t.y, H), d = Math.hypot(dx, dy)\n\t\t\tif (d > 0) {\n\t\t\t\twhile (acc + d >= need && segs.length < n) {\n\t\t\t\t\tconst f = (need - acc) / d\n\t\t\t\t\tsegs.push({ x: wrapX(px + dx * f), y: wrapY(py + dy * f) })\n\t\t\t\t\tneed += SPACING\n\t\t\t\t}\n\t\t\t}\n\t\t\tacc += d; px = t.x; py = t.y\n\t\t}\n\t\twhile (segs.length < n) segs.push({ x: px, y: py })\n\t}\n\n\tfunction endGame(reason) {\n\t\tif (G.state !== 'play') return\n\t\tG.state = 'dying'; G.dyingT = 1.1; G.over = reason\n\t\tSFX.over()\n\t\tG.shake = 14\n\t\tconst s = G.ship\n\t\tburst(s.x, s.y, '#7dff9a', 40, 220, 1, 3)\n\t\tfor (const sg of G.segs) burst(sg.x, sg.y, '#7dff9a', 3, 120, 0.8, 2)\n\t\tif (reason === 'core') { burst(G.core.x, G.core.y, '#ff5f7a', 60, 260, 1.2, 3); ring(G.core.x, G.core.y, 20, 260, '#ff5f7a', 0.9) }\n\t}\n\n\tfunction stepPlay(dt) {\n\t\tconst s = G.ship, core = G.core\n\t\tG.playT += dt\n\t\t// steering\n\t\tlet thrust = 0\n\t\tconst st = input.stick\n\t\tif (st) {\n\t\t\tconst dx = st.x - st.ox, dy = st.y - st.oy, m = Math.min(1, Math.hypot(dx, dy) / 50)\n\t\t\tif (m > 0.18) {\n\t\t\t\tconst target = Math.atan2(dy, dx)\n\t\t\t\tconst d = angDiff(s.a, target), rate = 5.4 * dt\n\t\t\t\ts.a += clamp(d, -rate, rate)\n\t\t\t\tthrust = clamp((m - 0.5) * 2, 0, 1)\n\t\t\t}\n\t\t}\n\t\tif (input.left) s.a -= 3.8 * dt\n\t\tif (input.right) s.a += 3.8 * dt\n\t\tif (input.up) thrust = 1\n\t\ts.thrust = thrust\n\t\tconst hx = Math.cos(s.a), hy = Math.sin(s.a)\n\t\ts.vx += hx * thrust * 430 * dt\n\t\ts.vy += hy * thrust * 430 * dt\n\t\tconst drag = Math.exp(-0.9 * dt)\n\t\ts.vx *= drag; s.vy *= drag\n\t\tlet fwd = s.vx * hx + s.vy * hy\n\t\tlet sx = s.vx - hx * fwd, sy = s.vy - hy * fwd\n\t\tconst side = Math.exp(-3.2 * dt)\n\t\tsx *= side; sy *= side\n\t\tif (fwd < 72) fwd += (72 - fwd) * Math.min(1, 3 * dt)\n\t\ts.vx = hx * fwd + sx; s.vy = hy * fwd + sy\n\t\tconst sp = Math.hypot(s.vx, s.vy)\n\t\tif (sp > 300) { s.vx *= 300 / sp; s.vy *= 300 / sp }\n\t\tconst ox = s.x, oy = s.y\n\t\ts.x = wrapX(s.x + s.vx * dt); s.y = wrapY(s.y + s.vy * dt)\n\t\ts.inv = Math.max(0, s.inv - dt)\n\t\t// trail\n\t\tconst last = G.trail[0]\n\t\tif (Math.hypot(dW(last.x, s.x, W), dW(last.y, s.y, H)) >= 2) G.trail.unshift({ x: s.x, y: s.y })\n\t\tconst maxPts = Math.ceil(((G.tailLen + 3) * SPACING) / 1.5) + 20\n\t\tif (G.trail.length > maxPts) G.trail.length = maxPts\n\t\tupdateSegs()\n\t\tfor (let i = 0; i < G.segs.length; i++) { if (G.segHit[i] > 0) G.segHit[i] -= dt; if (G.segFlash[i] > 0) G.segFlash[i] -= dt }\n\t\tvoid ox; void oy\n\n\t\t// ship autofire\n\t\ts.cd -= dt\n\t\tif (s.cd <= 0) {\n\t\t\tconst lvl = G.gunLvl\n\t\t\ts.cd = lvl >= 3 ? 0.17 : 0.24\n\t\t\tconst muzzle = [s.x + hx * 11, s.y + hy * 11]\n\t\t\tconst lanes = lvl >= 4 ? [-0.2, 0, 0.2] : lvl >= 2 ? [-0.09, 0.09] : [0]\n\t\t\tfor (const l of lanes) fire(muzzle[0], muzzle[1], s.a + l, 540, 0.85, 1, '#ffffff', { wrap: true })\n\t\t\tif (G.playT % 0.5 < 0.25) SFX.shoot()\n\t\t}\n\t\t// tail guns\n\t\tG.segs.forEach((sg, i) => {\n\t\t\tif (i % 3 !== 2) return\n\t\t\tG.segCd[i] = (G.segCd[i] ?? rand() * 1.2) - dt\n\t\t\tif (G.segCd[i] > 0) return\n\t\t\tconst rk = nearestRock(sg.x, sg.y, 150, false)\n\t\t\tG.segCd[i] = 1.3\n\t\t\tif (rk) fire(sg.x, sg.y, aim(sg.x, sg.y, rk, 420), 420, 0.7, 1, '#7dff9a', {})\n\t\t})\n\t\t// core turret\n\t\tcore.cd -= dt\n\t\tcore.flash = Math.max(0, core.flash - dt)\n\t\tif (core.cd <= 0) {\n\t\t\tconst rk = nearestRock(core.x, core.y, 150, false)\n\t\t\tif (rk) { core.cd = 0.9; fire(core.x, core.y, aim(core.x, core.y, rk, 380), 380, 0.7, 1, '#ff9ad5', {}) } else core.cd = 0.2\n\t\t}\n\t\t// towers\n\t\tfor (const t of G.towers) {\n\t\t\tt.hit = Math.max(0, t.hit - dt); t.born += dt\n\t\t\tconst def = TOWER[t.type]\n\t\t\tconst range = towerStat(t, 'range')\n\t\t\tif (t.type === 'frost') continue\n\t\t\tt.cd -= dt\n\t\t\tconst rk = nearestRock(t.x, t.y, range, false)\n\t\t\tif (rk) {\n\t\t\t\tconst speed = t.type === 'pulse' ? 440 : 300\n\t\t\t\tt.ang = aim(t.x, t.y, rk, speed)\n\t\t\t\tif (t.cd <= 0) {\n\t\t\t\t\tt.cd = 1 / towerStat(t, 'rate')\n\t\t\t\t\tif (t.type === 'pulse') fire(t.x, t.y, t.ang, speed, 0.8, towerStat(t, 'dmg'), def.color, {})\n\t\t\t\t\telse fire(t.x, t.y, t.ang, speed, 1.1, towerStat(t, 'dmg'), def.color, { splash: towerStat(t, 'splash'), big: true })\n\t\t\t\t\tSFX.tower()\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t// rocks\n\t\tfor (const rk of G.rocks) {\n\t\t\tif (rk.dead) continue\n\t\t\tlet slow = 0\n\t\t\tfor (const t of G.towers) if (t.type === 'frost' && Math.hypot(t.x - rk.x, t.y - rk.y) < towerStat(t, 'range') + rk.r) slow = Math.max(slow, towerStat(t, 'slow'))\n\t\t\trk.slow = slow\n\t\t\tconst k = 1 - slow\n\t\t\tconst dx = core.x - rk.x, dy = core.y - rk.y, d = Math.hypot(dx, dy) || 1\n\t\t\trk.vx += (dx / d) * 12 * dt; rk.vy += (dy / d) * 12 * dt\n\t\t\tconst cap = rockSpeed(rk.size) * 1.8\n\t\t\tconst v = Math.hypot(rk.vx, rk.vy)\n\t\t\tif (v > cap) { rk.vx *= cap / v; rk.vy *= cap / v }\n\t\t\trk.x += rk.vx * k * dt; rk.y += rk.vy * k * dt\n\t\t\trk.rot += rk.vr * dt * k\n\t\t\trk.hit = Math.max(0, rk.hit - dt); rk.safe = Math.max(0, rk.safe - dt)\n\t\t\t// core\n\t\t\tif (d < core.r + rk.r * 0.8) {\n\t\t\t\tcore.hp -= rk.boss ? 30 : rk.size * 5\n\t\t\t\tcore.flash = 0.25\n\t\t\t\tG.shake = Math.max(G.shake, 9)\n\t\t\t\tSFX.hurt()\n\t\t\t\tburst(rk.x, rk.y, '#ff5f7a', 16, 160, 0.6, 2)\n\t\t\t\tdestroyRock(rk, false)\n\t\t\t\tG.score -= 0\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\t// towers\n\t\t\tfor (const t of G.towers) {\n\t\t\t\tif (t.dead) continue\n\t\t\t\tif (Math.hypot(t.x - rk.x, t.y - rk.y) < rk.r + 11) {\n\t\t\t\t\tt.hp -= rk.boss ? 60 : rk.size * 15; t.hit = 0.2\n\t\t\t\t\tburst(t.x, t.y, '#ff9a5f', 10, 120, 0.5, 2)\n\t\t\t\t\tif (t.hp <= 0) { t.dead = true; burst(t.x, t.y, TOWER[t.type].color, 24, 200, 0.8, 2); ring(t.x, t.y, 10, 60, '#ff5f7a', 0.4); SFX.hurt() }\n\t\t\t\t\tdestroyRock(rk, false)\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (rk.dead || rk.safe > 0) continue\n\t\t\t// tail is a weapon: beads hurt rocks (per-bead and per-rock cooldowns keep it from grinding)\n\t\t\trk.tailCd = Math.max(0, rk.tailCd - dt)\n\t\t\tif (rk.safe <= 0.3 && rk.tailCd <= 0) {\n\t\t\t\tconst whip = Math.hypot(s.vx, s.vy) > TAIL_WHIP_SPEED ? 1 : 0\n\t\t\t\tfor (let i = 0; i < G.segs.length; i++) {\n\t\t\t\t\tconst sg = G.segs[i]\n\t\t\t\t\tif ((G.segHit[i] || 0) > 0) continue\n\t\t\t\t\tconst dx = dW(sg.x, rk.x, W), dy = dW(sg.y, rk.y, H), dd = Math.hypot(dx, dy)\n\t\t\t\t\tif (dd >= rk.r + SEG_R) continue\n\t\t\t\t\tG.segHit[i] = TAIL_BEAD_CD; G.segFlash[i] = 0.15; rk.tailCd = TAIL_ROCK_CD\n\t\t\t\t\tconst nx = dx / (dd || 1), ny = dy / (dd || 1)\n\t\t\t\t\tburst(sg.x, sg.y, '#b6ff7a', 8, 150, 0.4, 2)\n\t\t\t\t\tring(sg.x, sg.y, 4, 22, '#b6ff7a', 0.2)\n\t\t\t\t\tSFX.whip()\n\t\t\t\t\tG.shake = Math.max(G.shake, 2.5)\n\t\t\t\t\tif (rk.boss) {\n\t\t\t\t\t\t// Big rocks fight back: a boss hit chips it for 1 and knocks the bead off.\n\t\t\t\t\t\thitRock(rk, 1, true)\n\t\t\t\t\t\tG.tailLen = Math.max(0, G.tailLen - 1); G.segs.splice(i, 1); G.segHit.splice(i, 1); G.segFlash.splice(i, 1)\n\t\t\t\t\t} else {\n\t\t\t\t\t\trk.vx += nx * 110; rk.vy += ny * 110\n\t\t\t\t\t\thitRock(rk, TAIL_DMG + whip, true)\n\t\t\t\t\t}\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (rk.dead) continue\n\t\t\t// ship\n\t\t\tif (s.inv <= 0 && Math.hypot(dW(s.x, rk.x, W), dW(s.y, rk.y, H)) < rk.r + HEAD_R) {\n\t\t\t\ts.inv = 1.4\n\t\t\t\tif (G.tailLen > 0) G.tailLen = Math.max(0, G.tailLen - 2)\n\t\t\t\telse { core.hp -= 6; core.flash = 0.2 }\n\t\t\t\tG.shake = Math.max(G.shake, 8)\n\t\t\t\tburst(s.x, s.y, '#ffffff', 16, 180, 0.6, 2)\n\t\t\t\tSFX.hurt()\n\t\t\t\tdestroyRock(rk, true)\n\t\t\t\tsay('SHIP HIT', true)\n\t\t\t}\n\t\t}\n\t\tG.towers = G.towers.filter((t) => !t.dead)\n\t\t// bullets\n\t\tfor (const b of G.bullets) {\n\t\t\tb.x += b.vx * dt; b.y += b.vy * dt; b.life -= dt\n\t\t\tif (b.wrap) { b.x = wrapX(b.x); b.y = wrapY(b.y) }\n\t\t\tlet hit = false\n\t\t\tif (b.life > 0) {\n\t\t\t\tfor (const rk of G.rocks) {\n\t\t\t\t\tif (rk.dead) continue\n\t\t\t\t\tconst dx = b.wrap ? dW(b.x, rk.x, W) : rk.x - b.x, dy = b.wrap ? dW(b.y, rk.y, H) : rk.y - b.y\n\t\t\t\t\tif (Math.hypot(dx, dy) < rk.r + (b.big ? 4 : 2.5)) {\n\t\t\t\t\t\thit = true\n\t\t\t\t\t\tif (b.splash) {\n\t\t\t\t\t\t\tfor (const o of G.rocks) if (!o.dead && Math.hypot(o.x - b.x, o.y - b.y) < b.splash + o.r) hitRock(o, b.dmg)\n\t\t\t\t\t\t} else hitRock(rk, b.dmg)\n\t\t\t\t\t\tburst(b.x, b.y, b.color, 4, 80, 0.3, 1.5)\n\t\t\t\t\t\tbreak\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (hit) b.life = 0\n\t\t\telse if (b.life <= 0 && b.splash) {\n\t\t\t\tfor (const o of G.rocks) if (!o.dead && Math.hypot(o.x - b.x, o.y - b.y) < b.splash + o.r) hitRock(o, b.dmg)\n\t\t\t}\n\t\t\tif (b.life <= 0 && b.splash) { ring(b.x, b.y, 4, b.splash, b.color, 0.3); burst(b.x, b.y, b.color, 10, 120, 0.4, 2) }\n\t\t}\n\t\tG.bullets = G.bullets.filter((b) => b.life > 0)\n\t\tG.rocks = G.rocks.filter((r) => !r.dead && r.x > -220 && r.x < W + 220 && r.y > -220 && r.y < H + 220)\n\t\t// gems\n\t\tfor (const g of G.gems) {\n\t\t\tg.life -= dt; g.rot += dt * 3\n\t\t\tconst dx = dW(g.x, s.x, W), dy = dW(g.y, s.y, H), d = Math.hypot(dx, dy)\n\t\t\tconst mag = 60 + G.tailLen * 1.2\n\t\t\tif (d < mag) { const pull = (1 - d / mag) * 520 + 120; g.vx += (dx / d) * pull * dt; g.vy += (dy / d) * pull * dt }\n\t\t\tg.vx *= Math.exp(-1.6 * dt); g.vy *= Math.exp(-1.6 * dt)\n\t\t\tg.x = wrapX(g.x + g.vx * dt); g.y = wrapY(g.y + g.vy * dt)\n\t\t\tif (d < HEAD_R + 8) {\n\t\t\t\tg.life = 0\n\t\t\t\tG.crystals++; G.gemCount++; G.score += 5\n\t\t\t\tif (G.gemCount % GEMS_PER_SEG === 0 && G.tailLen < MAX_TAIL) G.tailLen++\n\t\t\t\tSFX.gem()\n\t\t\t\tburst(g.x, g.y, '#ff7ad9', 4, 70, 0.3, 1.5)\n\t\t\t}\n\t\t}\n\t\tG.gems = G.gems.filter((g) => g.life > 0)\n\t\t// own tail\n\t\tfor (let i = 6; i < G.segs.length; i++) {\n\t\t\tconst sg = G.segs[i]\n\t\t\tif (Math.hypot(dW(s.x, sg.x, W), dW(s.y, sg.y, H)) < HEAD_R + SEG_R - 1) { G.tailBit = sg; endGame('tail'); break }\n\t\t}\n\t\t// waves\n\t\tif (G.waveState === 'break') {\n\t\t\tG.breakT -= dt\n\t\t\tif (G.breakT <= 0) startWave()\n\t\t} else {\n\t\t\tG.spawnT -= dt\n\t\t\tif (G.queue.length && G.spawnT <= 0) {\n\t\t\t\tconst k = G.queue.pop()\n\t\t\t\tif (k === 'boss') spawnRock(3, true); else spawnRock(k)\n\t\t\t\tG.spawnT = Math.max(0.55, 2.2 - G.wave * 0.1) * rr(0.7, 1.2)\n\t\t\t}\n\t\t\tif (!G.queue.length && !G.rocks.some((r) => !r.dead)) {\n\t\t\t\tG.waveState = 'break'; G.breakT = 4\n\t\t\t\tconst bonus = 3 + G.wave\n\t\t\t\tG.crystals += bonus; G.score += 100 * G.wave\n\t\t\t\tcore.hp = Math.min(core.max, core.hp + 8)\n\t\t\t\tfor (const t of G.towers) t.hp = t.maxhp\n\t\t\t\tbanner(`WAVE ${G.wave} CLEARED`)\n\t\t\t\tsay(`+${bonus} CRYSTALS, TOWERS REPAIRED`)\n\t\t\t\tSFX.up()\n\t\t\t}\n\t\t}\n\t\tif (core.hp <= 0) { core.hp = 0; endGame('core') }\n\t\tif (G.state === 'play') {\n\t\t\tif (G.hintT > 0) G.hintT -= dt\n\t\t}\n\t}\n\n\tfunction stepFx(dt) {\n\t\tfor (const p of G.parts) { p.x += p.vx * dt; p.y += p.vy * dt; p.vx *= Math.exp(-2 * dt); p.vy *= Math.exp(-2 * dt); p.life -= dt }\n\t\tG.parts = G.parts.filter((p) => p.life > 0)\n\t\tfor (const r of G.rings) r.life -= dt\n\t\tG.rings = G.rings.filter((r) => r.life > 0)\n\t\tG.shake = Math.max(0, G.shake - dt * 30)\n\t\tG.banner.t = Math.max(0, G.banner.t - dt)\n\t\tG.toast.t = Math.max(0, G.toast.t - dt)\n\t}\n\n\tfunction stepAmbient(dt) {\n\t\tfor (const a of G.ambient) {\n\t\t\ta.x = wrapX(a.x + a.vx * dt); a.y = wrapY(a.y + a.vy * dt); a.rot += a.vr * dt\n\t\t}\n\t}\n\n\t// ---------- rendering ----------\n\tfunction strokeGlow(color, w, fn) {\n\t\tctx.lineJoin = 'round'; ctx.lineCap = 'round'\n\t\tctx.globalAlpha = 0.22; ctx.lineWidth = w + 5; ctx.strokeStyle = color; fn(); ctx.stroke()\n\t\tctx.globalAlpha = 1; ctx.lineWidth = w; fn(); ctx.stroke()\n\t}\n\tfunction wrapDraw(x, y, r, fn) {\n\t\tconst xs = [0], ys = [0]\n\t\tif (x < r) xs.push(W); else if (x > W - r) xs.push(-W)\n\t\tif (y < r) ys.push(H); else if (y > H - r) ys.push(-H)\n\t\tfor (const ox of xs) for (const oy of ys) fn(x + ox, y + oy)\n\t}\n\tfunction drawRock(rk) {\n\t\tctx.save()\n\t\tctx.translate(rk.x, rk.y); ctx.rotate(rk.rot)\n\t\tconst color = rk.hit > 0 ? '#ffffff' : rk.slow > 0 ? '#9bd1ff' : rk.boss ? '#ff8a5f' : '#aab8d6'\n\t\tconst path = () => { ctx.beginPath(); rk.shape.forEach((p, i) => (i ? ctx.lineTo(p[0], p[1]) : ctx.moveTo(p[0], p[1]))); ctx.closePath() }\n\t\tpath(); ctx.fillStyle = rk.boss ? '#2a1620' : '#0f1630'; ctx.fill()\n\t\tstrokeGlow(color, rk.boss ? 3 : 2, path)\n\t\tif (rk.max > 1 && rk.hp < rk.max) {\n\t\t\tctx.globalAlpha = 0.7; ctx.strokeStyle = color; ctx.lineWidth = 1.2\n\t\t\tctx.beginPath(); ctx.moveTo(-rk.r * 0.3, -rk.r * 0.1); ctx.lineTo(rk.r * 0.1, rk.r * 0.2); ctx.lineTo(rk.r * 0.35, rk.r * 0.1); ctx.stroke(); ctx.globalAlpha = 1\n\t\t}\n\t\tctx.restore()\n\t}\n\tfunction drawGem(g) {\n\t\tconst a = g.life < 3 ? (Math.sin(G.t * 18) > 0 ? 1 : 0.3) : 1\n\t\tctx.save(); ctx.translate(g.x, g.y); ctx.rotate(g.rot); ctx.globalAlpha = a\n\t\tconst path = () => { ctx.beginPath(); ctx.moveTo(0, -7); ctx.lineTo(5, 0); ctx.lineTo(0, 7); ctx.lineTo(-5, 0); ctx.closePath() }\n\t\tpath(); ctx.fillStyle = '#ff7ad9'; ctx.fill(); strokeGlow('#ffc2f0', 1.4, path)\n\t\tctx.restore()\n\t}\n\tfunction drawTower(t) {\n\t\tconst def = TOWER[t.type], c = t.hit > 0 ? '#ffffff' : def.color\n\t\tctx.save(); ctx.translate(t.x, t.y)\n\t\tconst pop = Math.min(1, t.born * 5), sc = 0.6 + 0.4 * pop\n\t\tctx.scale(sc, sc)\n\t\tctx.globalAlpha = 0.13; ctx.fillStyle = def.color; ctx.beginPath(); ctx.arc(0, 0, 15, 0, TAU); ctx.fill(); ctx.globalAlpha = 1\n\t\tlet path\n\t\tif (t.type === 'pulse') path = () => { ctx.beginPath(); for (let i = 0; i < 6; i++) { const a = (i / 6) * TAU; i ? ctx.lineTo(Math.cos(a) * 11, Math.sin(a) * 11) : ctx.moveTo(Math.cos(a) * 11, Math.sin(a) * 11) } ctx.closePath() }\n\t\telse if (t.type === 'cannon') path = () => { ctx.beginPath(); ctx.rect(-10, -10, 20, 20) }\n\t\telse path = () => { ctx.beginPath(); ctx.moveTo(0, -13); ctx.lineTo(10, 0); ctx.lineTo(0, 13); ctx.lineTo(-10, 0); ctx.closePath() }\n\t\tpath(); ctx.fillStyle = '#0b1328'; ctx.fill(); strokeGlow(c, 2.2, path)\n\t\tif (t.type !== 'frost') {\n\t\t\tctx.rotate(t.ang)\n\t\t\tctx.beginPath(); ctx.moveTo(0, 0); ctx.lineTo(t.type === 'cannon' ? 17 : 15, 0)\n\t\t\tctx.strokeStyle = c; ctx.lineWidth = t.type === 'cannon' ? 5 : 3; ctx.lineCap = 'round'; ctx.stroke()\n\t\t\tctx.rotate(-t.ang)\n\t\t} else {\n\t\t\tctx.beginPath(); ctx.arc(0, 0, 3, 0, TAU); ctx.fillStyle = c; ctx.fill()\n\t\t}\n\t\tfor (let i = 0; i < t.lvl; i++) { ctx.beginPath(); ctx.arc(-6 + i * 6, 17, 2, 0, TAU); ctx.fillStyle = c; ctx.fill() }\n\t\tctx.restore()\n\t\tif (t.hp < t.maxhp) {\n\t\t\tctx.fillStyle = '#00000088'; ctx.fillRect(t.x - 12, t.y - 21, 24, 3)\n\t\t\tctx.fillStyle = t.hp / t.maxhp > 0.4 ? '#7dff9a' : '#ff5f7a'; ctx.fillRect(t.x - 12, t.y - 21, 24 * (t.hp / t.maxhp), 3)\n\t\t}\n\t}\n\tfunction drawCore() {\n\t\tconst c = G.core, hpf = c.hp / c.max, pulse = 1 + Math.sin(G.t * 3) * 0.05\n\t\tctx.save(); ctx.translate(c.x, c.y)\n\t\tfor (const rad of [70, 110]) { ctx.beginPath(); ctx.arc(0, 0, rad, 0, TAU); ctx.strokeStyle = 'rgba(94,242,255,0.07)'; ctx.lineWidth = 1; ctx.stroke() }\n\t\tconst col = c.flash > 0 ? '#ffffff' : '#ff9ad5'\n\t\tctx.globalAlpha = 0.16; ctx.fillStyle = col; ctx.beginPath(); ctx.arc(0, 0, c.r * 1.6 * pulse, 0, TAU); ctx.fill(); ctx.globalAlpha = 1\n\t\tconst path = () => { ctx.beginPath(); ctx.moveTo(0, -c.r * pulse); ctx.lineTo(c.r * 0.85 * pulse, 0); ctx.lineTo(0, c.r * pulse); ctx.lineTo(-c.r * 0.85 * pulse, 0); ctx.closePath() }\n\t\tpath(); ctx.fillStyle = '#2a0f2e'; ctx.fill(); strokeGlow(col, 3, path)\n\t\tctx.beginPath(); ctx.arc(0, 0, 6, 0, TAU); ctx.fillStyle = col; ctx.fill()\n\t\tctx.beginPath(); ctx.arc(0, 0, c.r + 9, -Math.PI / 2, -Math.PI / 2 + TAU * hpf)\n\t\tctx.strokeStyle = hpf > 0.5 ? '#7dff9a' : hpf > 0.25 ? '#ffd24d' : '#ff5f7a'; ctx.lineWidth = 4; ctx.lineCap = 'round'; ctx.stroke()\n\t\tctx.restore()\n\t}\n\tfunction drawShip(dim) {\n\t\tconst s = G.ship\n\t\tif (s.inv > 0 && Math.floor(G.t * 16) % 2 === 0) return\n\t\twrapDraw(s.x, s.y, 24, (x, y) => {\n\t\t\tctx.save(); ctx.translate(x, y); ctx.rotate(s.a)\n\t\t\tif (s.thrust > 0.05 || dim === 0) {\n\t\t\t\tconst f = 8 + Math.random() * 7 * Math.max(s.thrust, 0.3)\n\t\t\t\tctx.beginPath(); ctx.moveTo(-8, -4); ctx.lineTo(-8 - f, 0); ctx.lineTo(-8, 4)\n\t\t\t\tctx.fillStyle = '#ffb347'; ctx.fill()\n\t\t\t}\n\t\t\tconst path = () => { ctx.beginPath(); ctx.moveTo(13, 0); ctx.lineTo(-9, -9); ctx.lineTo(-4, 0); ctx.lineTo(-9, 9); ctx.closePath() }\n\t\t\tpath(); ctx.fillStyle = '#0d2b2a'; ctx.fill(); strokeGlow('#c8fff0', 2.2, path)\n\t\t\tctx.restore()\n\t\t})\n\t}\n\tfunction drawTail() {\n\t\tconst segs = G.segs\n\t\tif (!segs.length) return\n\t\t// body line\n\t\tctx.lineCap = 'round'; ctx.lineJoin = 'round'\n\t\tlet prev = { x: G.ship.x, y: G.ship.y }\n\t\tfor (let i = 0; i < segs.length; i++) {\n\t\t\tconst sg = segs[i], d = Math.hypot(dW(prev.x, sg.x, W), dW(prev.y, sg.y, H))\n\t\t\tif (d < SPACING * 2) {\n\t\t\t\tctx.beginPath(); ctx.moveTo(prev.x, prev.y); ctx.lineTo(prev.x + dW(prev.x, sg.x, W), prev.y + dW(prev.y, sg.y, H))\n\t\t\t\tctx.strokeStyle = 'rgba(46,230,200,0.35)'; ctx.lineWidth = 6; ctx.stroke()\n\t\t\t}\n\t\t\tprev = sg\n\t\t}\n\t\tfor (let i = segs.length - 1; i >= 0; i--) {\n\t\t\tconst sg = segs[i], f = i / Math.max(1, segs.length - 1)\n\t\t\tconst gunner = i % 3 === 2\n\t\t\tconst col = (G.segFlash[i] || 0) > 0 ? '#ffffff' : gunner ? '#b6ff7a' : `hsl(${160 - f * 25},85%,${62 - f * 14}%)`\n\t\t\twrapDraw(sg.x, sg.y, 10, (x, y) => {\n\t\t\t\tctx.beginPath(); ctx.arc(x, y, SEG_R * (1 - f * 0.18) * ((G.segFlash[i] || 0) > 0 ? 1.35 : 1), 0, TAU)\n\t\t\t\tctx.fillStyle = col; ctx.fill()\n\t\t\t\tctx.lineWidth = 1.5; ctx.strokeStyle = '#06241f'; ctx.stroke()\n\t\t\t\tif (gunner) { ctx.beginPath(); ctx.arc(x, y, 2, 0, TAU); ctx.fillStyle = '#ff7a3d'; ctx.fill() }\n\t\t\t})\n\t\t}\n\t\tif (G.tailBit && G.state !== 'play') {\n\t\t\tctx.beginPath(); ctx.arc(G.tailBit.x, G.tailBit.y, 12 + Math.sin(G.t * 20) * 3, 0, TAU); ctx.strokeStyle = '#ff5f7a'; ctx.lineWidth = 3; ctx.stroke()\n\t\t}\n\t}\n\tfunction drawWorld() {\n\t\tctx.drawImage(stars, 0, 0)\n\t\tconst sh = G.shake\n\t\tctx.setTransform(dpr * scale, 0, 0, dpr * scale, (rand() - 0.5) * sh * dpr, (rand() - 0.5) * sh * dpr)\n\t\t// ambient rocks on title\n\t\tfor (const a of G.ambient) { const rk = { x: a.x, y: a.y, rot: a.rot, shape: a.shape, r: a.r, hit: 0, slow: 0, boss: false, max: 1, hp: 1 }; drawRock(rk) }\n\t\tif (G.state !== 'title') drawCore()\n\t\t// frost / range rings for nearby towers\n\t\tfor (const t of G.towers) {\n\t\t\tif (t.type === 'frost') {\n\t\t\t\tctx.beginPath(); ctx.arc(t.x, t.y, towerStat(t, 'range'), 0, TAU)\n\t\t\t\tctx.fillStyle = 'rgba(155,209,255,0.06)'; ctx.fill(); ctx.strokeStyle = 'rgba(155,209,255,0.25)'; ctx.lineWidth = 1; ctx.stroke()\n\t\t\t}\n\t\t}\n\t\tif (G.state === 'play') {\n\t\t\tconst t = nearTower(G.ship.x, G.ship.y, 30)\n\t\t\tif (t) {\n\t\t\t\tctx.beginPath(); ctx.arc(t.x, t.y, towerStat(t, 'range'), 0, TAU); ctx.setLineDash([4, 6]); ctx.strokeStyle = TOWER[t.type].color + '66'; ctx.lineWidth = 1; ctx.stroke(); ctx.setLineDash([])\n\t\t\t}\n\t\t}\n\t\tfor (const t of G.towers) drawTower(t)\n\t\tfor (const g of G.gems) drawGem(g)\n\t\tfor (const rk of G.rocks) drawRock(rk)\n\t\tfor (const b of G.bullets) {\n\t\t\tctx.beginPath(); ctx.arc(b.x, b.y, b.big ? 3.6 : 2.2, 0, TAU); ctx.fillStyle = b.color; ctx.fill()\n\t\t\tctx.globalAlpha = 0.3; ctx.beginPath(); ctx.arc(b.x, b.y, b.big ? 7 : 4.5, 0, TAU); ctx.fill(); ctx.globalAlpha = 1\n\t\t}\n\t\tif (G.state === 'play' || G.state === 'dying' && G.dyingT > 0.8 || G.state === 'paused') { drawTail(); drawShip() }\n\t\tfor (const r of G.rings) {\n\t\t\tconst k = 1 - r.life / r.max\n\t\t\tctx.globalAlpha = (1 - k) * 0.8; ctx.beginPath(); ctx.arc(r.x, r.y, r.r0 + (r.r1 - r.r0) * k, 0, TAU); ctx.strokeStyle = r.color; ctx.lineWidth = 2; ctx.stroke()\n\t\t}\n\t\tfor (const p of G.parts) { ctx.globalAlpha = clamp(p.life / p.max, 0, 1); ctx.fillStyle = p.color; ctx.fillRect(p.x - p.size / 2, p.y - p.size / 2, p.size, p.size) }\n\t\tctx.globalAlpha = 1\n\t\t// screen-space overlays\n\t\tctx.setTransform(dpr, 0, 0, dpr, 0, 0)\n\t\tconst st = input.stick\n\t\tif (st && G.state === 'play') {\n\t\t\tconst dx = st.x - st.ox, dy = st.y - st.oy, d = Math.hypot(dx, dy), k = d > 50 ? 50 / d : 1\n\t\t\tctx.beginPath(); ctx.arc(st.ox, st.oy, 50, 0, TAU); ctx.strokeStyle = 'rgba(200,230,255,0.35)'; ctx.lineWidth = 2; ctx.stroke()\n\t\t\tctx.beginPath(); ctx.arc(st.ox, st.oy, 50, 0, TAU); ctx.fillStyle = 'rgba(200,230,255,0.05)'; ctx.fill()\n\t\t\tctx.beginPath(); ctx.arc(st.ox + dx * k, st.oy + dy * k, 20, 0, TAU); ctx.fillStyle = 'rgba(200,230,255,0.35)'; ctx.fill()\n\t\t}\n\t\t// banner and toast\n\t\tif (G.banner.t > 0 && (G.state === 'play')) {\n\t\t\tconst a = Math.min(1, G.banner.t * 2, (2.2 - G.banner.t) * 4)\n\t\t\tctx.globalAlpha = clamp(a, 0, 1); ctx.fillStyle = '#e8f4ff'; ctx.textAlign = 'center'\n\t\t\tctx.font = `800 ${Math.min(34, vw / 12)}px ui-monospace, Menlo, monospace`\n\t\t\tctx.fillText(G.banner.text, vw / 2, vh * 0.3); ctx.globalAlpha = 1\n\t\t}\n\t\tif (G.toast.t > 0 && G.state === 'play') {\n\t\t\tctx.globalAlpha = clamp(G.toast.t * 3, 0, 1); ctx.textAlign = 'center'\n\t\t\tctx.font = `700 13px ui-monospace, Menlo, monospace`\n\t\t\tctx.fillStyle = G.toast.bad ? '#ff8a9a' : '#9dffd0'\n\t\t\tctx.fillText(G.toast.text, vw / 2, vh - 112 - (document.documentElement.classList.contains('touch') ? 0 : 0)); ctx.globalAlpha = 1\n\t\t}\n\t\tif (G.state === 'play' && G.hintT > 0) {\n\t\t\tctx.globalAlpha = clamp(G.hintT, 0, 1); ctx.textAlign = 'center'; ctx.fillStyle = '#c9dcff'\n\t\t\tctx.font = `600 13px ui-monospace, Menlo, monospace`\n\t\t\tconst lines = pointerCoarse\n\t\t\t\t? ['Drag anywhere to steer', 'Push far to boost', 'Collect pink crystals', 'Tap a button to drop a tower', 'Whip your tail into rocks', 'Never touch it with your head']\n\t\t\t\t: ['Arrows or A/D turn, Up or W boost', 'Collect pink crystals', 'Press 1, 2, 3 to drop towers', 'Whip your tail into rocks', 'Never touch it with your head']\n\t\t\tlines.forEach((l, i) => ctx.fillText(l, vw / 2, vh * 0.6 + i * 20)); ctx.globalAlpha = 1\n\t\t}\n\t}\n\n\t// ---------- HUD ----------\n\tconst el = { coreBar: $('coreBar'), wave: $('wave'), score: $('score'), gems: $('gems'), hi: $('hiTitle'), bar: $('buildBar') }\n\tconst hudCache = {}\n\tfunction setText(id, v) { if (hudCache[id] !== v) { hudCache[id] = v; $(id).textContent = v } }\n\tfunction updateHud() {\n\t\tconst c = G.core\n\t\tconst f = Math.max(0, c.hp / c.max)\n\t\tconst w = Math.round(f * 100) + '%'\n\t\tif (hudCache.cb !== w) { hudCache.cb = w; el.coreBar.style.width = w; el.coreBar.className = f > 0.5 ? '' : f > 0.25 ? 'mid' : 'low' }\n\t\tsetText('wave', String(Math.max(1, G.wave)))\n\t\tsetText('score', String(G.score))\n\t\tsetText('gems', String(G.crystals))\n\t\tconst s = G.ship\n\t\tconst under = G.state === 'play' ? nearTower(s.x, s.y, 30) : null\n\t\tfor (const b of el.bar.querySelectorAll('button')) {\n\t\t\tconst a = b.dataset.act\n\t\t\tlet cost, lab = ''\n\t\t\tif (TOWER[a]) {\n\t\t\t\tif (under && under.type === a) { const u = upgradeCost(under); cost = u == null ? 0 : u; lab = u == null ? 'MAX' : 'UP ' + u } else cost = TOWER[a].cost\n\t\t\t} else if (a === 'repair') cost = REPAIR_COST\n\t\t\telse cost = G.gunLvl >= 4 ? 0 : GUN_COSTS[G.gunLvl - 1]\n\t\t\tconst text = lab || (a === 'gun' && G.gunLvl >= 4 ? 'MAX' : a === 'gun' ? 'L' + (G.gunLvl + 1) + ' ' + cost : String(cost))\n\t\t\tconst cEl = b.querySelector('.c')\n\t\t\tif (cEl.textContent !== text) cEl.textContent = text\n\t\t\tconst poor = cost > 0 && G.crystals < cost\n\t\t\tb.classList.toggle('poor', poor)\n\t\t\tb.classList.toggle('hot', !!lab && lab !== 'MAX' && !poor)\n\t\t}\n\t}\n\n\t// ---------- screens ----------\n\tconst screens = { title: $('title'), how: $('how'), pause: $('pause'), over: $('over') }\n\tfunction show(name) {\n\t\tfor (const k in screens) screens[k].hidden = k !== name\n\t\tdocument.body.dataset.screen = name || ''\n\t}\n\tfunction toTitle() {\n\t\tdocument.body.classList.remove('playing')\n\t\tG = newGame(); G.state = 'title'\n\t\tG.ambient = []\n\t\tfor (let i = 0; i < 7; i++) { const r = rr(14, 34); G.ambient.push({ x: rand() * W, y: rand() * H, vx: rr(-18, 18), vy: rr(-18, 18), rot: rand(), vr: rr(-0.6, 0.6), r, shape: makeShape(r) }) }\n\t\tel.hi.textContent = String(Number(store.get(KEYS.hi) || 0))\n\t\tshow('title')\n\t}\n\tfunction startGame() {\n\t\taudio()\n\t\tconst keepAmbient = []\n\t\tG = newGame(); G.state = 'play'; G.ambient = keepAmbient\n\t\tG.hintT = store.get(KEYS.seen) ? 0 : 7\n\t\tstore.set(KEYS.seen, '1')\n\t\thudCache.wave = null\n\t\tshow('')\n\t\tdocument.body.classList.add('playing')\n\t\tSFX.build()\n\t}\n\tfunction pauseGame(auto) {\n\t\tif (!G || G.state !== 'play') return\n\t\tG.state = 'paused'\n\t\tinput.stick = null; pointers.clear(); input.left = input.right = input.up = false\n\t\tshow('pause')\n\t\tvoid auto\n\t}\n\tfunction resumeGame() {\n\t\tif (!G || G.state !== 'paused') return\n\t\tG.state = 'play'; show(''); audio()\n\t}\n\tfunction finishOver() {\n\t\tG.state = 'over'\n\t\tconst hi = Number(store.get(KEYS.hi) || 0)\n\t\tconst best = Math.max(hi, G.score)\n\t\tif (G.score > hi) store.set(KEYS.hi, String(G.score))\n\t\t$('overReason').textContent = G.over === 'tail' ? 'You bit your own tail.' : 'Your core was destroyed.'\n\t\t$('overScore').textContent = String(G.score)\n\t\t$('overWave').textContent = String(Math.max(1, G.wave))\n\t\t$('overTail').textContent = String(G.tailKills)\n\t\t$('overBest').textContent = String(best)\n\t\t$('overNew').hidden = !(G.score > hi && G.score > 0)\n\t\tdocument.body.classList.remove('playing')\n\t\tshow('over')\n\t}\n\n\t// ---------- events ----------\n\tfunction screenToWorld(x, y) { return { x: x / scale, y: y / scale } }\n\tcanvas.addEventListener('pointerdown', (e) => {\n\t\taudio()\n\t\tif (!G || G.state !== 'play') return\n\t\te.preventDefault()\n\t\ttry { canvas.setPointerCapture(e.pointerId) } catch {}\n\t\tpointers.set(e.pointerId, { x: e.clientX, y: e.clientY, t: performance.now(), moved: 0 })\n\t\tif (!input.stick) input.stick = { id: e.pointerId, ox: e.clientX, oy: e.clientY, x: e.clientX, y: e.clientY }\n\t})\n\tcanvas.addEventListener('pointermove', (e) => {\n\t\tconst p = pointers.get(e.pointerId)\n\t\tif (!p) return\n\t\te.preventDefault()\n\t\tp.moved = Math.max(p.moved, Math.hypot(e.clientX - p.x, e.clientY - p.y))\n\t\tconst st = input.stick\n\t\tif (st && st.id === e.pointerId) {\n\t\t\tst.x = e.clientX; st.y = e.clientY\n\t\t\tconst dx = st.x - st.ox, dy = st.y - st.oy, d = Math.hypot(dx, dy)\n\t\t\tif (d > 70) { st.ox += (dx / d) * (d - 70); st.oy += (dy / d) * (d - 70) }\n\t\t}\n\t})\n\tfunction endPointer(e) {\n\t\tconst p = pointers.get(e.pointerId)\n\t\tpointers.delete(e.pointerId)\n\t\tif (input.stick && input.stick.id === e.pointerId) input.stick = null\n\t\tif (p && G && G.state === 'play' && performance.now() - p.t < 280 && p.moved < 12) {\n\t\t\tconst w = screenToWorld(e.clientX, e.clientY)\n\t\t\tconst t = nearTower(w.x, w.y, 26)\n\t\t\tif (t) tryUpgrade(t)\n\t\t}\n\t}\n\tcanvas.addEventListener('pointerup', endPointer)\n\tcanvas.addEventListener('pointercancel', endPointer)\n\tcanvas.addEventListener('contextmenu', (e) => e.preventDefault())\n\t// no zoom or scroll gestures\n\tfor (const ev of ['gesturestart', 'gesturechange', 'gestureend']) document.addEventListener(ev, (e) => e.preventDefault())\n\tdocument.addEventListener('touchmove', (e) => { if (!e.target.closest('.scroll')) e.preventDefault() }, { passive: false })\n\t// Double-tap zoom is blocked by touch-action (none on the page, manipulation on buttons) plus the viewport meta.\n\tdocument.addEventListener('dblclick', (e) => e.preventDefault())\n\tdocument.addEventListener('wheel', (e) => { if (e.ctrlKey) e.preventDefault() }, { passive: false })\n\n\tconst KEYMAP = { ArrowLeft: 'left', KeyA: 'left', ArrowRight: 'right', KeyD: 'right', ArrowUp: 'up', KeyW: 'up' }\n\taddEventListener('keydown', (e) => {\n\t\tif (e.ctrlKey || e.metaKey || e.altKey) return\n\t\taudio()\n\t\tconst st = G && G.state\n\t\tif (KEYMAP[e.code]) { input[KEYMAP[e.code]] = true; if (st === 'play') e.preventDefault(); return }\n\t\tif (e.code === 'Space' || e.code === 'Enter') {\n\t\t\tif (st === 'title' && document.activeElement === document.body) { e.preventDefault(); startGame() }\n\t\t\telse if (st === 'over' && document.activeElement === document.body) { e.preventDefault(); startGame() }\n\t\t\telse if (st === 'paused' && document.activeElement === document.body) { e.preventDefault(); resumeGame() }\n\t\t\treturn\n\t\t}\n\t\tif (e.code === 'Escape' || e.code === 'KeyP') { if (st === 'play') pauseGame(); else if (st === 'paused') resumeGame(); else if (st === 'title' && !screens.how.hidden) show('title'); return }\n\t\tif (e.code === 'KeyM') { toggleMute(); return }\n\t\tif (st !== 'play') return\n\t\tif (e.code === 'Digit1') build('pulse')\n\t\telse if (e.code === 'Digit2') build('cannon')\n\t\telse if (e.code === 'Digit3') build('frost')\n\t\telse if (e.code === 'Digit4' || e.code === 'KeyR') repair()\n\t\telse if (e.code === 'Digit5' || e.code === 'KeyG') gunUp()\n\t\telse if (e.code === 'KeyE' || e.code === 'KeyU') upgradeNearest()\n\t})\n\taddEventListener('keyup', (e) => { if (KEYMAP[e.code]) input[KEYMAP[e.code]] = false })\n\taddEventListener('blur', () => { input.left = input.right = input.up = false; pauseGame(true) })\n\tdocument.addEventListener('visibilitychange', () => { if (document.hidden) pauseGame(true) })\n\taddEventListener('pagehide', () => pauseGame(true))\n\taddEventListener('resize', () => { resize() })\n\taddEventListener('orientationchange', () => setTimeout(resize, 150))\n\tif (window.visualViewport) window.visualViewport.addEventListener('resize', resize)\n\n\t$('playBtn').addEventListener('click', startGame)\n\t$('howBtn').addEventListener('click', () => show('how'))\n\t$('howBack').addEventListener('click', () => show(G && G.state === 'paused' ? 'pause' : 'title'))\n\t$('howBtn2').addEventListener('click', () => show('how'))\n\t$('resumeBtn').addEventListener('click', resumeGame)\n\t$('quitBtn').addEventListener('click', toTitle)\n\t$('againBtn').addEventListener('click', startGame)\n\t$('menuBtn').addEventListener('click', toTitle)\n\t$('pauseBtn').addEventListener('click', () => pauseGame())\n\t$('muteBtn').addEventListener('click', toggleMute)\n\t// Build buttons: act on pointerdown so a tap while steering with the other thumb is instant.\n\tfor (const b of el.bar.querySelectorAll('button')) {\n\t\tb.addEventListener('pointerdown', (e) => {\n\t\t\te.preventDefault(); audio()\n\t\t\tconst a = b.dataset.act\n\t\t\tif (TOWER[a]) build(a); else if (a === 'repair') repair(); else if (a === 'gun') gunUp()\n\t\t})\n\t\tb.addEventListener('click', (e) => e.preventDefault())\n\t}\n\n\t// ---------- loop ----------\n\tlet last = performance.now()\n\tfunction frame(now) {\n\t\tconst dt = Math.min(0.033, (now - last) / 1000)\n\t\tlast = now\n\t\tG.t += dt\n\t\tif (G.state === 'play') stepPlay(dt)\n\t\telse if (G.state === 'dying') {\n\t\t\tG.dyingT -= dt\n\t\t\tstepPlay_dying(dt)\n\t\t\tif (G.dyingT <= 0) finishOver()\n\t\t} else if (G.state === 'title') stepAmbient(dt)\n\t\tif (G.state !== 'paused') stepFx(dt)\n\t\tdrawWorld()\n\t\tupdateHud()\n\t\trequestAnimationFrame(frame)\n\t}\n\tfunction stepPlay_dying(dt) {\n\t\tfor (const rk of G.rocks) { rk.x += rk.vx * dt * 0.3; rk.y += rk.vy * dt * 0.3; rk.rot += rk.vr * dt }\n\t}\n\n\t// ---------- debug / test hooks (read-mostly, no secrets) ----------\n\twindow.__starsnake = {\n\t\tget state() { return G.state },\n\t\tget snap() {\n\t\t\treturn { state: G.state, score: G.score, wave: G.wave, crystals: G.crystals, tail: G.tailLen, towers: G.towers.length, rocks: G.rocks.length, tailKills: G.tailKills, rockHp: G.rocks[0] ? G.rocks[0].hp : 0,\n\t\t\t\tcore: G.core.hp, gun: G.gunLvl, ship: { x: Math.round(G.ship.x), y: Math.round(G.ship.y), a: +G.ship.a.toFixed(2) }, over: G.over, world: [Math.round(W), Math.round(H)] }\n\t\t},\n\t\tstart: startGame,\n\t\tgive(n) { G.crystals += n },\n\t\tsetTail(n) { G.tailLen = n },\n\t\tbuild, repair, gunUp,\n\t\tscene() { // staged scene used for the social preview art\n\t\t\tstartGame()\n\t\t\tG.hintT = 0; G.crystals = 40; G.wave = 6; G.score = 2840; G.tailLen = 22; G.gunLvl = 3; G.core.hp = 78; G.waveState = 'fight'; G.queue = [3, 3, 2]\n\t\t\tG.banner.t = 0\n\t\t\tconst cx = G.core.x, cy = G.core.y\n\t\t\tconst T = (dx, dy, type, lvl) => G.towers.push({ x: cx + dx, y: cy + dy, type, lvl, cd: rand(), hp: 60, maxhp: 60, ang: rand() * TAU, hit: 0, born: 9 })\n\t\t\tT(-95, -60, 'pulse', 2); T(100, -40, 'cannon', 2); T(-70, 90, 'frost', 2); T(85, 100, 'pulse', 3); T(0, -130, 'cannon', 1); T(-150, 20, 'pulse', 1)\n\t\t\tconst s = G.ship; s.x = cx + 120; s.y = cy + 200; s.a = -0.9; s.vx = 0; s.vy = 0\n\t\t\tG.trail = []; for (let i = 0; i < 360; i++) { const a = i * 0.02; G.trail.push({ x: s.x - 150 * Math.sin(a * 3) * 0.5 - i * 0.2 + 40 * Math.cos(a * 5), y: s.y + i * 1.1 }) }\n\t\t\tconst mk = (dx, dy, size, vx, vy) => { addRock(cx + dx, cy + dy, vx, vy, size) }\n\t\t\tmk(-210, -250, 3, 20, 30); mk(190, -300, 3, -15, 25); mk(-60, -330, 2, 5, 30); mk(240, 20, 2, -30, 5); mk(-250, 110, 1, 28, -4); mk(30, -230, 1, 4, 28)\n\t\t\tfor (let i = 0; i < 7; i++) G.gems.push({ x: cx + rr(-180, 180), y: cy + rr(-160, 200), vx: 0, vy: 0, life: 20, rot: rand() * TAU })\n\t\t\tG.spawnT = 99\n\t\t\tfor (let i = 0; i < 6; i++) { stepFx(0.05) }\n\t\t\tG.state = 'play'\n\t\t},\n\t\ttailTest(size) { // test hook: park a rock on the tail so the tail has to deal with it\n\t\t\tG.rocks = []; G.towers = []; G.queue = []; G.waveState = 'break'; G.breakT = 99\n\t\t\tG.core.cd = 99\n\t\t\tconst sg = G.segs[Math.min(8, G.segs.length - 1)]\n\t\t\taddRock(sg.x, sg.y, 0, 0, size)\n\t\t\tG.rocks[0].safe = 0\n\t\t\treturn { rock: { hp: G.rocks[0].hp, size }, tail: G.tailLen }\n\t\t},\n\t\trockToTail() { // test hook: move surviving rocks back onto a tail bead\n\t\t\tconst sg = G.segs[Math.min(8, G.segs.length - 1)]\n\t\t\tfor (const r of G.rocks) { r.x = sg.x; r.y = sg.y; r.vx = 0; r.vy = 0 }\n\t\t},\n\t\tfreeze() { G.state = 'paused'; show('') },\n\t}\n\n\t// ---------- boot ----------\n\tupdateMuteBtn()\n\tresize()\n\ttoTitle()\n\tif (params.has('autostart')) startGame()\n\trequestAnimationFrame((t) => { last = t; requestAnimationFrame(frame) })\n})()\n"