لعبة الفراخ الفضائية 🐦
htmlv1MITنُشر ٢٠ أكتوبر ٢٠٢٦· ♥ 0 · ⬇ 20 · 💬 0
لعبة أركيد فضائية بتشتغل مباشرة في المتصفح على الموبايل والكمبيوتر، من غير تحميل ولا مكتبات خارجية. بتدافع عن المجرة ضد موجات أعداء متصاعدة، وبتواجه زعيم كل 5 موجات.
code-6.html
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, viewport-fit=cover">
<meta name="theme-color" content="#05061a">
<title>مدافع الفضاء</title>
<link href="https://fonts.googleapis.com/css2?family=Lalezar&family=IBM+Plex+Sans+Arabic:wght@500;700&display=swap" rel="stylesheet">
<style>
*{box-sizing:border-box;margin:0;-webkit-tap-highlight-color:transparent;-webkit-user-select:none;user-select:none;-webkit-touch-callout:none}
:root{--bg:#05061a;--ink:#f4f2ff;--mut:#a9a4d0;--a:#ff3d81;--b:#7c4dff;--gold:#ffc83d;--cy:#2de2c5;--ok:#4ade80;--line:#ffffff26;--p:#ffffff14;--r:14px}
html,body{height:100%;overflow:hidden;background:var(--bg);touch-action:none}
body{font-family:'IBM Plex Sans Arabic',Tahoma,Arial,sans-serif;color:var(--ink);display:flex;align-items:center;justify-content:center;
background:radial-gradient(60% 45% at 15% 0,#5b2bd044,transparent),radial-gradient(55% 45% at 100% 100%,#ff3d8133,transparent),var(--bg)}
#stage{position:relative;width:min(100vw,calc(100vh * .6));width:min(100vw,calc(100dvh * .6));aspect-ratio:3/5;max-height:100dvh;overflow:hidden;background:#05061a;box-shadow:0 0 0 1px var(--line),0 30px 80px -10px #000}
canvas{position:absolute;inset:0;width:100%;height:100%;display:block;touch-action:none}
button{font:700 1rem 'IBM Plex Sans Arabic',Tahoma,sans-serif;color:var(--ink);background:var(--p);border:1px solid var(--line);border-radius:var(--r);padding:8px 14px;cursor:pointer;min-height:44px;backdrop-filter:blur(8px);-webkit-backdrop-filter:blur(8px);outline:none;transition:background .15s,transform .1s,filter .15s}
button:hover{background:#ffffff26}
button:active{transform:scale(.96)}
:focus-visible{outline:3px solid var(--gold);outline-offset:2px}
/* HUD */
.hud{position:absolute;top:max(8px,env(safe-area-inset-top));inset-inline:8px;display:none;gap:6px;align-items:stretch;z-index:3;pointer-events:none}
.hud.on{display:flex}
.box{flex:1;background:linear-gradient(180deg,#ffffff1c,#ffffff0a);border:1px solid var(--line);border-radius:var(--r);padding:4px 8px;text-align:center;backdrop-filter:blur(10px);-webkit-backdrop-filter:blur(10px);min-width:0}
.box small{display:block;color:var(--mut);font-size:clamp(.58rem,2.3vw,.7rem);line-height:1.3}
.box b{direction:ltr;unicode-bidi:isolate;font-size:clamp(.95rem,4.2vw,1.25rem);font-variant-numeric:tabular-nums;white-space:nowrap;line-height:1.25;display:block}
#lv{color:var(--a);letter-spacing:1px}
#bm2{color:var(--cy)}
.hud .pb{pointer-events:auto;min-height:0;width:46px;padding:0;font-size:1rem;flex:none}
#combo{position:absolute;top:calc(max(8px,env(safe-area-inset-top)) + 62px);inset-inline-start:10px;z-index:3;display:none;align-items:center;gap:8px;pointer-events:none}
#combo.on{display:flex}
#combo b{direction:ltr;unicode-bidi:isolate;font:400 1.5rem Lalezar,sans-serif;color:var(--gold);text-shadow:0 0 14px #ffc83daa;min-width:2.2em}
#combo i{display:block;width:70px;height:5px;border-radius:9px;background:#ffffff26;overflow:hidden}
#combo i u{display:block;height:100%;width:100%;background:linear-gradient(90deg,var(--gold),var(--a));transform-origin:var(--o,left);text-decoration:none}
#boss{position:absolute;top:calc(max(8px,env(safe-area-inset-top)) + 96px);inset-inline:30px;z-index:3;display:none;text-align:center;pointer-events:none}
#boss.on{display:block}
#boss span{font:400 1rem Lalezar,sans-serif;color:var(--a);text-shadow:0 0 10px #ff3d81aa}
#boss i{display:block;height:9px;border-radius:9px;background:#00000099;border:1px solid #ffffff55;overflow:hidden;margin-top:2px}
#boss i u{display:block;height:100%;width:100%;text-decoration:none;background:linear-gradient(90deg,#ff3d81,#ffc83d);transform-origin:var(--o,left);transition:transform .1s}
#fx{position:absolute;bottom:max(10px,env(safe-area-inset-bottom));inset-inline:84px;display:flex;gap:6px;justify-content:center;flex-wrap:wrap;pointer-events:none;z-index:3}
#fx span{background:var(--p);border:1px solid var(--line);border-radius:99px;padding:2px 10px;font-size:.78rem;font-weight:700;backdrop-filter:blur(8px);-webkit-backdrop-filter:blur(8px)}
#bomb{position:absolute;right:12px;bottom:max(12px,env(safe-area-inset-bottom));width:64px;height:64px;border-radius:50%;z-index:3;padding:0;display:none;flex-direction:column;align-items:center;justify-content:center;font-size:.7rem;line-height:1.1;background:radial-gradient(circle at 35% 30%,#ffffff40,#ffffff10);border:2px solid #ffffff55}
#bomb.on{display:flex}
#bomb b{font-size:1.35rem}
#bomb[disabled]{opacity:.35}
#hint{position:absolute;bottom:calc(max(10px,env(safe-area-inset-bottom)) + 76px);inset-inline:30px;text-align:center;z-index:3;pointer-events:none;opacity:0;transition:opacity .5s}
#hint span{display:inline-block;background:#05061acc;border:1px solid var(--line);padding:8px 16px;border-radius:99px;font-size:.9rem;color:var(--ink)}
#hint.on{opacity:1}
#cd{position:absolute;inset:0;z-index:4;display:none;align-items:center;justify-content:center;font:400 9rem Lalezar,sans-serif;color:var(--gold);text-shadow:0 0 40px #ff3d81;pointer-events:none}
#cd.on{display:flex}
#cd.pop{animation:pop .6s ease-out}
@keyframes pop{0%{transform:scale(1.9);opacity:0}30%{opacity:1}100%{transform:scale(.9);opacity:.2}}
/* overlays */
.ov{position:absolute;inset:0;z-index:6;display:none;flex-direction:column;align-items:center;justify-content:center;gap:12px;text-align:center;padding:max(22px,env(safe-area-inset-top)) 22px max(22px,env(safe-area-inset-bottom));background:#05061ae6;backdrop-filter:blur(8px);-webkit-backdrop-filter:blur(8px);overflow-y:auto}
.ov.on{display:flex;animation:fadeIn .3s ease-out}
@keyframes fadeIn{from{opacity:0;transform:translateY(8px)}to{opacity:1;transform:none}}
#menu{justify-content:space-between;background:linear-gradient(#05061af0,#05061a40 32%,#05061a00 52%,#05061ae0);backdrop-filter:none;-webkit-backdrop-filter:none}
.mt{display:flex;flex-direction:column;align-items:center;gap:6px}
.mb{display:flex;flex-direction:column;align-items:center;gap:10px;width:100%}
h1{font:400 clamp(2.4rem,12vw,3.9rem)/1.1 Lalezar,sans-serif;background:linear-gradient(90deg,var(--gold),var(--a),var(--b),var(--gold));background-size:200% 100%;animation:sheen 6s linear infinite;-webkit-background-clip:text;background-clip:text;color:transparent;filter:drop-shadow(0 4px 24px #7c4dff66)}
@keyframes sheen{to{background-position:200% 0}}
h2{font:400 clamp(1.8rem,8vw,2.5rem) Lalezar,sans-serif;color:var(--gold)}
.sub{color:var(--mut);font-size:.92rem;line-height:1.8;max-width:340px}
.big{direction:ltr;font:400 3.2rem/1 Lalezar,sans-serif;font-variant-numeric:tabular-nums;background:linear-gradient(90deg,var(--gold),var(--a));-webkit-background-clip:text;background-clip:text;color:transparent}
.pill{background:var(--p);border:1px solid var(--line);border-radius:99px;padding:5px 16px;font-weight:700;font-size:.9rem}
.play{background:linear-gradient(135deg,var(--a),var(--b));border:0;font-size:1.2rem;padding:10px 38px;border-radius:16px;box-shadow:0 10px 30px -8px #ff3d81bb;min-width:200px}
.play:hover{filter:brightness(1.12);background:linear-gradient(135deg,var(--a),var(--b))}
.seg{display:flex;background:var(--p);border:1px solid var(--line);border-radius:14px;padding:3px;gap:3px;width:100%;max-width:320px}
.seg button{flex:1;min-height:38px;border:0;background:transparent;border-radius:11px;font-size:.95rem}
.seg button.on{background:linear-gradient(135deg,var(--a),var(--b));box-shadow:0 4px 14px -4px #ff3d81aa}
.row{display:flex;gap:8px;flex-wrap:wrap;justify-content:center}
.legend{display:flex;gap:6px 12px;flex-wrap:wrap;justify-content:center;font-size:.76rem;color:var(--mut);max-width:340px}
.legend i{display:inline-block;width:9px;height:9px;border-radius:50%;margin-inline-end:4px}
.tops{width:100%;max-width:320px;font-size:.85rem;list-style:none;padding:0;display:flex;flex-direction:column;gap:3px}
.tops li{display:flex;justify-content:space-between;background:var(--p);border:1px solid var(--line);border-radius:10px;padding:4px 12px;color:var(--mut)}
.tops li b{direction:ltr;unicode-bidi:isolate;color:var(--ink);font-variant-numeric:tabular-nums}
.tops li:first-child b{color:var(--gold)}
.tops li.me{border-color:var(--cy);color:var(--cy)}
.grid{display:grid;grid-template-columns:repeat(2,1fr);gap:8px;width:100%;max-width:320px}
.grid div{background:var(--p);border:1px solid var(--line);border-radius:12px;padding:6px 4px}
.grid small{display:block;color:var(--mut);font-size:.72rem}
.grid b{direction:ltr;unicode-bidi:isolate;font-size:1.15rem;font-variant-numeric:tabular-nums}
.medals{display:flex;gap:6px;flex-wrap:wrap;justify-content:center;max-width:340px}
.medals span{background:linear-gradient(135deg,#ffc83d33,#ff3d8133);border:1px solid #ffc83d88;border-radius:99px;padding:3px 12px;font-size:.8rem;font-weight:700;color:var(--gold)}
.rec{color:var(--cy);font-weight:700;animation:pulse 1s ease-in-out infinite alternate}
@keyframes pulse{to{transform:scale(1.08)}}
.tg{display:flex;justify-content:space-between;align-items:center;width:100%;max-width:300px;background:var(--p);border:1px solid var(--line);border-radius:12px;padding:6px 14px;font-weight:700}
.tg button{min-height:32px;padding:2px 14px;font-size:.85rem;border-radius:99px}
.tg button.on{background:linear-gradient(135deg,var(--a),var(--b));border-color:transparent}
.gear{position:absolute;top:max(10px,env(safe-area-inset-top));inset-inline-end:10px;width:44px;padding:0}
@media (prefers-reduced-motion:reduce){h1,.rec{animation:none}}
</style>
</head>
<body>
<main id="stage">
<canvas id="c" aria-label="Space Defender"></canvas>
<div class="hud" id="hud">
<div class="box"><small data-t="score"></small><b id="sc">0</b></div>
<div class="box"><small data-t="wave"></small><b id="wv">1</b></div>
<div class="box"><small data-t="lives"></small><b id="lv">♥♥♥</b></div>
<div class="box"><small data-t="bombs"></small><b id="bm2">●●</b></div>
<button class="pb" id="bp" type="button" aria-label="Pause">⏸</button>
</div>
<div id="combo"><b id="cx">×1</b><i><u id="cb"></u></i></div>
<div id="boss"><span id="bn"></span><i><u id="bb"></u></i></div>
<div id="fx"></div>
<div id="hint"><span id="hintT"></span></div>
<button id="bomb" type="button" aria-label="Bomb"><b>✺</b><span id="bombN">2</span></button>
<div id="cd"></div>
<section class="ov on" id="menu">
<button class="gear" id="gear1" type="button" aria-label="Settings">⚙</button>
<div class="mt">
<h1 data-t="title"></h1>
<p class="sub" data-t="tag"></p>
</div>
<div class="mb">
<div class="seg" id="diff">
<button type="button" data-d="easy" data-t="easy"></button>
<button type="button" data-d="normal" data-t="normal"></button>
<button type="button" data-d="hard" data-t="hard"></button>
</div>
<div class="pill"><span data-t="best"></span>: <span id="best1">0</span></div>
<ol class="tops" id="tops"></ol>
<p class="sub" data-t="how"></p>
<button class="play" id="go" type="button" data-t="play"></button>
</div>
</section>
<section class="ov" id="pause">
<h2 data-t="pause"></h2>
<button class="play" id="resume" type="button" data-t="resume"></button>
<button id="gear2" type="button" data-t="settings"></button>
<button id="restart1" type="button" data-t="restart"></button>
<button id="tomenu1" type="button" data-t="menu"></button>
</section>
<section class="ov" id="settings">
<h2 data-t="settings"></h2>
<div class="tg"><span data-t="sfx"></span><button type="button" data-k="sfx"></button></div>
<div class="tg"><span data-t="music"></span><button type="button" data-k="music"></button></div>
<div class="tg"><span data-t="vib"></span><button type="button" data-k="vib"></button></div>
<div class="tg"><span data-t="shake"></span><button type="button" data-k="shake"></button></div>
<button class="play" id="back" type="button" data-t="back"></button>
</section>
<section class="ov" id="over">
<h2 data-t="over"></h2>
<div class="big" id="fs">0</div>
<div class="rec" id="rec" hidden data-t="newrec"></div>
<div class="medals" id="medals"></div>
<div class="grid">
<div><small data-t="wave"></small><b id="s1">1</b></div>
<div><small data-t="kills"></small><b id="s2">0</b></div>
<div><small data-t="acc"></small><b id="s3">0%</b></div>
<div><small data-t="maxc"></small><b id="s4">×1</b></div>
</div>
<div class="pill"><span data-t="best"></span>: <span id="best2">0</span></div>
<button class="play" id="retry" type="button" data-t="again"></button>
<button id="tomenu2" type="button" data-t="menu"></button>
</section>
</main>
<script>
(() => {
'use strict';
const W = 480, H = 800, TAU = Math.PI * 2;
const $ = s => document.querySelector(s), $$ = s => [...document.querySelectorAll(s)];
const R = (a, b) => a + Math.random() * (b - a);
const clamp = (v, a, b) => Math.max(a, Math.min(b, v));
const pick = a => a[(Math.random() * a.length) | 0];
const cv = $('#c'), ctx = cv.getContext('2d');
/* ================= settings / storage ================= */
const reduce = !!(window.matchMedia && matchMedia('(prefers-reduced-motion: reduce)').matches);
const S = { best: { easy: 0, normal: 0, hard: 0 }, top: [], sfx: true, music: true, vib: true, shake: !reduce, diff: 'normal', tut: false };
try {
Object.assign(S, JSON.parse(localStorage.getItem('spaceDefender2') || '{}'));
const old = Number(localStorage.getItem('spaceDefenderBest') || 0);
if (old > (S.best.normal || 0)) S.best.normal = old;
} catch (e) {}
S.best = Object.assign({ easy: 0, normal: 0, hard: 0 }, S.best);
if (!Array.isArray(S.top)) S.top = [];
const save = () => { try { localStorage.setItem('spaceDefender2', JSON.stringify(S)); } catch (e) {} };
/* ================= i18n ================= */
let LANG = 'ar';
try { LANG = localStorage.getItem('khalid-lang') === 'en' ? 'en' : 'ar'; } catch (e) {}
document.documentElement.lang = LANG;
document.documentElement.dir = LANG === 'ar' ? 'rtl' : 'ltr';
const L = {
ar: {
title: 'مدافع الفضاء', tag: 'دافع عن المجرة، اهزم الزعماء، واكسر رقمك القياسي.',
score: 'النقاط', wave: 'الموجة', lives: 'الحياة', bombs: 'قنابل',
easy: 'سهل', normal: 'عادي', hard: 'صعب', best: 'أعلى نتيجة',
how: 'اسحب بإصبعك أو حرّك الماوس أو الأسهم / WASD • الإطلاق تلقائي • B أو زر القنبلة للطوارئ • P للإيقاف',
play: 'ابدأ اللعب', pause: 'إيقاف مؤقت', resume: 'متابعة', restart: 'إعادة من البداية', menu: 'القائمة الرئيسية',
settings: 'الإعدادات', sfx: 'المؤثرات الصوتية', music: 'الموسيقى', vib: 'الاهتزاز', shake: 'هزّ الشاشة', back: 'رجوع',
on: 'مفعّل', off: 'معطّل', over: 'انتهت اللعبة', again: 'العب مرة أخرى', newrec: 'رقم قياسي جديد!',
kills: 'الأعداء المُسقطون', acc: 'الدقة', maxc: 'أعلى مضاعف',
bwave: 'الموجة {n}', bboss: '⚠ زعيم قادم', bperfect: 'موجة مثالية! +{n}', bclear: 'تم تطهير الموجة +{n}',
hint: 'اسحب لتتحرك • زر القنبلة للطوارئ',
bosses: ['المدمّر', 'الحارس', 'الملكة'],
pu: { S: 'درع', R: 'إطلاق سريع', T: 'قوة نيران', H: 'حياة', B: 'قنبلة', D: 'مرافقين' },
m_boss: 'قاهر الزعماء', m_clean: 'لا يُمَس', m_combo: 'مضاعف ×8', m_sniper: 'قنّاص', m_surv: 'ناجٍ',
key: { sfx: 'sfx', music: 'music', vib: 'vib', shake: 'shake' }
},
en: {
title: 'Space Defender', tag: 'Defend the galaxy, beat the bosses and break your record.',
score: 'SCORE', wave: 'WAVE', lives: 'LIVES', bombs: 'BOMBS',
easy: 'Easy', normal: 'Normal', hard: 'Hard', best: 'Best',
how: 'Drag, move the mouse or use arrows / WASD • Auto-fire • B or the bomb button for emergencies • P to pause',
play: 'Play', pause: 'Paused', resume: 'Resume', restart: 'Restart', menu: 'Main menu',
settings: 'Settings', sfx: 'Sound effects', music: 'Music', vib: 'Vibration', shake: 'Screen shake', back: 'Back',
on: 'On', off: 'Off', over: 'Game over', again: 'Play again', newrec: 'New record!',
kills: 'Enemies down', acc: 'Accuracy', maxc: 'Best multiplier',
bwave: 'Wave {n}', bboss: '⚠ Boss incoming', bperfect: 'Perfect wave! +{n}', bclear: 'Wave cleared +{n}',
hint: 'Drag to move • Bomb button for emergencies',
bosses: ['The Destroyer', 'The Warden', 'The Queen'],
pu: { S: 'Shield', R: 'Rapid fire', T: 'Power up', H: 'Life', B: 'Bomb', D: 'Wingmen' },
m_boss: 'Boss slayer', m_clean: 'Untouchable', m_combo: 'Multiplier x8', m_sniper: 'Sharpshooter', m_surv: 'Survivor'
}
};
const t = (k, v) => {
let s = L[LANG][k]; if (s === undefined) s = L.ar[k]; if (s === undefined) s = k;
if (v && typeof s === 'string') for (const x in v) s = s.replace('{' + x + '}', v[x]);
return s;
};
document.title = t('title');
$$('[data-t]').forEach(e => { e.textContent = t(e.dataset.t); });
/* ================= difficulty ================= */
const DIFFS = {
easy: { lives: 4, spd: .85, fire: 1.35, hp: .85, sc: .8 },
normal: { lives: 3, spd: 1, fire: 1, hp: 1, sc: 1 },
hard: { lives: 2, spd: 1.15, fire: .8, hp: 1.25, sc: 1.3 }
};
let DF = DIFFS[S.diff] || DIFFS.normal;
/* ================= audio ================= */
let AC = null, master, sfxG, musG, noiseBuf;
function audio() {
if (!AC) {
try {
AC = new (window.AudioContext || window.webkitAudioContext)();
const comp = AC.createDynamicsCompressor();
master = AC.createGain(); master.gain.value = .9;
sfxG = AC.createGain(); musG = AC.createGain(); musG.gain.value = .5;
sfxG.connect(master); musG.connect(master); master.connect(comp); comp.connect(AC.destination);
noiseBuf = AC.createBuffer(1, Math.floor(AC.sampleRate * .8), AC.sampleRate);
const d = noiseBuf.getChannelData(0);
for (let i = 0; i < d.length; i++) d[i] = Math.random() * 2 - 1;
} catch (e) { AC = null; }
}
if (AC && AC.state === 'suspended') AC.resume();
}
function osc(f1, f2, d, type, v, when, dest) {
if (!AC) return;
const n = when || AC.currentTime, o = AC.createOscillator(), g = AC.createGain();
o.type = type; o.frequency.setValueAtTime(f1, n);
if (f2 !== f1) o.frequency.exponentialRampToValueAtTime(Math.max(20, f2), n + d);
g.gain.setValueAtTime(v, n); g.gain.exponentialRampToValueAtTime(.0001, n + d);
o.connect(g); g.connect(dest || sfxG); o.start(n); o.stop(n + d + .02);
}
function noise(d, v, freq, type, when, dest) {
if (!AC) return;
const n = when || AC.currentTime, s = AC.createBufferSource(), f = AC.createBiquadFilter(), g = AC.createGain();
s.buffer = noiseBuf; f.type = type; f.frequency.value = freq;
g.gain.setValueAtTime(v, n); g.gain.exponentialRampToValueAtTime(.0001, n + d);
s.connect(f); f.connect(g); g.connect(dest || sfxG); s.start(n, Math.random() * .3); s.stop(n + d + .02);
}
const sfx = {
shoot() { S.sfx && osc(900, 420, .07, 'square', .01); },
hit() { S.sfx && osc(320, 140, .06, 'square', .018); },
boom() { S.sfx && noise(.3, .12, 1300, 'lowpass'); },
big() { S.sfx && (noise(.7, .3, 520, 'lowpass'), osc(110, 30, .6, 'sine', .3)); },
pick() { if (!S.sfx) return; osc(520, 880, .11, 'sine', .05); osc(880, 1320, .12, 'sine', .04, AC && AC.currentTime + .07); },
hurt() { S.sfx && (osc(220, 50, .35, 'sawtooth', .06), noise(.5, .25, 500, 'lowpass')); },
bomb() { S.sfx && (osc(80, 700, .5, 'sawtooth', .07), noise(.9, .32, 700, 'lowpass')); },
warn() { if (!S.sfx) return; for (let i = 0; i < 3; i++) osc(440, 440, .22, 'square', .035, AC && AC.currentTime + i * .32); },
tick() { S.sfx && osc(660, 660, .1, 'sine', .05); },
go() { S.sfx && osc(990, 990, .25, 'sine', .06); }
};
function vibe(ms) { if (S.vib) { try { navigator.vibrate && navigator.vibrate(ms); } catch (e) {} } }
/* procedural music */
const M = { step: 0, next: 0, id: 0, boss: false };
const CHORDS = [[110, 1], [87.31, 0], [130.81, 0], [98, 0]];
const ARP = [0, 1, 2, 3, 2, 1, 3, 2];
function musicStep(s, w) {
const [root, minor] = CHORDS[(s >> 4) % 4], i = s & 15, boss = M.boss;
if (i % 4 === 0) osc(150, 42, .16, 'sine', .55, w, musG);
if (i === 4 || i === 12) noise(.12, .09, 1800, 'bandpass', w, musG);
if (i % 2 === 1 || boss) noise(.03, .03, 7000, 'highpass', w, musG);
if (boss ? i % 2 === 0 : (i === 0 || i === 3 || i === 6 || i === 8 || i === 11 || i === 14)) osc(root, root, .22, boss ? 'sawtooth' : 'triangle', boss ? .1 : .16, w, musG);
if (boss || i % 2 === 0) {
const r = minor ? [1, 1.189, 1.498, 2] : [1, 1.26, 1.498, 2];
const f = root * 2 * r[ARP[(i >> 1) % 8]];
osc(f, f, .13, boss ? 'sawtooth' : 'square', boss ? .03 : .03, w, musG);
}
}
function musicSched() {
if (!AC) return;
const dur = 60 / (M.boss ? 140 : 116) / 4;
while (M.next < AC.currentTime + .14) { musicStep(M.step, M.next); M.next += dur; M.step++; }
}
function musicStart() {
if (!AC || !S.music || M.id) return;
M.next = AC.currentTime + .06; M.id = setInterval(musicSched, 40);
}
function musicStop() { if (M.id) { clearInterval(M.id); M.id = 0; } }
/* ================= sprites ================= */
function grad(g, y0, y1, stops) { const l = g.createLinearGradient(0, y0, 0, y1); stops.forEach(([o, c]) => l.addColorStop(o, c)); return l; }
function sym(g, half) {
g.beginPath();
for (let i = 0; i < half.length; i += 2) i ? g.lineTo(half[i], half[i + 1]) : g.moveTo(half[i], half[i + 1]);
for (let i = half.length - 2; i >= 0; i -= 2) g.lineTo(-half[i], half[i + 1]);
g.closePath();
}
function sprite(w, h, fn) {
const mk = white => {
const c = document.createElement('canvas'); c.width = w * 2; c.height = h * 2;
const g = c.getContext('2d'); g.scale(2, 2); g.translate(w / 2, h / 2); g.lineJoin = 'round';
fn(g, white); return c;
};
return { w, h, c: mk(false), wh: mk(true) };
}
const SP = {};
const Gd = (g, wh, y0, y1, a, b, c) => wh ? '#fff' : grad(g, y0, y1, [[0, a], [.5, b], [1, c]]);
const Fl = (wh, c) => wh ? '#fff' : c;
SP.ship = sprite(76, 76, (g, wh) => {
sym(g, [0, -30, 7, -14, 13, -9, 32, 12, 30, 22, 15, 17, 8, 23, 0, 19]);
g.fillStyle = Gd(g, wh, -30, 23, '#d8ccff', '#6d4aff', '#271688'); g.fill();
g.strokeStyle = Fl(wh, '#ffffffb0'); g.lineWidth = 1.2; g.stroke();
if (!wh) {
g.fillStyle = '#ff3d81';
[1, -1].forEach(s => { g.beginPath(); g.moveTo(s * 26, 9); g.lineTo(s * 32, 12); g.lineTo(s * 30, 22); g.lineTo(s * 24, 18); g.closePath(); g.fill(); });
g.strokeStyle = '#ffffff40'; g.lineWidth = .8;
[1, -1].forEach(s => { g.beginPath(); g.moveTo(s * 10, -4); g.lineTo(s * 26, 14); g.moveTo(s * 9, 4); g.lineTo(s * 20, 17); g.stroke(); });
}
sym(g, [0, -27, 4.5, -8, 5.5, 10, 3.5, 22, 0, 20]);
g.fillStyle = Gd(g, wh, -27, 22, '#ffffff', '#b8a6ff', '#4b2fd0'); g.fill();
g.strokeStyle = Fl(wh, '#ffffff99'); g.lineWidth = .8; g.stroke();
g.fillStyle = wh ? '#fff' : grad(g, -14, 4, [[0, '#f2fffd'], [1, '#2de2c5']]);
g.beginPath(); g.ellipse(0, -5, 3.6, 8, 0, 0, TAU); g.fill();
if (!wh) {
g.fillStyle = '#ffffffcc'; g.beginPath(); g.ellipse(-1, -8, 1, 3, 0, 0, TAU); g.fill();
g.fillStyle = '#1c1840'; g.fillRect(-9, 17, 6, 6); g.fillRect(3, 17, 6, 6);
g.fillStyle = '#2de2c5'; g.fillRect(-8, 22, 4, 1.5); g.fillRect(4, 22, 4, 1.5);
}
});
SP.scout = sprite(54, 54, (g, wh) => {
sym(g, [0, 21, 9, 7, 22, -8, 25, -17, 12, -15, 6, -6, 0, -10]);
g.fillStyle = Gd(g, wh, -17, 21, '#ff86b0', '#e0245e', '#6f0d36'); g.fill();
g.strokeStyle = Fl(wh, '#ffffffaa'); g.lineWidth = 1.2; g.stroke();
if (!wh) {
sym(g, [0, 16, 3, 3, 3, -6, 0, -8]); g.fillStyle = '#2a0a1e'; g.fill();
g.fillStyle = '#ffe066'; g.beginPath(); g.arc(0, -1, 3.2, 0, TAU); g.fill();
g.fillStyle = '#fff'; g.beginPath(); g.arc(-.8, -1.8, 1, 0, TAU); g.fill();
}
});
SP.zig = sprite(60, 60, (g, wh) => {
sym(g, [0, 25, 10, 13, 22, 1, 19, -14, 9, -21, 0, -15]);
g.fillStyle = Gd(g, wh, -21, 25, '#ffe58f', '#ffb000', '#8f4a00'); g.fill();
g.strokeStyle = Fl(wh, '#ffffffaa'); g.lineWidth = 1.2; g.stroke();
if (!wh) {
g.fillStyle = '#2a1458'; [1, -1].forEach(s => g.fillRect(s > 0 ? 19 : -25, -5, 6, 15));
sym(g, [0, 14, 7, 3, 0, -9]); g.fillStyle = '#1b0b3d'; g.fill();
g.fillStyle = '#ff3d81'; g.beginPath(); g.arc(0, 1, 3.4, 0, TAU); g.fill();
g.strokeStyle = '#ffffff55'; g.lineWidth = .8; g.beginPath(); g.moveTo(-9, -12); g.lineTo(9, -12); g.stroke();
}
});
SP.tank = sprite(92, 92, (g, wh) => {
const hex = (r, a0) => { g.beginPath(); for (let i = 0; i < 6; i++) { const a = i / 6 * TAU + a0; g.lineTo(Math.cos(a) * r, Math.sin(a) * r); } g.closePath(); };
if (!wh) { g.fillStyle = '#394a58'; [-16, 0, 16].forEach(x => g.fillRect(x - 3, 30, 6, 15)); g.fillStyle = '#ff3d81'; [-16, 0, 16].forEach(x => g.fillRect(x - 3, 43, 6, 2)); }
hex(40, Math.PI / 6); g.fillStyle = Gd(g, wh, -40, 40, '#5cf0d2', '#1fa98f', '#0a4f4c'); g.fill();
g.strokeStyle = Fl(wh, '#ffffffaa'); g.lineWidth = 1.5; g.stroke();
if (!wh) {
hex(28, Math.PI / 6); g.fillStyle = '#0a2a30'; g.fill(); g.strokeStyle = '#2de2c588'; g.lineWidth = 1; g.stroke();
g.fillStyle = '#ffffff55'; for (let i = 0; i < 6; i++) { const a = i / 6 * TAU; g.beginPath(); g.arc(Math.cos(a) * 34, Math.sin(a) * 34, 1.6, 0, TAU); g.fill(); }
const c = g.createRadialGradient(0, 0, 1, 0, 0, 12); c.addColorStop(0, '#fff'); c.addColorStop(.4, '#ff3d81'); c.addColorStop(1, '#ff3d8100');
g.fillStyle = c; g.beginPath(); g.arc(0, 0, 12, 0, TAU); g.fill();
}
});
SP.kami = sprite(56, 56, (g, wh) => {
g.beginPath(); for (let i = 0; i < 16; i++) { const a = i / 16 * TAU, r = i % 2 ? 11 : 25; g.lineTo(Math.cos(a) * r, Math.sin(a) * r); } g.closePath();
g.fillStyle = wh ? '#fff' : (() => { const c = g.createRadialGradient(0, 0, 3, 0, 0, 25); c.addColorStop(0, '#ffd27a'); c.addColorStop(.6, '#ff7a3d'); c.addColorStop(1, '#a3200f'); return c; })();
g.fill(); g.strokeStyle = Fl(wh, '#ffffffaa'); g.lineWidth = 1.2; g.stroke();
if (!wh) { g.fillStyle = '#2a1458'; g.beginPath(); g.arc(0, 0, 6, 0, TAU); g.fill(); g.fillStyle = '#ffe066'; g.beginPath(); g.arc(0, 0, 2.4, 0, TAU); g.fill(); }
});
SP.boss0 = sprite(240, 200, (g, wh) => {
if (!wh) { g.fillStyle = '#ff3d81'; [1, -1].forEach(s => { g.fillRect(s > 0 ? 88 : -104, -14, 16, 66); g.fillStyle = '#1b0b3d'; g.fillRect(s > 0 ? 91 : -101, 46, 10, 14); g.fillStyle = '#ff3d81'; }); }
sym(g, [0, -74, 42, -70, 82, -32, 96, 10, 72, 52, 36, 80, 0, 86]);
g.fillStyle = Gd(g, wh, -74, 86, '#8a5cff', '#3a1a94', '#150a40'); g.fill();
g.strokeStyle = Fl(wh, '#ff3d81'); g.lineWidth = 3; g.stroke();
if (!wh) {
sym(g, [0, -56, 30, -52, 62, -24, 70, 6, 50, 36, 0, 40]); g.fillStyle = '#ffffff10'; g.fill();
sym(g, [0, -42, 26, -38, 36, -2, 20, 30, 0, 34]); g.fillStyle = '#12082e'; g.fill(); g.strokeStyle = '#ff3d8199'; g.lineWidth = 1.5; g.stroke();
g.strokeStyle = '#ffffff33'; g.lineWidth = 1; [-1, 1].forEach(s => { g.beginPath(); g.moveTo(s * 44, -56); g.lineTo(s * 76, 6); g.moveTo(s * 40, -40); g.lineTo(s * 64, 12); g.stroke(); });
g.fillStyle = '#ffc83d'; [-1, 1].forEach(s => { g.beginPath(); g.arc(s * 60, 30, 4, 0, TAU); g.fill(); });
const c = g.createRadialGradient(0, -4, 2, 0, -4, 26); c.addColorStop(0, '#fff'); c.addColorStop(.35, '#ffc83d'); c.addColorStop(1, '#ff3d8100');
g.fillStyle = c; g.beginPath(); g.arc(0, -4, 26, 0, TAU); g.fill();
}
});
SP.boss1 = sprite(240, 200, (g, wh) => {
sym(g, [0, -78, 30, -72, 60, -50, 66, -4, 40, 40, 0, 52]);
g.fillStyle = Gd(g, wh, -78, 52, '#7ff5e6', '#1fb7a6', '#075a58'); g.fill();
g.strokeStyle = Fl(wh, '#ffffffb0'); g.lineWidth = 2.5; g.stroke();
[1, -1].forEach(s => {
g.beginPath(); g.moveTo(s * 70, -60); g.lineTo(s * 108, -20); g.lineTo(s * 100, 60); g.lineTo(s * 70, 80); g.lineTo(s * 62, 10); g.closePath();
g.fillStyle = Gd(g, wh, -60, 80, '#c9fff8', '#2ccfbd', '#0b4a52'); g.fill(); g.strokeStyle = Fl(wh, '#ffffffb0'); g.lineWidth = 2.5; g.stroke();
if (!wh) { g.fillStyle = '#ff3d81'; g.fillRect(s * 95 - 4, 46, 8, 22); g.strokeStyle = '#ffffff44'; g.lineWidth = 1; g.beginPath(); g.moveTo(s * 72, -40); g.lineTo(s * 98, 0); g.stroke(); }
});
if (!wh) {
sym(g, [0, -50, 20, -44, 28, -8, 10, 24, 0, 28]); g.fillStyle = '#062c33'; g.fill();
const c = g.createRadialGradient(0, -10, 1, 0, -10, 22); c.addColorStop(0, '#fff'); c.addColorStop(.35, '#ff3d81'); c.addColorStop(1, '#ff3d8100');
g.fillStyle = c; g.beginPath(); g.arc(0, -10, 22, 0, TAU); g.fill();
g.fillStyle = '#2a0a1e'; g.beginPath(); g.ellipse(0, -10, 4, 9, 0, 0, TAU); g.fill();
}
});
SP.boss2 = sprite(240, 200, (g, wh) => {
[1, -1].forEach(s => {
g.beginPath(); g.moveTo(s * 18, -30); g.quadraticCurveTo(s * 110, -90, s * 112, 10); g.quadraticCurveTo(s * 96, 60, s * 30, 40); g.closePath();
g.fillStyle = Gd(g, wh, -80, 60, '#ffe58f', '#e6a800', '#7a3b00'); g.fill(); g.strokeStyle = Fl(wh, '#ffffffaa'); g.lineWidth = 2.5; g.stroke();
if (!wh) { g.strokeStyle = '#ffffff44'; g.lineWidth = 1; for (let k = 0; k < 3; k++) { g.beginPath(); g.moveTo(s * 30, -14 + k * 14); g.quadraticCurveTo(s * 80, -40 + k * 18, s * 104, -4 + k * 14); g.stroke(); } }
});
g.beginPath(); g.ellipse(0, 0, 40, 66, 0, 0, TAU);
g.fillStyle = Gd(g, wh, -66, 66, '#9dffb0', '#25b85a', '#0a4a2a'); g.fill(); g.strokeStyle = Fl(wh, '#ffffffb0'); g.lineWidth = 2.5; g.stroke();
if (!wh) {
g.fillStyle = '#082a18'; g.beginPath(); g.ellipse(0, -8, 22, 34, 0, 0, TAU); g.fill();
const c = g.createRadialGradient(0, -8, 1, 0, -8, 24); c.addColorStop(0, '#fff'); c.addColorStop(.4, '#ffc83d'); c.addColorStop(1, '#ffc83d00');
g.fillStyle = c; g.beginPath(); g.arc(0, -8, 24, 0, TAU); g.fill();
g.fillStyle = '#ff3d81'; [-1, 1].forEach(s => { g.beginPath(); g.arc(s * 26, 50, 5, 0, TAU); g.fill(); });
}
});
SP.drone = sprite(26, 26, (g, wh) => {
g.beginPath(); g.moveTo(0, -10); g.lineTo(8, 0); g.lineTo(0, 10); g.lineTo(-8, 0); g.closePath();
g.fillStyle = Gd(g, wh, -10, 10, '#d9fffa', '#2de2c5', '#0b6a62'); g.fill(); g.strokeStyle = Fl(wh, '#ffffffcc'); g.lineWidth = 1; g.stroke();
if (!wh) { g.fillStyle = '#fff'; g.beginPath(); g.arc(0, 0, 2.2, 0, TAU); g.fill(); }
});
/* power-up icons */
const PUC = { S: '#2de2c5', R: '#ffc83d', T: '#ff3d81', H: '#4ade80', B: '#ff9a3d', D: '#7cb7ff' };
const PUS = {};
Object.keys(PUC).forEach(k => {
PUS[k] = sprite(52, 52, g => {
const c = PUC[k];
const gl = g.createRadialGradient(0, 0, 8, 0, 0, 26); gl.addColorStop(0, c + '88'); gl.addColorStop(1, c + '00');
g.fillStyle = gl; g.beginPath(); g.arc(0, 0, 26, 0, TAU); g.fill();
g.fillStyle = '#0d0a33'; g.strokeStyle = c; g.lineWidth = 2.5; g.beginPath(); g.arc(0, 0, 15, 0, TAU); g.fill(); g.stroke();
g.fillStyle = c; g.strokeStyle = c; g.lineWidth = 2.4; g.lineCap = 'round';
if (k === 'S') { g.beginPath(); g.moveTo(0, -8); g.lineTo(7, -5); g.lineTo(6, 3); g.quadraticCurveTo(0, 9, -6, 3); g.lineTo(-7, -5); g.closePath(); g.fill(); }
else if (k === 'R') { for (const y of [-3, 4]) { g.beginPath(); g.moveTo(-6, y - 4); g.lineTo(0, y + 1); g.lineTo(6, y - 4); g.stroke(); } }
else if (k === 'T') { g.beginPath(); for (let i = 0; i < 8; i++) { const a = i / 8 * TAU - Math.PI / 2, r = i % 2 ? 3 : 9; g.lineTo(Math.cos(a) * r, Math.sin(a) * r); } g.closePath(); g.fill(); }
else if (k === 'H') { g.fillRect(-2.5, -8, 5, 16); g.fillRect(-8, -2.5, 16, 5); }
else if (k === 'B') { g.beginPath(); g.arc(0, 2, 6.5, 0, TAU); g.fill(); g.beginPath(); g.moveTo(3, -4); g.lineTo(6, -8); g.stroke(); }
else if (k === 'D') { g.beginPath(); g.arc(-6, 2, 3.2, 0, TAU); g.arc(6, 2, 3.2, 0, TAU); g.fill(); g.beginPath(); g.moveTo(-6, 2); g.lineTo(0, -5); g.lineTo(6, 2); g.stroke(); }
});
});
/* glow textures */
function glowTex(color, size) {
const c = document.createElement('canvas'); c.width = c.height = size;
const g = c.getContext('2d'), r = size / 2, gr = g.createRadialGradient(r, r, 0, r, r, r);
gr.addColorStop(0, '#fff'); gr.addColorStop(.25, color); gr.addColorStop(1, color.slice(0, 7) + '00');
g.fillStyle = gr; g.fillRect(0, 0, size, size); return c;
}
const GL = { cy: glowTex('#2de2c5ff', 64), pk: glowTex('#ff3d81ff', 64), or: glowTex('#ffb347ff', 64), wh: glowTex('#ffffffff', 64), gd: glowTex('#ffc83dff', 64) };
const SMOKE = glowTex('#2a1a55ff', 64);
/* background art */
const BLOBS = [
{ tex: glowTex('#5b2bd0ff', 256), x: 90, y: 140, s: 520, sp: 14, a: .5 },
{ tex: glowTex('#ff3d81ff', 256), x: 420, y: 430, s: 560, sp: 20, a: .22 },
{ tex: glowTex('#2de2c5ff', 256), x: 130, y: 640, s: 500, sp: 17, a: .16 },
{ tex: glowTex('#7c4dffff', 256), x: 380, y: -30, s: 420, sp: 24, a: .3 }
];
const baseBG = document.createElement('canvas'); baseBG.width = W; baseBG.height = H;
(() => { const g = baseBG.getContext('2d'); g.fillStyle = grad(g, 0, H, [[0, '#03041a'], [.6, '#0b0930'], [1, '#170a3a']]); g.fillRect(0, 0, W, H); })();
function makePlanet(base, ring) {
const s = 260, c = document.createElement('canvas'); c.width = c.height = s;
const g = c.getContext('2d'), r = 100, cx = s / 2, cy = s / 2;
g.save(); g.beginPath(); g.arc(cx, cy, r, 0, TAU); g.clip();
const b = g.createRadialGradient(cx - 40, cy - 40, 8, cx, cy, r * 1.1);
b.addColorStop(0, base[0]); b.addColorStop(.6, base[1]); b.addColorStop(1, '#02020c');
g.fillStyle = b; g.fillRect(0, 0, s, s);
g.globalAlpha = .18; g.fillStyle = '#fff';
for (let i = 0; i < 6; i++) g.fillRect(0, cy - r + i * 34 + R(0, 12), s, R(6, 16));
g.globalAlpha = 1;
const sh = g.createRadialGradient(cx + 60, cy + 50, 20, cx + 40, cy + 30, r * 1.5);
sh.addColorStop(0, '#00000000'); sh.addColorStop(1, '#000000ee'); g.fillStyle = sh; g.fillRect(0, 0, s, s);
g.restore();
g.strokeStyle = ring; g.globalAlpha = .55; g.lineWidth = 3; g.beginPath(); g.ellipse(cx, cy, r + 38, 20, -.35, Math.PI, TAU); g.stroke();
return c;
}
const PLANETS = [makePlanet(['#9b7bff', '#3a1a94'], '#c8b8ff'), makePlanet(['#ff9a6b', '#8f2a3a'], '#ffd1a6'), makePlanet(['#6be8d2', '#0e5a66'], '#b6fff3')];
let planet = { img: PLANETS[0], x: 360, y: -200, s: .8, sp: 9 };
const vignette = document.createElement('canvas'); vignette.width = W; vignette.height = H;
(() => { const g = vignette.getContext('2d'), v = g.createRadialGradient(W / 2, H / 2, H * .35, W / 2, H / 2, H * .72); v.addColorStop(0, '#00000000'); v.addColorStop(1, '#000000aa'); g.fillStyle = v; g.fillRect(0, 0, W, H); })();
const stars = [];
for (let l = 0; l < 3; l++) for (let i = 0; i < 36; i++) stars.push({ x: R(0, W), y: R(0, H), l, s: .7 + l * .6, tw: R(0, TAU) });
const SPEED = [26, 66, 140];
/* ================= canvas sizing ================= */
let dpr = 1;
function resize() {
const r = cv.getBoundingClientRect(); if (!r.width) return;
dpr = Math.min(2.5, Math.max(1, (r.width / W) * (window.devicePixelRatio || 1)));
cv.width = Math.round(W * dpr); cv.height = Math.round(H * dpr);
}
if (window.ResizeObserver) new ResizeObserver(resize).observe($('#stage')); else addEventListener('resize', resize);
resize();
/* ================= game state ================= */
let state = 'menu', t0 = 0, time = 0, score = 0, wave = 0, combo = 0, comboT = 0, mult = 1, maxMult = 1;
let shake = 0, flash = 0, redF = 0, waveT = 0, clearT = 0, timeScale = 1, cdT = 0, cdN = 0, settingsFrom = 'menu';
let kills = 0, shots = 0, hits = 0, bossKills = 0, dmgTaken = 0, waveDmg = false, bombCd = 0;
let P = null, E = [], PB = [], EB = [], PU = [], FX = [], queue = [];
let banner = { txt: '', t: 0, c: '#ffc83d' };
const keys = {};
function reset() {
DF = DIFFS[S.diff] || DIFFS.normal;
P = { x: W / 2, y: H - 140, tx: W / 2, ty: H - 140, vx: 0, r: 14, lives: DF.lives, inv: 0, shield: 0, rapid: 0, drone: 0, lvl: 1, cd: 0, bombs: 2 };
E = []; PB = []; EB = []; PU = []; FX = []; queue = [];
score = 0; wave = 0; combo = 0; comboT = 0; mult = 1; maxMult = 1; time = 0; shake = 0; flash = 0; redF = 0;
kills = 0; shots = 0; hits = 0; bossKills = 0; dmgTaken = 0; timeScale = 1; bombCd = 0;
banner.t = 0; startWave();
}
function addGroup(tt, kind, n) {
const q = (o) => queue.push(Object.assign({ t: tt }, o));
const allowed = ['scout']; if (n >= 2) allowed.push('zig'); if (n >= 3) allowed.push('kami'); if (n >= 4) allowed.push('tank');
switch (kind) {
case 'line': { const k = Math.min(3 + Math.floor(n / 3), 6); for (let i = 1; i <= k; i++) q({ type: 'scout', x: W / (k + 1) * i, amp: 0 }); return 2; }
case 'v': for (let i = -2; i <= 2; i++) q({ type: 'scout', x: W / 2 + i * 56, yo: Math.abs(i) * 36, amp: 0 }); return 2.4;
case 'snake': { const s = R(0, 1) > .5 ? 1 : -1; for (let i = 0; i < 7; i++) queue.push({ t: tt + i * .3, type: 'scout', x: W / 2, amp: 150 * s, ph: 0 }); return 3; }
case 'rain': for (let i = 0; i < 3; i++) q({ type: 'kami', x: R(50, W - 50) }); return 2;
case 'guard': q({ type: 'tank', x: W / 2 }); q({ type: 'zig', x: W / 2 - 110, yo: 40 }); q({ type: 'zig', x: W / 2 + 110, yo: 40 }); return 3.4;
case 'pair': q({ type: 'zig', x: 90 }); q({ type: 'zig', x: W - 90 }); return 1.8;
default: q({ type: pick(allowed), x: R(50, W - 50), amp: R(20, 70) }); return 1.2;
}
}
function startWave() {
wave++; waveT = 0; clearT = 0; queue = []; waveDmg = false;
const n = wave;
if (n % 5 === 0) {
const v = ((n / 5) - 1) % 3;
queue.push({ t: 1.6, type: 'boss', v });
queue.push({ t: 5, type: 'scout', x: 100, amp: 0 }, { t: 5, type: 'scout', x: W - 100, amp: 0 }, { t: 11, type: 'zig', x: W / 2 });
banner = { txt: t('bboss'), t: 2.8, c: '#ff3d81' }; sfx.warn();
} else {
const groups = Math.min(5 + Math.floor(n * 1.1), 14);
const kinds = ['single', 'single', 'line', 'v', 'snake'];
if (n >= 2) kinds.push('pair'); if (n >= 3) kinds.push('rain', 'v'); if (n >= 4) kinds.push('guard');
let tt = 1.1;
for (let i = 0; i < groups; i++) tt += addGroup(tt, i === 0 ? 'line' : pick(kinds), n) * Math.max(.55, 1 - n * .035);
banner = { txt: t('bwave', { n }), t: 2, c: '#ffc83d' };
}
queue.sort((a, b) => a.t - b.t);
}
function spawn(q) {
const n = wave, hs = b => Math.max(1, Math.round(b * DF.hp));
const e = { type: q.type, x: q.x === undefined ? R(40, W - 40) : q.x, y: -50 - (q.yo || 0), vx: 0, vy: 0, hp: 1, max: 1, r: 16, t: 0, cd: R(.8, 2) * DF.fire, hit: 0, score: 10, ph: q.ph || 0, bx: 0, amp: q.amp === undefined ? 0 : q.amp, rot: 0 };
e.bx = e.x;
switch (q.type) {
case 'scout': e.hp = hs(1 + (n >= 9 ? 1 : 0)); e.vy = (95 + n * 5) * DF.spd; e.r = 16; break;
case 'zig': e.hp = hs(2 + (n >= 8 ? 1 : 0)); e.vy = (70 + n * 4) * DF.spd; e.r = 19; e.score = 25; break;
case 'tank': e.hp = hs(5 + Math.floor(n / 2)); e.vy = 48; e.r = 30; e.score = 60; e.cd = 1; break;
case 'kami': e.vy = 130 * DF.spd; e.r = 15; e.score = 20; break;
case 'boss': e.v = q.v; e.hp = hs((60 + n * 12) * (q.v === 1 ? 1.1 : 1)); e.vy = 70; e.r = 58; e.x = W / 2; e.bx = W / 2; e.y = -120; e.score = 1000; e.pat = 0; e.cd = 2; e.sched = []; e.spiral = 0; e.sa = 0; e.sc = 0; e.entered = false; bossMusic(true); break;
}
e.max = e.hp; E.push(e);
}
function bossMusic(on) { M.boss = on; }
/* ================= effects ================= */
const CAP = 420;
function explode(x, y, n, big) {
const cols = ['#ffc83d', '#ff7a3d', '#ff3d81', '#ffffff'];
FX.push({ k: 'g', x, y, tex: GL.wh, r: big ? 90 : 38, life: .18, max: .18 });
FX.push({ k: 'g', x, y, tex: GL.or, r: big ? 150 : 64, life: .45, max: .45 });
FX.push({ k: 'r', x, y, r: 4, mr: big ? 130 : 56, life: .45, max: .45, lw: 3, c: '#ffb347' });
if (FX.length > CAP) return;
for (let i = 0; i < n; i++) {
const a = R(0, TAU), sp = R(40, big ? 360 : 240);
FX.push({ k: 'p', x, y, vx: Math.cos(a) * sp, vy: Math.sin(a) * sp, life: R(.35, big ? 1.1 : .75), max: 1, sz: R(1.5, big ? 5 : 3.5), c: pick(cols) });
}
for (let i = 0; i < (big ? 8 : 3); i++) FX.push({ k: 's', x: x + R(-10, 10), y: y + R(-10, 10), vx: R(-30, 30), vy: R(-30, 30), r: R(8, 16), life: R(.6, 1.1), max: 1.1 });
for (let i = 0; i < (big ? 12 : 5); i++) { const a = R(0, TAU), sp = R(60, 200); FX.push({ k: 'd', x, y, vx: Math.cos(a) * sp, vy: Math.sin(a) * sp, rot: R(0, TAU), vr: R(-9, 9), sz: R(2, 5), life: R(.5, 1), max: 1, c: pick(['#6d4aff', '#ff3d81', '#3a1a94', '#ffc83d']) }); }
}
function sparks(x, y, c) { if (FX.length > CAP) return; for (let i = 0; i < 4; i++) FX.push({ k: 'p', x, y, vx: R(-100, 100), vy: R(-150, -20), life: R(.15, .3), max: 1, sz: 2, c }); }
function popup(x, y, txt, c) { FX.push({ k: 't', x, y, txt, c, vy: -55, life: .9, max: .9 }); }
function trail(x, y, c) { if (FX.length > CAP) return; FX.push({ k: 'p', x: x + R(-2, 2), y, vx: R(-14, 14), vy: R(120, 220), life: R(.14, .28), max: 1, sz: R(2, 4), c }); }
/* ================= gameplay ================= */
function fireAt(x, y, ang, sp, r) { if (EB.length < 320) EB.push({ x, y, vx: Math.cos(ang) * sp, vy: Math.sin(ang) * sp, r: r || 5 }); }
const aimAng = e => Math.atan2(P.y - e.y, P.x - e.x);
const aimSpread = (e, n, arc, sp, r) => { const a = aimAng(e); for (let i = 0; i < n; i++) fireAt(e.x, e.y + 20, a + (i - (n - 1) / 2) * arc, sp, r); };
const ring = (e, n, off, sp, r) => { for (let i = 0; i < n; i++) fireAt(e.x, e.y, off + i / n * TAU, sp, r); };
function drop(x, y, force) {
if (!force && Math.random() > .12) return;
let type = force;
if (!type) { const pool = ['S', 'R', 'T', 'T', 'H', 'D', 'B']; if (P.lvl >= 3) pool.splice(pool.indexOf('T'), 2); type = pick(pool); }
PU.push({ x, y, type, vy: 80, ph: R(0, TAU) });
}
function addCombo() {
combo++; comboT = 3.4; mult = Math.min(8, 1 + Math.floor(combo / 6)); if (mult > maxMult) maxMult = mult;
}
function kill(e) {
const big = e.type === 'boss' || e.type === 'tank';
explode(e.x, e.y, big ? 50 : 20, big); big ? sfx.big() : sfx.boom();
kills++; addCombo();
const pts = Math.round(e.score * mult * DF.sc); score += pts;
popup(e.x, e.y - 14, '+' + pts, '#ffc83d');
if (e.type === 'boss') {
bossKills++; bossMusic(false); shake = 24; flash = .9; timeScale = .25; vibe(200);
drop(e.x - 34, e.y, 'H'); drop(e.x + 34, e.y, 'T'); drop(e.x, e.y + 30, 'B');
EB = []; E.forEach(x => { if (x !== e && x.hp > 0) { x.hp = 0; explode(x.x, x.y, 12, false); } });
for (let i = 0; i < 4; i++) FX.push({ k: 'r', x: e.x, y: e.y, r: 4, mr: 160 + i * 90, life: .6 + i * .12, max: .6 + i * .12, lw: 3, c: i % 2 ? '#ff3d81' : '#ffc83d' });
} else drop(e.x, e.y);
}
function hurt() {
if (P.inv > 0 || state !== 'play') return;
if (P.shield > 0) { P.shield = 0; P.inv = .9; shake = 8; sfx.hit(); FX.push({ k: 'r', x: P.x, y: P.y, r: 10, mr: 70, life: .35, max: .35, lw: 4, c: '#2de2c5' }); return; }
P.lives--; P.lvl = Math.max(1, P.lvl - 1); P.rapid = 0; P.drone = 0;
combo = 0; mult = 1; comboT = 0; P.inv = 2.2; shake = 16; redF = 1; flash = .25; dmgTaken++; waveDmg = true;
explode(P.x, P.y, 26, true); sfx.hurt(); vibe(140);
if (P.lives <= 0) gameOver();
}
function bomb() {
if (state !== 'play' || P.bombs <= 0 || bombCd > 0) return;
P.bombs--; bombCd = .6; P.inv = Math.max(P.inv, 1.3); shake = 16; flash = .55; sfx.bomb(); vibe(80);
FX.push({ k: 'r', x: P.x, y: P.y, r: 10, mr: 760, life: .7, max: .7, lw: 10, c: '#2de2c5' });
FX.push({ k: 'r', x: P.x, y: P.y, r: 10, mr: 560, life: .55, max: .55, lw: 5, c: '#ffffff' });
FX.push({ k: 'g', x: P.x, y: P.y, tex: GL.cy, r: 320, life: .5, max: .5 });
EB.forEach(b => { if (FX.length < CAP) FX.push({ k: 'p', x: b.x, y: b.y, vx: R(-40, 40), vy: R(-40, 40), life: .4, max: 1, sz: 3, c: '#ffffff' }); });
EB = [];
E.forEach(e => { e.hp -= e.type === 'boss' ? 22 : 999; e.hit = .15; if (e.hp <= 0) kill(e); });
E = E.filter(e => e.hp > 0);
}
function collect(p) {
sfx.pick();
if (p.type === 'S') P.shield = 10;
else if (p.type === 'R') P.rapid = 10;
else if (p.type === 'T') { if (P.lvl < 3) P.lvl++; else score += 150; }
else if (p.type === 'H') { if (P.lives < 5) P.lives++; else score += 150; }
else if (p.type === 'B') { if (P.bombs < 5) P.bombs++; else score += 150; }
else if (p.type === 'D') P.drone = 15;
popup(p.x, p.y - 14, t('pu')[p.type], PUC[p.type]);
FX.push({ k: 'r', x: p.x, y: p.y, r: 6, mr: 44, life: .3, max: .3, lw: 3, c: PUC[p.type] });
}
function bossAI(e, dt) {
const enr = e.hp < e.max * .5, v = e.v, f = DF.fire;
if (!e.entered) { e.y += 80 * dt; if (e.y >= 225) e.entered = true; return; }
if (v === 0) e.x = W / 2 + Math.sin(e.t * (enr ? 1.1 : .75)) * (W / 2 - 118);
else if (v === 1) e.x = W / 2 + Math.sin(e.t * (enr ? 1.45 : 1.05)) * (W / 2 - 122);
else { e.x = W / 2 + Math.sin(e.t * .7) * (W / 2 - 122); e.y = 225 + Math.sin(e.t * 1.4) * 26; }
for (const s of e.sched) { s.t -= dt; if (s.t <= 0) { s.fn(); s.done = true; } }
e.sched = e.sched.filter(s => !s.done);
if (v === 1 && e.spiral > 0) {
e.spiral -= dt; e.sc -= dt;
if (e.sc <= 0) { e.sc = enr ? .07 : .1; e.sa += .38; fireAt(e.x, e.y, e.sa, 190, 5); fireAt(e.x, e.y, e.sa + Math.PI, 190, 5); }
}
if (e.cd > 0) return;
const p = e.pat++ % 3;
if (v === 0) {
e.cd = (enr ? .95 : 1.4) * f;
if (p === 0) aimSpread(e, enr ? 7 : 5, .2, 240, 6);
else if (p === 1) ring(e, enr ? 20 : 14, R(0, TAU), 170, 5);
else for (let i = 0; i < 3; i++) e.sched.push({ t: i * .15, fn: () => fireAt(e.x, e.y + 20, aimAng(e), 310, 6) });
} else if (v === 1) {
if (p === 0) { e.spiral = enr ? 3 : 2.4; e.cd = (enr ? 3.2 : 2.8) * f; }
else if (p === 1) { aimSpread(e, enr ? 9 : 7, .13, 215, 5); e.cd = 1.4 * f; }
else { spawn({ type: 'scout', x: clamp(e.x - 110, 30, W - 30), amp: 0 }); spawn({ type: 'scout', x: clamp(e.x + 110, 30, W - 30), amp: 0 }); e.cd = 1.2 * f; }
} else {
e.cd = (enr ? 1.05 : 1.5) * f;
if (p === 0) { const n = 12, gap = P.x; for (let i = 0; i <= n; i++) { const x = i * W / n; if (Math.abs(x - gap) > 52) fireAt(x, e.y + 30, Math.PI / 2, 165, 5); } }
else if (p === 1) ring(e, enr ? 22 : 16, R(0, TAU), 160, 5);
else for (let i = 0; i < 4; i++) e.sched.push({ t: i * .12, fn: () => aimSpread(e, enr ? 3 : 1, .18, 330, 6) });
}
}
function updateEnemies(dt) {
for (const e of E) {
e.t += dt; e.hit = Math.max(0, e.hit - dt); e.cd -= dt;
switch (e.type) {
case 'scout': e.y += e.vy * dt; e.x = clamp(e.bx + Math.sin(e.t * 2 + e.ph) * e.amp, 20, W - 20); break;
case 'zig':
e.y += e.vy * dt; e.x = clamp(e.x + Math.cos(e.t * 3 + e.ph) * 120 * dt, 20, W - 20);
if (e.cd <= 0 && e.y > 0 && e.y < H - 240) { e.cd = R(1.5, 2.4) * DF.fire; fireAt(e.x, e.y + 14, Math.PI / 2, 230, 5); }
break;
case 'tank':
if (e.y < 150) e.y += e.vy * dt; else e.x = clamp(e.x + Math.sin(e.t * .9 + e.ph) * 50 * dt, 36, W - 36);
if (e.cd <= 0 && e.y > 40) { e.cd = 1.8 * DF.fire; aimSpread(e, 3, .22, 215, 5); }
break;
case 'kami':
e.vx = clamp(e.vx + clamp(P.x - e.x, -1, 1) * (e.y > 60 ? 520 : 0) * dt, -230, 230);
e.vy = Math.min(340, e.vy + 45 * dt); e.x += e.vx * dt; e.y += e.vy * dt; e.rot += dt * 6;
if (Math.random() < .5) trail(e.x, e.y - 10, '#ff7a3d');
break;
case 'boss': bossAI(e, dt); break;
}
}
E = E.filter(e => e.y < H + 70 && e.x > -90 && e.x < W + 90);
}
function update(dt) {
time += dt; waveT += dt; bombCd -= dt;
if (banner.t > 0) banner.t -= dt;
shake = Math.max(0, shake - dt * 42); flash = Math.max(0, flash - dt * 1.6); redF = Math.max(0, redF - dt * 1.4);
const kx = (keys.ArrowRight || keys.d || keys.D ? 1 : 0) - (keys.ArrowLeft || keys.a || keys.A ? 1 : 0);
const ky = (keys.ArrowDown || keys.s || keys.S ? 1 : 0) - (keys.ArrowUp || keys.w || keys.W ? 1 : 0);
if (kx || ky) { const m = Math.hypot(kx, ky); P.tx = clamp(P.x + kx / m * 440 * dt, 24, W - 24); P.ty = clamp(P.y + ky / m * 440 * dt, 110, H - 50); }
const px = P.x; P.x += (P.tx - P.x) * Math.min(1, dt * 16); P.y += (P.ty - P.y) * Math.min(1, dt * 16);
P.vx += ((P.x - px) / Math.max(dt, .001) - P.vx) * Math.min(1, dt * 14);
P.inv = Math.max(0, P.inv - dt); P.shield = Math.max(0, P.shield - dt); P.rapid = Math.max(0, P.rapid - dt); P.drone = Math.max(0, P.drone - dt);
trail(P.x - 5, P.y + 20, '#ff9a3d'); trail(P.x + 5, P.y + 20, '#2de2c5');
P.cd -= dt;
if (P.cd <= 0) {
P.cd = P.rapid > 0 ? .1 : .19;
const b = (dx, vx, dy) => { PB.push({ x: P.x + dx, y: P.y - (dy || 24), vx, vy: -760, r: 4 }); shots++; };
if (P.lvl === 1) b(0, 0); else if (P.lvl === 2) { b(-9, 0); b(9, 0); } else { b(0, 0); b(-14, -95, 18); b(14, 95, 18); }
if (P.drone > 0) { b(-40, 0, -4); b(40, 0, -4); }
sfx.shoot();
}
while (queue.length && waveT >= queue[0].t) spawn(queue.shift());
updateEnemies(dt);
for (const b of PB) { b.x += b.vx * dt; b.y += b.vy * dt; }
PB = PB.filter(b => b.y > -20 && b.x > -20 && b.x < W + 20);
for (const b of EB) { b.x += b.vx * dt; b.y += b.vy * dt; }
EB = EB.filter(b => b.y > -20 && b.y < H + 20 && b.x > -20 && b.x < W + 20);
for (const p of PU) {
p.ph += dt * 4;
const dx = P.x - p.x, dy = P.y - p.y, d = Math.hypot(dx, dy);
if (d < 130) { p.x += dx / d * 380 * dt; p.y += dy / d * 380 * dt; } else p.y += p.vy * dt;
}
PU = PU.filter(p => { if (Math.hypot(p.x - P.x, p.y - P.y) < 28) { collect(p); return false; } return p.y < H + 30; });
for (const b of PB) for (const e of E) {
if (e.hp <= 0 || b.dead) continue;
if (Math.hypot(b.x - e.x, b.y - e.y) < e.r + b.r) {
b.dead = true; e.hp--; e.hit = .07; hits++; sparks(b.x, b.y, '#2de2c5');
if (e.hp <= 0) kill(e); else if (e.type !== 'boss') sfx.hit();
}
}
PB = PB.filter(b => !b.dead); E = E.filter(e => e.hp > 0);
for (const b of EB) if (!b.dead && Math.hypot(b.x - P.x, b.y - P.y) < P.r * .65 + b.r) { b.dead = true; hurt(); }
EB = EB.filter(b => !b.dead);
for (const e of E) if (Math.hypot(e.x - P.x, e.y - P.y) < e.r * .85 + P.r * .6) { hurt(); if (e.type !== 'boss') { e.hp = 0; kill(e); } }
E = E.filter(e => e.hp > 0);
if (comboT > 0) { comboT -= dt; if (comboT <= 0) { combo = 0; mult = 1; } }
if (!queue.length && !E.length) {
clearT += dt;
if (clearT > 1.5) {
const bonus = wave * (waveDmg ? 40 : 100); score += bonus;
popup(W / 2, 300, '+' + bonus, waveDmg ? '#ffc83d' : '#4ade80');
if (!waveDmg) banner = { txt: t('bperfect', { n: bonus }), t: 1.4, c: '#4ade80' };
startWave();
}
}
if (!E.some(e => e.type === 'boss') && M.boss) bossMusic(false);
}
function updateFX(dt) {
for (const f of FX) {
f.life -= dt;
if (f.k === 'p') { f.x += f.vx * dt; f.y += f.vy * dt; f.vx *= .98; f.vy *= .98; }
else if (f.k === 'r') f.r += (f.mr - f.r) * Math.min(1, dt * 7);
else if (f.k === 't') f.y += f.vy * dt;
else if (f.k === 's') { f.x += f.vx * dt; f.y += f.vy * dt; f.r += dt * 26; }
else if (f.k === 'd') { f.x += f.vx * dt; f.y += f.vy * dt; f.vx *= .985; f.vy *= .985; f.rot += f.vr * dt; }
}
FX = FX.filter(f => f.life > 0);
}
/* ================= drawing ================= */
function spr(s, x, y, rot, sc, white) {
const c = white ? s.wh : s.c, w = s.w * (sc || 1), h = s.h * (sc || 1);
if (rot) { ctx.save(); ctx.translate(x, y); ctx.rotate(rot); ctx.drawImage(c, -w / 2, -h / 2, w, h); ctx.restore(); }
else ctx.drawImage(c, x - w / 2, y - h / 2, w, h);
}
function glow(tex, x, y, r, a) { ctx.globalAlpha = a; ctx.drawImage(tex, x - r, y - r, r * 2, r * 2); }
function scrollBG(dt) {
for (const s of stars) {
s.y += SPEED[s.l] * dt * (state === 'menu' ? .6 : 1) * (state === 'pause' ? 0 : 1);
if (s.y > H) { s.y = -4; s.x = R(0, W); }
}
if (state === 'pause') return;
for (const b of BLOBS) { b.y += b.sp * dt; if (b.y - b.s / 2 > H) { b.y = -b.s / 2; b.x = R(40, W - 40); } }
planet.y += planet.sp * dt;
if (planet.y - 150 > H) planet = { img: pick(PLANETS), x: R(60, W - 60), y: -220, s: R(.55, 1), sp: R(7, 12) };
}
function drawBG() {
ctx.drawImage(baseBG, 0, 0);
ctx.globalCompositeOperation = 'lighter';
for (const b of BLOBS) { ctx.globalAlpha = b.a; ctx.drawImage(b.tex, b.x - b.s / 2, b.y - b.s / 2, b.s, b.s); }
ctx.globalCompositeOperation = 'source-over'; ctx.globalAlpha = 1;
const ps = 260 * planet.s; ctx.globalAlpha = .9; ctx.drawImage(planet.img, planet.x - ps / 2, planet.y - ps / 2, ps, ps); ctx.globalAlpha = 1;
ctx.fillStyle = '#fff';
const now = performance.now() / 1000;
for (const s of stars) { ctx.globalAlpha = (.22 + s.l * .28) * (.75 + .25 * Math.sin(now * 2 + s.tw)); ctx.fillRect(s.x, s.y, s.s, s.s * (1 + s.l * .9)); }
ctx.globalAlpha = 1;
}
function drawShip(x, y, sc, bank, inv, shield) {
ctx.save();
if (inv > 0 && Math.floor(inv * 14) % 2) ctx.globalAlpha = .35;
ctx.globalCompositeOperation = 'lighter';
const fl = 1 + Math.sin(performance.now() / 40) * .2;
glow(GL.or, x - 6 * sc, y + 22 * sc, 15 * sc * fl, .9); glow(GL.cy, x + 6 * sc, y + 22 * sc, 15 * sc * fl, .9);
ctx.globalCompositeOperation = 'source-over'; ctx.globalAlpha = (inv > 0 && Math.floor(inv * 14) % 2) ? .35 : 1;
spr(SP.ship, x, y, bank, sc);
if (shield > 0) {
const a = (shield < 2 && Math.floor(shield * 8) % 2) ? .15 : .5 + Math.sin(performance.now() / 120) * .12;
ctx.globalAlpha = a; ctx.fillStyle = '#2de2c522'; ctx.strokeStyle = '#2de2c5'; ctx.lineWidth = 2.5;
ctx.beginPath(); ctx.arc(x, y, 36 * sc, 0, TAU); ctx.fill(); ctx.stroke();
ctx.globalCompositeOperation = 'lighter'; glow(GL.cy, x, y, 44 * sc, .25);
}
ctx.restore();
}
function drawEnemy(e) {
const f = e.hit > 0;
if (e.type === 'boss') {
const s = SP['boss' + e.v];
spr(s, e.x, e.y, 0, 1, f);
ctx.globalCompositeOperation = 'lighter'; glow(GL.pk, e.x, e.y - 4, 70 + Math.sin(time * 5) * 8, .22); ctx.globalCompositeOperation = 'source-over'; ctx.globalAlpha = 1;
return;
}
const sc = e.type === 'tank' ? 1 : 1;
spr(SP[e.type], e.x, e.y, e.type === 'kami' ? e.rot : 0, sc, f);
if (e.type === 'tank') { ctx.globalCompositeOperation = 'lighter'; glow(GL.pk, e.x, e.y, 22 + Math.sin(time * 6) * 3, .5); ctx.globalCompositeOperation = 'source-over'; ctx.globalAlpha = 1; }
if (e.max > 1) {
const w = e.type === 'tank' ? 44 : 32, y = e.y - e.r - 10;
ctx.fillStyle = '#000000aa'; ctx.fillRect(e.x - w / 2, y, w, 4);
ctx.fillStyle = e.hp / e.max > .4 ? '#4ade80' : '#ff5a5a'; ctx.fillRect(e.x - w / 2, y, w * e.hp / e.max, 4);
}
}
function drawWorld() {
for (const p of PU) { const y = p.y + Math.sin(p.ph) * 3; spr(PUS[p.type], p.x, y, 0, 1); }
E.forEach(drawEnemy);
if (P.drone > 0) for (const s of [-1, 1]) {
const x = P.x + s * 40, y = P.y + 6 + Math.sin(time * 4 + s) * 3;
ctx.globalAlpha = P.drone < 2 && Math.floor(P.drone * 8) % 2 ? .3 : 1; spr(SP.drone, x, y, 0, 1); ctx.globalAlpha = 1;
}
// smoke & debris under glow
for (const f of FX) {
if (f.k === 's') { ctx.globalAlpha = clamp(f.life / f.max, 0, 1) * .5; ctx.drawImage(SMOKE, f.x - f.r, f.y - f.r, f.r * 2, f.r * 2); }
else if (f.k === 'd') { ctx.globalAlpha = clamp(f.life * 2, 0, 1); ctx.save(); ctx.translate(f.x, f.y); ctx.rotate(f.rot); ctx.fillStyle = f.c; ctx.fillRect(-f.sz, -f.sz / 2, f.sz * 2, f.sz); ctx.restore(); }
}
ctx.globalAlpha = 1;
// bullets (additive)
ctx.globalCompositeOperation = 'lighter';
for (const b of EB) { ctx.globalAlpha = .9; ctx.drawImage(GL.pk, b.x - b.r * 3, b.y - b.r * 3, b.r * 6, b.r * 6); ctx.globalAlpha = 1; ctx.drawImage(GL.wh, b.x - b.r * 1.2, b.y - b.r * 1.2, b.r * 2.4, b.r * 2.4); }
for (const b of PB) { ctx.globalAlpha = .8; ctx.drawImage(GL.cy, b.x - 12, b.y - 22, 24, 44); ctx.globalAlpha = 1; ctx.fillStyle = '#fff'; ctx.fillRect(b.x - 1.2, b.y - 9, 2.4, 16); }
ctx.globalCompositeOperation = 'source-over'; ctx.globalAlpha = 1;
if (state !== 'over') drawShip(P.x, P.y, 1, clamp(P.vx / 1400, -.3, .3), P.inv, P.shield);
// additive particles
ctx.globalCompositeOperation = 'lighter';
for (const f of FX) {
if (f.k === 'p') { ctx.globalAlpha = clamp(f.life * 2.4, 0, 1); ctx.fillStyle = f.c; ctx.fillRect(f.x - f.sz / 2, f.y - f.sz / 2, f.sz, f.sz); }
else if (f.k === 'r') { ctx.globalAlpha = clamp(f.life / f.max, 0, 1) * .75; ctx.strokeStyle = f.c; ctx.lineWidth = f.lw * clamp(f.life / f.max, .2, 1); ctx.beginPath(); ctx.arc(f.x, f.y, f.r, 0, TAU); ctx.stroke(); }
else if (f.k === 'g') glow(f.tex, f.x, f.y, f.r, clamp(f.life / f.max, 0, 1));
}
ctx.globalCompositeOperation = 'source-over'; ctx.globalAlpha = 1;
ctx.font = '700 15px "IBM Plex Sans Arabic",Arial'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.direction = 'ltr';
for (const f of FX) if (f.k === 't') { ctx.globalAlpha = clamp(f.life / f.max * 1.6, 0, 1); ctx.fillStyle = '#000000aa'; ctx.fillText(f.txt, f.x + 1, f.y + 1); ctx.fillStyle = f.c; ctx.fillText(f.txt, f.x, f.y); }
ctx.globalAlpha = 1;
if (banner.t > 0 && (state === 'play' || state === 'count')) {
const a = clamp(Math.min(banner.t, 2.2 - banner.t + .5) * 2, 0, 1), sc = 1 + (1 - a) * .15;
ctx.save(); ctx.translate(W / 2, H * .34); ctx.scale(sc, sc); ctx.globalAlpha = a;
ctx.direction = LANG === 'ar' ? 'rtl' : 'ltr'; ctx.font = '400 56px Lalezar,Arial'; ctx.fillStyle = banner.c; ctx.shadowColor = banner.c; ctx.shadowBlur = 24; ctx.fillText(banner.txt, 0, 0); ctx.restore();
}
}
function draw() {
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.save();
if (shake > 0 && S.shake) ctx.translate(R(-shake, shake) * .5, R(-shake, shake) * .5);
drawBG();
if (state === 'menu') {
const y = 400 + Math.sin(performance.now() / 700) * 10;
drawShip(W / 2, y, 2.4, Math.sin(performance.now() / 1100) * .06, 0, 0);
ctx.globalCompositeOperation = 'lighter';
for (const f of FX) if (f.k === 'p') { ctx.globalAlpha = clamp(f.life * 2.4, 0, 1); ctx.fillStyle = f.c; ctx.fillRect(f.x - f.sz / 2, f.y - f.sz / 2, f.sz * 1.6, f.sz * 1.6); }
ctx.globalCompositeOperation = 'source-over'; ctx.globalAlpha = 1;
} else if (P) drawWorld();
ctx.restore();
ctx.globalAlpha = .9; ctx.drawImage(vignette, 0, 0, W, H); ctx.globalAlpha = 1;
if (redF > 0) { const g = ctx.createRadialGradient(W / 2, H / 2, H * .25, W / 2, H / 2, H * .7); g.addColorStop(0, '#ff000000'); g.addColorStop(1, `rgba(255,30,60,${redF * .55})`); ctx.fillStyle = g; ctx.fillRect(0, 0, W, H); }
if (flash > 0) { ctx.fillStyle = `rgba(255,255,255,${flash * .55})`; ctx.fillRect(0, 0, W, H); }
}
/* ================= HUD / UI ================= */
const el = { hud: $('#hud'), sc: $('#sc'), wv: $('#wv'), lv: $('#lv'), bm2: $('#bm2'), combo: $('#combo'), cx: $('#cx'), cb: $('#cb'), boss: $('#boss'), bn: $('#bn'), bb: $('#bb'), fx: $('#fx'), bomb: $('#bomb'), bombN: $('#bombN'), cd: $('#cd'), hint: $('#hint') };
let lastHud = '', lastBar = -1, lastBoss = -1;
const dir = LANG === 'ar' ? 'right' : 'left';
el.cb.style.setProperty('--o', dir); el.bb.style.setProperty('--o', dir);
function hud() {
const key = [score, wave, P.lives, P.bombs, mult, Math.ceil(P.shield), Math.ceil(P.rapid), Math.ceil(P.drone), P.lvl].join('|');
if (key !== lastHud) {
lastHud = key;
el.sc.textContent = score.toLocaleString('en-US'); el.wv.textContent = wave;
el.lv.textContent = '♥'.repeat(P.lives) || '—'; el.bm2.textContent = '●'.repeat(P.bombs) || '—';
el.bombN.textContent = P.bombs; el.bomb.disabled = P.bombs <= 0;
el.cx.textContent = '×' + mult;
const chips = [];
if (P.shield > 0) chips.push(`<span style="color:${PUC.S}">◈ ${Math.ceil(P.shield)}</span>`);
if (P.rapid > 0) chips.push(`<span style="color:${PUC.R}">» ${Math.ceil(P.rapid)}</span>`);
if (P.drone > 0) chips.push(`<span style="color:${PUC.D}">◇ ${Math.ceil(P.drone)}</span>`);
if (P.lvl > 1) chips.push(`<span style="color:${PUC.T}">✦ ${P.lvl}</span>`);
el.fx.innerHTML = chips.join('');
}
el.combo.classList.toggle('on', combo > 1);
const bar = comboT > 0 ? Math.round(comboT / 3.4 * 50) : 0;
if (bar !== lastBar) { lastBar = bar; el.cb.style.transform = `scaleX(${bar / 50})`; }
const b = E.find(e => e.type === 'boss');
if (b) {
if (lastBoss === -1) { el.bn.textContent = t('bosses')[b.v]; el.boss.classList.add('on'); }
const v = Math.round(b.hp / b.max * 100); if (v !== lastBoss) { lastBoss = v; el.bb.style.transform = `scaleX(${v / 100})`; }
} else if (lastBoss !== -1) { lastBoss = -1; el.boss.classList.remove('on'); }
}
const screens = { menu: $('#menu'), pause: $('#pause'), settings: $('#settings'), over: $('#over') };
function show(name) { for (const k in screens) screens[k].classList.toggle('on', k === name); }
function inGameUI(on) { el.hud.classList.toggle('on', on); el.bomb.classList.toggle('on', on); if (!on) { el.combo.classList.remove('on'); el.boss.classList.remove('on'); el.fx.innerHTML = ''; lastBoss = -1; lastHud = ''; } }
function renderMenu() {
$$('#diff button').forEach(b => b.classList.toggle('on', b.dataset.d === S.diff));
$('#best1').textContent = (S.best[S.diff] || 0).toLocaleString('en-US');
const list = S.top.filter(x => x.d === S.diff).slice(0, 3);
$('#tops').innerHTML = list.map((x, i) => `<li><span>${i + 1}. ${t('wave')} ${x.w}</span><b>${x.s.toLocaleString('en-US')}</b></li>`).join('');
}
function renderSettings() {
$$('#settings .tg button').forEach(b => { const on = S[b.dataset.k]; b.classList.toggle('on', on); b.textContent = t(on ? 'on' : 'off'); });
}
function startGame() {
audio(); reset(); inGameUI(true); show(null); lastHud = ''; hud();
beginCount();
if (!S.tut) { S.tut = true; save(); $('#hintT').textContent = t('hint'); el.hint.classList.add('on'); setTimeout(() => el.hint.classList.remove('on'), 5200); }
}
function beginCount() { state = 'count'; cdT = 1.8; cdN = 0; el.cd.classList.add('on'); musicStart(); }
function pause() { if (state !== 'play' && state !== 'count') return; el.cd.classList.remove('on'); state = 'pause'; musicStop(); show('pause'); }
function resume() { if (state !== 'pause') return; show(null); beginCount(); }
function toMenu() { musicStop(); bossMusic(false); state = 'menu'; P = null; E = []; EB = []; PB = []; PU = []; FX = []; inGameUI(false); el.cd.classList.remove('on'); renderMenu(); show('menu'); }
function gameOver() {
state = 'over'; musicStop(); bossMusic(false); inGameUI(false);
const prev = S.best[S.diff] || 0, isNew = score > prev;
if (isNew) S.best[S.diff] = score;
S.top.push({ s: score, w: wave, d: S.diff }); S.top.sort((a, b) => b.s - a.s); S.top = S.top.slice(0, 15); save();
const acc = shots ? Math.round(hits / shots * 100) : 0;
$('#fs').textContent = score.toLocaleString('en-US'); $('#s1').textContent = wave; $('#s2').textContent = kills; $('#s3').textContent = acc + '%'; $('#s4').textContent = '×' + maxMult;
$('#best2').textContent = S.best[S.diff].toLocaleString('en-US'); $('#rec').hidden = !isNew;
const med = [];
if (bossKills > 0) med.push(t('m_boss')); if (dmgTaken === 0 && wave >= 5) med.push(t('m_clean'));
if (maxMult >= 8) med.push(t('m_combo')); if (acc >= 60 && shots > 80) med.push(t('m_sniper')); if (wave >= 10) med.push(t('m_surv'));
$('#medals').innerHTML = med.map(m => `<span>★ ${m}</span>`).join('');
setTimeout(() => { if (state === 'over') show('over'); }, 1100);
}
/* ================= input ================= */
function setTarget(e) {
if (state !== 'play' && state !== 'count') return;
if (e.target.closest && e.target.closest('button')) return;
const r = cv.getBoundingClientRect();
const x = (e.clientX - r.left) * W / r.width, y = (e.clientY - r.top) * H / r.height;
const off = e.pointerType === 'mouse' ? 0 : 72;
P.tx = clamp(x, 24, W - 24); P.ty = clamp(y - off, 110, H - 50);
}
const stage = $('#stage');
stage.addEventListener('pointermove', setTarget);
stage.addEventListener('pointerdown', e => { audio(); setTarget(e); });
stage.addEventListener('contextmenu', e => e.preventDefault());
addEventListener('keydown', e => {
keys[e.key] = true;
if (['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', ' '].includes(e.key)) e.preventDefault();
if (e.key === 'p' || e.key === 'P') { state === 'pause' ? resume() : pause(); }
if (e.key === 'b' || e.key === 'B' || e.key === ' ') { if (state === 'play') bomb(); else if (e.key === ' ' && (state === 'menu' && screens.menu.classList.contains('on'))) startGame(); }
if (e.key === 'Enter' && state === 'menu' && screens.menu.classList.contains('on')) startGame();
if (e.key === 'Enter' && state === 'over') startGame();
});
addEventListener('keyup', e => { keys[e.key] = false; });
addEventListener('blur', () => { for (const k in keys) keys[k] = false; pause(); });
document.addEventListener('visibilitychange', () => { if (document.hidden) pause(); });
$('#go').onclick = startGame; $('#retry').onclick = startGame; $('#restart1').onclick = startGame;
$('#resume').onclick = resume; $('#bp').onclick = () => state === 'pause' ? resume() : pause();
$('#tomenu1').onclick = toMenu; $('#tomenu2').onclick = toMenu;
$('#bomb').addEventListener('pointerdown', e => { e.stopPropagation(); e.preventDefault(); bomb(); });
$$('#diff button').forEach(b => b.onclick = () => { S.diff = b.dataset.d; save(); renderMenu(); });
$('#gear1').onclick = () => { settingsFrom = 'menu'; renderSettings(); show('settings'); };
$('#gear2').onclick = () => { settingsFrom = 'pause'; renderSettings(); show('settings'); };
$('#back').onclick = () => show(settingsFrom);
$$('#settings .tg button').forEach(b => b.onclick = () => {
const k = b.dataset.k; S[k] = !S[k]; save(); renderSettings(); audio();
if (k === 'music' && !S.music) musicStop();
});
/* ================= loop ================= */
let last = 0, lastN = 0;
function frame(ts) {
requestAnimationFrame(frame);
const dt = Math.min(.033, (ts - last) / 1000 || 0); last = ts;
scrollBG(dt);
if (state === 'count') {
cdT -= dt; const n = Math.ceil(cdT / .6);
if (n !== lastN) { lastN = n; el.cd.textContent = n > 0 ? n : ''; el.cd.classList.remove('pop'); void el.cd.offsetWidth; el.cd.classList.add('pop'); n > 0 ? sfx.tick() : sfx.go(); }
if (cdT <= 0) { state = 'play'; el.cd.classList.remove('on'); lastN = 0; }
updateFX(dt);
} else if (state === 'play') {
timeScale = Math.min(1, timeScale + dt * .7); update(dt * timeScale); updateFX(dt * timeScale); hud();
} else if (state === 'menu') {
if (Math.random() < .8) { const y = 400 + 62 + Math.sin(performance.now() / 700) * 10; FX.push({ k: 'p', x: W / 2 + (Math.random() < .5 ? -13 : 13) + R(-3, 3), y, vx: R(-15, 15), vy: R(150, 260), life: R(.2, .45), max: 1, sz: R(2, 5), c: Math.random() < .5 ? '#ff9a3d' : '#2de2c5' }); }
updateFX(dt);
} else if (state === 'over') { shake = Math.max(0, shake - dt * 42); flash = Math.max(0, flash - dt * 1.6); redF = Math.max(0, redF - dt * 1.4); updateFX(dt); }
draw();
}
renderMenu(); renderSettings();
requestAnimationFrame(frame);
})();
</script>
</body>
</html>
1280×720 · 58%
محاكاة عرض المتصفح: يتغير عرض الصفحة الفعلي فتُطبَّق استعلامات CSS، لكنها ليست جهازاً حقيقياً (لا لمس ولا نظام تشغيل مختلف). المعاينة معزولة بلا ملفات تعريف ارتباط ولا اتصال بالشبكة.
ملفات الويب تُعرض في تبويب «المعاينة».
التعليقات (0)
سجّل الدخول للتعليق.