-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefender.html
More file actions
1142 lines (1128 loc) · 41.4 KB
/
Copy pathDefender.html
File metadata and controls
1142 lines (1128 loc) · 41.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" href="data:,">
<title>DEFENDER — HTML5 Arcade Clone</title>
<style>
html,body{margin:0;padding:0;height:100%;background:#000;overflow:hidden;}
#game{position:absolute;inset:0;display:block;}
#scanlines{position:fixed;inset:0;pointer-events:none;z-index:2;
background:repeating-linear-gradient(0deg, rgba(0,0,0,0.22) 0px, rgba(0,0,0,0.22) 1px, transparent 1px, transparent 3px);}
#vignette{position:fixed;inset:0;pointer-events:none;z-index:3;
background:radial-gradient(ellipse at center, transparent 55%, rgba(0,0,0,0.55) 100%);}
</style>
</head>
<body>
<style>
#arcade-menu-button{all:unset;box-sizing:border-box;position:fixed;top:max(12px,env(safe-area-inset-top));left:max(12px,env(safe-area-inset-left));z-index:9999;display:flex;align-items:center;gap:9px;padding:9px 11px;border:2px solid #ff3cac;border-radius:4px;background:rgba(3,3,12,.88);box-shadow:0 0 0 2px #050505,0 0 16px rgba(255,60,172,.72),inset 0 0 12px rgba(0,229,255,.12);color:#fff36a;font:700 11px/1 "Courier New",monospace;letter-spacing:.08em;text-shadow:0 0 7px #fff36a;cursor:pointer;opacity:.78;transition:opacity .15s,transform .15s,box-shadow .15s}
#arcade-menu-button kbd{padding:3px 5px;border:1px solid #00e5ff;border-radius:3px;color:#00e5ff;font:inherit;text-shadow:0 0 6px #00e5ff}
#arcade-menu-button:hover,#arcade-menu-button:focus-visible{opacity:1;transform:translateY(-1px);box-shadow:0 0 0 2px #050505,0 0 24px rgba(255,60,172,.95),inset 0 0 12px rgba(0,229,255,.2);outline:2px solid #fff;outline-offset:3px}
</style>
<button id="arcade-menu-button" type="button" aria-label="Exit Defender and return to the arcade menu" aria-keyshortcuts="Escape"><span>EXIT TO ARCADE</span><kbd>ESC</kbd></button>
<script>
(() => {
const exitToArcade = () => window.location.assign('menu.html');
document.getElementById('arcade-menu-button').addEventListener('click', event => { event.stopPropagation(); exitToArcade(); });
window.addEventListener('keydown', event => {
if (event.key === 'Escape' || event.code === 'Escape') {
event.preventDefault(); event.stopImmediatePropagation(); exitToArcade();
}
}, { capture: true });
})();
</script>
<canvas id="game"></canvas>
<div id="scanlines"></div>
<div id="vignette"></div>
<script>
'use strict';
/* ==========================================================================
DEFENDER (1981) — single-file HTML5 clone
Canvas 2D vector-glow rendering · Web Audio synthesized SFX ·
delta-time loop · wraparound cylindrical world
========================================================================== */
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
// ---------------- Logical resolution & world ----------------
const VIEW_W = 960, VIEW_H = 540;
const WORLD_W = Math.floor(VIEW_W * 3.5); // ~3.5 screens, loops horizontally
const T_STEP = 24;
const T_COUNT = Math.round(WORLD_W / T_STEP);
const BEAM_LEN = 310;
let scale = 1, offX = 0, offY = 0;
function resize(){
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
scale = Math.min(canvas.width / VIEW_W, canvas.height / VIEW_H);
offX = (canvas.width - VIEW_W * scale) / 2;
offY = (canvas.height - VIEW_H * scale) / 2;
}
window.addEventListener('resize', resize);
resize();
// ---------------- Utils ----------------
const rand = (a,b)=>a+Math.random()*(b-a);
const irand = (a,b)=>Math.floor(a+Math.random()*(b-a+1));
const clamp = (v,a,b)=>v<a?a:(v>b?b:v);
const wrapX = x=>((x%WORLD_W)+WORLD_W)%WORLD_W;
function wrapDelta(a,b){ let d=(a-b)%WORLD_W; if(d>WORLD_W/2)d-=WORLD_W; else if(d<-WORLD_W/2)d+=WORLD_W; return d; }
const wrapDist = (a,b)=>Math.abs(wrapDelta(a,b));
function pad(n,w){ let s=String(Math.floor(n)); while(s.length<w) s='0'+s; return s; }
// ---------------- Terrain ----------------
let terrain = new Float32Array(T_COUNT);
function genTerrain(){
const arr = new Array(T_COUNT);
let h = rand(430, 470);
for(let i=0;i<T_COUNT;i++){ h += rand(-18,18); h = clamp(h,395,505); arr[i]=h; }
const drift = arr[T_COUNT-1]-arr[0]; // stitch seam for clean wrap
for(let i=0;i<T_COUNT;i++) arr[i] -= drift*(i/(T_COUNT-1));
terrain = Float32Array.from(arr);
}
function terrainY(x){
x = wrapX(x);
const i = Math.min(T_COUNT-1, Math.floor(x/T_STEP));
const j = (i+1)%T_COUNT;
const t = (x - i*T_STEP)/T_STEP;
return terrain[i]*(1-t) + terrain[j]*t;
}
// ---------------- Web Audio synth ----------------
const Snd = {
ctx:null, master:null, noiseBuf:null, thrustGain:null,
init(){
if(this.ctx) return;
try{
const AC = window.AudioContext || window.webkitAudioContext;
this.ctx = new AC();
this.master = this.ctx.createGain();
this.master.gain.value = 0.32;
this.master.connect(this.ctx.destination);
const len = this.ctx.sampleRate;
this.noiseBuf = this.ctx.createBuffer(1, len, this.ctx.sampleRate);
const d = this.noiseBuf.getChannelData(0);
for(let i=0;i<len;i++) d[i]=Math.random()*2-1;
// continuous gated thrust hum (saw + noise through lowpass)
const filt = this.ctx.createBiquadFilter();
filt.type='lowpass'; filt.frequency.value=240;
this.thrustGain = this.ctx.createGain(); this.thrustGain.gain.value=0;
const osc = this.ctx.createOscillator(); osc.type='sawtooth'; osc.frequency.value=48;
const nz = this.ctx.createBufferSource(); nz.buffer=this.noiseBuf; nz.loop=true;
const ng = this.ctx.createGain(); ng.gain.value=0.5;
osc.connect(filt); nz.connect(ng); ng.connect(filt);
filt.connect(this.thrustGain); this.thrustGain.connect(this.master);
osc.start(); nz.start();
}catch(e){ this.ctx=null; }
},
resume(){ if(this.ctx && this.ctx.state==='suspended') this.ctx.resume(); },
setThrust(on){
if(!this.ctx||!this.thrustGain) return;
this.thrustGain.gain.setTargetAtTime(on?0.10:0, this.ctx.currentTime, 0.06);
},
tone(o){
if(!this.ctx) return;
const t0=this.ctx.currentTime+(o.when||0);
const osc=this.ctx.createOscillator(), g=this.ctx.createGain();
osc.type=o.type||'square';
osc.frequency.setValueAtTime(Math.max(1,o.f),t0);
if(o.f1!==undefined) osc.frequency.exponentialRampToValueAtTime(Math.max(1,o.f1),t0+o.dur);
g.gain.setValueAtTime(o.vol||0.25,t0);
g.gain.exponentialRampToValueAtTime(0.001,t0+o.dur);
osc.connect(g); g.connect(this.master);
osc.start(t0); osc.stop(t0+o.dur+0.05);
},
noise(o){
if(!this.ctx) return;
const t0=this.ctx.currentTime+(o.when||0);
const src=this.ctx.createBufferSource(); src.buffer=this.noiseBuf; src.loop=true;
const f=this.ctx.createBiquadFilter();
f.type=o.type||'lowpass';
f.frequency.setValueAtTime(o.f||1200,t0);
if(o.f1!==undefined) f.frequency.exponentialRampToValueAtTime(Math.max(20,o.f1),t0+o.dur);
f.Q.value=o.q||0.8;
const g=this.ctx.createGain();
g.gain.setValueAtTime(o.vol||0.4,t0);
g.gain.exponentialRampToValueAtTime(0.001,t0+o.dur);
src.connect(f); f.connect(g); g.connect(this.master);
src.start(t0); src.stop(t0+o.dur+0.05);
},
laser(){ this.tone({f:1500,f1:160,dur:0.11,type:'square',vol:0.16});
this.noise({dur:0.05,vol:0.06,f:5000,f1:1500,type:'highpass'}); },
eShoot(){ this.tone({f:820,f1:240,dur:0.09,type:'square',vol:0.10}); },
explode(s){ s=s||0.5;
this.noise({dur:0.25+s*0.4,vol:0.30+s*0.25,f:2600,f1:90});
this.tone({f:130,f1:30,dur:0.30+s*0.3,type:'triangle',vol:0.30}); },
playerExplode(){
this.noise({dur:1.1,vol:0.6,f:3200,f1:50});
this.tone({f:90,f1:22,dur:0.9,type:'sine',vol:0.5});
this.tone({f:400,f1:40,dur:0.6,type:'sawtooth',vol:0.2}); },
smartBomb(){
this.noise({dur:0.9,vol:0.5,f:400,f1:5000,type:'bandpass',q:1.5});
this.tone({f:70,f1:14,dur:0.9,type:'sine',vol:0.55});
this.tone({f:1200,f1:100,dur:0.5,type:'square',vol:0.15}); },
pickup(){ this.tone({f:520,f1:1040,dur:0.10,vol:0.25});
this.tone({f:780,f1:1560,dur:0.12,vol:0.22,when:0.07}); },
humanReturn(){ this.tone({f:660,dur:0.08,vol:0.22});
this.tone({f:880,dur:0.10,vol:0.22,when:0.08});
this.tone({f:1320,dur:0.14,vol:0.2,when:0.16}); },
humanDie(){ this.tone({f:480,f1:70,dur:0.4,type:'square',vol:0.25}); },
mutantize(){ this.tone({f:220,f1:1400,dur:0.4,type:'sawtooth',vol:0.25});
this.tone({f:1400,f1:200,dur:0.3,type:'sawtooth',vol:0.2,when:0.35}); },
alarmPulse(){ this.tone({f:940,dur:0.09,type:'square',vol:0.18});
this.tone({f:700,dur:0.09,type:'square',vol:0.18,when:0.11}); },
hyper(){ this.tone({f:180,f1:1800,dur:0.32,type:'sine',vol:0.3});
this.noise({dur:0.3,vol:0.15,f:800,f1:6000,type:'highpass'}); },
extra(){ [523,659,784,1046].forEach((f,i)=>this.tone({f,dur:0.12,vol:0.22,when:i*0.09})); },
waveClear(){ [392,523,659,784].forEach((f,i)=>this.tone({f,dur:0.14,type:'triangle',vol:0.28,when:i*0.1})); },
gameOver(){ [392,330,262,196].forEach((f,i)=>this.tone({f,dur:0.3,type:'triangle',vol:0.28,when:i*0.22})); },
baiterWarn(){ for(let i=0;i<3;i++) this.tone({f:1600,f1:900,dur:0.07,type:'square',vol:0.18,when:i*0.09}); },
planetBoom(){
this.noise({dur:1.6,vol:0.7,f:2000,f1:40});
this.tone({f:60,f1:12,dur:1.4,type:'sine',vol:0.6});
this.tone({f:300,f1:30,dur:1.0,type:'sawtooth',vol:0.25}); }
};
// ---------------- Input ----------------
const keys = {left:false,right:false,up:false,down:false,fire:false};
const KEYMAP = {
ArrowLeft:'left', KeyA:'left', ArrowRight:'right', KeyD:'right',
ArrowUp:'up', KeyW:'up', ArrowDown:'down', KeyS:'down', Space:'fire'
};
window.addEventListener('keydown', e=>{
Snd.init(); Snd.resume();
const k = KEYMAP[e.code];
if(k){ keys[k]=true; e.preventDefault(); }
if(e.code==='Enter'){ e.preventDefault(); onEnter(); }
else if(e.code==='KeyB'||e.code==='ShiftLeft'||e.code==='ShiftRight'){ if(!e.repeat){ e.preventDefault(); doSmartBomb(); } }
else if(e.code==='KeyH'||e.code==='ControlLeft'||e.code==='ControlRight'){ if(!e.repeat){ e.preventDefault(); doHyperspace(); } }
else if(e.code==='KeyP'){
if(state==='playing'||state==='waveclear'){ paused=!paused; if(paused) Snd.setThrust(false); }
}
});
window.addEventListener('keyup', e=>{
const k=KEYMAP[e.code];
if(k){ keys[k]=false; e.preventDefault(); }
});
window.addEventListener('blur', ()=>{
for(const k in keys) keys[k]=false;
Snd.setThrust(false);
});
canvas.addEventListener('mousedown', ()=>{ Snd.init(); Snd.resume(); onEnter(); });
// ---------------- Game state ----------------
let state='title'; // title | playing | waveclear | gameover
let paused=false, time=0;
let player=null;
let enemies=[], mines=[], ebullets=[], beams=[], humanoids=[], parts=[], ftexts=[], stars=[];
let camX=0, flash=0, shake=0;
let score=0, high=0, lives=3, bombs=3, wave=1, nextAward=10000;
let planetAlive=true;
let pendingSpawns=[], spawnTimer=0, waveTimer=0, baiterTimer=0;
let deathTimer=0, stateTimer=0, alarmT=0;
let message={text:'',sub:'',t:0,dur:1};
try{ high = parseInt(localStorage.getItem('defender_high')||'0',10)||0; }catch(e){ high=0; }
function saveHigh(){ try{ localStorage.setItem('defender_high', String(high)); }catch(e){} }
function setMessage(text, sub, dur){ message={text:text||'', sub:sub||'', t:dur||2, dur:dur||2}; }
function addTextSafe(x,y,text,color){ ftexts.push({x:x,y:y,text:text,color:color||'#ff5',t:1.2}); }
function makeStars(){
stars=[];
for(let i=0;i<130;i++)
stars.push({x:rand(0,VIEW_W), y:rand(0,VIEW_H*0.9), p:[0.12,0.25,0.45][irand(0,2)], b:rand(0.25,1)});
}
makeStars();
// ---------------- Entity factories & game flow ----------------
function newPlayer(){
return {x:rand(0,WORLD_W), y:rand(60,160), vx:0, vy:0, facing:1,
alive:true, invuln:3, carrying:null, cool:0, thrust:false};
}
function resetGame(){
score=0; lives=3; bombs=3; wave=1; nextAward=10000; planetAlive=true;
humanoids=[];
for(let i=0;i<10;i++)
humanoids.push({x:0,y:0,dir:1,state:'walk',vy:0,fallStart:0,changeT:rand(1,4)});
startWave();
state='playing'; paused=false;
}
function startWave(){
genTerrain();
enemies=[]; mines=[]; ebullets=[]; beams=[]; ftexts=[]; parts=[];
if(!planetAlive) humanoids=[];
for(const h of humanoids){ // restore survivors to ground
if(h.state==='dead') continue;
h.state='walk'; h.x=rand(0,WORLD_W); h.dir=Math.random()<0.5?-1:1;
h.vy=0; h.y=terrainY(h.x)-4;
}
player=newPlayer();
camX=wrapX(player.x-VIEW_W/2);
buildSpawns();
spawnTimer=0.6; waveTimer=0; baiterTimer=0; alarmT=0;
setMessage('WAVE '+wave, planetAlive?'DEFEND THE HUMANOIDS':'THE PLANET IS GONE — SURVIVE', 2.4);
}
function buildSpawns(){
pendingSpawns=[];
const add=(t,n)=>{ for(let i=0;i<n;i++) pendingSpawns.push(t); };
if(planetAlive){
add('lander', Math.min(4+wave,12));
if(wave>=2) add('bomber', Math.min(1+Math.floor((wave-1)/2),4));
if(wave>=2) add('pod', Math.min(Math.floor(wave/2),3));
} else {
add('mutant', Math.min(6+wave*2,18));
}
for(let i=pendingSpawns.length-1;i>0;i--){
const j=irand(0,i); const tmp=pendingSpawns[i]; pendingSpawns[i]=pendingSpawns[j]; pendingSpawns[j]=tmp;
}
}
function spawnEnemy(type, px, py){
let x = px!==undefined ? px : rand(0,WORLD_W);
if(px===undefined){
let tries=0;
while(wrapDist(x, player?player.x:0) < VIEW_W*0.55 && tries++<24) x=rand(0,WORLD_W);
}
const sp = 1+(wave-1)*0.07;
const e = {type:type, x:x, y:py!==undefined?py:rand(60,220), vx:0, vy:0, r:12,
t:rand(0,6.28), dead:false, fireT:rand(1.2,3), state:'seek',
carry:null, target:null, dir:Math.random()<0.5?-1:1,
mineT:rand(2,4), speed:60, acc:200, score:100};
switch(type){
case 'lander': e.r=12; e.speed=55*sp; e.score=150; if(py===undefined)e.y=rand(40,120); break;
case 'mutant': e.r=11; e.speed=(planetAlive?150:190)*sp; e.acc=260*sp; e.score=150; break;
case 'bomber': e.r=14; e.speed=42*sp; e.score=250; if(py===undefined)e.y=rand(60,170); break;
case 'pod': e.r=13; e.speed=24; e.score=1000; if(py===undefined)e.y=rand(80,260); break;
case 'swarmer': e.r=8; e.speed=195*sp; e.acc=320*sp; e.score=150; break;
case 'baiter': e.r=10; e.speed=285*sp; e.acc=540*sp; e.score=200; break;
}
enemies.push(e);
return e;
}
function addScore(n,x,y,label){
score+=n;
if(x!==undefined) ftexts.push({x:x,y:y,text:label||('+'+n),color:'#ff5',t:1.1});
if(score>high){ high=score; saveHigh(); }
while(score>=nextAward){
nextAward+=10000; lives++; bombs++;
setMessage('EXTRA SHIP + SMART BOMB','',2); Snd.extra();
}
}
function spawnBurst(x,y,color,n){
for(let i=0;i<n;i++){
const a=rand(0,Math.PI*2), sp=rand(30,240);
parts.push({x:x,y:y,vx:Math.cos(a)*sp,vy:Math.sin(a)*sp,
life:rand(0.3,0.85),max:0.85,color:color,size:rand(1.5,3)});
}
}
function killEnemy(e, award){
if(e.dead) return;
e.dead=true;
spawnBurst(e.x,e.y, e.type==='pod'?'#fb0':(e.type==='mutant'?'#f4f':'#ff6'), e.type==='pod'?26:16);
Snd.explode(e.type==='pod'?0.9:0.5);
if(award) addScore(e.score,e.x,e.y);
if(e.type==='pod'){
const n=irand(4,6);
for(let i=0;i<n;i++){
const s=spawnEnemy('swarmer', wrapX(e.x+rand(-14,14)), e.y+rand(-14,14));
s.vx=rand(-220,220); s.vy=rand(-160,160);
}
}
if(e.carry){ // dropped humanoid -> falls
const h=e.carry; e.carry=null;
h.state='falling'; h.vy=0; h.x=e.x; h.y=e.y+10; h.fallStart=h.y;
}
}
function shootAt(e, speed, spreadDeg){
if(ebullets.length>70) return;
const dx=wrapDelta(player.x,e.x), dy=player.y-e.y;
const a=Math.atan2(dy,dx)+rand(-spreadDeg,spreadDeg)*Math.PI/180;
const sp=speed*(1+(wave-1)*0.05);
ebullets.push({x:e.x,y:e.y,vx:Math.cos(a)*sp,vy:Math.sin(a)*sp,life:5,dead:false});
Snd.eShoot();
}
// ---------------- Player weapons & actions ----------------
function fireBeam(){
const p=player;
beams.push({x:wrapX(p.x+p.facing*14), y:p.y, dir:p.facing, len:BEAM_LEN, life:0.09, max:0.09});
Snd.laser();
const hits=e=>{
const dx=wrapDelta(e.x,p.x);
const ahead = p.facing>0 ? (dx>=-4 && dx<=BEAM_LEN) : (dx<=4 && dx>=-BEAM_LEN);
return ahead && Math.abs(e.y-p.y)<(e.r+4);
};
for(const en of enemies) if(!en.dead && hits(en)) killEnemy(en,true);
for(const m of mines){
if(!m.dead && hits(m)){ m.dead=true; spawnBurst(m.x,m.y,'#fa0',8); Snd.explode(0.3); addScore(25,m.x,m.y); }
}
}
function doSmartBomb(){
if(state!=='playing'||paused||!player||!player.alive||bombs<=0) return;
bombs--;
flash=1; shake=1;
Snd.smartBomb();
const cx=wrapX(camX+VIEW_W/2);
for(const e of enemies)
if(!e.dead && Math.abs(wrapDelta(e.x,cx))<VIEW_W/2+60) killEnemy(e,true);
for(const m of mines)
if(!m.dead && Math.abs(wrapDelta(m.x,cx))<VIEW_W/2+60){ m.dead=true; spawnBurst(m.x,m.y,'#fa0',8); }
}
function doHyperspace(){
if(state!=='playing'||paused||!player||!player.alive) return;
spawnBurst(player.x,player.y,'#8ff',18);
Snd.hyper();
if(Math.random()<0.25){
addTextSafe(player.x,player.y-16,'HYPERSPACE MALFUNCTION','#f66');
killPlayer();
return;
}
player.x=rand(0,WORLD_W);
const maxY=planetAlive?terrainY(player.x)-120:VIEW_H-60;
player.y=rand(40,Math.max(60,maxY));
player.vx=0; player.vy=0;
player.invuln=Math.max(player.invuln,1);
spawnBurst(player.x,player.y,'#8ff',18);
}
function killPlayer(){
const p=player;
if(!p.alive) return;
p.alive=false; deathTimer=2;
lives--;
Snd.setThrust(false);
Snd.playerExplode();
spawnBurst(p.x,p.y,'#5ff',36);
spawnBurst(p.x,p.y,'#fff',16);
flash=0.7; shake=1.3;
if(p.carrying){
const h=p.carrying; p.carrying=null;
h.state='falling'; h.vy=0; h.fallStart=h.y;
}
ebullets=[];
}
function respawn(){
player=newPlayer();
player.invuln=3;
bombs=Math.max(bombs,3);
camX=wrapX(player.x-VIEW_W/2);
setMessage('SHIPS REMAINING: '+lives,'',1.6);
}
function gameOver(){
state='gameover';
if(score>=high){ high=score; saveHigh(); }
Snd.gameOver();
}
function onEnter(){ if(state==='title'||state==='gameover') resetGame(); }
// ---------------- Updates ----------------
function updatePlayer(dt){
const p=player;
if(!p.alive){
deathTimer-=dt;
if(deathTimer<=0){ if(lives>0) respawn(); else gameOver(); }
return;
}
const ACC=1350, ACCY=1050, MAXVX=440, MAXVY=340;
let ix=0, iy=0;
if(keys.left){ ix-=1; p.facing=-1; }
if(keys.right){ ix+=1; p.facing=1; }
if(keys.up) iy-=1;
if(keys.down) iy+=1;
p.vx += ix*ACC*dt;
p.vy += iy*ACCY*dt;
p.vx *= Math.exp(-0.55*dt); // light damping => momentum drift
p.vy *= Math.exp(-1.05*dt);
p.vx = clamp(p.vx,-MAXVX,MAXVX);
p.vy = clamp(p.vy,-MAXVY,MAXVY);
p.x = wrapX(p.x + p.vx*dt);
p.y += p.vy*dt;
if(p.y<22){ p.y=22; p.vy=Math.max(0,p.vy); }
if(planetAlive){
const ty=terrainY(p.x);
if(p.y>ty-9){ p.y=ty-9; killPlayer(); return; }
} else if(p.y>VIEW_H-26){ p.y=VIEW_H-26; p.vy=Math.min(0,p.vy); }
p.thrust = !!(ix||iy);
Snd.setThrust(p.thrust);
if(p.thrust && Math.random()<0.6){
parts.push({x:wrapX(p.x-p.facing*14), y:p.y+rand(-2,3),
vx:-p.facing*rand(60,140)+p.vx*0.3, vy:rand(-30,30)+p.vy*0.3,
life:rand(0.15,0.3), max:0.3, color:'#fa0', size:2});
}
p.cool-=dt;
if(keys.fire && p.cool<=0){ p.cool=0.14; fireBeam(); }
p.invuln=Math.max(0,p.invuln-dt);
if(p.carrying){
const h=p.carrying;
h.x=p.x; h.y=p.y+16;
if(planetAlive && terrainY(p.x)-p.y<48 && Math.abs(p.vy)<130){
p.carrying=null; h.state='walk'; h.y=terrainY(h.x)-4;
addScore(250,h.x,h.y-12,'RETURNED +250');
Snd.humanReturn();
}
}
}
function updateCamera(dt){
if(!player) return;
const look = player.facing*120 + player.vx*0.12; // dynamic look-ahead
const target = wrapX(player.x + look - VIEW_W/2);
camX = wrapX(camX + wrapDelta(target,camX)*Math.min(1,4*dt));
}
function nearestHumanoid(x){
let best=null, bd=1e9;
for(const h of humanoids){
if(h.state!=='walk') continue;
const d=wrapDist(h.x,x);
if(d<bd){ bd=d; best=h; }
}
return best;
}
function updateLander(e,dt,ag){
if(e.state==='seek'){
if(!e.target || e.target.state!=='walk') e.target=nearestHumanoid(e.x);
if(e.target){
const dx=wrapDelta(e.target.x,e.x);
e.x=wrapX(e.x+clamp(dx,-1,1)*e.speed*dt);
const goal = Math.abs(dx)>10 ? (e.target.y-150) : (e.target.y-12);
e.y+=clamp(goal-e.y,-1,1)*e.speed*0.9*dt;
if(Math.abs(dx)<9 && Math.abs(e.y-(e.target.y-11))<9){
e.target.state='grabbed'; e.carry=e.target; e.state='ascend';
setMessage('HUMANOID ABDUCTED','SHOOT THE LANDER BEFORE IT ESCAPES!',1.6);
Snd.alarmPulse(); alarmT=0.5;
}
} else {
e.x=wrapX(e.x+Math.sin(e.t*0.6)*e.speed*0.6*dt);
e.y+=Math.cos(e.t*0.8)*18*dt;
}
} else if(e.state==='ascend'){
e.y-=(48+wave*5)*dt;
e.x=wrapX(e.x+Math.sin(e.t*3)*12*dt);
if(e.carry){ e.carry.x=e.x; e.carry.y=e.y+13; }
if(e.y<44){
const ex=e.x;
if(e.carry){ e.carry.state='dead'; e.carry=null; }
e.dead=true;
spawnEnemy('mutant',ex,40);
spawnBurst(ex,40,'#f4f',18);
Snd.mutantize();
addTextSafe(ex,26,'MUTANT!','#f4f');
checkPlanetDeath();
}
}
e.fireT-=dt*ag;
if(e.fireT<=0){ e.fireT=rand(2.4,4.2); if(wrapDist(e.x,player.x)<VIEW_W*0.7) shootAt(e,250,30); }
}
function updateMutant(e,dt,ag){
const dx=wrapDelta(player.x,e.x), dy=player.y-e.y;
e.vx+=Math.sign(dx)*e.acc*dt;
e.vy+=clamp(dy,-80,80)/80*e.acc*0.7*dt;
e.vx=clamp(e.vx,-e.speed,e.speed);
e.vy=clamp(e.vy,-e.speed*0.85,e.speed*0.85);
e.x=wrapX(e.x+e.vx*dt); e.y+=e.vy*dt;
e.fireT-=dt*ag;
if(e.fireT<=0){ e.fireT=rand(1.5,2.8); if(wrapDist(e.x,player.x)<VIEW_W*0.8) shootAt(e,280,18); }
}
function updateSwarmer(e,dt){
const dx=wrapDelta(player.x,e.x), dy=player.y-e.y;
e.vx+=(Math.sign(dx)*e.acc+Math.sin(e.t*11)*300)*dt;
e.vy+=(Math.sign(dy)*e.acc*0.7+Math.cos(e.t*8.5)*240)*dt;
const m=e.speed*1.2;
e.vx=clamp(e.vx,-m,m); e.vy=clamp(e.vy,-m,m);
e.x=wrapX(e.x+e.vx*dt); e.y+=e.vy*dt;
}
function updateBaiter(e,dt,ag){
const dx=wrapDelta(player.x,e.x), dy=player.y-e.y;
e.vx+=Math.sign(dx)*e.acc*dt;
e.vy+=Math.sign(dy)*e.acc*0.8*dt;
e.vx=clamp(e.vx,-e.speed,e.speed);
e.vy=clamp(e.vy,-e.speed,e.speed);
e.x=wrapX(e.x+e.vx*dt); e.y+=e.vy*dt;
e.fireT-=dt*ag;
if(e.fireT<=0){ e.fireT=rand(0.9,1.6); shootAt(e,340,10); }
}
function updateBomber(e,dt){
e.x=wrapX(e.x+e.dir*e.speed*dt);
e.y+=Math.sin(e.t*1.4)*16*dt;
e.mineT-=dt;
if(e.mineT<=0){
e.mineT=rand(2.4,4.4);
if(mines.length<36){ mines.push({x:e.x,y:e.y+10,r:5,t:0,dead:false}); Snd.eShoot(); }
}
}
function updatePod(e,dt){
e.x=wrapX(e.x+Math.sin(e.t*0.5)*e.speed*dt);
e.y+=Math.cos(e.t*0.7)*e.speed*0.8*dt;
}
function updateEnemy(e,dt){
e.t+=dt;
const ag=(1+(wave-1)*0.12)*(planetAlive?1:1.5);
switch(e.type){
case 'lander': updateLander(e,dt,ag); break;
case 'mutant': updateMutant(e,dt,ag); break;
case 'swarmer': updateSwarmer(e,dt); break;
case 'baiter': updateBaiter(e,dt,ag); break;
case 'bomber': updateBomber(e,dt); break;
case 'pod': updatePod(e,dt); break;
}
const floor = planetAlive ? terrainY(e.x)-8 : VIEW_H-14;
if(e.y>floor){ e.y=floor; if(e.vy>0) e.vy*=-0.35; }
if(e.y<14){ e.y=14; if(e.vy<0) e.vy=0; }
}
function updateHumanoid(h,dt){
switch(h.state){
case 'walk':
h.changeT-=dt;
if(h.changeT<=0){ h.changeT=rand(1,4); h.dir=Math.random()<0.3?0:(Math.random()<0.5?-1:1); }
h.x=wrapX(h.x+h.dir*10*dt);
h.y=terrainY(h.x)-4;
break;
case 'falling':
h.vy+=620*dt; h.y+=h.vy*dt;
if(player.alive && wrapDist(h.x,player.x)<18 && Math.abs(h.y-player.y)<18){
h.state='carried'; player.carrying=h;
addScore(500,h.x,h.y,'CAUGHT +500');
Snd.pickup();
break;
}
if(h.y>=terrainY(h.x)-4){
h.y=terrainY(h.x)-4;
if((terrainY(h.x)-h.fallStart)>230){
h.state='dead';
spawnBurst(h.x,h.y,'#f66',10);
Snd.humanDie();
addTextSafe(h.x,h.y-14,'HUMANOID LOST','#f66');
checkPlanetDeath();
} else {
h.state='walk';
}
}
break;
// 'grabbed' driven by lander · 'carried' driven by player
}
}
function updateEBullets(dt){
for(const b of ebullets){
b.life-=dt;
if(b.life<=0){ b.dead=true; continue; }
b.x=wrapX(b.x+b.vx*dt); b.y+=b.vy*dt;
if(planetAlive && b.y>=terrainY(b.x)){ b.dead=true; spawnBurst(b.x,terrainY(b.x)-2,'#f88',3); continue; }
if(b.y>VIEW_H-4||b.y<2){ b.dead=true; continue; }
if(player.alive && player.invuln<=0 && wrapDist(b.x,player.x)<11 && Math.abs(b.y-player.y)<9){
b.dead=true; killPlayer(); continue;
}
for(const h of humanoids){
if(h.state==='walk' && wrapDist(b.x,h.x)<7 && Math.abs(b.y-h.y)<8){
b.dead=true; h.state='dead';
spawnBurst(h.x,h.y,'#6cf',8);
Snd.humanDie();
addTextSafe(h.x,h.y-12,'HUMANOID LOST','#f66');
checkPlanetDeath();
break;
}
}
}
ebullets=ebullets.filter(b=>!b.dead);
}
function collisions(){
const p=player;
if(!p.alive||p.invuln>0) return;
for(const e of enemies){
if(e.dead) continue;
if(wrapDist(e.x,p.x)<e.r+9 && Math.abs(e.y-p.y)<e.r+7){
killEnemy(e,false); killPlayer(); return;
}
}
for(const m of mines){
if(m.dead) continue;
if(wrapDist(m.x,p.x)<m.r+8 && Math.abs(m.y-p.y)<m.r+7){
m.dead=true; spawnBurst(m.x,m.y,'#fa0',12); Snd.explode(0.5); killPlayer(); return;
}
}
}
function checkPlanetDeath(){
if(!planetAlive) return;
for(const h of humanoids) if(h.state!=='dead') return;
planetAlive=false;
flash=1.2; shake=1.6;
Snd.planetBoom();
setMessage('THE PLANET HAS EXPLODED','ALL HUMANOIDS LOST — MUTANTS INBOUND',3.2);
for(const e of enemies){
if(e.dead) continue;
if(e.type==='lander'||e.type==='bomber'||e.type==='pod'){
e.type='mutant'; e.score=150;
e.speed=190*(1+(wave-1)*0.07); e.acc=280;
if(e.carry){ e.carry.state='dead'; e.carry=null; }
}
}
}
function updateParts(dt){
for(const p of parts){ p.life-=dt; p.x=wrapX(p.x+p.vx*dt); p.y+=p.vy*dt; }
parts=parts.filter(p=>p.life>0);
}
function updateTexts(dt){
for(const t of ftexts){ t.t-=dt; t.y-=26*dt; }
ftexts=ftexts.filter(t=>t.t>0);
}
function updateBeams(dt){
for(const b of beams) b.life-=dt;
beams=beams.filter(b=>b.life>0);
}
function updateHumanoidsList(dt){
for(const h of humanoids) if(h.state!=='dead') updateHumanoid(h,dt);
}
function update(dt){
time+=dt;
flash=Math.max(0,flash-dt*2.0);
shake=Math.max(0,shake-dt*2.2);
if(message.t>0) message.t-=dt;
updateParts(dt); updateTexts(dt); updateBeams(dt);
if(state==='title'){ camX=wrapX(camX+12*dt); return; }
if(state==='gameover') return;
updatePlayer(dt);
updateCamera(dt);
if(state==='waveclear'){
updateHumanoidsList(dt);
stateTimer-=dt;
if(stateTimer<=0){ wave++; startWave(); state='playing'; }
return;
}
// ---- playing ----
spawnTimer-=dt;
if(pendingSpawns.length && spawnTimer<=0){
spawnEnemy(pendingSpawns.pop());
spawnTimer=1.1;
}
waveTimer+=dt;
const baiterStart=Math.max(10,30-wave*2)*(planetAlive?1:0.5);
if(waveTimer>baiterStart){
baiterTimer-=dt;
let nb=0; for(const e of enemies) if(e.type==='baiter'&&!e.dead) nb++;
if(baiterTimer<=0 && nb<3){
baiterTimer=16;
const side=Math.random()<0.5?-1:1;
spawnEnemy('baiter', wrapX(player.x+side*(VIEW_W/2+140)), clamp(player.y+rand(-100,100),40,VIEW_H-80));
setMessage('BAITER!','',1); Snd.baiterWarn();
}
}
const n=enemies.length;
for(let i=0;i<n;i++){ const e=enemies[i]; if(!e.dead) updateEnemy(e,dt); }
updateHumanoidsList(dt);
updateEBullets(dt);
for(const m of mines) m.t+=dt;
collisions();
enemies=enemies.filter(e=>!e.dead);
mines=mines.filter(m=>!m.dead);
let grabbed=false;
for(const h of humanoids) if(h.state==='grabbed'){ grabbed=true; break; }
if(grabbed){ alarmT-=dt; if(alarmT<=0){ Snd.alarmPulse(); alarmT=0.5; } }
if(enemies.length===0 && pendingSpawns.length===0 && player.alive){
let bonus=0;
if(planetAlive) for(const h of humanoids) if(h.state!=='dead') bonus+=100;
if(bonus>0) addScore(bonus);
state='waveclear'; stateTimer=2.6;
ebullets=[]; mines=[];
setMessage('WAVE '+wave+' CLEARED', bonus>0?('HUMANOID BONUS +'+bonus):'', 2.4);
Snd.waveClear();
}
}
// ---------------- Rendering ----------------
function drawWrapped(x,margin,fn){
const rel=wrapX(x-camX);
if(rel<VIEW_W+margin) fn(rel);
if(rel>WORLD_W-margin) fn(rel-WORLD_W);
}
function glowText(txt,x,y,color,size,align){
ctx.save();
ctx.font='bold '+size+'px "Courier New",monospace';
ctx.textAlign=align||'center';
ctx.textBaseline='middle';
ctx.fillStyle=color;
ctx.shadowColor=color;
ctx.shadowBlur=10;
ctx.fillText(txt,x,y);
ctx.restore();
}
function drawStars(){
if(!planetAlive && state!=='title') return; // pitch-black void
for(const s of stars){
const sx=((s.x-camX*s.p)%VIEW_W+VIEW_W)%VIEW_W;
ctx.globalAlpha=s.b*(0.6+0.4*Math.sin(time*2+s.x));
ctx.fillStyle='#9df';
ctx.fillRect(sx,s.y,1.6,1.6);
}
ctx.globalAlpha=1;
}
function drawTerrain(){
if(!planetAlive) return;
const start=Math.floor(camX/T_STEP);
const count=Math.ceil(VIEW_W/T_STEP)+2;
ctx.beginPath();
for(let k=0;k<=count;k++){
const idx=((start+k)%T_COUNT+T_COUNT)%T_COUNT;
const sx=(start+k)*T_STEP-camX;
const y=terrain[idx];
if(k===0) ctx.moveTo(sx,y); else ctx.lineTo(sx,y);
}
ctx.lineTo((start+count)*T_STEP-camX,VIEW_H+4);
ctx.lineTo(start*T_STEP-camX,VIEW_H+4);
ctx.closePath();
ctx.fillStyle='rgba(0,70,25,0.45)';
ctx.fill();
ctx.beginPath();
for(let k=0;k<=count;k++){
const idx=((start+k)%T_COUNT+T_COUNT)%T_COUNT;
const sx=(start+k)*T_STEP-camX;
const y=terrain[idx];
if(k===0) ctx.moveTo(sx,y); else ctx.lineTo(sx,y);
}
ctx.strokeStyle='#2aff7a';
ctx.lineWidth=2;
ctx.shadowColor='#2aff7a';
ctx.shadowBlur=8;
ctx.stroke();
ctx.shadowBlur=0;
}
function drawHumanoids(){
for(const h of humanoids){
if(h.state==='dead') continue;
drawWrapped(h.x,20,sx=>{
const blink=(h.state==='falling'||h.state==='grabbed')&&(Math.floor(time*10)%2===0);
const c=blink?'#fff':'#6cf';
ctx.save();
ctx.translate(sx,h.y);
ctx.strokeStyle=c; ctx.fillStyle=c;
ctx.shadowColor=c; ctx.shadowBlur=6;
ctx.lineWidth=1.5;
ctx.beginPath(); ctx.arc(0,-7,2,0,6.283); ctx.fill();
const w=h.state==='walk'?Math.sin(time*10+h.x)*1.5:0;
ctx.beginPath();
ctx.moveTo(0,-5); ctx.lineTo(0,0);
ctx.moveTo(0,0); ctx.lineTo(-2+w,4);
ctx.moveTo(0,0); ctx.lineTo(2-w,4);
ctx.moveTo(0,-4); ctx.lineTo(-2,-1);
ctx.moveTo(0,-4); ctx.lineTo(2,-1);
ctx.stroke();
ctx.restore();
});
}
}
function drawMines(){
for(const m of mines){
drawWrapped(m.x,20,sx=>{
const p=1+0.25*Math.sin(m.t*6);
ctx.save();
ctx.translate(sx,m.y); ctx.scale(p,p);
ctx.strokeStyle='#f80'; ctx.shadowColor='#f80'; ctx.shadowBlur=7; ctx.lineWidth=1.5;
ctx.beginPath(); ctx.arc(0,0,4,0,6.283); ctx.stroke();
ctx.beginPath();
ctx.moveTo(-6,0); ctx.lineTo(6,0);
ctx.moveTo(0,-6); ctx.lineTo(0,6);
ctx.stroke();
ctx.restore();
});
}
}
function drawEnemies(){
for(const e of enemies){
if(e.dead) continue;
drawWrapped(e.x,30,sx=>drawEnemyShape(e,sx));
}
}
function drawEnemyShape(e,sx){
ctx.save();
ctx.translate(sx,e.y);
ctx.lineWidth=1.8;
switch(e.type){
case 'lander':
ctx.strokeStyle='#4f4'; ctx.shadowColor='#4f4'; ctx.shadowBlur=8;
ctx.strokeRect(-8,-4,16,9);
ctx.beginPath();
ctx.moveTo(-8,-4); ctx.lineTo(-11,-10);
ctx.moveTo(8,-4); ctx.lineTo(11,-10);
ctx.moveTo(0,-4); ctx.lineTo(0,-9);
ctx.stroke();
ctx.fillStyle='#f44'; ctx.shadowColor='#f44';
ctx.fillRect(-2,-2,4,3);
break;
case 'mutant':
ctx.strokeStyle='#f4f'; ctx.shadowColor='#f4f'; ctx.shadowBlur=9;
ctx.rotate(Math.sin(e.t*6)*0.2);
ctx.beginPath();
for(let i=0;i<8;i++){
const a=i/8*Math.PI*2, r=(i%2)?4:11;
const px=Math.cos(a)*r, py=Math.sin(a)*r;
if(i===0) ctx.moveTo(px,py); else ctx.lineTo(px,py);
}
ctx.closePath(); ctx.stroke();
break;
case 'bomber':
ctx.strokeStyle='#ff5'; ctx.shadowColor='#ff5'; ctx.shadowBlur=8;
ctx.beginPath();
ctx.moveTo(-13,4); ctx.lineTo(-9,-6); ctx.lineTo(9,-6); ctx.lineTo(13,4);
ctx.closePath(); ctx.stroke();
ctx.beginPath(); ctx.arc(0,-6,4,Math.PI,0); ctx.stroke();
break;
case 'pod':
ctx.strokeStyle='#fb0'; ctx.shadowColor='#fb0'; ctx.shadowBlur=9;
ctx.beginPath(); ctx.arc(0,0,10,0,6.283); ctx.stroke();
ctx.save(); ctx.rotate(e.t*2); ctx.strokeRect(-5,-5,10,10); ctx.restore();
break;
case 'swarmer': {
ctx.strokeStyle='#f66'; ctx.shadowColor='#f66'; ctx.shadowBlur=8;
ctx.rotate(Math.atan2(e.vy,e.vx||0.001));
ctx.beginPath();
ctx.moveTo(8,0); ctx.lineTo(-6,-5); ctx.lineTo(-3,0); ctx.lineTo(-6,5);
ctx.closePath(); ctx.stroke();
break;
}
case 'baiter': {
ctx.strokeStyle='#8ff'; ctx.shadowColor='#8ff'; ctx.shadowBlur=10;
ctx.rotate(Math.atan2(e.vy,e.vx||0.001));
ctx.beginPath();
ctx.moveTo(12,0); ctx.lineTo(-8,-4); ctx.lineTo(-4,0); ctx.lineTo(-8,4);
ctx.closePath(); ctx.stroke();
break;
}
}
ctx.restore();
}
function drawPlayer(){
const p=player;
if(!p||!p.alive) return;
if(p.invuln>0 && Math.floor(p.invuln*12)%2===0) return; // invulnerability blink
drawWrapped(p.x,30,sx=>{
ctx.save();
ctx.translate(sx,p.y);
ctx.scale(p.facing,1);
ctx.lineWidth=2;
ctx.strokeStyle='#5ff'; ctx.shadowColor='#5ff'; ctx.shadowBlur=10;
ctx.beginPath();
ctx.moveTo(15,0); ctx.lineTo(4,-6); ctx.lineTo(-9,-5);
ctx.lineTo(-13,-1); ctx.lineTo(-9,4); ctx.lineTo(3,5);
ctx.closePath(); ctx.stroke();
ctx.beginPath();
ctx.moveTo(2,-6); ctx.lineTo(6,-9); ctx.lineTo(10,-6);
ctx.stroke();
if(p.thrust){
const f=rand(6,14);
ctx.strokeStyle='#fa0'; ctx.shadowColor='#fa0'; ctx.shadowBlur=8;
ctx.beginPath();
ctx.moveTo(-13,-2); ctx.lineTo(-13-f,1); ctx.lineTo(-13,3);
ctx.stroke();
}
ctx.restore();
});
}
function drawBeams(){
for(const b of beams){
const a=b.life/b.max;
drawWrapped(b.x,b.len+30,sx=>{
const x2=sx+b.dir*b.len;
const g=ctx.createLinearGradient(sx,b.y,x2,b.y);
g.addColorStop(0,'rgba(120,255,255,'+a+')');
g.addColorStop(1,'rgba(255,60,120,'+(a*0.9)+')');
ctx.strokeStyle=g;
ctx.lineWidth=3;
ctx.shadowColor='#8ff'; ctx.shadowBlur=8;
ctx.beginPath(); ctx.moveTo(sx,b.y); ctx.lineTo(x2,b.y); ctx.stroke();
ctx.shadowBlur=0;
});
}
}
function drawEBullets(){
ctx.fillStyle='#f8a';
ctx.shadowColor='#f4a'; ctx.shadowBlur=6;
for(const b of ebullets){
drawWrapped(b.x,10,sx=>{
ctx.beginPath(); ctx.arc(sx,b.y,2.4,0,6.283); ctx.fill();
});
}
ctx.shadowBlur=0;
}
function drawParts(){
for(const p of parts){
ctx.globalAlpha=p.life/p.max;
ctx.fillStyle=p.color;
drawWrapped(p.x,10,sx=>ctx.fillRect(sx-p.size/2,p.y-p.size/2,p.size,p.size));
}
ctx.globalAlpha=1;
}
function drawTexts(){
ctx.textAlign='center'; ctx.textBaseline='middle';
ctx.font='bold 13px "Courier New",monospace';
for(const t of ftexts){
const a=Math.min(1,t.t);
drawWrapped(t.x,80,sx=>{
ctx.globalAlpha=a;
ctx.fillStyle=t.color;
ctx.shadowColor=t.color; ctx.shadowBlur=6;
ctx.fillText(t.text,sx,t.y);
ctx.shadowBlur=0; ctx.globalAlpha=1;
});
}
}
function drawShipIcon(x,y){
ctx.save(); ctx.translate(x,y);
ctx.strokeStyle='#5ff'; ctx.shadowColor='#5ff'; ctx.shadowBlur=5; ctx.lineWidth=1.5;
ctx.beginPath();
ctx.moveTo(8,0); ctx.lineTo(-6,-5); ctx.lineTo(-6,5);
ctx.closePath(); ctx.stroke();
ctx.restore();
}
function drawBombIcon(x,y){
ctx.save(); ctx.translate(x,y);
ctx.strokeStyle='#fa0'; ctx.shadowColor='#fa0'; ctx.shadowBlur=5; ctx.lineWidth=1.5;
ctx.beginPath(); ctx.arc(0,0,4.5,0,6.283); ctx.stroke();
ctx.beginPath(); ctx.moveTo(0,-4.5); ctx.lineTo(0,-8); ctx.stroke();
ctx.restore();
}
function drawRadar(){
const rw=300, rh=42, rx=VIEW_W/2-rw/2, ry=10;
const kx=rw/WORLD_W, ky=rh/VIEW_H;
ctx.save();
ctx.fillStyle='rgba(0,0,0,0.55)';
ctx.fillRect(rx,ry,rw,rh);
ctx.strokeStyle='rgba(42,255,122,0.8)';
ctx.lineWidth=1;
ctx.shadowColor='#2aff7a'; ctx.shadowBlur=5;
ctx.strokeRect(rx+0.5,ry+0.5,rw-1,rh-1);
ctx.shadowBlur=0;
if(planetAlive){
ctx.strokeStyle='#1a8f4a';
ctx.beginPath();
for(let i=0;i<=T_COUNT;i++){
const px=rx+(i%T_COUNT)*T_STEP*kx;
const py=ry+terrain[i%T_COUNT]*ky;
if(i===0) ctx.moveTo(px,py); else ctx.lineTo(px,py);
}
ctx.stroke();
}