-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspace-scroller.html
More file actions
1331 lines (1204 loc) · 43.7 KB
/
space-scroller.html
File metadata and controls
1331 lines (1204 loc) · 43.7 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, user-scalable=no">
<link rel="manifest" href="manifest.webmanifest">
<meta name="theme-color" content="#0b1e40">
<meta name="application-name" content="IdleGames">
<script src="assets/js/pwa.js" defer></script>
<title>Space Scroller</title>
<link rel="icon" href="favicon.ico" type="image/x-icon">
<style>
html,
body {
height: 100%;
margin: 0;
padding: 0;
font-family: 'Arial', sans-serif;
background: #000;
color: #fff;
overflow: hidden;
touch-action: none;
/* Prevent any touch gestures on the page */
-webkit-user-select: none;
/* Prevent text selection */
user-select: none;
}
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
}
canvas {
outline: 0.125rem solid #fff;
background: #000;
display: block;
touch-action: none;
/* Prevent default touch behaviors */
width: 100vw;
height: 100vh;
box-sizing: border-box;
max-width: none;
max-height: none;
}
@media (max-width: 48em) {
.hud {
font-size: var(--hudFont, 0.875rem);
}
}
/* Heads-up display */
.hud {
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 150;
padding: 0.5em 0.75em;
box-sizing: border-box;
font-size: var(--hudFont, 1.125rem);
/* allow selective interactivity per row */
}
.hud-row {
width: 100%;
margin-bottom: 0.375em;
pointer-events: none;
}
.hud-row.stats {
display: grid;
/* Score reduced, hearts expanded; others unchanged */
grid-template-columns: 2fr 4fr 3fr 2fr 2fr 2fr 2fr;
align-items: center;
gap: 0.5em;
}
.stat {
display: flex;
align-items: center;
gap: 0.375em;
min-height: 1.5em;
}
.stat .emoji {
filter: drop-shadow(0 0.0625em 0.0625em rgba(0, 0, 0, 0.4));
}
.dim {
opacity: 0.4;
}
/* Color pills to match power-up colors */
.pill {
display: inline-flex;
align-items: center;
justify-content: center;
padding: 0 0.375em;
border-radius: 0.5em;
margin-right: 0.125em;
line-height: 1.1;
border: 0.0625em solid rgba(255, 255, 255, 0.35);
}
.magenta {
background: rgba(255, 0, 255, 0.18);
border-color: rgba(255, 0, 255, 0.85);
}
.cyan {
background: rgba(0, 255, 255, 0.18);
border-color: rgba(0, 255, 255, 0.85);
}
.white {
background: rgba(255, 255, 255, 0.18);
border-color: rgba(255, 255, 255, 0.85);
}
.green {
background: rgba(0, 255, 136, 0.18);
border-color: rgba(0, 255, 136, 0.85);
}
.orange {
background: rgba(255, 170, 0, 0.18);
border-color: rgba(255, 170, 0, 0.85);
}
.red {
background: rgba(255, 0, 0, 0.18);
border-color: rgba(255, 0, 0, 0.85);
}
/* Game Over Modal */
.game-over-modal {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.9);
display: none;
flex-direction: column;
align-items: center;
justify-content: center;
z-index: 1000;
touch-action: manipulation;
}
.game-over-content {
text-align: center;
color: #fff;
padding: 2rem;
max-width: 90%;
}
.game-over-title {
font-size: 3rem;
font-weight: bold;
color: #ff4444;
margin-bottom: 1rem;
text-shadow: 0.125rem 0.125rem 0.25rem rgba(0, 0, 0, 0.8);
}
.game-over-score {
font-size: 2rem;
margin-bottom: 2rem;
color: #ffff44;
}
.game-over-instruction {
font-size: 1.2rem;
opacity: 0.8;
animation: pulse 2s infinite;
}
@keyframes pulse {
0%,
100% {
opacity: 0.8;
}
50% {
opacity: 0.4;
}
}
@media (max-width: 48em) {
.game-over-title {
font-size: 2rem;
}
.game-over-score {
font-size: 1.5rem;
}
.game-over-instruction {
font-size: 1rem;
}
}
.ui div {
margin-bottom: 0.3125rem;
}
.overheat-bar {
width: 100%;
height: 1em;
background: #333;
border: 0.0625em solid rgba(255, 255, 255, 0.35);
border-radius: 0.375em;
margin-top: 0.625em;
}
.overheat-fill {
height: 100%;
background: linear-gradient(to right, #00ff00, #ffff00, #ff0000);
width: 0%;
transition: width 0.1s;
border-radius: 0.375em;
}
/* Controls row below overheat bar */
.controls-row {
display: flex;
justify-content: flex-end;
gap: 0.5em;
pointer-events: auto;
/* enable interactivity for buttons */
}
.fullscreen-btn,
.pause-btn {
position: static;
width: 2.6em;
height: 2.6em;
border-radius: 50%;
background: rgba(255, 255, 255, 0.1);
color: #fff;
border: 0.125rem solid #fff;
display: inline-flex;
align-items: center;
justify-content: center;
font-size: 1.1em;
cursor: pointer;
pointer-events: auto;
/* clickable despite parent */
backdrop-filter: blur(0.125rem);
}
.fullscreen-btn:active,
.pause-btn:active {
transform: scale(0.97);
}
.cooldown {
background: #666 !important;
}
/* Paused overlay */
.paused-overlay {
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.5);
display: none;
align-items: center;
justify-content: center;
z-index: 900;
/* below game-over modal (1000), above canvas */
pointer-events: none;
/* allow clicks to pass through */
}
.paused-overlay .paused-text {
color: #fff;
font-weight: bold;
text-shadow: 0.125rem 0.125rem 0.375rem rgba(0, 0, 0, 0.8);
font-size: clamp(1.5rem, 10vw, 4.5rem);
letter-spacing: 0.25em;
}
</style>
</head>
<body>
<div class="hud">
<div class="hud-row stats">
<div class="stat"><span class="emoji">🪙</span><span id="score">0</span></div>
<div class="stat"><span id="hearts"></span></div>
<div class="stat"><span id="bomb-icons"></span></div>
<div class="stat"><span id="wingmen"></span></div>
<div class="stat"><span id="fire-power"></span></div>
<div class="stat"><span id="missile-power"></span></div>
<div class="stat"><span id="homing-power"></span></div>
</div>
<div class="hud-row">
<div class="overheat-bar">
<div class="overheat-fill" id="overheat-fill"></div>
</div>
</div>
<div class="hud-row controls-row">
<button id="pauseBtn" class="pause-btn" aria-label="Pause">⏸</button>
<button id="fullscreenBtn" class="fullscreen-btn" aria-label="Toggle Fullscreen">⛶</button>
</div>
</div>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<!-- Paused Overlay -->
<div id="pausedOverlay" class="paused-overlay">
<div class="paused-text">PAUSED</div>
</div>
<!-- Game Over Modal -->
<div id="gameOverModal" class="game-over-modal">
<div class="game-over-content">
<div class="game-over-title">GAME OVER</div>
<div class="game-over-score">Final Score: <span id="finalScore">0</span></div>
<div class="game-over-instruction">Tap anywhere to restart</div>
</div>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreEl = document.getElementById('score');
const heartsEl = document.getElementById('hearts');
const bombIconsEl = document.getElementById('bomb-icons');
const wingmenEl = document.getElementById('wingmen');
const firePowerEl = document.getElementById('fire-power');
const missilePowerEl = document.getElementById('missile-power');
const homingPowerEl = document.getElementById('homing-power');
const overheatFill = document.getElementById('overheat-fill');
const fullscreenBtn = document.getElementById('fullscreenBtn');
const pauseBtn = document.getElementById('pauseBtn');
const gameOverModal = document.getElementById('gameOverModal');
const finalScoreEl = document.getElementById('finalScore');
const pausedOverlay = document.getElementById('pausedOverlay');
// Game variables
let player = { x: 0, y: 0, width: 30, height: 30, speed: 5 };
let bullets = [];
let enemies = [];
let powerUps = [];
let missiles = [];
let bombs = 0;
let score = 0;
let health = 100;
let overheat = 0;
let maxOverheat = 100;
let cooldown = false;
let fireRate = 0; // directions: 0=single, 1=3-way, 2=5-way
let missileRate = 0; // 0=none, 1=up, 2=±15°, 3=up + ±15°
// Wingmen state
let wingmenCount = 0; // 0..3
let wingmenPositions = [];
const WINGMAN_MAX = 3;
// Homing missiles power-up state (stacked)
let homingActive = false; // derived from stacks > 0
let homingStacks = 0; // 0..3 stacks queued
let homingNextExpiry = 0; // timestamp when current stack expires
let lastHomingLaunch = 0;
const HOMING_INTERVAL = 500; // ms
const HOMING_SPEED = 6; // px per frame
const HOMING_TURN = 0.12; // radians per frame
const COOL_RATE = 1; // heat reduced per frame when not firing
let overheatPenaltyApplied = false; // ensure penalty applies once per life until power is minimal
// Power-ups spawn randomly with equal probability per type
let mouseX = 0;
let mouseY = 0;
// Viewport scaling
let dpr = Math.max(1, window.devicePixelRatio || 1);
let viewW = 0, viewH = 0;
let scaleUnit = 1; // relative to base 600 min-dimension
// Scaled metrics
let shipW = 30, shipH = 30, enemySize = 30, powerUpSize = 20, bulletW = 4, bulletH = 10, missileW = 6, missileH = 16;
let shipOffsetTouch = 80; // finger offset
let collBulletEnemy = 20, collPlayerEnemy = 25, collPlayerPower = 20;
let spread10 = 10, spread20 = 20, wingmanOff = 60;
// Mini-boss state
let boss = null; // { x, y, t, hp, lastShot }
let bossBullets = [];
let lastBossSpawn = Date.now();
const BOSS_SPAWN_INTERVAL = 300000; // 5 minutes
const BOSS_HITS_TO_KILL = 500;
const BOSS_BULLET_INTERVAL = 1000; // 1s
// Handle canvas/device pixel ratio and scaled metrics
function resizeCanvas() {
const rect = document.documentElement.getBoundingClientRect();
viewW = window.innerWidth || rect.width;
viewH = window.innerHeight || rect.height;
dpr = Math.max(1, window.devicePixelRatio || 1);
canvas.width = Math.floor(viewW * dpr);
canvas.height = Math.floor(viewH * dpr);
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.scale(dpr, dpr);
// scale unit based on smaller dimension vs base 600
scaleUnit = Math.max(0.5, Math.min(2.5, Math.min(viewW, viewH) / 600));
// update scaled metrics
shipW = 30 * scaleUnit; shipH = 30 * scaleUnit;
enemySize = 30 * scaleUnit; powerUpSize = 20 * scaleUnit;
bulletW = 4 * scaleUnit; bulletH = 10 * scaleUnit;
missileW = 6 * scaleUnit; missileH = 16 * scaleUnit;
shipOffsetTouch = 80 * scaleUnit;
collBulletEnemy = 20 * scaleUnit; collPlayerEnemy = 25 * scaleUnit; collPlayerPower = 20 * scaleUnit;
spread10 = 10 * scaleUnit; spread20 = 20 * scaleUnit; wingmanOff = 60 * scaleUnit;
// Ensure boss stays in-bounds after resize
if (boss) {
boss.x = Math.max(0, Math.min(viewW, boss.x));
boss.y = Math.max(0, Math.min(viewH, boss.y));
}
// sync player size to scaled metrics and clamp position to viewport
player.width = shipW; player.height = shipH;
player.x = Math.max(player.width / 2, Math.min(viewW - player.width / 2, player.x));
player.y = Math.max(player.height / 2, Math.min(viewH - player.height / 2, player.y));
// scale HUD font proportional to scaleUnit, clamped to a readable range
const hudFont = Math.round(16 * scaleUnit);
const hudPx = Math.max(12, Math.min(24, hudFont));
document.documentElement.style.setProperty('--hudFont', (hudPx / 16).toFixed(3) + 'rem');
}
window.addEventListener('resize', resizeCanvas);
// Initial sizing
resizeCanvas();
// initialize position to bottom-center relative to viewport
player.x = viewW / 2;
player.y = viewH - shipH;
mouseX = player.x; mouseY = player.y;
let firing = false;
let lastShot = 0;
let touchStartTime = 0;
let touchStartPos = { x: 0, y: 0 };
let isCurrentlyTouch = false; // Track if current interaction is touch
let gameRunning = true; // Track if game is running
let isPaused = false; // Track pause state
// Game Over Modal Functions
function showGameOver() {
gameRunning = false;
finalScoreEl.textContent = score;
gameOverModal.style.display = 'flex';
}
function hideGameOver() {
gameOverModal.style.display = 'none';
gameRunning = true;
}
function restartGame() {
// Reset all game variables
player = { x: viewW / 2, y: viewH - shipH, width: shipW, height: shipH, speed: 5 };
bullets = [];
enemies = [];
powerUps = [];
missiles = [];
bombs = 0;
score = 0;
health = 100;
overheat = 0;
cooldown = false;
fireRate = 0;
missileRate = 0;
wingmenCount = 0;
wingmenPositions = [];
homingActive = false;
homingStacks = 0;
homingNextExpiry = 0;
lastHomingLaunch = 0;
// Reset boss state
boss = null;
bossBullets = [];
lastBossSpawn = Date.now();
overheatPenaltyApplied = false;
mouseX = viewW / 2;
mouseY = viewH - shipH;
firing = false;
lastShot = 0;
// Clear pause state on restart
isPaused = false;
gameRunning = true;
if (pauseBtn) {
pauseBtn.textContent = '⏸';
pauseBtn.setAttribute('aria-label', 'Pause');
}
hideGameOver();
gameLoop(); // Restart the game loop
}
// Modal event handlers
gameOverModal.addEventListener('click', restartGame);
gameOverModal.addEventListener('touchend', (e) => {
e.preventDefault();
restartGame();
});
// --- Floating Emoji Animations ---
const floatAnims = [];
function getElementCenter(el) {
if (!el) return { x: viewW / 2, y: 0 };
const r = el.getBoundingClientRect();
const c = canvas.getBoundingClientRect();
return { x: r.left + r.width / 2 - c.left, y: r.top + r.height / 2 - c.top };
}
function addEmojiFloat(fromX, fromY, toX, toY, emoji, options = {}) {
const start = Date.now();
const speed = 1 + 0.7 * (Math.random() - 0.5);
const duration = (options.duration || 700) * speed;
const angle = (-1.2 + 2.4 * Math.random());
const ampX = (options.ampX || 120) * (options.scaleAmpWithUnit ? scaleUnit : 1);
const ampY = (options.ampY || 60) * (options.scaleAmpWithUnit ? scaleUnit : 1);
const size = options.size || Math.max(16, Math.floor(viewH / 24));
floatAnims.push({ start, duration, fromX, fromY, toX, toY, angle, ampX, ampY, emoji, size });
}
function addEmojiFloatToElement(fromX, fromY, toEl, emoji, options = {}) {
const { x, y } = getElementCenter(toEl);
addEmojiFloat(fromX, fromY, x, y, emoji, options);
}
function addEmojiFloatFromElement(fromEl, toX, toY, emoji, options = {}) {
const { x, y } = getElementCenter(fromEl);
addEmojiFloat(x, y, toX, toY, emoji, options);
}
function renderEmojiAnims() {
if (!floatAnims.length) return false;
const now = Date.now();
let still = false;
for (const anim of floatAnims) {
const t = Math.min(1, (now - anim.start) / anim.duration);
if (t < 1) still = true;
const ease = 1 - Math.pow(1 - t, 3);
const x = anim.fromX + (anim.toX - anim.fromX) * ease + Math.sin(anim.angle) * anim.ampX * (1 - t);
const y = anim.fromY + (anim.toY - anim.fromY) * ease + Math.sin(anim.angle) * anim.ampY * (1 - t);
ctx.save();
ctx.font = `${anim.size}px serif`;
ctx.globalAlpha = 0.7 + 0.3 * (1 - t);
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(anim.emoji, x, y);
ctx.restore();
}
// Purge finished
for (let i = floatAnims.length - 1; i >= 0; i--) {
const anim = floatAnims[i];
if (now - anim.start >= anim.duration) floatAnims.splice(i, 1);
}
return still;
}
// Event handling functions
function getEventPos(e) {
const rect = canvas.getBoundingClientRect();
let clientX, clientY;
if (e.touches && e.touches.length > 0) {
// Touch event
clientX = e.touches[0].clientX;
clientY = e.touches[0].clientY;
} else {
// Mouse event
clientX = e.clientX;
clientY = e.clientY;
}
// Position in CSS pixels relative to canvas
const x = clientX - rect.left;
const y = clientY - rect.top;
return { x, y };
}
function handleMove(e) {
e.preventDefault(); // Prevent scrolling
const pos = getEventPos(e);
mouseX = pos.x;
mouseY = pos.y;
// Track if this is a touch interaction
isCurrentlyTouch = !!(e.touches && e.touches.length > 0);
}
function handleStart(e) {
e.preventDefault(); // Prevent default behaviors
firing = true;
// Track if this is a touch interaction
isCurrentlyTouch = !!(e.touches && e.touches.length > 0);
// Update position on touch start
if (e.touches) {
const pos = getEventPos(e);
mouseX = pos.x;
mouseY = pos.y;
touchStartTime = Date.now();
touchStartPos = { x: pos.x, y: pos.y };
}
}
function handleEnd(e) {
e.preventDefault(); // Prevent default behaviors
firing = false;
// Check for bomb trigger on touch (tap without much movement)
if (e.changedTouches) {
const touchDuration = Date.now() - touchStartTime;
const pos = getEventPos({ touches: e.changedTouches });
const distance = Math.sqrt(
Math.pow(pos.x - touchStartPos.x, 2) +
Math.pow(pos.y - touchStartPos.y, 2)
);
// If it was a short tap (< 200ms) with minimal movement (< 20px), trigger bomb
if (touchDuration < 200 && distance < 20) {
useBomb();
}
// Reset touch tracking when touch ends
isCurrentlyTouch = false;
}
}
// Mouse event listeners
canvas.addEventListener('mousemove', handleMove);
canvas.addEventListener('mousedown', handleStart);
canvas.addEventListener('mouseup', handleEnd);
// Touch event listeners
canvas.addEventListener('touchstart', handleStart, { passive: false });
canvas.addEventListener('touchmove', handleMove, { passive: false });
canvas.addEventListener('touchend', handleEnd, { passive: false });
// Game functions
function drawPlayer() {
ctx.fillStyle = '#00ff00';
ctx.fillRect(player.x - player.width / 2, player.y - player.height / 2, player.width, player.height);
}
function getWingmanOffsets(count) {
switch (count) {
case 1:
return [{ x: -wingmanOff, y: 0 }];
case 2:
return [{ x: -wingmanOff, y: 0 }, { x: wingmanOff, y: 0 }];
case 3:
return [{ x: -wingmanOff, y: 0 }, { x: wingmanOff, y: 0 }, { x: 0, y: -wingmanOff }];
default:
return [];
}
}
function updateWingmenPositions() {
const offsets = getWingmanOffsets(wingmenCount);
wingmenPositions = offsets.map(off => {
const x = player.x + off.x;
const y = player.y + off.y;
return {
x: Math.max(player.width / 2, Math.min(viewW - player.width / 2, x)),
y: Math.max(player.height / 2, Math.min(viewH - player.height / 2, y))
};
});
}
function drawWingmen() {
if (wingmenCount <= 0) return;
ctx.fillStyle = '#44ff44';
wingmenPositions.forEach(p => {
ctx.fillRect(p.x - player.width / 2, p.y - player.height / 2, player.width, player.height);
});
}
function drawBullets() {
ctx.fillStyle = '#ffff00';
bullets.forEach(bullet => {
ctx.fillRect(bullet.x - bulletW / 2, bullet.y - bulletH / 2, bulletW, bulletH);
});
}
function drawEnemies() {
ctx.fillStyle = '#ff0000';
enemies.forEach(enemy => {
ctx.fillRect(enemy.x - enemySize / 2, enemy.y - enemySize / 2, enemySize, enemySize);
});
}
// Mini-boss drawing (double size, darker red border)
function drawBoss() {
if (!boss) return;
const size = enemySize * 2;
ctx.fillStyle = '#ff2222';
ctx.fillRect(boss.x - size / 2, boss.y - size / 2, size, size);
ctx.strokeStyle = '#880000';
ctx.lineWidth = 2;
ctx.strokeRect(boss.x - size / 2, boss.y - size / 2, size, size);
// Draw boss HP as hearts inside (2 rows x 5 columns)
const totalHearts = 10;
const fraction = Math.max(0, Math.min(1, boss.hp / BOSS_HITS_TO_KILL));
const filled = Math.max(0, Math.min(totalHearts, Math.ceil(fraction * totalHearts)));
const cols = 5, rows = 2;
const cellW = size / cols;
const cellH = size / rows;
const fontSize = Math.min(cellW, cellH) * 0.7; // fit comfortably in each cell
ctx.save();
ctx.font = `${fontSize}px serif`;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.shadowColor = 'rgba(0,0,0,0.5)';
ctx.shadowBlur = 2;
for (let i = 0; i < totalHearts; i++) {
const col = i % cols;
const row = Math.floor(i / cols);
const cx = boss.x - size / 2 + cellW * col + cellW / 2;
const cy = boss.y - size / 2 + cellH * row + cellH / 2;
const heart = i < filled ? '❤️' : '🖤';
ctx.fillText(heart, cx, cy);
}
ctx.restore();
}
// Boss bullets (yellow slow circles)
function drawBossBullets() {
if (!bossBullets.length) return;
ctx.fillStyle = '#ffff66';
bossBullets.forEach(b => {
ctx.beginPath();
ctx.arc(b.x, b.y, 5 * scaleUnit, 0, Math.PI * 2);
ctx.fill();
});
}
function drawPowerUps() {
powerUps.forEach(powerUp => {
if (powerUp.type === 'fire') ctx.fillStyle = '#ff00ff';
else if (powerUp.type === 'missile') ctx.fillStyle = '#00ffff';
else if (powerUp.type === 'bomb') ctx.fillStyle = '#ffffff';
else if (powerUp.type === 'wingman') ctx.fillStyle = '#00ff88';
else if (powerUp.type === 'homing') ctx.fillStyle = '#ffaa00';
else ctx.fillStyle = '#ffffff';
ctx.fillRect(powerUp.x - powerUpSize / 2, powerUp.y - powerUpSize / 2, powerUpSize, powerUpSize);
});
}
function drawMissiles() {
missiles.forEach(missile => {
if (missile.homing) {
// Light grey and rotated to face current velocity
const angle = Math.atan2(missile.vy || 0, missile.vx || 0) + Math.PI / 2;
ctx.save();
ctx.translate(missile.x, missile.y);
ctx.rotate(angle);
ctx.fillStyle = '#d0d0d0';
ctx.fillRect(-missileW / 2, -missileH / 2, missileW, missileH);
ctx.restore();
} else {
// Regular missiles: straight up, orange
ctx.fillStyle = '#ff8800';
ctx.fillRect(missile.x - missileW / 2, missile.y - missileH / 2, missileW, missileH);
}
});
}
function updatePlayer() {
if (isCurrentlyTouch) {
// For touch: offset the ship above the finger to avoid blocking the view
const shipOffset = shipOffsetTouch; // scaled finger offset
player.x = Math.max(player.width / 2, Math.min(viewW - player.width / 2, mouseX));
player.y = Math.max(player.height / 2, Math.min(viewH - player.height / 2, mouseY - shipOffset));
} else {
// For mouse: normal positioning
player.x = Math.max(player.width / 2, Math.min(viewW - player.width / 2, mouseX));
player.y = Math.max(player.height / 2, Math.min(viewH - player.height / 2, mouseY));
}
}
function updateBullets() {
bullets = bullets.filter(bullet => bullet.y > 0);
bullets.forEach(bullet => {
bullet.y -= 10;
});
}
function updateEnemies() {
enemies.forEach(enemy => {
enemy.y += 2;
});
enemies = enemies.filter(enemy => enemy.y < viewH + enemySize);
}
// Spawn mini-boss if due
function spawnBossIfDue() {
const now = Date.now();
if (boss) return;
if (now - lastBossSpawn < BOSS_SPAWN_INTERVAL) return;
lastBossSpawn = now;
boss = { x: viewW / 2, y: viewH * 0.18, t: 0, hp: BOSS_HITS_TO_KILL, lastShot: 0 };
}
// Debug: spawn/reset the mini-boss immediately
function spawnBossNow() {
boss = { x: viewW / 2, y: viewH * 0.18, t: 0, hp: BOSS_HITS_TO_KILL, lastShot: 0 };
lastBossSpawn = Date.now();
bossBullets = [];
}
function updateBoss() {
if (!boss) return;
// Figure-eight path in top third
boss.t += 0.0075; // quarter speed step per frame
const ax = (viewW * 0.4);
const ay = (viewH * 0.07);
const cx = viewW / 2;
const cy = viewH * 0.18;
boss.x = cx + ax * Math.sin(boss.t);
boss.y = cy + ay * Math.sin(2 * boss.t);
// Shooting toward player every 1 second
const now = Date.now();
if (now - boss.lastShot >= BOSS_BULLET_INTERVAL) {
boss.lastShot = now;
const dx = player.x - boss.x;
const dy = player.y - boss.y;
const len = Math.hypot(dx, dy) || 1;
const spd = 3 * scaleUnit; // slow
bossBullets.push({ x: boss.x, y: boss.y, vx: (dx / len) * spd, vy: (dy / len) * spd });
}
}
function updateBossBullets() {
if (!bossBullets.length) return;
bossBullets.forEach(b => { b.x += b.vx; b.y += b.vy; });
const m = 10 * scaleUnit;
bossBullets = bossBullets.filter(b => b.x > -m && b.x < viewW + m && b.y > -m && b.y < viewH + m);
}
function updatePowerUps() {
powerUps.forEach(powerUp => {
powerUp.y += 1;
});
powerUps = powerUps.filter(powerUp => powerUp.y < viewH + powerUpSize);
}
function updateMissiles() {
missiles.forEach(missile => {
if (missile.homing) {
// Find nearest enemy and steer toward it
let nearest = null;
let bestD2 = Infinity;
for (const e of enemies) {
const dx = e.x - missile.x;
const dy = e.y - missile.y;
const d2 = dx * dx + dy * dy;
if (d2 < bestD2) { bestD2 = d2; nearest = e; }
}
if (nearest) {
const desired = Math.atan2(nearest.y - missile.y, nearest.x - missile.x);
const current = Math.atan2(missile.vy, missile.vx);
let diff = desired - current;
while (diff > Math.PI) diff -= 2 * Math.PI;
while (diff < -Math.PI) diff += 2 * Math.PI;
const turn = Math.max(-HOMING_TURN, Math.min(HOMING_TURN, diff));
const newAngle = current + turn;
const spd = missile.speed || HOMING_SPEED;
missile.vx = Math.cos(newAngle) * spd;
missile.vy = Math.sin(newAngle) * spd;
}
missile.x += missile.vx;
missile.y += missile.vy;
} else {
if (typeof missile.vx === 'number' && typeof missile.vy === 'number') {
missile.x += missile.vx;
missile.y += missile.vy;
} else {
missile.y -= 8;
}
}
});
missiles = missiles.filter(missile => {
if (missile.homing) {
return missile.x > -missileW && missile.x < viewW + missileW && missile.y > -missileH && missile.y < viewH + missileH;
} else {
return missile.x > -missileW && missile.x < viewW + missileW && missile.y > -missileH && missile.y < viewH + missileH;
}
});
}
function checkCollisions() {
// Bullets vs Enemies
bullets.forEach(bullet => {
enemies.forEach(enemy => {
if (Math.abs(bullet.x - enemy.x) < collBulletEnemy && Math.abs(bullet.y - enemy.y) < collBulletEnemy) {
bullets.splice(bullets.indexOf(bullet), 1);
enemies.splice(enemies.indexOf(enemy), 1);
score += 10;
}
});
});
// Missiles vs Enemies
missiles.forEach(missile => {
enemies.forEach(enemy => {
if (Math.abs(missile.x - enemy.x) < collBulletEnemy && Math.abs(missile.y - enemy.y) < collBulletEnemy) {
missiles.splice(missiles.indexOf(missile), 1);
enemies.splice(enemies.indexOf(enemy), 1);
score += 20;
}
});
});
// Bullets vs Boss
if (boss) {
bullets.slice().forEach(bullet => {
const size = enemySize * 2;
if (Math.abs(bullet.x - boss.x) < size / 2 && Math.abs(bullet.y - boss.y) < size / 2) {
const bi = bullets.indexOf(bullet);
if (bi !== -1) bullets.splice(bi, 1);
boss.hp -= 1;
}
});
// Missiles vs Boss
missiles.slice().forEach(missile => {
const size = enemySize * 2;
if (Math.abs(missile.x - boss.x) < size / 2 && Math.abs(missile.y - boss.y) < size / 2) {
const mi = missiles.indexOf(missile);
if (mi !== -1) missiles.splice(mi, 1);
boss.hp -= 1;
}
});
// Destroy boss if HP depleted
if (boss && boss.hp <= 0) {
boss = null;
score += 200; // reward for defeating mini-boss
}
}
// Wingmen vs Enemies: if a wingman hits an enemy, remove both and lose one wingman
if (wingmenCount > 0 && wingmenPositions.length > 0) {
enemies.slice().forEach(enemy => {
for (const w of wingmenPositions) {
if (Math.abs(w.x - enemy.x) < collPlayerEnemy && Math.abs(w.y - enemy.y) < collPlayerEnemy) {
const idx = enemies.indexOf(enemy);
if (idx !== -1) { enemies.splice(idx, 1); }
wingmenCount = Math.max(0, wingmenCount - 1);
break;
}
}
});
}
// Player vs Enemies
enemies.slice().forEach(enemy => {
if (Math.abs(player.x - enemy.x) < collPlayerEnemy && Math.abs(player.y - enemy.y) < collPlayerEnemy) {
const idx = enemies.indexOf(enemy);
if (idx !== -1) { enemies.splice(idx, 1); }
if (wingmenCount > 0) {
wingmenCount = Math.max(0, wingmenCount - 1);
} else {
health -= 10;
// Animate heart from hearts HUD to player ship
addEmojiFloatFromElement(heartsEl, player.x, player.y, '❤️', { scaleAmpWithUnit: true });
}
}
});
// Player vs Boss
if (boss) {
const size = enemySize * 2;
if (Math.abs(player.x - boss.x) < size / 2 && Math.abs(player.y - boss.y) < size / 2) {
if (wingmenCount > 0) {
wingmenCount = Math.max(0, wingmenCount - 1);
} else {
health -= 10;
addEmojiFloatFromElement(heartsEl, player.x, player.y, '❤️', { scaleAmpWithUnit: true });
}
}
}
// Boss bullets vs Player
if (bossBullets.length) {
bossBullets.slice().forEach(b => {
if (Math.abs(player.x - b.x) < collPlayerEnemy && Math.abs(player.y - b.y) < collPlayerEnemy) {
const bi = bossBullets.indexOf(b);
if (bi !== -1) bossBullets.splice(bi, 1);
if (wingmenCount > 0) {
wingmenCount = Math.max(0, wingmenCount - 1);
} else {
health -= 10;
addEmojiFloatFromElement(heartsEl, player.x, player.y, '❤️', { scaleAmpWithUnit: true });
}
}
});
}
// Player vs PowerUps
powerUps.forEach(powerUp => {
if (Math.abs(player.x - powerUp.x) < collPlayerPower && Math.abs(player.y - powerUp.y) < collPlayerPower) {
powerUps.splice(powerUps.indexOf(powerUp), 1);