-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
1710 lines (1601 loc) Β· 87.3 KB
/
Copy pathscript.js
File metadata and controls
1710 lines (1601 loc) Β· 87.3 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
// βββββββββββββββββββββββββββββββββββββββββββββββ
// UNDERRUN β SEASON 1 (3 Levels)
// Lv1: Surface+Sewer Lv2: Bike+AcidRain Lv3: Arena+Boss
// βββββββββββββββββββββββββββββββββββββββββββββββ
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const mapCanvas = document.getElementById('mapCanvas');
const mapCtx = mapCanvas.getContext('2d');
function resize() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; }
resize(); window.addEventListener('resize', resize);
// ββ CONSTANTS ββ
const GRAVITY = 0.55;
const WORLD_LEN = 14000;
const DAY_DUR = 30, NIGHT_DUR = 25, MORN_DUR = 12;
let view3D = false;
const PERSP = { fov: 0.18 };
// ββ STATE MACHINE ββ
// gameState: 'start' | 'playing' | 'levelTrans' | 'gameover' | 'seasonEnd'
// gameLevel: 1 (surface/sewer), 2 (bike), 3 (arena)
let gameState = 'start';
let gameLevel = 1;
let score = 0, kills = 0;
// ββ PHASE (Level 1) ββ
let phase = 'day', phaseTimer = DAY_DUR, phaseDuration = DAY_DUR;
let dayCount = 0;
// ββ INPUT ββ
const keys = {};
window.addEventListener('keydown', e => {
if (['Space','ArrowUp','ArrowDown','ArrowLeft','ArrowRight'].includes(e.code)) e.preventDefault();
keys[e.code] = true;
if (gameState === 'playing') handleAction(e.code);
if (e.code === 'Tab') { e.preventDefault(); view3D = !view3D; showStatus(view3D?'3D MODE ON':'2D MODE'); }
});
window.addEventListener('keyup', e => { keys[e.code] = false; });
// βββββββββββββββββββββββββββββββββββββββββββββββ
// SHARED HELPERS
// βββββββββββββββββββββββββββββββββββββββββββββββ
function groundY() { return canvas.height * 0.62; }
function sewCeilY() { return groundY() + 30; }
function sewFloorY() { return sewCeilY() + 160; }
let cam = { x: 0 };
function updateCamera() {
const tx = player.x - canvas.width * 0.35;
cam.x += (tx - cam.x) * 0.1;
cam.x = Math.max(0, Math.min(cam.x, WORLD_LEN - canvas.width));
}
let particles = [];
function spawnParticles(x, y, color, count=8, opts={}) {
for (let i=0; i<count; i++) {
const ang = Math.random()*Math.PI*2, spd = (opts.speed||3)+Math.random()*2;
particles.push({x,y,vx:Math.cos(ang)*spd,vy:Math.sin(ang)*spd,life:opts.life||35,maxLife:opts.life||35,color,size:opts.size||(2+Math.random()*3)});
}
}
function updateParticles() {
for (let i=particles.length-1;i>=0;i--) {
const p=particles[i]; p.x+=p.vx; p.y+=p.vy; p.vy+=0.12; p.life--;
if(p.life<=0) particles.splice(i,1);
}
}
function drawParticles() {
particles.forEach(p=>{
const a=p.life/p.maxLife;
const px=view3D?projectX(p.x,p.y):p.x-cam.x;
const py=view3D?projectY(p.y):p.y;
ctx.globalAlpha=a; ctx.fillStyle=p.color;
ctx.beginPath(); ctx.arc(px,py,p.size*a,0,Math.PI*2); ctx.fill();
});
ctx.globalAlpha=1;
}
function projectX(wx,wy) {
if(!view3D) return wx-cam.x;
const gy=groundY(), dep=1-(wy/gy)*PERSP.fov, cx=canvas.width/2;
return cx+(wx-cam.x-cx)*dep;
}
function projectY(wy) {
if(!view3D) return wy;
const gy=groundY();
if(wy>=gy) return wy+(wy-gy)*0.15;
return wy*(1-PERSP.fov*0.3)+gy*PERSP.fov*0.3;
}
let statusTimer=0, trapWarnTimer=0, phaseAlertTimer=0;
function showStatus(msg) { const el=document.getElementById('statusMsg'); el.textContent=msg; el.classList.add('show'); statusTimer=160; }
function showTrapWarn(msg) { const el=document.getElementById('trapWarn'); el.textContent=msg; el.classList.add('show'); trapWarnTimer=240; }
function tickStatus() { if(statusTimer>0){statusTimer--;if(statusTimer===0)document.getElementById('statusMsg').classList.remove('show');} }
function tickTrapWarn() { if(trapWarnTimer>0){trapWarnTimer--;if(trapWarnTimer===0)document.getElementById('trapWarn').classList.remove('show');} }
function showPhaseAlert(text,cls) {
const el=document.getElementById('phaseAlert');
el.innerHTML=text.replace('\n','<br>'); el.className='show '+cls; phaseAlertTimer=180;
}
// βββββββββββββββββββββββββββββββββββββββββββββββ
// PLAYER
// βββββββββββββββββββββββββββββββββββββββββββββββ
let player = {};
function spawnPlayer() {
player = {
x:120, y:0, vx:0, vy:0, w:18, h:36,
hp:100, maxHp:100, ammo:12, maxAmmo:12,
facing:1, onGround:false, jumpCount:0, maxJumps:2,
crawling:false, underground:false, inManhole:false, manholeIndex:-1,
reloading:false, reloadTimer:0,
shooting:false, shootTimer:0,
invincible:0, stunned:0, poisoned:0, poisonTick:0,
onFire:0, fireTick:0,
animTick:0, animFrame:0,
distanceTraveled:0,
nightDrainAcc:0,
deathCause:'', dead:false,
// Bike-specific
onBike:false, bikeSpeed:0, bikeMaxSpeed:9, bikeAccel:0.15, bikeBrake:0.3,
bikeJumped:false, bikeJumpCount:0,
// Arena
punchCooldown:0, blocking:false,
};
player.y = groundY() - player.h - 2;
}
function damagePlayer(dmg, cause) {
if(player.invincible>0) return;
player.hp-=dmg; player.invincible=40; player.deathCause=cause||'';
spawnParticles(player.x+player.w/2,player.y+player.h/2,'#ef4444',6);
if(player.hp<=0) { player.hp=0; player.dead=true; doGameOver(); }
updateHpUI();
}
function updateHpUI() { document.getElementById('hpFill').style.width=(player.hp/player.maxHp*100)+'%'; }
function updateAmmoUI() {
document.getElementById('ammoFill').style.width=(player.ammo/player.maxAmmo*100)+'%';
document.getElementById('ammoCount').textContent=player.ammo+'/'+player.maxAmmo;
}
// βββββββββββββββββββββββββββββββββββββββββββββββ
// LEVEL 1 β SURFACE + SEWERS
// βββββββββββββββββββββββββββββββββββββββββββββββ
let world = {};
let bullets = [];
let enemies = [];
function generateLevel1() {
world = { manholes:[], ammoPickups:[], traps:[], sewPipes:[], sewRats:[],
healthPacks:[], crates:[], lampPosts:[], checkpointX: WORLD_LEN - 500 };
const mCount = 10;
const spacing = WORLD_LEN / mCount;
for (let i=0; i<mCount; i++) {
world.manholes.push({ x: 400+spacing*i+(Math.random()*spacing*0.4-spacing*0.2), open:true });
}
for (let i=0; i<22; i++) world.ammoPickups.push({x:300+Math.random()*(WORLD_LEN-600),collected:false,underground:false});
for (let i=0; i<14; i++) world.ammoPickups.push({x:400+Math.random()*(WORLD_LEN-800),collected:false,underground:true});
for (let i=0; i<10; i++) world.healthPacks.push({x:500+Math.random()*(WORLD_LEN-1000),collected:false,underground:Math.random()<0.4});
for (let i=0; i<25; i++) world.crates.push({x:200+Math.random()*(WORLD_LEN-400), h:20+Math.random()*25|0});
for (let x=300; x<WORLD_LEN; x+=200+Math.random()*160) world.lampPosts.push({x});
// Sewer pipes
for (let i=0; i<20; i++) world.sewPipes.push({x:300+Math.random()*(WORLD_LEN-600), drip:0, dripSpeed:0.3+Math.random()*0.4});
// Sewer rats
for (let i=0; i<12; i++) world.sewRats.push({x:400+Math.random()*(WORLD_LEN-800), vx:(Math.random()<0.5?-1:1)*(0.8+Math.random()), animTick:0, animFrame:0});
// Surface traps
for (let i=0; i<12; i++) world.traps.push({type:'bear',x:500+Math.random()*(WORLD_LEN-1100),triggered:false,resetTimer:0});
for (let i=0; i<6; i++) world.traps.push({type:'spike',x:600+Math.random()*(WORLD_LEN-1200),w:50+Math.random()*40});
for (let i=0; i<8; i++) world.traps.push({type:'gas',x:400+Math.random()*(WORLD_LEN-800),timer:0,period:160+Math.random()*120|0,active:false,cloudLife:0});
for (let i=0; i<5; i++) world.traps.push({type:'crusher',x:700+Math.random()*(WORLD_LEN-1400),timer:0,period:120+Math.random()*100|0,crushing:false,crushY:0});
for (let i=0; i<7; i++) world.traps.push({type:'flame',x:500+Math.random()*(WORLD_LEN-1000),timer:0,period:100+Math.random()*80|0,active:false,flameLife:0});
// Underground traps
for (let i=0; i<8; i++) world.traps.push({type:'sewspike',x:400+Math.random()*(WORLD_LEN-800),w:40+Math.random()*30});
for (let i=0; i<6; i++) world.traps.push({type:'sewelectric',x:500+Math.random()*(WORLD_LEN-1000),w:60+Math.random()*40,timer:0,period:80+Math.random()*60|0,active:false});
for (let i=0; i<5; i++) world.traps.push({type:'sewgas',x:400+Math.random()*(WORLD_LEN-800),timer:0,period:150+Math.random()*120|0,active:false,life:0});
}
function spawnEnemies() {
const count = 4 + dayCount;
for (let i=0; i<count; i++) {
const type = Math.random()<0.25?'brute':(Math.random()<0.5?'vamp':'zombie');
const ex = cam.x + canvas.width + 100 + Math.random()*500;
const ey = groundY() - (type==='brute'?52:38);
enemies.push({x:ex,y:ey,vx:type==='brute'?-0.8:-1.2,w:type==='brute'?30:22,h:type==='brute'?52:38,
hp:type==='brute'?5:2,maxHp:type==='brute'?5:2,type,animTick:0,animFrame:0,underground:false,alert:false});
}
}
function fireBullet() {
if(player.ammo<=0||player.reloading||player.stunned>0) return;
player.ammo--; player.shooting=true; player.shootTimer=8;
const bx=player.x+(player.facing>0?player.w:0);
const by=player.y+player.h*0.4;
bullets.push({x:bx,y:by,vx:player.facing*18,vy:0,life:55,underground:player.underground});
updateAmmoUI();
}
function tryManhole() {
if(player.inManhole) {
if(phase==='night'){showStatus('TOO DANGEROUS β WAIT FOR MORNING');return;}
exitManhole(); return;
}
for (let i=0;i<world.manholes.length;i++) {
const m=world.manholes[i];
if(Math.abs(m.x-player.x)<44&&!player.underground){
player.inManhole=true; player.manholeIndex=i;
player.underground=true;
player.x=m.x; player.y=sewCeilY()+10;
player.vy=2; player.vx=0;
document.getElementById('underground').classList.add('show');
showStatus('IN THE SEWERS β SAFE FROM NIGHT');
return;
}
}
showStatus('NO MANHOLE NEARBY');
}
function exitManhole() {
player.underground=false; player.inManhole=false;
player.y=groundY()-player.h-2; player.vy=-8;
document.getElementById('underground').classList.remove('show');
showStatus('EMERGED β WATCH FOR TRAPS!');
}
function updateLevel1Player(dt) {
if(player.dead) return;
player.invincible=Math.max(0,player.invincible-1);
if(player.stunned>0){player.stunned--;player.vx=0;}
if(player.poisoned>0){
player.poisoned--;player.poisonTick++;
if(player.poisonTick%40===0){damagePlayer(3,'TOXIC POISONING');spawnParticles(player.x+player.w/2,player.y,'#4ade80',4,{size:3,speed:1.5});}
}
if(player.onFire>0){
player.onFire--;player.fireTick=(player.fireTick||0)+1;
if(player.fireTick%25===0){damagePlayer(4,'BURNING');spawnParticles(player.x+player.w/2,player.y+player.h*0.3,'#f97316',5,{speed:2,life:18,size:3});}
}
if(player.shootTimer>0){player.shootTimer--;}else{player.shooting=false;}
if(player.reloading){
player.reloadTimer--;
if(player.reloadTimer<=0){player.ammo=player.maxAmmo;player.reloading=false;updateAmmoUI();showStatus('RELOADED');}
}
if(player.stunned===0){
const speed=player.crawling?2.2:4.5;
if(keys['KeyA']||keys['ArrowLeft']){player.vx=-speed;player.facing=-1;}
else if(keys['KeyD']||keys['ArrowRight']){player.vx=speed;player.facing=1;}
else{player.vx*=0.75;}
}
player.crawling=!!(keys['KeyS']||keys['ArrowDown'])&&player.onGround&&!player.underground;
player.h=player.crawling?18:36;
if(keys['Space']&&!player.shooting&&player.shootTimer===0) fireBullet();
player.vy+=GRAVITY; player.x+=player.vx; player.y+=player.vy;
player.distanceTraveled+=Math.abs(player.vx);
const floorY=player.underground?sewFloorY()-player.h:groundY()-player.h;
const ceilY=player.underground?sewCeilY()+4:-500;
if(player.y>=floorY){player.y=floorY;player.vy=0;player.onGround=true;player.jumpCount=0;}
else{player.onGround=false;}
if(player.y<=ceilY){player.y=ceilY;player.vy=2;}
player.x=Math.max(0,Math.min(player.x,WORLD_LEN-player.w));
// Pickups
world.ammoPickups.forEach(a=>{
if(a.collected||a.underground!==player.underground) return;
const ay=a.underground?sewFloorY()-30:groundY()-22;
if(Math.abs(a.x-player.x)<28&&Math.abs(ay-player.y)<36){
a.collected=true; player.ammo=Math.min(player.maxAmmo,player.ammo+6);
updateAmmoUI(); showStatus('+6 AMMO'); spawnParticles(a.x,ay,'#3b82f6',6); score+=10;
}
});
world.healthPacks.forEach(h=>{
if(h.collected||h.underground!==player.underground) return;
const hy=h.underground?sewFloorY()-30:groundY()-22;
if(Math.abs(h.x-player.x)<28&&Math.abs(hy-player.y)<36){
h.collected=true; player.hp=Math.min(player.maxHp,player.hp+25);
updateHpUI(); showStatus('+25 HP'); spawnParticles(h.x,hy,'#ef4444',8,{speed:3}); score+=20;
}
});
// Sewer rats
world.sewRats.forEach(r=>{
r.animTick++; if(r.animTick%10===0)r.animFrame=(r.animFrame+1)%2;
r.x+=r.vx; if(r.x<100||r.x>WORLD_LEN-100)r.vx*=-1;
});
// Night drain
if(phase==='night'&&!player.underground){
player.nightDrainAcc=(player.nightDrainAcc||0)+dt;
if(player.nightDrainAcc>=0.33){player.nightDrainAcc=0;player.hp=Math.max(0,player.hp-1);updateHpUI();if(player.hp<=0){player.dead=true;player.deathCause='NIGHT EXPOSURE';doGameOver();}}
} else {player.nightDrainAcc=0;}
// Enemy contact
if(phase==='night'&&!player.underground){
enemies.forEach(e=>{if(Math.abs(e.x-player.x)<40&&Math.abs(e.y-player.y)<50&&player.invincible===0)damagePlayer(5,'VAMPIRE ATTACK');});
}
// Checkpoint
if(!player.underground && player.x >= world.checkpointX) {
triggerLevelTransition(1);
}
player.animTick++; if(player.animTick%8===0)player.animFrame=(player.animFrame+1)%4;
score+=0.01; document.getElementById('scoreVal').textContent=Math.floor(score);
updateHpUI(); updateAmmoUI();
}
function updateLevel1Enemies() {
enemies.forEach((e,i)=>{
if(player.underground&&!e.underground){e.x+=e.vx;e.animTick++;if(e.animTick%8===0)e.animFrame=(e.animFrame+1)%4;if(e.x<0||e.x>WORLD_LEN)e.vx*=-1;return;}
const dx=player.x-e.x, dist=Math.abs(dx);
e.alert=dist<450;
const speed=e.type==='brute'?0.9:1.2;
if(dist>5) e.vx=Math.sign(dx)*speed;
e.x+=e.vx; e.animTick++; if(e.animTick%8===0)e.animFrame=(e.animFrame+1)%4;
if(dist<35&&Math.abs(e.y-player.y)<45&&!player.underground&&player.invincible===0)
damagePlayer(e.type==='brute'?14:8,e.type==='brute'?'BRUTE ATTACK':'ENEMY ATTACK');
});
}
function updateBullets() {
for(let bi=bullets.length-1;bi>=0;bi--){
const b=bullets[bi]; b.x+=b.vx; b.y+=b.vy; b.life--;
if(b.life<=0){bullets.splice(bi,1);continue;}
let hit=false;
for(let ei=enemies.length-1;ei>=0;ei--){
const e=enemies[ei];
if(b.x>e.x&&b.x<e.x+e.w&&b.y>e.y&&b.y<e.y+e.h&&b.underground===e.underground){
e.hp--; spawnParticles(b.x,b.y,'#ef4444',6); hit=true;
if(e.hp<=0){
const col=e.type==='vamp'?'#8b5cf6':e.type==='brute'?'#f97316':'#6b7280';
spawnParticles(e.x+e.w/2,e.y+e.h/2,col,14);
enemies.splice(ei,1); kills++; score+=60;
showStatus(e.type==='vamp'?'VAMP SLAIN!':e.type==='brute'?'BRUTE DOWN!':'ZOMBIE DOWN!');
}
break;
}
}
if(hit) bullets.splice(bi,1);
}
}
function updateLevel1Traps() {
const gy=groundY(), sfy=sewFloorY(), scy=sewCeilY();
world.traps.forEach(t=>{
const tx=t.x, px=player.x+player.w/2, pdist=Math.abs(px-tx);
if(['bear','spike','gas','crusher','flame'].includes(t.type)&&phase!=='morning') return;
if(t.type==='bear'){
if(t.triggered){t.resetTimer--;if(t.resetTimer<=0)t.triggered=false;return;}
if(!player.underground&&pdist<18&&player.onGround){t.triggered=true;t.resetTimer=200;player.stunned=80;damagePlayer(12,'BEAR TRAP');spawnParticles(tx,gy-10,'#ef4444',10);showStatus('β BEAR TRAP!');}
} else if(t.type==='spike'){
if(!player.underground&&px>t.x&&px<t.x+t.w&&player.vy>2&&player.y+player.h>gy-5){damagePlayer(35,'SPIKE PIT');spawnParticles(px,gy,'#ef4444',16,{speed:5});player.vy=-8;showStatus('β SPIKE PIT!');}
} else if(t.type==='gas'){
t.timer++;if(t.timer>=t.period){t.timer=0;t.active=true;t.cloudLife=140;}
if(t.active){t.cloudLife--;if(t.cloudLife<=0)t.active=false;if(!player.underground&&pdist<65&&player.poisoned===0){player.poisoned=200;showStatus('β TOXIC GAS!');}}
} else if(t.type==='crusher'){
t.timer++;if(t.timer>=t.period){t.timer=0;t.crushing=true;t.crushY=0;}
if(t.crushing){t.crushY=Math.min(t.crushY+3,60);if(t.crushY>=60){t.crushing=false;t.crushY=0;}if(!player.underground&&pdist<25&&player.onGround&&t.crushY>40){damagePlayer(25,'CRUSHER');spawnParticles(tx,gy-30,'#ef4444',12,{speed:5});showStatus('π₯ CRUSHER!');}}
} else if(t.type==='flame'){
t.timer++;if(t.timer>=t.period){t.timer=0;t.active=true;t.flameLife=70;}
if(t.active){t.flameLife--;if(t.flameLife<=0)t.active=false;if(!player.underground&&pdist<30&&player.onFire===0){player.onFire=150;showStatus('π₯ ON FIRE!');spawnParticles(player.x+player.w/2,player.y,'#f97316',8,{speed:3,life:25});}}
} else if(t.type==='sewspike'){
if(player.underground&&px>t.x&&px<t.x+t.w&&player.vy>1&&player.y+player.h>sfy-25){damagePlayer(20,'SEWER SPIKES');player.vy=-7;spawnParticles(px,sfy-10,'#dc2626',10,{speed:4});showStatus('β SEWER SPIKES!');}
} else if(t.type==='sewelectric'){
t.timer++;if(t.timer>=t.period){t.timer=0;t.active=!t.active;}
if(t.active&&player.underground&&px>t.x&&px<t.x+t.w&&player.y+player.h>sfy-24&&player.invincible===0){damagePlayer(8,'ELECTRIFIED SEWAGE');player.vy=-5;spawnParticles(px,sfy-12,'#fef08a',8,{speed:3});showStatus('β‘ ELECTRIFIED WATER!');}
} else if(t.type==='sewgas'){
t.timer++;if(t.timer>=t.period){t.timer=0;t.active=true;t.life=120;}
if(t.active){t.life--;if(t.life<=0)t.active=false;if(player.underground&&pdist<50&&player.poisoned===0){player.poisoned=160;showStatus('β SEWER GAS!');}}
}
});
}
function tickPhase(dt) {
phaseTimer-=dt;
const frac=Math.max(0,phaseTimer/phaseDuration);
document.getElementById('phaseFill').style.width=(frac*100)+'%';
document.getElementById('phaseCountdown').textContent=Math.ceil(Math.max(0,phaseTimer))+'s';
if(phaseTimer<=0) switchPhase();
if(phaseTimer<=10&&phaseTimer>0){
const cls=phase==='night'?'show-night':phase==='morning'?'show-morning':'';
const tog=Math.floor(phaseTimer*2)%2===0;
document.getElementById('dangerOverlay').className=tog&&cls?cls:'';
} else {document.getElementById('dangerOverlay').className='';}
if(phaseAlertTimer>0){phaseAlertTimer--;}else{document.getElementById('phaseAlert').classList.remove('show');}
}
function switchPhase() {
if(phase==='day'){
phase='night';phaseDuration=NIGHT_DUR;phaseTimer=NIGHT_DUR;dayCount++;
spawnEnemies();
document.getElementById('timeDisplay').className='night';
document.getElementById('timeDisplay').textContent='π NIGHT';
document.getElementById('phaseFill').className='night';
showPhaseAlert('NIGHT FALLS\nπ SEEK SHELTER','night');
if(!player.underground)showStatus('β GET IN A MANHOLE NOW!');
} else if(phase==='night'){
phase='morning';phaseDuration=MORN_DUR;phaseTimer=MORN_DUR;
enemies=[];
document.getElementById('timeDisplay').className='morning';
document.getElementById('timeDisplay').textContent='π
MORNING';
document.getElementById('phaseFill').className='morning';
showPhaseAlert('DAWN BREAKS\nβ TRAPS ARMED','morning');
showTrapWarn('β MORNING TRAPS ACTIVE β MOVE CAREFULLY');
world.traps.forEach(t=>{if(t.type==='bear')t.triggered=false;if(t.type==='crusher')t.timer=0;});
} else {
phase='day';phaseDuration=DAY_DUR;phaseTimer=DAY_DUR;
player.hp=Math.min(player.maxHp,player.hp+15);updateHpUI();
document.getElementById('timeDisplay').className='day';
document.getElementById('timeDisplay').textContent='β DAY';
document.getElementById('phaseFill').className='day';
showPhaseAlert('SAFE ZONE\nβ REST & COLLECT','day');
world.traps.forEach(t=>{if(t.type==='bear')t.triggered=false;});
}
}
// βββββββββββββββββββββββββββββββββββββββββββββββ
// LEVEL 2 β BIKE + ACID RAIN
// βββββββββββββββββββββββββββββββββββββββββββββββ
let bikeWorld = {};
let acidRainDrops = [];
let bikeObstacles = [];
let acidRainActive = false;
let acidRainTimer = 0;
let acidRainDuration = 0;
let bikeHalted = false, bikeHaltTimer = 0;
let level2Progress = 0;
const LEVEL2_LEN = 16000;
const BIKE_Y_BASE = 0; // relative to groundY()
function generateLevel2() {
bikeWorld = {
checkpointX: LEVEL2_LEN - 600,
roadCracks: [],
potholes: [],
roadBumps: [],
healthPacks: [],
ammoPickups: [],
coverZones: [],
};
// Road damage
for(let i=0;i<40;i++) bikeWorld.roadCracks.push({x:300+Math.random()*(LEVEL2_LEN-600)});
for(let i=0;i<20;i++) bikeWorld.potholes.push({x:400+Math.random()*(LEVEL2_LEN-800),w:40+Math.random()*50,depth:10+Math.random()*15});
for(let i=0;i<25;i++) bikeWorld.roadBumps.push({x:300+Math.random()*(LEVEL2_LEN-600),h:8+Math.random()*12});
for(let i=0;i<15;i++) bikeWorld.healthPacks.push({x:600+Math.random()*(LEVEL2_LEN-1200),collected:false});
for(let i=0;i<20;i++) bikeWorld.ammoPickups.push({x:400+Math.random()*(LEVEL2_LEN-800),collected:false});
// Cover zones (tunnels / overhangs player can shelter in during acid rain)
for(let i=0;i<8;i++) bikeWorld.coverZones.push({x:500+i*(LEVEL2_LEN/9)+Math.random()*200,w:180+Math.random()*80});
// Reset acid rain
acidRainDrops=[];
acidRainActive=false; acidRainTimer=0; acidRainDuration=0;
bikeHalted=false; bikeHaltTimer=0;
level2Progress=0;
player.onBike=true; player.bikeSpeed=3;
player.x=200; player.y=groundY()-player.h-2;
cam.x=0;
document.getElementById('speedBar').classList.add('show');
document.getElementById('timeDisplay').className='bike';
document.getElementById('timeDisplay').textContent='π RIDE';
document.getElementById('phaseFill').className='bike';
document.getElementById('phaseCountdown').textContent='';
document.getElementById('bossBar').classList.remove('show');
}
function spawnAcidRain() {
acidRainActive=true; acidRainDuration=600+Math.random()*400;
showPhaseAlert('β ACID RAIN!\nSEEK COVER','bike');
showTrapWarn('β ACID RAIN β TAKE COVER OR HALT!');
document.getElementById('dangerOverlay').className='show-acid';
for(let i=0;i<150;i++) {
acidRainDrops.push({
x:cam.x+Math.random()*canvas.width, y:Math.random()*groundY(),
vy:8+Math.random()*6, vx:(Math.random()-0.5)*2, splash:false, splashTimer:0
});
}
}
function isUnderCover() {
const px=player.x;
return bikeWorld.coverZones.some(c=>px>=c.x&&px<=c.x+c.w);
}
function updateLevel2Player(dt) {
if(player.dead) return;
player.invincible=Math.max(0,player.invincible-1);
// Acid rain timer
if(!acidRainActive) {
acidRainTimer+=dt;
if(acidRainTimer>=12+Math.random()*8) { acidRainTimer=0; spawnAcidRain(); }
} else {
acidRainDuration--;
if(acidRainDuration<=0){acidRainActive=false;acidRainDrops=[];document.getElementById('dangerOverlay').className='';showStatus('ACID RAIN STOPPED');}
}
// Halt mechanic
if(bikeHalted){
bikeHaltTimer--;
player.bikeSpeed=Math.max(0,player.bikeSpeed-0.3);
if(bikeHaltTimer<=0){bikeHalted=false;showStatus('ROAD CLEAR β ACCELERATE!');}
// Still take acid damage if not under cover
if(acidRainActive&&!isUnderCover()&&player.invincible===0&&Math.random()<0.04){
damagePlayer(3,'ACID RAIN EXPOSURE');
spawnParticles(player.x+Math.random()*player.w,player.y,'#84cc16',3,{speed:1,life:15,size:2});
}
} else {
// Accelerate / brake
if(keys['KeyS']||keys['ArrowDown']){
player.bikeSpeed=Math.max(1,player.bikeSpeed-player.bikeBrake);
// Manual halt = safe from rain (stop completely)
if(player.bikeSpeed<=1&&acidRainActive){
bikeHalted=true; bikeHaltTimer=120;
showStatus(isUnderCover()?'SHELTERED β SAFE FROM ACID':'β EXPOSED! FIND COVER!');
}
} else {
player.bikeSpeed=Math.min(player.bikeMaxSpeed,player.bikeSpeed+player.bikeAccel);
}
// Acid rain damage (exposed)
if(acidRainActive&&!isUnderCover()&&player.invincible===0&&Math.random()<0.015){
damagePlayer(2,'ACID BURN'); spawnParticles(player.x+player.w/2,player.y,'#84cc16',4,{speed:1.5,life:18,size:2});
}
}
// Jump
if((keys['KeyW']||keys['ArrowUp'])&&player.onGround&&!player.bikeJumped){
player.vy=-11; player.bikeJumped=true;
}
if(keys['KeyA']||keys['ArrowLeft']){player.vx=-1;player.facing=-1;}
else if(keys['KeyD']||keys['ArrowRight']){player.vx=1;player.facing=1;}
else {player.vx=0;}
player.vy+=GRAVITY;
const gy=groundY();
player.x+=player.bikeSpeed+(keys['KeyA']?-1:keys['KeyD']?1:0);
player.y+=player.vy;
if(player.y>=gy-player.h-2){player.y=gy-player.h-2;player.vy=0;player.onGround=true;player.bikeJumped=false;}
else{player.onGround=false;}
player.x=Math.max(0,Math.min(player.x,LEVEL2_LEN-player.w));
// Pothole damage
bikeWorld.potholes.forEach(p=>{
if(player.x+player.w>p.x&&player.x<p.x+p.w&&player.onGround&&player.invincible===0&&!bikeHalted){
damagePlayer(8,'POTHOLE'); player.vy=-5;
spawnParticles(player.x+player.w/2,gy,'#6b7280',8,{speed:4});
showStatus('π₯ POTHOLE!');
}
});
// Bump
bikeWorld.roadBumps.forEach(b=>{
if(Math.abs(player.x-b.x)<20&&player.onGround&&player.bikeSpeed>4&&player.invincible===0){
player.vy=-(5+b.h*0.4); damagePlayer(4,'ROAD BUMP');
spawnParticles(b.x,gy,'#9ca3af',5,{speed:3});
}
});
// Pickups
bikeWorld.healthPacks.forEach(h=>{
if(h.collected) return;
const hy=gy-22;
if(Math.abs(h.x-player.x)<28&&Math.abs(hy-player.y)<36){h.collected=true;player.hp=Math.min(player.maxHp,player.hp+20);updateHpUI();showStatus('+20 HP');spawnParticles(h.x,hy,'#ef4444',6);}
});
bikeWorld.ammoPickups.forEach(a=>{
if(a.collected) return;
const ay=gy-22;
if(Math.abs(a.x-player.x)<28&&Math.abs(ay-player.y)<36){a.collected=true;player.ammo=Math.min(player.maxAmmo,player.ammo+6);updateAmmoUI();showStatus('+6 AMMO');spawnParticles(a.x,ay,'#3b82f6',5);}
});
// Update acid drops
for(let i=acidRainDrops.length-1;i>=0;i--){
const d=acidRainDrops[i];
if(d.splash){d.splashTimer--;if(d.splashTimer<=0)acidRainDrops.splice(i,1);continue;}
d.x+=d.vx; d.y+=d.vy;
if(d.y>=gy){d.splash=true;d.splashTimer=8;spawnParticles(d.x,gy,'#84cc16',1,{speed:1.5,life:8,size:1.5});}
if(d.x<cam.x-50||d.x>cam.x+canvas.width+50){acidRainDrops.splice(i,1);}
// Replenish
if(acidRainActive&&acidRainDrops.length<150){
acidRainDrops.push({x:cam.x+Math.random()*canvas.width,y:-20,vy:8+Math.random()*6,vx:(Math.random()-0.5)*2,splash:false,splashTimer:0});
}
}
// Speed HUD
document.getElementById('speedFill').style.width=(player.bikeSpeed/player.bikeMaxSpeed*100)+'%';
// Update distance
player.distanceTraveled+=player.bikeSpeed;
level2Progress=player.x/LEVEL2_LEN;
// Phase bar as distance
document.getElementById('phaseFill').style.width=(level2Progress*100)+'%';
if(player.x>=bikeWorld.checkpointX) triggerLevelTransition(2);
score+=0.02; document.getElementById('scoreVal').textContent=Math.floor(score);
updateHpUI(); updateAmmoUI();
}
// βββββββββββββββββββββββββββββββββββββββββββββββ
// LEVEL 3 β ARENA COMBAT + BOSS
// βββββββββββββββββββββββββββββββββββββββββββββββ
let arenaEnemies = [];
let boss = null;
let arenaPhase = 'wave1'; // wave1, wave2, bossIntro, bossFight, done
let arenaWaveTimer = 0;
let arenaEnemiesKilled = 0;
let arenaWave1Count = 6, arenaWave2Count = 10;
const ARENA_WIDTH = 2400;
function generateLevel3() {
player.onBike=false;
document.getElementById('speedBar').classList.remove('show');
arenaEnemies=[]; boss=null;
arenaPhase='wave1'; arenaWaveTimer=0; arenaEnemiesKilled=0;
player.x=300; player.y=groundY()-player.h-2;
player.vx=0; player.vy=0;
cam.x=0;
document.getElementById('timeDisplay').className='arena';
document.getElementById('timeDisplay').textContent='β ARENA';
document.getElementById('phaseFill').className='arena';
document.getElementById('phaseCountdown').textContent='';
document.getElementById('dangerOverlay').className='show-arena';
showPhaseAlert('ARENA\nβ FIGHT TO THE DEATH','arena');
spawnArenaWave1();
}
function spawnArenaWave1() {
arenaEnemies=[];
for(let i=0;i<arenaWave1Count;i++){
const side=i%2===0?1:-1;
const ex=ARENA_WIDTH/2+side*(300+i*80);
arenaEnemies.push({
x:ex, y:groundY()-44, vx:-side*1.2, w:22, h:44,
hp:4, maxHp:4, type:'soldier', armed:true,
gun:true, animTick:0, animFrame:0,
shootCooldown:80+Math.random()*60|0, shootTimer:0,
alert:true, dead:false,
});
}
showStatus('WAVE 1: ARMED SOLDIERS!');
showTrapWarn('β 6 ARMED MEN APPROACHING!');
}
function spawnArenaWave2() {
arenaEnemies=[];
for(let i=0;i<arenaWave2Count;i++){
const side=i%2===0?1:-1;
const ex=ARENA_WIDTH/2+side*(200+i*60)+Math.random()*100;
const type=i<4?'soldier':i<7?'brute':i<9?'knife':'soldier';
arenaEnemies.push({
x:ex, y:groundY()-44, vx:-side*1.4, w:type==='brute'?30:22, h:type==='brute'?52:44,
hp:type==='brute'?8:5, maxHp:type==='brute'?8:5, type,
armed:true, gun:type!=='knife'&&type!=='brute',
animTick:0, animFrame:0,
shootCooldown:60+Math.random()*50|0, shootTimer:0,
alert:true, dead:false,
});
}
showStatus('WAVE 2: ELITE FORCES!');
showTrapWarn('β 10 ELITE FIGHTERS β KNIVES, GUNS, BRUTES!');
}
function spawnBoss() {
boss = {
x:ARENA_WIDTH*0.65, y:0,
vx:-1.5, w:40, h:60,
hp:40, maxHp:40,
phase:'approach', // approach, fight, stunned
attackTimer:0, attackCooldown:80,
punchPhase:0, // 0=idle, 1=wind-up, 2=striking, 3=recovery
punchTimer:0,
stunTimer:0,
animTick:0, animFrame:0,
dashTimer:0, dashCooldown:180,
enraged:false, enrageThreshold:20,
};
boss.y=groundY()-boss.h-2;
document.getElementById('bossBar').classList.add('show');
document.getElementById('bossName').textContent='β WARLORD KADE β';
updateBossHpUI();
showPhaseAlert('FINAL BOSS\nβ WARLORD KADE','arena');
showStatus('1v1 FIST FIGHT! USE SPACE TO PUNCH!');
showTrapWarn('NO GUNS β FISTS ONLY! DODGE & PUNCH!');
}
function updateBossHpUI() {
if(!boss) return;
document.getElementById('bossHpFill').style.width=(boss.hp/boss.maxHp*100)+'%';
}
function updateLevel3Player(dt) {
if(player.dead) return;
player.invincible=Math.max(0,player.invincible-1);
if(player.stunned>0){player.stunned--;player.vx*=0.3;}
if(player.punchCooldown>0) player.punchCooldown--;
const speed=4.5;
if(player.stunned===0){
if(keys['KeyA']||keys['ArrowLeft']){player.vx=-speed;player.facing=-1;}
else if(keys['KeyD']||keys['ArrowRight']){player.vx=speed;player.facing=1;}
else {player.vx*=0.75;}
}
player.vy+=GRAVITY;
player.x+=player.vx; player.y+=player.vy;
player.x=Math.max(0,Math.min(player.x,ARENA_WIDTH-player.w));
const gy=groundY();
if(player.y>=gy-player.h-2){player.y=gy-player.h-2;player.vy=0;player.onGround=true;player.jumpCount=0;}
else{player.onGround=false;}
// Punch (SPACE in arena fist fight = melee, bullets in wave phases)
if(keys['Space']){
if(arenaPhase==='bossFight') {
// Fist fight β punch the boss
if(player.punchCooldown===0){
player.punchCooldown=35; player.shooting=true; player.shootTimer=12;
const dist=Math.abs(boss.x+boss.w/2 - (player.x+player.w/2));
if(dist<70){
const dmg = player.stunned>0?0:8+(player.punchCooldown<5?4:0);
boss.hp=Math.max(0,boss.hp-dmg);
spawnParticles(boss.x+boss.w/2,boss.y+boss.h*0.3,'#ef4444',10,{speed:5});
updateBossHpUI(); score+=30;
if(Math.random()<0.3) { boss.stunTimer=30; boss.punchPhase=3; }
showStatus('PUNCH CONNECTS! -'+dmg+' HP');
if(boss.hp<=0) { boss.hp=0; updateBossHpUI(); triggerLevelTransition(3); }
} else { showStatus('TOO FAR β GET CLOSER!'); }
}
} else if(!player.shooting&&player.shootTimer===0) {
fireBullet(); // Waves β use gun
}
}
if(player.shootTimer>0){player.shootTimer--;}else{player.shooting=false;}
if(player.reloading){player.reloadTimer--;if(player.reloadTimer<=0){player.ammo=player.maxAmmo;player.reloading=false;updateAmmoUI();showStatus('RELOADED');}}
player.animTick++; if(player.animTick%8===0)player.animFrame=(player.animFrame+1)%4;
score+=0.02; document.getElementById('scoreVal').textContent=Math.floor(score);
updateHpUI(); updateAmmoUI();
}
function updateArenaEnemies() {
const gy=groundY();
for(let i=arenaEnemies.length-1;i>=0;i--){
const e=arenaEnemies[i];
const dx=player.x-e.x, dist=Math.abs(dx);
e.vx=Math.sign(dx)*(e.type==='brute'?0.8:1.3);
e.x+=e.vx; e.animTick++; if(e.animTick%8===0)e.animFrame=(e.animFrame+1)%4;
e.x=Math.max(0,Math.min(e.x,ARENA_WIDTH-e.w));
// Enemy shooting
if(e.gun&&e.shootCooldown>0){e.shootCooldown--;}
else if(e.gun&&dist<600&&player.invincible===0){
e.shootCooldown=80+Math.random()*60|0;
// Shoot at player
const bx=e.x+(e.vx>0?e.w:0);
const by=e.y+e.h*0.4;
bullets.push({x:bx,y:by,vx:Math.sign(dx)*14,vy:0,life:55,underground:false,enemy:true});
}
// Melee
if(dist<32&&Math.abs(e.y-player.y)<50&&player.invincible===0){
damagePlayer(e.type==='brute'?16:e.type==='knife'?12:7,e.type+' ATTACK');
spawnParticles(player.x+player.w/2,player.y,'#ef4444',8);
}
}
// Enemy bullets hitting player
for(let bi=bullets.length-1;bi>=0;bi--){
const b=bullets[bi];
if(b.enemy){
b.x+=b.vx; b.y+=b.vy; b.life--;
if(b.life<=0){bullets.splice(bi,1);continue;}
if(b.x>player.x&&b.x<player.x+player.w&&b.y>player.y&&b.y<player.y+player.h&&player.invincible===0){
damagePlayer(10,'ENEMY BULLET'); bullets.splice(bi,1);
}
}
}
}
function updateArenaPlayerBullets() {
for(let bi=bullets.length-1;bi>=0;bi--){
const b=bullets[bi];
if(b.enemy) continue;
b.x+=b.vx; b.y+=b.vy; b.life--;
if(b.life<=0){bullets.splice(bi,1);continue;}
let hit=false;
for(let ei=arenaEnemies.length-1;ei>=0;ei--){
const e=arenaEnemies[ei];
if(b.x>e.x&&b.x<e.x+e.w&&b.y>e.y&&b.y<e.y+e.h){
e.hp--; spawnParticles(b.x,b.y,'#ef4444',6); hit=true;
if(e.hp<=0){
spawnParticles(e.x+e.w/2,e.y+e.h/2,'#f97316',14);
arenaEnemies.splice(ei,1); kills++; score+=80;
arenaEnemiesKilled++;
showStatus('DOWN! '+(arenaEnemies.length)+' REMAIN');
}
break;
}
}
if(hit) bullets.splice(bi,1);
}
}
function updateBoss(dt) {
if(!boss||boss.hp<=0) return;
const gy=groundY();
if(boss.y<gy-boss.h-2){boss.vy=(boss.vy||0)+GRAVITY;boss.y+=boss.vy;}
else{boss.y=gy-boss.h-2;boss.vy=0;}
if(boss.hp<=boss.enrageThreshold&&!boss.enraged){
boss.enraged=true;
showStatus('β BOSS ENRAGED! FASTER ATTACKS!');
spawnParticles(boss.x+boss.w/2,boss.y,'#ef4444',30,{speed:8,life:60});
}
const dx=player.x-boss.x, dist=Math.abs(dx);
const spd=boss.enraged?2.2:1.6;
if(boss.stunTimer>0){boss.stunTimer--;return;}
// Approach / movement
boss.vx=Math.sign(dx)*spd;
boss.x+=boss.vx;
boss.x=Math.max(0,Math.min(boss.x,ARENA_WIDTH-boss.w));
boss.animTick++; if(boss.animTick%6===0)boss.animFrame=(boss.animFrame+1)%4;
// Dash attack
boss.dashTimer++;
if(boss.dashTimer>=(boss.enraged?120:180)){
boss.dashTimer=0;
boss.x+=Math.sign(dx)*120; // dash
spawnParticles(boss.x+boss.w/2,boss.y+boss.h/2,'#ef4444',12,{speed:6,size:4});
if(dist<120&&player.invincible===0){damagePlayer(20,'BOSS DASH');showStatus('π₯ DASH ATTACK!');}
}
// Punch attack
boss.attackTimer++;
const atkCd=boss.enraged?50:80;
if(boss.attackTimer>=atkCd&&dist<90){
boss.attackTimer=0;
if(player.invincible===0){
const dmg=boss.enraged?18:12;
damagePlayer(dmg,'BOSS PUNCH');
player.vx=Math.sign(player.x-boss.x)*8; player.vy=-7;
spawnParticles(player.x+player.w/2,player.y,'#dc2626',14,{speed:6});
showStatus('π₯ BOSS PUNCH! -'+dmg+' HP');
}
}
}
function updateArenaPhase(dt) {
arenaWaveTimer+=dt;
if(arenaPhase==='wave1'){
if(arenaEnemies.length===0&&arenaEnemiesKilled>=arenaWave1Count){
arenaPhase='wave2'; arenaEnemiesKilled=0; spawnArenaWave2();
player.hp=Math.min(player.maxHp,player.hp+30); updateHpUI();
showStatus('+30 HP BONUS β WAVE 2 INCOMING!');
}
} else if(arenaPhase==='wave2'){
if(arenaEnemies.length===0&&arenaEnemiesKilled>=arenaWave2Count){
arenaPhase='bossIntro'; arenaEnemiesKilled=0;
player.hp=Math.min(player.maxHp,player.hp+20); updateHpUI(); player.ammo=player.maxAmmo; updateAmmoUI();
showStatus('ALL CLEARED! BOSS APPROACHING...');
setTimeout(()=>{arenaPhase='bossFight';spawnBoss();},3000);
}
}
// Update phase bar
if(arenaPhase==='wave1') document.getElementById('phaseFill').style.width=(arenaEnemiesKilled/arenaWave1Count*100)+'%';
else if(arenaPhase==='wave2') document.getElementById('phaseFill').style.width=(arenaEnemiesKilled/arenaWave2Count*100)+'%';
else if(arenaPhase==='bossFight'&&boss) document.getElementById('phaseFill').style.width=((1-boss.hp/boss.maxHp)*100)+'%';
}
// βββββββββββββββββββββββββββββββββββββββββββββββ
// LEVEL TRANSITIONS
// βββββββββββββββββββββββββββββββββββββββββββββββ
function triggerLevelTransition(completedLevel) {
gameState='levelTrans';
document.getElementById('dangerOverlay').className='';
const el=document.getElementById('lvlTransition');
if(completedLevel===1){
document.getElementById('lvtBadge').textContent='LEVEL 1 COMPLETE';
document.getElementById('lvtTitle').textContent='STREETS SURVIVED';
document.getElementById('lvtTitle').style.color='#22d3ee';
document.getElementById('lvtSub').innerHTML='You endured the day/night cycles and the sewers.<br>A motorcycle waits at the checkpoint ahead.<br><span style="color:#ffd700">Ride on. The city still hunts you.</span>';
document.getElementById('lvtBtn').textContent='GET ON THE BIKE β';
} else if(completedLevel===2){
document.getElementById('lvtBadge').textContent='LEVEL 2 COMPLETE';
document.getElementById('lvtTitle').textContent='RIDE COMPLETE';
document.getElementById('lvtTitle').style.color='#a3e635';
document.getElementById('lvtSub').innerHTML='You outran the acid rain and survived the bad roads.<br>The bike breaks down at the arena entrance.<br><span style="color:#ef4444">The final fight awaits. No more guns for long.</span>';
document.getElementById('lvtBtn').textContent='ENTER THE ARENA β';
} else if(completedLevel===3){
// Season end
gameState='seasonEnd';
document.getElementById('seScore').textContent=Math.floor(score);
document.getElementById('seKills').textContent=kills;
document.getElementById('seDist').textContent=Math.floor(player.distanceTraveled/100)+'m';
const seEl=document.getElementById('seasonEnd');
seEl.classList.add('show');
// Season end canvas effect
launchSeasonEndParticles();
return;
}
el.classList.add('show');
}
function launchSeasonEndParticles() {
const colors=['#ffd700','#c4b5fd','#22d3ee','#ef4444','#4ade80','#fbbf24'];
const int=setInterval(()=>{
if(gameState!=='seasonEnd'){clearInterval(int);return;}
for(let i=0;i<5;i++){
const x=Math.random()*canvas.width, y=Math.random()*canvas.height*0.5;
spawnParticles(x,y,colors[Math.floor(Math.random()*colors.length)],4,{speed:4+Math.random()*4,life:80+Math.random()*60,size:3+Math.random()*3});
}
},60);
}
document.getElementById('lvtBtn').addEventListener('click',()=>{
const prevLevel=gameLevel;
gameLevel++;
document.getElementById('lvlTransition').classList.remove('show');
gameState='playing';
if(gameLevel===2){
generateLevel2();
updateStageDisplay();
} else if(gameLevel===3){
generateLevel3();
updateStageDisplay();
}
});
function updateStageDisplay() {
document.getElementById('stageVal').textContent=gameLevel===1?(dayCount+1+'-1'):(gameLevel===2?'2-BIKE':'3-ARENA');
const badges=['LEVEL 1','LEVEL 2 β BIKE','LEVEL 3 β ARENA'];
document.getElementById('levelBadge').textContent=badges[gameLevel-1]||'LEVEL '+gameLevel;
}
// βββββββββββββββββββββββββββββββββββββββββββββββ
// HANDLE INPUT
// βββββββββββββββββββββββββββββββββββββββββββββββ
function handleAction(code) {
if(player.stunned>0&&code!=='Space') return;
if(gameLevel===1){
if((code==='KeyW'||code==='ArrowUp')&&!player.crawling){
if(player.jumpCount<player.maxJumps&&!player.inManhole){player.vy=-12;player.jumpCount++;player.onGround=false;}
}
if(code==='Space') fireBullet();
if(code==='KeyR'&&!player.reloading){player.reloading=true;player.reloadTimer=90;showStatus('RELOADING...');}
if(code==='KeyE') tryManhole();
} else if(gameLevel===2){
if((code==='KeyW'||code==='ArrowUp')&&player.onGround){player.vy=-12;player.onGround=false;}
if(code==='Space') fireBullet();
if(code==='KeyR'&&!player.reloading){player.reloading=true;player.reloadTimer=90;showStatus('RELOADING...');}
} else if(gameLevel===3){
if((code==='KeyW'||code==='ArrowUp')&&player.onGround&&player.jumpCount<2){player.vy=-12;player.jumpCount++;player.onGround=false;}
if(code==='KeyR'&&!player.reloading&&arenaPhase!=='bossFight'){player.reloading=true;player.reloadTimer=90;showStatus('RELOADING...');}
}
}
// βββββββββββββββββββββββββββββββββββββββββββββββ
// GAME FLOW
// βββββββββββββββββββββββββββββββββββββββββββββββ
function startGame() {
score=0; kills=0; dayCount=0; gameLevel=1;
phase='day'; phaseTimer=DAY_DUR; phaseDuration=DAY_DUR;
bullets=[]; particles=[]; enemies=[];
generateLevel1(); spawnPlayer(); updateCamera();
document.getElementById('startScreen').classList.add('hidden');
document.getElementById('gameOverScreen').classList.add('hidden');
document.getElementById('seasonEnd').classList.remove('show');
document.getElementById('lvlTransition').classList.remove('show');
document.getElementById('timeDisplay').className='day';
document.getElementById('timeDisplay').textContent='β DAY';
document.getElementById('phaseFill').className='day';
document.getElementById('bossBar').classList.remove('show');
document.getElementById('speedBar').classList.remove('show');
document.getElementById('underground').classList.remove('show');
document.getElementById('dangerOverlay').className='';
document.getElementById('phaseCountdown').textContent=DAY_DUR+'s';
updateStageDisplay();
updateHpUI(); updateAmmoUI();
gameState='playing';
}
function doGameOver() {
gameState='gameover';
document.getElementById('goScore').textContent=Math.floor(score);
document.getElementById('goStage').textContent=gameLevel;
document.getElementById('goKills').textContent=kills;
document.getElementById('goDistance').textContent=Math.floor(player.distanceTraveled/100)+'m';
document.getElementById('goCause').textContent=player.deathCause?'CAUSE: '+player.deathCause:'';
setTimeout(()=>document.getElementById('gameOverScreen').classList.remove('hidden'),1000);
}
// βββββββββββββββββββββββββββββββββββββββββββββββ
// DRAW β SHARED SKY
// βββββββββββββββββββββββββββββββββββββββββββββββ
function drawSky() {
const w=canvas.width, gy=groundY();
let skyGrad=ctx.createLinearGradient(0,0,0,gy);
if(gameLevel===1){
if(phase==='day'){const t=phaseTimer/DAY_DUR;if(t>0.85||t<0.15){skyGrad.addColorStop(0,'#1a0a2e');skyGrad.addColorStop(0.5,'#7c2d12');skyGrad.addColorStop(1,'#f97316');}else{skyGrad.addColorStop(0,'#075985');skyGrad.addColorStop(0.6,'#0c6490');skyGrad.addColorStop(1,'#7dd3fc');}}
else if(phase==='night'){skyGrad.addColorStop(0,'#020617');skyGrad.addColorStop(0.5,'#0f0a2e');skyGrad.addColorStop(1,'#1e1b4b');}
else{skyGrad.addColorStop(0,'#1c0a00');skyGrad.addColorStop(0.4,'#7c2d12');skyGrad.addColorStop(1,'#f97316');}
} else if(gameLevel===2){
// Dark stormy sky for bike level
if(acidRainActive){skyGrad.addColorStop(0,'#0a1a00');skyGrad.addColorStop(0.5,'#1a2e00');skyGrad.addColorStop(1,'#2a3d00');}
else{skyGrad.addColorStop(0,'#0c1a2e');skyGrad.addColorStop(0.5,'#1a2d4a');skyGrad.addColorStop(1,'#2d3d5a');}
} else {
// Arena β red-lit underground arena