-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1425 lines (1213 loc) · 61.2 KB
/
index.html
File metadata and controls
1425 lines (1213 loc) · 61.2 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">
<title>ROUNDS</title>
<script src="https://www.gstatic.com/firebasejs/9.22.0/firebase-app-compat.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.22.0/firebase-database-compat.js"></script>
<script>
const firebaseConfig = {
apiKey: "AIzaSyB_t0aqBd6HG-F2ncaarCQyApQoFN9yR48",
authDomain: "rounds-mc.firebaseapp.com",
databaseURL: "https://rounds-mc-default-rtdb.firebaseio.com",
projectId: "rounds-mc",
storageBucket: "rounds-mc.firebasestorage.app",
messagingSenderId: "594850049392",
appId: "1:594850049392:web:8b883a924fe2428113f3b5"
};
firebase.initializeApp(firebaseConfig);
const database = firebase.database();
</script>
<style>
@import url('https://fonts.googleapis.com/css2?family=Bebas+Neue&family=Inter:wght@400;600;700&display=swap');
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
background: #0a0a0f;
font-family: 'Inter', sans-serif;
overflow: hidden;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
#gameContainer { position: relative; width: 1200px; height: 700px; }
canvas {
display: block;
border-radius: 8px;
box-shadow: 0 0 60px rgba(255, 100, 50, 0.15);
cursor: crosshair;
}
#mainMenu, #onlineMenu, #lobby, #customizeMenu {
position: absolute;
top: 0; left: 0;
width: 100%; height: 100%;
background: linear-gradient(135deg, #0a0a0f 0%, #1a1a2e 50%, #0a0a0f 100%);
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
z-index: 100;
}
.hidden { display: none !important; }
.menu-title {
font-family: 'Bebas Neue', sans-serif;
font-size: 140px;
color: #fff;
text-shadow: 0 0 40px rgba(255, 100, 50, 0.8), 0 0 80px rgba(255, 50, 50, 0.5);
letter-spacing: 20px;
margin-bottom: 60px;
animation: titlePulse 2s ease-in-out infinite;
}
@keyframes titlePulse {
0%, 100% { text-shadow: 0 0 40px rgba(255, 100, 50, 0.8), 0 0 80px rgba(255, 50, 50, 0.5); }
50% { text-shadow: 0 0 60px rgba(255, 100, 50, 1), 0 0 120px rgba(255, 50, 50, 0.7); }
}
.menu-btn {
font-family: 'Bebas Neue', sans-serif;
font-size: 36px;
padding: 18px 80px;
margin: 12px;
background: linear-gradient(180deg, #ff6b35 0%, #e63946 100%);
border: none;
border-radius: 8px;
color: white;
cursor: pointer;
letter-spacing: 4px;
transition: all 0.2s ease;
box-shadow: 0 6px 0 #a62639, 0 10px 30px rgba(230, 57, 70, 0.4);
}
.menu-btn:hover { transform: translateY(-4px); box-shadow: 0 10px 0 #a62639, 0 16px 40px rgba(230, 57, 70, 0.6); }
.menu-btn:active { transform: translateY(4px); box-shadow: 0 2px 0 #a62639, 0 4px 20px rgba(230, 57, 70, 0.4); }
.menu-btn.secondary { background: linear-gradient(180deg, #4361ee 0%, #3a0ca3 100%); box-shadow: 0 6px 0 #240046, 0 10px 30px rgba(67, 97, 238, 0.4); }
.menu-btn.online { background: linear-gradient(180deg, #10b981 0%, #059669 100%); box-shadow: 0 6px 0 #047857, 0 10px 30px rgba(16, 185, 129, 0.4); }
.menu-btn.customize { background: linear-gradient(180deg, #f59e0b 0%, #d97706 100%); box-shadow: 0 6px 0 #b45309, 0 10px 30px rgba(245, 158, 11, 0.4); }
.menu-btn.back-btn { background: linear-gradient(180deg, #6b7280 0%, #4b5563 100%); box-shadow: 0 6px 0 #374151, 0 10px 30px rgba(107, 114, 128, 0.4); margin-top: 30px; padding: 12px 50px; font-size: 24px; }
.room-input {
font-family: 'Bebas Neue', sans-serif;
font-size: 36px;
padding: 15px 30px;
background: #1a1a2e;
border: 3px solid #4361ee;
border-radius: 8px;
color: white;
text-align: center;
letter-spacing: 8px;
width: 220px;
outline: none;
text-transform: uppercase;
}
.room-input:focus { border-color: #6b8cff; box-shadow: 0 0 20px rgba(67, 97, 238, 0.5); }
.room-code {
font-family: 'Bebas Neue', sans-serif;
font-size: 72px;
color: #10b981;
letter-spacing: 15px;
text-shadow: 0 0 30px rgba(16, 185, 129, 0.6);
margin: 20px 0;
}
.lobby-status { color: #888; font-size: 18px; margin-bottom: 30px; }
.lobby-players { display: flex; gap: 60px; margin-bottom: 40px; }
.lobby-player {
display: flex;
flex-direction: column;
align-items: center;
gap: 10px;
padding: 20px 40px;
background: rgba(255, 255, 255, 0.05);
border-radius: 12px;
border: 2px solid #333;
}
.lobby-player.p1 { border-color: #ff6b35; }
.lobby-player.p2 { border-color: #4361ee; }
.lobby-player.connected { background: rgba(255, 255, 255, 0.1); }
.lobby-player-icon { width: 60px; height: 60px; border-radius: 50%; background: #333; }
.lobby-player.p1 .lobby-player-icon { background: #ff6b35; }
.lobby-player.p2.connected .lobby-player-icon { background: #4361ee; }
.lobby-player span { font-family: 'Bebas Neue', sans-serif; font-size: 20px; letter-spacing: 2px; color: #fff; }
.ready-status { font-size: 14px !important; color: #888 !important; }
.ready-status.ready { color: #10b981 !important; }
.menu-controls { margin-top: 50px; color: #666; font-size: 14px; text-align: center; line-height: 2; }
.menu-controls span { color: #ff6b35; font-weight: 600; }
#cardSelect {
position: absolute;
top: 0; left: 0;
width: 100%; height: 100%;
background: rgba(10, 10, 15, 0.95);
display: none;
flex-direction: column;
justify-content: center;
align-items: center;
z-index: 90;
}
#cardSelect.active { display: flex; }
.card-title { font-family: 'Bebas Neue', sans-serif; font-size: 48px; margin-bottom: 10px; letter-spacing: 6px; }
.card-title.p1 { color: #ff6b35; }
.card-title.p2 { color: #4361ee; }
.card-subtitle { color: #888; margin-bottom: 40px; font-size: 18px; }
.cards-container { display: flex; gap: 30px; perspective: 1000px; }
.card {
width: 220px; height: 300px;
background: linear-gradient(180deg, #1a1a2e 0%, #16213e 100%);
border-radius: 16px;
padding: 20px;
cursor: pointer;
transition: all 0.3s ease;
border: 3px solid #333;
display: flex;
flex-direction: column;
position: relative;
overflow: hidden;
}
.card::before {
content: '';
position: absolute;
top: 0; left: 0; right: 0;
height: 6px;
background: linear-gradient(90deg, #ff6b35, #e63946);
opacity: 0;
transition: opacity 0.3s ease;
}
.card:hover { transform: translateY(-10px) rotateX(5deg); border-color: #ff6b35; box-shadow: 0 20px 40px rgba(255, 107, 53, 0.3); }
.card:hover::before { opacity: 1; }
.card-name { font-family: 'Bebas Neue', sans-serif; font-size: 26px; color: #fff; letter-spacing: 2px; margin-bottom: 10px; }
.card-desc { color: #aaa; font-size: 13px; line-height: 1.5; flex: 1; }
.card-stats { margin-top: 10px; padding-top: 10px; border-top: 1px solid #333; }
.stat { font-size: 11px; margin: 3px 0; }
.stat.positive { color: #4ade80; }
.stat.negative { color: #f87171; }
#hud { position: absolute; top: 0; left: 0; width: 100%; height: 100%; pointer-events: none; display: none; }
#hud.active { display: block; }
.player-hud { position: absolute; top: 20px; padding: 15px 25px; background: rgba(0, 0, 0, 0.7); border-radius: 12px; backdrop-filter: blur(10px); }
.player-hud.p1 { left: 20px; border-left: 4px solid #ff6b35; }
.player-hud.p2 { right: 20px; border-right: 4px solid #4361ee; }
.player-name { font-family: 'Bebas Neue', sans-serif; font-size: 24px; letter-spacing: 3px; margin-bottom: 8px; }
.player-name.p1 { color: #ff6b35; }
.player-name.p2 { color: #4361ee; }
.health-bar { width: 200px; height: 12px; background: #333; border-radius: 6px; overflow: hidden; margin-bottom: 8px; }
.health-fill { height: 100%; transition: width 0.2s ease; border-radius: 6px; }
.health-fill.p1 { background: linear-gradient(90deg, #ff6b35, #e63946); }
.health-fill.p2 { background: linear-gradient(90deg, #4361ee, #3a0ca3); }
.ammo-display { font-size: 14px; color: #888; }
.ammo-display span { color: #fff; font-weight: 600; }
.score-display { position: absolute; top: 20px; left: 50%; transform: translateX(-50%); display: flex; align-items: center; gap: 20px; font-family: 'Bebas Neue', sans-serif; font-size: 48px; }
.score-p1 { color: #ff6b35; }
.score-p2 { color: #4361ee; }
.score-divider { color: #444; }
#roundAnnounce {
position: absolute;
top: 50%; left: 50%;
transform: translate(-50%, -50%);
font-family: 'Bebas Neue', sans-serif;
font-size: 80px;
color: #fff;
text-shadow: 0 0 40px rgba(255, 255, 255, 0.5);
letter-spacing: 10px;
opacity: 0;
transition: opacity 0.3s ease;
pointer-events: none;
}
#roundAnnounce.show { opacity: 1; }
#winnerScreen {
position: absolute;
top: 0; left: 0;
width: 100%; height: 100%;
background: rgba(10, 10, 15, 0.95);
display: none;
flex-direction: column;
justify-content: center;
align-items: center;
z-index: 95;
}
#winnerScreen.active { display: flex; }
.winner-text { font-family: 'Bebas Neue', sans-serif; font-size: 100px; letter-spacing: 10px; margin-bottom: 40px; }
.winner-text.p1 { color: #ff6b35; text-shadow: 0 0 60px rgba(255, 107, 53, 0.8); }
.winner-text.p2 { color: #4361ee; text-shadow: 0 0 60px rgba(67, 97, 238, 0.8); }
.ping-display { position: absolute; bottom: 10px; right: 10px; color: #666; font-size: 12px; font-family: monospace; }
</style>
</head>
<body>
<div id="gameContainer">
<canvas id="gameCanvas" width="1200" height="700"></canvas>
<div id="mainMenu">
<div class="menu-title">ROUNDS</div>
<button class="menu-btn" onclick="startGame('local')">LOCAL MATCH</button>
<button class="menu-btn secondary" onclick="startGame('ai')">VS AI</button>
<button class="menu-btn online" onclick="showOnlineMenu()">ONLINE</button>
<div class="menu-controls">
<strong>PLAYER 1 (Orange)</strong><br>
<span>WASD</span> - Move | <span>Left Click</span> - Shoot | <span>Right Click / F</span> - Block<br><br>
<strong>PLAYER 2 (Blue)</strong><br>
<span>Arrow Keys</span> - Move | <span>.</span> - Shoot | <span>/</span> - Block
</div>
</div>
<div id="onlineMenu" class="hidden">
<div class="menu-title" style="font-size: 80px;">ONLINE</div>
<button class="menu-btn" onclick="createRoom()">CREATE ROOM</button>
<div style="display: flex; flex-direction: column; align-items: center; gap: 15px; margin: 20px 0;">
<input type="text" id="roomCodeInput" class="room-input" placeholder="CODE" maxlength="4">
<button class="menu-btn secondary" onclick="joinRoom()">JOIN ROOM</button>
</div>
<button class="menu-btn back-btn" onclick="hideOnlineMenu()">BACK</button>
</div>
<div id="lobby" class="hidden">
<div class="menu-title" style="font-size: 60px;">ROOM</div>
<div class="room-code" id="displayRoomCode">XXXX</div>
<div class="lobby-status" id="lobbyStatus">Waiting for opponent...</div>
<div class="lobby-players">
<div class="lobby-player p1 connected" id="lobbyP1">
<div class="lobby-player-icon"></div>
<span>PLAYER 1</span>
<span class="ready-status" id="p1Ready">NOT READY</span>
</div>
<div class="lobby-player p2" id="lobbyP2">
<div class="lobby-player-icon"></div>
<span>PLAYER 2</span>
<span class="ready-status" id="p2Ready">WAITING...</span>
</div>
</div>
<button class="menu-btn" id="readyBtn" onclick="toggleReady()">READY</button>
<button class="menu-btn back-btn" onclick="leaveLobby()">LEAVE</button>
</div>
<div id="cardSelect">
<div class="card-title p1" id="cardPlayerTitle">PLAYER 1 - PICK A CARD</div>
<div class="card-subtitle">Choose your upgrade</div>
<div class="cards-container" id="cardsContainer"></div>
</div>
<div id="hud">
<div class="player-hud p1">
<div class="player-name p1">PLAYER 1</div>
<div class="health-bar"><div class="health-fill p1" id="health1"></div></div>
<div class="ammo-display">AMMO: <span id="ammo1">3/3</span></div>
</div>
<div class="player-hud p2">
<div class="player-name p2">PLAYER 2</div>
<div class="health-bar"><div class="health-fill p2" id="health2"></div></div>
<div class="ammo-display">AMMO: <span id="ammo2">3/3</span></div>
</div>
<div class="score-display">
<span class="score-p1" id="score1">0</span>
<span class="score-divider">-</span>
<span class="score-p2" id="score2">0</span>
</div>
<div class="ping-display" id="pingDisplay"></div>
</div>
<div id="roundAnnounce"></div>
<div id="winnerScreen">
<div class="winner-text" id="winnerText">PLAYER 1 WINS</div>
<button class="menu-btn" onclick="returnToMenu()">MAIN MENU</button>
<button class="menu-btn secondary" onclick="rematch()">REMATCH</button>
</div>
</div>
<script>
// ==================== CANVAS & CONSTANTS ====================
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const GRAVITY = 0.5, FRICTION = 0.92;
// ==================== GAME STATE ====================
let gameMode = 'local', gameState = 'menu', currentRound = 1;
let player1, player2, bullets = [], explosions = [], particles = [];
let keys = {}, mouse = { x: 600, y: 350 };
// ==================== ONLINE STATE (OPTIMIZED) ====================
let roomCode = null, playerId = null, roomRef = null, isHost = false, isReady = false;
let lastSyncTime = 0, pingStart = 0, currentPing = 0;
const SYNC_RATE = 50; // 20 updates/sec instead of 30
// Interpolation for smooth opponent movement
let opponentState = { x: 0, y: 0, vx: 0, vy: 0, timestamp: 0 };
let opponentPrevState = { x: 0, y: 0, vx: 0, vy: 0, timestamp: 0 };
const INTERPOLATION_DELAY = 100; // ms
// Input buffer for network - only send changes
let lastSentInput = { left: false, right: false, up: false };
let pendingBullets = [];
// ==================== MAPS ====================
const MAPS = [
{ name: "Classic", bg: '#0a0a0f', plat: '#1a1a2e', hi: '#2a2a4e', platforms: [
{x:0,y:650,w:1200,h:50},{x:100,y:520,w:200,h:20},{x:900,y:520,w:200,h:20},
{x:450,y:450,w:300,h:20},{x:50,y:350,w:150,h:20},{x:1000,y:350,w:150,h:20},
{x:500,y:280,w:200,h:20},{x:200,y:200,w:150,h:20},{x:850,y:200,w:150,h:20}
]},
{ name: "Neon", bg: '#0d0221', plat: '#541388', hi: '#ff00ff', platforms: [
{x:0,y:650,w:1200,h:50},{x:50,y:500,w:150,h:15},{x:250,y:400,w:150,h:15},
{x:500,y:550,w:200,h:15},{x:800,y:400,w:150,h:15},{x:1000,y:500,w:150,h:15},
{x:400,y:300,w:100,h:15},{x:700,y:300,w:100,h:15},{x:550,y:180,w:100,h:15}
]},
{ name: "Lava", bg: '#1a0a0a', plat: '#4a1a1a', hi: '#ff4400', platforms: [
{x:0,y:650,w:400,h:50},{x:800,y:650,w:400,h:50},{x:450,y:600,w:300,h:20},
{x:100,y:480,w:180,h:20},{x:920,y:480,w:180,h:20},{x:350,y:400,w:150,h:20},
{x:700,y:400,w:150,h:20},{x:500,y:300,w:200,h:20},{x:550,y:150,w:100,h:20}
]}
];
let currentMap = MAPS[0], platforms = currentMap.platforms;
// ==================== CARDS ====================
const CARDS = [
{ name: "Big Bullet", desc: "Bigger bullets", stats: ["+50% size","+0.25s reload"], effect: p => { p.bulletSize *= 1.5; p.reloadTime += 0.25; }},
{ name: "Barrage", desc: "Fire many bullets", stats: ["+4 bullets","-70% dmg"], effect: p => { p.bulletsPerShot += 4; p.maxAmmo += 5; p.damage *= 0.3; }},
{ name: "Bouncy", desc: "Bullets bounce", stats: ["+2 bounces"], effect: p => { p.bulletBounces += 2; p.damage *= 1.25; }},
{ name: "Homing", desc: "Seek enemies", stats: ["Homing","-25% dmg"], effect: p => { p.homing = true; p.damage *= 0.75; }},
{ name: "Explosive", desc: "Explode on hit", stats: ["Explosive"], effect: p => { p.explosive = true; p.fireRate *= 1.5; }},
{ name: "Quick Reload", desc: "Fast reload", stats: ["-70% reload"], effect: p => { p.reloadTime *= 0.3; }},
{ name: "Tank", desc: "More HP", stats: ["+100% HP"], effect: p => { p.maxHealth *= 2; p.health = p.maxHealth; }},
{ name: "Glass Cannon", desc: "High risk", stats: ["+100% dmg","-50% HP"], effect: p => { p.damage *= 2; p.maxHealth *= 0.5; p.health = Math.min(p.health, p.maxHealth); }},
{ name: "Spray", desc: "Bullet hose", stats: ["+500% fire rate","-75% dmg"], effect: p => { p.fireRate *= 0.2; p.maxAmmo += 12; p.damage *= 0.25; }},
{ name: "Fastball", desc: "Fast bullets", stats: ["+150% speed"], effect: p => { p.bulletSpeed *= 2.5; }},
{ name: "Buckshot", desc: "Shotgun", stats: ["+4 bullets","spread"], effect: p => { p.bulletsPerShot += 4; p.spread = 0.4; p.damage *= 0.4; }},
{ name: "Leech", desc: "Life steal", stats: ["+50% steal"], effect: p => { p.lifeSteal = 0.5; p.maxHealth *= 1.3; p.health = p.maxHealth; }}
];
// ==================== PLAYER CLASS ====================
class Player {
constructor(x, y, color, controls, id) {
this.id = id; this.x = x; this.y = y; this.spawnX = x; this.spawnY = y;
this.vx = 0; this.vy = 0; this.radius = 25; this.color = color; this.controls = controls;
this.maxHealth = 100; this.health = 100; this.damage = 25; this.bulletSpeed = 15;
this.bulletSize = 8; this.fireRate = 0.3; this.reloadTime = 1.5; this.maxAmmo = 3;
this.ammo = 3; this.bulletsPerShot = 1; this.bulletBounces = 0; this.spread = 0.05;
this.blockCooldown = 2; this.homing = false; this.explosive = false; this.lifeSteal = 0;
this.grounded = false; this.facingRight = id === 1; this.lastShot = 0;
this.reloading = false; this.reloadStart = 0; this.blocking = false;
this.blockTime = 0; this.lastBlock = -10; this.cards = []; this.score = 0;
// Leg physics (rendered locally, not synced)
this.legLength = 35;
this.leftLeg = { angle: 0, targetAngle: 0, angularVel: 0 };
this.rightLeg = { angle: 0, targetAngle: 0, angularVel: 0 };
this.legPhase = 0;
this.bodyShake = { x: 0, y: 0, targetX: 0, targetY: 0 };
this.lastVx = 0;
}
reset() {
this.x = this.spawnX; this.y = this.spawnY; this.vx = 0; this.vy = 0;
this.health = this.maxHealth; this.ammo = this.maxAmmo; this.reloading = false;
this.blocking = false; this.lastShot = 0; this.lastBlock = -10;
}
fullReset() {
this.radius = 25; this.maxHealth = 100; this.health = 100; this.damage = 25;
this.bulletSpeed = 15; this.bulletSize = 8; this.fireRate = 0.3; this.reloadTime = 1.5;
this.maxAmmo = 3; this.ammo = 3; this.bulletsPerShot = 1; this.bulletBounces = 0;
this.spread = 0.05; this.blockCooldown = 2; this.homing = false; this.explosive = false;
this.lifeSteal = 0; this.cards = []; this.score = 0; this.reset();
}
update(inputKeys, other, now) {
const moveSpeed = 0.6, jumpForce = -14;
if (inputKeys[this.controls.left]) { this.vx -= moveSpeed; this.facingRight = false; }
if (inputKeys[this.controls.right]) { this.vx += moveSpeed; this.facingRight = true; }
if (inputKeys[this.controls.up] && this.grounded) { this.vy = jumpForce; this.grounded = false; }
this.vy += GRAVITY;
this.vx *= FRICTION;
this.x += this.vx;
this.y += this.vy;
// Collision
this.grounded = false;
const feetOffset = this.radius * 0.5 + 35;
for (const p of platforms) {
if (this.x + this.radius > p.x && this.x - this.radius < p.x + p.w &&
this.y + feetOffset > p.y && this.y - this.radius < p.y + p.h) {
if (this.vy > 0 && this.y + feetOffset - this.vy <= p.y + 5) {
this.y = p.y - feetOffset; this.vy = 0; this.grounded = true;
} else if (this.vy < 0 && this.y - this.radius - this.vy >= p.y + p.h - 5) {
this.y = p.y + p.h + this.radius; this.vy = 0;
} else if (this.vx > 0) { this.x = p.x - this.radius; this.vx = 0; }
else if (this.vx < 0) { this.x = p.x + p.w + this.radius; this.vx = 0; }
}
}
// Bounds
if (this.x < this.radius) { this.x = this.radius; this.vx = 0; }
if (this.x > canvas.width - this.radius) { this.x = canvas.width - this.radius; this.vx = 0; }
if (this.y < this.radius) { this.y = this.radius; this.vy = 0; }
if (this.y + feetOffset > canvas.height) { this.y = canvas.height - feetOffset; this.vy = 0; this.grounded = true; }
// Reload
if (this.reloading && now - this.reloadStart >= this.reloadTime * 1000) {
this.ammo = this.maxAmmo; this.reloading = false;
}
if (this.ammo <= 0 && !this.reloading) { this.reloading = true; this.reloadStart = now; }
if (this.blocking && now - this.blockTime > 200) this.blocking = false;
// Leg physics (local animation - not networked)
const speed = Math.abs(this.vx);
const moving = speed > 0.5;
if (moving && this.grounded) {
this.legPhase += speed * 0.15;
this.leftLeg.targetAngle = Math.sin(this.legPhase) * 0.6;
this.rightLeg.targetAngle = Math.sin(this.legPhase + Math.PI) * 0.6;
} else if (!this.grounded) {
this.leftLeg.targetAngle = 0.3 + this.vy * 0.02;
this.rightLeg.targetAngle = 0.2 + this.vy * 0.02;
} else {
this.leftLeg.targetAngle = 0.1;
this.rightLeg.targetAngle = -0.1;
}
// Leg spring physics
const legSpring = 0.3, legDamping = 0.7;
this.leftLeg.angularVel += (this.leftLeg.targetAngle - this.leftLeg.angle) * legSpring;
this.leftLeg.angularVel *= legDamping;
this.leftLeg.angle += this.leftLeg.angularVel;
this.rightLeg.angularVel += (this.rightLeg.targetAngle - this.rightLeg.angle) * legSpring;
this.rightLeg.angularVel *= legDamping;
this.rightLeg.angle += this.rightLeg.angularVel;
// Body shake based on acceleration
const accel = this.vx - this.lastVx;
this.lastVx = this.vx;
this.bodyShake.targetX = -accel * 2 + (moving ? Math.sin(this.legPhase * 2) * speed * 0.3 : 0);
this.bodyShake.targetY = moving && this.grounded ? Math.abs(Math.sin(this.legPhase)) * 2 : 0;
const shakeSpring = 0.4, shakeDamping = 0.75;
this.bodyShake.x += (this.bodyShake.targetX - this.bodyShake.x) * shakeSpring;
this.bodyShake.y += (this.bodyShake.targetY - this.bodyShake.y) * shakeSpring;
this.bodyShake.x *= shakeDamping;
this.bodyShake.y *= shakeDamping;
}
shoot(other, now, mouseTarget = null) {
if (this.ammo <= 0 || this.reloading || now - this.lastShot < this.fireRate * 1000) return [];
this.lastShot = now; this.ammo--;
const newBullets = [];
let baseAngle = mouseTarget ? Math.atan2(mouseTarget.y - this.y, mouseTarget.x - this.x) : (this.facingRight ? 0 : Math.PI);
if (mouseTarget) this.facingRight = mouseTarget.x > this.x;
for (let i = 0; i < this.bulletsPerShot; i++) {
let angle = baseAngle;
if (this.bulletsPerShot > 1) angle += (i / (this.bulletsPerShot - 1) - 0.5) * this.spread * 2;
angle += (Math.random() - 0.5) * this.spread;
newBullets.push(new Bullet(
this.x + Math.cos(baseAngle) * (this.radius + 5),
this.y + Math.sin(baseAngle) * (this.radius + 5),
Math.cos(angle) * this.bulletSpeed,
Math.sin(angle) * this.bulletSpeed,
this
));
}
return newBullets;
}
block(other, now) {
if (now - this.lastBlock < this.blockCooldown * 1000) return;
this.blocking = true; this.blockTime = now; this.lastBlock = now;
}
takeDamage(amount, source) {
if (this.blocking) return false;
this.health -= amount;
if (source?.owner?.lifeSteal > 0) {
source.owner.health = Math.min(source.owner.maxHealth, source.owner.health + amount * source.owner.lifeSteal);
}
return true;
}
draw(mousePos = null) {
ctx.save();
// Apply body shake offset
const drawX = this.x + this.bodyShake.x;
const drawY = this.y + this.bodyShake.y;
// Shield
if (this.blocking) {
ctx.beginPath();
ctx.arc(drawX, drawY, this.radius + 15, 0, Math.PI * 2);
ctx.strokeStyle = this.color;
ctx.lineWidth = 4;
ctx.globalAlpha = 0.6;
ctx.stroke();
ctx.globalAlpha = 1;
}
// Legs
const legLength = this.legLength;
const legWidth = 3;
const legAttachY = drawY + this.radius * 0.5;
const legSpacing = this.radius * 0.3;
ctx.lineCap = 'round';
ctx.lineWidth = legWidth;
ctx.strokeStyle = this.color;
// Left leg
ctx.save();
ctx.translate(drawX - legSpacing, legAttachY);
ctx.rotate(this.leftLeg.angle);
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(0, legLength);
ctx.stroke();
// Foot
ctx.beginPath();
ctx.ellipse(0, legLength, legWidth + 2, legWidth, 0, 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.fill();
ctx.restore();
// Right leg
ctx.save();
ctx.translate(drawX + legSpacing, legAttachY);
ctx.rotate(this.rightLeg.angle);
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(0, legLength);
ctx.stroke();
// Foot
ctx.beginPath();
ctx.ellipse(0, legLength, legWidth + 2, legWidth, 0, 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.fill();
ctx.restore();
// Body with squash/stretch
const squash = 1 + this.bodyShake.y * 0.02;
const stretch = 1 - this.bodyShake.y * 0.01;
ctx.save();
ctx.translate(drawX, drawY);
ctx.scale(stretch, squash);
// Main body
ctx.beginPath();
ctx.arc(0, 0, this.radius, 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.fill();
// Body highlight
ctx.beginPath();
ctx.arc(-this.radius * 0.3, -this.radius * 0.3, this.radius * 0.3, 0, Math.PI * 2);
ctx.fillStyle = 'rgba(255, 255, 255, 0.2)';
ctx.fill();
ctx.restore();
// Gun angle
let gunAngle;
if (mousePos && this.id === 1) {
gunAngle = Math.atan2(mousePos.y - drawY, mousePos.x - drawX);
this.facingRight = mousePos.x > this.x;
} else {
gunAngle = this.facingRight ? 0 : Math.PI;
}
// Eye
const eyeOffsetX = Math.cos(gunAngle) * 8;
const eyeOffsetY = Math.sin(gunAngle) * 4;
ctx.beginPath();
ctx.arc(drawX + eyeOffsetX, drawY - 5 + eyeOffsetY, 5, 0, Math.PI * 2);
ctx.fillStyle = '#fff';
ctx.fill();
// Pupil
ctx.beginPath();
ctx.arc(drawX + eyeOffsetX + Math.cos(gunAngle) * 2, drawY - 5 + eyeOffsetY + Math.sin(gunAngle) * 2, 2, 0, Math.PI * 2);
ctx.fillStyle = '#000';
ctx.fill();
// Gun
ctx.save();
ctx.translate(drawX, drawY);
ctx.rotate(gunAngle);
ctx.fillStyle = '#333';
ctx.fillRect(this.radius - 5, -4, 25, 8);
ctx.fillStyle = '#555';
ctx.fillRect(this.radius - 5, -4, 25, 2);
ctx.restore();
// Health bar
const barW = 50, barH = 6, barX = this.x - barW / 2, barY = drawY - this.radius - 15;
ctx.fillStyle = '#333';
ctx.fillRect(barX, barY, barW, barH);
ctx.fillStyle = this.color;
ctx.fillRect(barX, barY, barW * (this.health / this.maxHealth), barH);
// Reload indicator
if (this.reloading) {
const progress = (Date.now() - this.reloadStart) / (this.reloadTime * 1000);
ctx.beginPath();
ctx.arc(drawX, drawY, this.radius + 10, -Math.PI / 2, -Math.PI / 2 + Math.PI * 2 * progress);
ctx.strokeStyle = '#ffff00';
ctx.lineWidth = 3;
ctx.stroke();
}
ctx.restore();
}
}
// ==================== BULLET CLASS ====================
class Bullet {
constructor(x, y, vx, vy, owner) {
this.x = x; this.y = y; this.vx = vx; this.vy = vy; this.owner = owner;
this.radius = owner.bulletSize; this.damage = owner.damage;
this.bouncesLeft = owner.bulletBounces; this.homing = owner.homing;
this.explosive = owner.explosive; this.alive = true;
}
update(target) {
if (this.homing && target) {
const dx = target.x - this.x, dy = target.y - this.y;
const dist = Math.sqrt(dx * dx + dy * dy);
if (dist > 0) {
const speed = Math.sqrt(this.vx * this.vx + this.vy * this.vy);
this.vx += (dx / dist) * 0.5; this.vy += (dy / dist) * 0.5;
const newSpeed = Math.sqrt(this.vx * this.vx + this.vy * this.vy);
this.vx = (this.vx / newSpeed) * speed; this.vy = (this.vy / newSpeed) * speed;
}
}
this.vy += 0.1;
this.x += this.vx; this.y += this.vy;
// Wall collision
if (this.x < this.radius || this.x > canvas.width - this.radius) {
if (this.bouncesLeft > 0) { this.vx *= -1; this.bouncesLeft--; this.x = Math.max(this.radius, Math.min(canvas.width - this.radius, this.x)); }
else this.alive = false;
}
if (this.y < this.radius || this.y > canvas.height - this.radius) {
if (this.bouncesLeft > 0) { this.vy *= -1; this.bouncesLeft--; this.y = Math.max(this.radius, Math.min(canvas.height - this.radius, this.y)); }
else this.alive = false;
}
// Platform collision
for (const p of platforms) {
if (this.x + this.radius > p.x && this.x - this.radius < p.x + p.w &&
this.y + this.radius > p.y && this.y - this.radius < p.y + p.h) {
if (this.bouncesLeft > 0) {
const oL = this.x + this.radius - p.x, oR = p.x + p.w - this.x + this.radius;
const oT = this.y + this.radius - p.y, oB = p.y + p.h - this.y + this.radius;
if (Math.min(oT, oB) < Math.min(oL, oR)) this.vy *= -1; else this.vx *= -1;
this.bouncesLeft--;
} else this.alive = false;
break;
}
}
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fillStyle = this.explosive ? '#ff4444' : this.homing ? '#ff44ff' : this.owner.color;
ctx.fill();
// Trail
ctx.beginPath();
ctx.moveTo(this.x, this.y);
ctx.lineTo(this.x - this.vx * 2, this.y - this.vy * 2);
ctx.strokeStyle = this.owner.color;
ctx.lineWidth = this.radius * 0.8;
ctx.globalAlpha = 0.5; ctx.stroke(); ctx.globalAlpha = 1;
}
}
// ==================== EXPLOSION CLASS ====================
class Explosion {
constructor(x, y, radius, damage, owner) {
this.x = x; this.y = y; this.radius = radius; this.damage = damage;
this.owner = owner; this.time = 0; this.maxTime = 20;
}
update() { return ++this.time < this.maxTime; }
draw() {
const p = this.time / this.maxTime;
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius * (0.5 + p * 0.5), 0, Math.PI * 2);
ctx.fillStyle = `rgba(255, 100, 50, ${1 - p})`; ctx.fill();
}
}
// ==================== ONLINE NETWORKING (OPTIMIZED) ====================
function generateRoomCode() {
const chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
return Array(4).fill().map(() => chars[Math.floor(Math.random() * chars.length)]).join('');
}
function showOnlineMenu() {
document.getElementById('mainMenu').classList.add('hidden');
document.getElementById('onlineMenu').classList.remove('hidden');
}
function hideOnlineMenu() {
document.getElementById('onlineMenu').classList.add('hidden');
document.getElementById('mainMenu').classList.remove('hidden');
}
async function createRoom() {
roomCode = generateRoomCode();
isHost = true; playerId = 1;
currentMap = MAPS[Math.floor(Math.random() * MAPS.length)];
platforms = currentMap.platforms;
roomRef = database.ref('rooms/' + roomCode);
await roomRef.set({
mapIndex: MAPS.indexOf(currentMap),
state: 'waiting',
p1: { ready: false, connected: true },
round: 1, s1: 0, s2: 0
});
roomRef.onDisconnect().remove();
setupRoomListeners();
showLobby();
}
async function joinRoom() {
const code = document.getElementById('roomCodeInput').value.toUpperCase().trim();
if (code.length !== 4) { alert('Enter a valid 4-character code'); return; }
roomCode = code; isHost = false; playerId = 2;
roomRef = database.ref('rooms/' + roomCode);
const snapshot = await roomRef.once('value');
if (!snapshot.exists()) { alert('Room not found!'); return; }
const data = snapshot.val();
if (data.p2?.connected) { alert('Room is full!'); return; }
currentMap = MAPS[data.mapIndex];
platforms = currentMap.platforms;
await roomRef.child('p2').set({ ready: false, connected: true });
roomRef.child('p2').onDisconnect().remove();
setupRoomListeners();
showLobby();
}
function setupRoomListeners() {
// Compact player state listener
roomRef.on('value', snapshot => {
const data = snapshot.val();
if (!data) return;
// Lobby updates
const p1c = data.p1?.connected, p2c = data.p2?.connected;
document.getElementById('lobbyP1').classList.toggle('connected', p1c);
document.getElementById('lobbyP2').classList.toggle('connected', p2c);
const p1r = document.getElementById('p1Ready'), p2r = document.getElementById('p2Ready');
p1r.textContent = data.p1?.ready ? 'READY' : 'NOT READY';
p1r.classList.toggle('ready', data.p1?.ready);
p2r.textContent = p2c ? (data.p2?.ready ? 'READY' : 'NOT READY') : 'WAITING...';
p2r.classList.toggle('ready', data.p2?.ready);
document.getElementById('lobbyStatus').textContent = p2c ? 'Opponent connected!' : 'Waiting for opponent...';
if (data.p1?.ready && data.p2?.ready && data.state === 'waiting') {
if (isHost) roomRef.child('state').set('starting');
}
if (data.state === 'starting' && gameState === 'menu') startOnlineGame();
// Game state sync
if (gameState === 'playing' && data.g) {
const oppId = playerId === 1 ? 2 : 1;
const oppData = data.g['p' + oppId];
if (oppData && oppData.t > opponentState.timestamp) {
opponentPrevState = { ...opponentState };
opponentState = { x: oppData.x, y: oppData.y, vx: oppData.vx, vy: oppData.vy, h: oppData.h, timestamp: oppData.t };
}
}
// Score sync
if (data.s1 !== undefined) { player1.score = data.s1; player2.score = data.s2; }
});
// Bullet listener - use child_added for efficiency
roomRef.child('b').on('child_added', snapshot => {
const b = snapshot.val();
if (!b || b.o === playerId || gameState !== 'playing') return;
const owner = b.o === 1 ? player1 : player2;
bullets.push(new Bullet(b.x, b.y, b.vx, b.vy, owner));
snapshot.ref.remove();
});
// Card pick listener
roomRef.child('card').on('value', snapshot => {
const pick = snapshot.val();
if (!pick || pick.p === playerId) return;
const opponent = pick.p === 1 ? player1 : player2;
const card = CARDS.find(c => c.name === pick.n);
if (card) { card.effect(opponent); opponent.cards.push(card.name); opponent.ammo = opponent.maxAmmo; }
});
}
function showLobby() {
document.getElementById('onlineMenu').classList.add('hidden');
document.getElementById('lobby').classList.remove('hidden');
document.getElementById('displayRoomCode').textContent = roomCode;
isReady = false;
document.getElementById('readyBtn').textContent = 'READY';
}
function toggleReady() {
isReady = !isReady;
document.getElementById('readyBtn').textContent = isReady ? 'NOT READY' : 'READY';
roomRef.child('p' + playerId + '/ready').set(isReady);
}
function leaveLobby() {
if (roomRef) {
roomRef.child('p' + playerId).remove();
if (isHost) roomRef.remove();
roomRef.off(); roomRef = null;
}
roomCode = null; playerId = null; isHost = false; isReady = false;
document.getElementById('lobby').classList.add('hidden');
document.getElementById('mainMenu').classList.remove('hidden');
}
function startOnlineGame() {
document.getElementById('lobby').classList.add('hidden');
gameMode = 'online';
initPlayers();
currentRound = 1; player1.score = 0; player2.score = 0;
updateHUD();
startRound();
}
// Optimized sync - only send when needed, minimal data
function syncPlayerState() {
if (gameMode !== 'online' || !roomRef || gameState !== 'playing') return;
const now = Date.now();
if (now - lastSyncTime < SYNC_RATE) return;
lastSyncTime = now;
const p = playerId === 1 ? player1 : player2;
// Compact data structure
roomRef.child('g/p' + playerId).set({
x: Math.round(p.x), y: Math.round(p.y),
vx: Math.round(p.vx * 10) / 10, vy: Math.round(p.vy * 10) / 10,
h: Math.round(p.health), t: now
});
// Ping calculation
pingStart = now;
}
function sendBullet(bullet) {
if (gameMode !== 'online' || !roomRef) return;
roomRef.child('b').push({
o: playerId,
x: Math.round(bullet.x), y: Math.round(bullet.y),
vx: Math.round(bullet.vx * 10) / 10, vy: Math.round(bullet.vy * 10) / 10
});
}
// Interpolate opponent position for smooth movement
function interpolateOpponent() {
if (gameMode !== 'online' || gameState !== 'playing') return;
const opponent = playerId === 1 ? player2 : player1;
const now = Date.now();
const renderTime = now - INTERPOLATION_DELAY;
if (opponentState.timestamp > opponentPrevState.timestamp) {
const dt = opponentState.timestamp - opponentPrevState.timestamp;
const t = Math.min(1, Math.max(0, (renderTime - opponentPrevState.timestamp) / dt));
// Lerp position
opponent.x = opponentPrevState.x + (opponentState.x - opponentPrevState.x) * t;
opponent.y = opponentPrevState.y + (opponentState.y - opponentPrevState.y) * t;
opponent.vx = opponentState.vx;
opponent.vy = opponentState.vy;
if (opponentState.h !== undefined) opponent.health = opponentState.h;
// Update facing direction based on velocity
if (Math.abs(opponent.vx) > 0.5) {
opponent.facingRight = opponent.vx > 0;
}
}
// Run leg animation locally (based on interpolated velocity)
updateLegAnimation(opponent);
}
// Update leg animation for a player (can be called for any player)
function updateLegAnimation(player) {
const speed = Math.abs(player.vx);
const moving = speed > 0.5;
// Estimate grounded state from position
const feetOffset = player.radius * 0.5 + 35;
let grounded = false;
for (const p of platforms) {
if (player.x + player.radius > p.x && player.x - player.radius < p.x + p.w &&