-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoe_galaga.html
More file actions
1631 lines (1401 loc) · 62.5 KB
/
poe_galaga.html
File metadata and controls
1631 lines (1401 loc) · 62.5 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>Galaga Clone</title>
<style>
:root {
--primary-color: #5D5CDE;
--text-color: #333;
--bg-color: #FFFFFF;
--ui-bg-color: rgba(0, 0, 0, 0.7);
--ui-text-color: #FFFFFF;
}
.dark {
--text-color: #EEE;
--bg-color: #181818;
--ui-bg-color: rgba(50, 50, 50, 0.8);
--ui-text-color: #FFFFFF;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: var(--bg-color);
color: var(--text-color);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
overflow: hidden;
transition: background-color 0.3s ease;
}
.game-container {
position: relative;
width: 100%;
max-width: 500px;
margin: 0 auto;
overflow: hidden;
}
.game-canvas {
display: block;
background-color: #000;
width: 100%;
height: auto;
image-rendering: pixelated;
border-radius: 4px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
}
.controls {
margin-top: 15px;
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 10px;
}
button {
background-color: var(--primary-color);
color: white;
border: none;
padding: 10px 15px;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
transition: all 0.2s;
}
button:hover {
opacity: 0.9;
transform: translateY(-1px);
}
button:active {
transform: translateY(1px);
}
.game-title {
font-size: 2.5rem;
font-weight: bold;
margin-bottom: 20px;
color: var(--primary-color);
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
}
.menu {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
background-color: rgba(0, 0, 0, 0.8);
z-index: 100;
}
.menu-content {
background-color: var(--ui-bg-color);
padding: 30px;
border-radius: 8px;
max-width: 80%;
text-align: center;
}
.menu h2 {
font-size: 2rem;
color: var(--ui-text-color);
margin-bottom: 20px;
}
.game-over {
display: none;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.7);
color: white;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
z-index: 10;
}
.game-over h2 {
font-size: 2.5rem;
margin-bottom: 15px;
}
.game-over p {
font-size: 1.5rem;
margin-bottom: 25px;
}
.hud {
position: absolute;
top: 10px;
left: 0;
width: 100%;
display: flex;
justify-content: space-between;
padding: 0 10px;
color: white;
font-size: 1.2rem;
z-index: 5;
text-shadow: 1px 1px 2px #000;
}
.lives-display {
display: flex;
align-items: center;
}
.life-icon {
width: 20px;
height: 20px;
margin-left: 5px;
background-color: #5D5CDE;
clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%);
}
.stage-announcement {
position: absolute;
top: 50%;
left: 0;
width: 100%;
text-align: center;
color: white;
font-size: 2rem;
transform: translateY(-50%);
z-index: 8;
display: none;
}
.touch-controls {
display: none;
position: absolute;
bottom: 20px;
width: 100%;
justify-content: space-between;
padding: 0 20px;
z-index: 5;
}
.direction-btn {
width: 60px;
height: 60px;
background-color: rgba(255, 255, 255, 0.3);
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
user-select: none;
font-size: 24px;
color: white;
}
.fire-btn {
width: 80px;
height: 80px;
background-color: var(--primary-color);
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
user-select: none;
font-size: 16px;
color: white;
}
.paused-overlay {
display: none;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.7);
color: white;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
z-index: 20;
}
.instructions {
margin-top: 20px;
font-size: 0.9rem;
color: var(--ui-text-color);
text-align: left;
max-width: 100%;
}
.instructions h3 {
text-align: center;
margin-bottom: 10px;
}
.instructions ul {
list-style-type: none;
padding: 0 20px;
}
.instructions li {
margin-bottom: 5px;
}
.stars {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
}
/* Media Queries for Responsive Design */
@media (max-width: 768px) {
.game-title {
font-size: 2rem;
margin-bottom: 15px;
}
.touch-controls {
display: flex;
}
.instructions {
font-size: 0.8rem;
}
}
@media (max-width: 480px) {
.game-title {
font-size: 1.8rem;
margin-top: 10px;
}
.menu-content {
padding: 15px;
}
}
</style>
</head>
<body>
<div class="game-title">GALAGA</div>
<div class="game-container">
<canvas id="gameCanvas" class="game-canvas" width="400" height="600"></canvas>
<canvas id="starsCanvas" class="stars" width="400" height="600"></canvas>
<div class="hud">
<div>
<div>SCORE: <span id="score">0</span></div>
<div>HIGH: <span id="highScore">0</span></div>
</div>
<div>
<div>STAGE: <span id="stage">1</span></div>
<div class="lives-display">
LIVES: <span id="livesText">3</span>
<div class="life-icons" id="lifeIcons"></div>
</div>
</div>
</div>
<div id="stageAnnouncement" class="stage-announcement">
STAGE <span id="stageNumber">1</span>
</div>
<div id="menu" class="menu">
<div class="menu-content">
<h2>GALAGA</h2>
<p>The classic arcade space shooter</p>
<button id="startGame">Start Game</button>
<div class="instructions">
<h3>Controls</h3>
<ul>
<li>Left/Right Arrow keys: Move ship</li>
<li>Space: Fire weapon</li>
<li>P: Pause game</li>
<li>Touch controls available on mobile devices</li>
</ul>
<h3>Gameplay</h3>
<ul>
<li>Destroy enemy ships as they fly in formation</li>
<li>Watch out for boss ships that can capture your fighter</li>
<li>Rescue your captured ship to get dual fighters</li>
<li>Different points for different enemy types</li>
</ul>
</div>
</div>
</div>
<div id="gameOver" class="game-over" style="display: none;">
<h2>GAME OVER</h2>
<p>Score: <span id="finalScore">0</span></p>
<button id="restartGame">Play Again</button>
</div>
<div id="pausedOverlay" class="paused-overlay" style="display: none;">
<h2>PAUSED</h2>
<button id="resumeGame">Resume</button>
</div>
<div class="touch-controls">
<div class="direction-btn" id="leftBtn">←</div>
<div class="fire-btn" id="fireBtn">FIRE</div>
<div class="direction-btn" id="rightBtn">→</div>
</div>
</div>
<div class="controls">
<button id="pauseBtn">Pause</button>
<button id="muteBtn">Mute Sound</button>
</div>
<script>
// Check for dark mode preference
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.documentElement.classList.add('dark');
}
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => {
if (event.matches) {
document.documentElement.classList.add('dark');
} else {
document.documentElement.classList.remove('dark');
}
});
// Game Constants
const CANVAS_WIDTH = 400;
const CANVAS_HEIGHT = 600;
const PLAYER_WIDTH = 30;
const PLAYER_HEIGHT = 30;
const PLAYER_SPEED = 5;
const ENEMY_WIDTH = 24;
const ENEMY_HEIGHT = 24;
const BOSS_WIDTH = 30;
const BOSS_HEIGHT = 30;
const BULLET_WIDTH = 3;
const BULLET_HEIGHT = 10;
const BULLET_SPEED = 10;
const ENEMY_BULLET_SPEED = 5;
const FORMATION_ROWS = 5;
const FORMATION_COLS = 10;
const FORMATION_START_Y = 100;
const GRID_SIZE = 30;
const ENEMY_DIVING_SPEED = 3;
const ENEMY_FIRING_CHANCE = 0.005; // Chance per frame per enemy
const TRACTOR_BEAM_SPEED = 1;
const SCORE_REGULAR = 50;
const SCORE_BOSS = 150;
const SCORE_CHALLENGING = 800; // Challenging stage bonus
const SCORE_RESCUE = 1000; // Bonus for rescuing captured ship
// Game State
let gameRunning = false;
let gamePaused = false;
let score = 0;
let highScore = 0;
let lives = 3;
let stage = 1;
let frameCount = 0;
let muted = false;
let starsSpeed = 1;
let dualShips = false;
let capturedShip = false;
let capturingBoss = null;
let gameStage = 'waiting'; // waiting, starting, playing, challenging, stageClear, gameOver
let stageTimeout = null;
let gameOverTimeout = null;
// Canvas and Context
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const starsCanvas = document.getElementById('starsCanvas');
const starsCtx = starsCanvas.getContext('2d');
// Set actual canvas dimensions based on CSS dimensions to prevent stretching
function resizeCanvas() {
const containerWidth = canvas.parentElement.clientWidth;
const containerHeight = (containerWidth / CANVAS_WIDTH) * CANVAS_HEIGHT;
// Set canvas display size (CSS pixels)
canvas.style.width = `${containerWidth}px`;
canvas.style.height = `${containerHeight}px`;
starsCanvas.style.width = `${containerWidth}px`;
starsCanvas.style.height = `${containerHeight}px`;
// Calculate pixel ratio
const dpr = window.devicePixelRatio || 1;
// Set canvas actual size in memory (scaled for DPI)
canvas.width = CANVAS_WIDTH * dpr;
canvas.height = CANVAS_HEIGHT * dpr;
starsCanvas.width = CANVAS_WIDTH * dpr;
starsCanvas.height = CANVAS_HEIGHT * dpr;
// Scale the context to ensure correct drawing operations
ctx.scale(dpr, dpr);
starsCtx.scale(dpr, dpr);
// Scale all drawing operations to maintain aspect ratio
gameScale = CANVAS_WIDTH / containerWidth;
}
// Handle window resize
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
// Player ship
const player = {
x: CANVAS_WIDTH / 2 - PLAYER_WIDTH / 2,
y: CANVAS_HEIGHT - 70,
width: PLAYER_WIDTH,
height: PLAYER_HEIGHT,
speed: PLAYER_SPEED,
bullets: [],
firing: false,
firingCooldown: 0,
isInvulnerable: false,
invulnerabilityTime: 0,
respawnTimer: 0,
isDead: false,
update: function() {
if (!gameRunning || gamePaused || this.isDead) return;
// Update bullets
for (let i = this.bullets.length - 1; i >= 0; i--) {
this.bullets[i].y -= BULLET_SPEED;
// Remove bullets that go off-screen
if (this.bullets[i].y < 0) {
this.bullets.splice(i, 1);
}
}
// Reduce firing cooldown
if (this.firingCooldown > 0) {
this.firingCooldown--;
}
// Handle invulnerability
if (this.isInvulnerable) {
this.invulnerabilityTime--;
if (this.invulnerabilityTime <= 0) {
this.isInvulnerable = false;
}
}
// Respawn timer
if (this.respawnTimer > 0) {
this.respawnTimer--;
if (this.respawnTimer <= 0) {
this.isDead = false;
this.isInvulnerable = true;
this.invulnerabilityTime = 180; // 3 seconds at 60 FPS
}
}
// Fire weapon if firing button is held
if (this.firing && this.firingCooldown <= 0 && !this.isDead) {
this.fire();
}
},
draw: function() {
if (this.isDead) return;
// Skip drawing if player is blinking during invulnerability
if (this.isInvulnerable && frameCount % 6 < 3) {
// Draw only half the frames during invulnerability
} else {
// Draw player ship
ctx.fillStyle = '#5D5CDE';
// Draw the main ship
ctx.beginPath();
ctx.moveTo(this.x + this.width / 2, this.y);
ctx.lineTo(this.x + this.width, this.y + this.height);
ctx.lineTo(this.x, this.y + this.height);
ctx.closePath();
ctx.fill();
// If dual ships, draw the second ship offset to the right
if (dualShips) {
ctx.beginPath();
ctx.moveTo(this.x + this.width / 2 + 20, this.y);
ctx.lineTo(this.x + this.width + 20, this.y + this.height);
ctx.lineTo(this.x + 20, this.y + this.height);
ctx.closePath();
ctx.fill();
}
}
// Draw bullets
ctx.fillStyle = '#FFF';
for (const bullet of this.bullets) {
ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
}
},
fire: function() {
if (this.firingCooldown > 0 || this.isDead) return;
// Create bullet from main ship
this.bullets.push({
x: this.x + this.width / 2 - BULLET_WIDTH / 2,
y: this.y,
width: BULLET_WIDTH,
height: BULLET_HEIGHT
});
// If dual ships, create a second bullet
if (dualShips) {
this.bullets.push({
x: this.x + this.width / 2 - BULLET_WIDTH / 2 + 20,
y: this.y,
width: BULLET_WIDTH,
height: BULLET_HEIGHT
});
}
playSound('shoot');
this.firingCooldown = 15;
},
hit: function() {
if (this.isInvulnerable || this.isDead) return;
// Create explosion
createExplosion(this.x + this.width / 2, this.y + this.height / 2, 40);
playSound('playerHit');
// If dual ships, lose the dual ship first
if (dualShips) {
dualShips = false;
this.isInvulnerable = true;
this.invulnerabilityTime = 120; // 2 seconds at 60 FPS
} else {
lives--;
updateHUD();
if (lives <= 0) {
// Game over
gameOver();
} else {
// Respawn after delay
this.isDead = true;
this.respawnTimer = 120; // 2 seconds at 60 FPS
}
}
},
captured: function() {
if (this.isInvulnerable || this.isDead || capturedShip) return false;
// Player can be captured
capturedShip = true;
this.isDead = true;
this.respawnTimer = 120; // 2 seconds at 60 FPS
playSound('capture');
return true;
},
rescued: function() {
// Player rescued their captured ship
dualShips = true;
capturedShip = false;
playSound('rescue');
score += SCORE_RESCUE;
updateHUD();
}
};
// Enemy formation
const enemies = [];
const enemyBullets = [];
const explosions = [];
const tractorBeams = [];
function createFormation() {
enemies.length = 0; // Clear existing enemies
enemyBullets.length = 0; // Clear bullets
tractorBeams.length = 0; // Clear tractor beams
// Create enemy formation
for (let row = 0; row < FORMATION_ROWS; row++) {
for (let col = 0; col < FORMATION_COLS; col++) {
// Skip some positions to create the Galaga formation shape
if ((row === 0 && (col < 2 || col > 7)) ||
(row === 1 && (col < 1 || col > 8))) {
continue;
}
const isBoss = row === 0 && (col === 3 || col === 6);
enemies.push({
// Initial position off-screen
x: -100,
y: -100,
// Target position in formation
formationX: col * GRID_SIZE + (CANVAS_WIDTH - FORMATION_COLS * GRID_SIZE) / 2,
formationY: row * GRID_SIZE + FORMATION_START_Y,
width: isBoss ? BOSS_WIDTH : ENEMY_WIDTH,
height: isBoss ? BOSS_HEIGHT : ENEMY_HEIGHT,
type: isBoss ? 'boss' : 'regular',
state: 'entering', // entering, formation, diving, returning, capturing, escorting
entryPath: Math.floor(Math.random() * 4), // 0-3 different entry patterns
entryProgress: 0,
diveProgress: 0,
returnProgress: 0,
captured: false,
capturedShipX: 0,
capturedShipY: 0,
hasCapturedShip: false,
diveChance: 0.002 + (stage * 0.0005), // Increases with stage
color: isBoss ? '#FF0000' :
row === 1 ? '#00FFFF' :
row === 2 ? '#FF00FF' :
row === 3 ? '#FFFF00' : '#00FF00'
});
}
}
// Randomize entry order
enemies.sort(() => Math.random() - 0.5);
// Set entry delay for each enemy
for (let i = 0; i < enemies.length; i++) {
enemies[i].entryDelay = i * 10; // Stagger entries
}
}
function createChallengingStage() {
enemies.length = 0;
enemyBullets.length = 0;
tractorBeams.length = 0;
// Create enemy formation for challenging stage (all enemies dive in patterns)
const enemyCount = 20 + stage * 2; // More enemies in later stages
for (let i = 0; i < enemyCount; i++) {
enemies.push({
x: -100,
y: -100,
formationX: -100, // No real formation position
formationY: -100,
width: ENEMY_WIDTH,
height: ENEMY_HEIGHT,
type: 'regular',
state: 'waiting', // waiting, diving
entryPath: i % 4, // Different patterns
entryDelay: i * 20, // Stagger entrances
diveProgress: 0,
color: '#' + Math.floor(Math.random()*16777215).toString(16) // Random color
});
}
}
function updateEnemies() {
if (!gameRunning || gamePaused) return;
// Update existing enemies
for (let i = enemies.length - 1; i >= 0; i--) {
const enemy = enemies[i];
// Handle different enemy states
if (enemy.state === 'entering') {
if (enemy.entryDelay > 0) {
enemy.entryDelay--;
continue;
}
enemy.entryProgress += 0.01;
// Calculate entry path
const t = enemy.entryProgress;
if (enemy.entryPath === 0) {
// Left side entry
enemy.x = CANVAS_WIDTH * (0.5 - 0.5 * Math.cos(t * Math.PI));
enemy.y = CANVAS_HEIGHT * 0.2 * Math.sin(t * Math.PI * 2);
} else if (enemy.entryPath === 1) {
// Right side entry
enemy.x = CANVAS_WIDTH * (0.5 + 0.5 * Math.cos(t * Math.PI));
enemy.y = CANVAS_HEIGHT * 0.2 * Math.sin(t * Math.PI * 2);
} else if (enemy.entryPath === 2) {
// Center sweep
enemy.x = CANVAS_WIDTH * 0.5 + CANVAS_WIDTH * 0.4 * Math.sin(t * Math.PI * 2);
enemy.y = CANVAS_HEIGHT * 0.3 * t;
} else {
// Figure 8 pattern
enemy.x = CANVAS_WIDTH * 0.5 + CANVAS_WIDTH * 0.3 * Math.sin(t * Math.PI * 4);
enemy.y = CANVAS_HEIGHT * 0.3 * (1 - Math.cos(t * Math.PI * 2));
}
// When entry is complete, transition to formation
if (enemy.entryProgress >= 1) {
enemy.state = 'formation';
}
} else if (enemy.state === 'formation') {
// Move towards formation position
const dx = enemy.formationX - enemy.x;
const dy = enemy.formationY - enemy.y;
enemy.x += dx * 0.1;
enemy.y += dy * 0.1;
// Add slight movement to make formation less static
enemy.x += Math.sin(frameCount * 0.05 + i) * 0.5;
enemy.y += Math.sin(frameCount * 0.03 + i) * 0.5;
// Random chance to dive toward player
if (Math.random() < enemy.diveChance) {
enemy.state = 'diving';
enemy.diveTargetX = player.x;
enemy.diveTargetY = CANVAS_HEIGHT + 50;
enemy.diveProgress = 0;
// Boss has a chance to try to capture player's ship
if (enemy.type === 'boss' && !capturedShip && Math.random() < 0.3) {
enemy.state = 'capturing';
enemy.diveProgress = 0;
capturingBoss = enemy;
}
}
// Random chance to fire at player
if (Math.random() < ENEMY_FIRING_CHANCE) {
enemyBullets.push({
x: enemy.x + enemy.width / 2 - BULLET_WIDTH / 2,
y: enemy.y + enemy.height,
width: BULLET_WIDTH,
height: BULLET_HEIGHT,
speed: ENEMY_BULLET_SPEED
});
playSound('enemyShoot');
}
} else if (enemy.state === 'diving') {
enemy.diveProgress += 0.02;
// Calculate dive path
const t = enemy.diveProgress;
const startX = enemy.formationX;
const startY = enemy.formationY;
const controlX = startX + (enemy.diveTargetX - startX) * 0.5;
const controlY = startY - 100; // Control point above start
// Quadratic Bezier curve
enemy.x = (1-t)*(1-t)*startX + 2*(1-t)*t*controlX + t*t*enemy.diveTargetX;
enemy.y = (1-t)*(1-t)*startY + 2*(1-t)*t*controlY + t*t*enemy.diveTargetY;
// When dive is complete, either remove enemy or return to formation
if (enemy.diveProgress >= 1) {
if (gameStage === 'challenging') {
// Remove enemy in challenging stage
enemies.splice(i, 1);
} else {
// Return to formation in normal stage
enemy.state = 'returning';
enemy.returnProgress = 0;
}
}
} else if (enemy.state === 'returning') {
enemy.returnProgress += 0.02;
// Calculate return path
const t = enemy.returnProgress;
const startX = enemy.x;
const startY = enemy.y;
const controlX = startX + (enemy.formationX - startX) * 0.5;
const controlY = startY - 100; // Control point above start
// Quadratic Bezier curve
enemy.x = (1-t)*(1-t)*startX + 2*(1-t)*t*controlX + t*t*enemy.formationX;
enemy.y = (1-t)*(1-t)*startY + 2*(1-t)*t*controlY + t*t*enemy.formationY;
// When return is complete, go back to formation
if (enemy.returnProgress >= 1) {
enemy.state = 'formation';
}
} else if (enemy.state === 'capturing') {
enemy.diveProgress += 0.015;
// Calculate path to player
const t = Math.min(enemy.diveProgress, 1);
const startX = enemy.formationX;
const startY = enemy.formationY;
if (t < 0.7) {
// First part: dive toward player
const segment = t / 0.7;
const controlX = startX;
const controlY = startY + 100;
enemy.x = (1-segment)*(1-segment)*startX + 2*(1-segment)*segment*controlX + segment*segment*player.x;
enemy.y = (1-segment)*(1-segment)*startY + 2*(1-segment)*segment*controlY + segment*segment*player.y;
// If reached player, try to capture
if (t >= 0.69 && t <= 0.7) {
if (player.captured()) {
// Successful capture
enemy.hasCapturedShip = true;
// Store exact position of captured ship relative to boss
enemy.capturedShipX = player.x - enemy.x;
enemy.capturedShipY = player.y - enemy.y;
} else {
// Failed capture attempt
enemy.state = 'returning';
enemy.returnProgress = 0;
capturingBoss = null;
}
}
} else {
// Second part: return to formation with captured ship
const segment = (t - 0.7) / 0.3;
const returnX = enemy.formationX;
const returnY = enemy.formationY;
const controlX = player.x;
const controlY = player.y - 100;
enemy.x = (1-segment)*(1-segment)*player.x + 2*(1-segment)*segment*controlX + segment*segment*returnX;
enemy.y = (1-segment)*(1-segment)*player.y + 2*(1-segment)*segment*controlY + segment*segment*returnY;
// Draw tractor beam during return (first half of return)
if (segment < 0.5 && enemy.hasCapturedShip) {
tractorBeams.push({
x: enemy.x + enemy.width / 2,
y: enemy.y + enemy.height,
width: 20,
height: 100,
alpha: 1 - segment
});
}
}
// When capturing sequence is complete
if (enemy.diveProgress >= 1) {
enemy.state = 'escorting';
capturingBoss = null;
}
} else if (enemy.state === 'escorting') {
// Move towards formation position with slight movement
const dx = enemy.formationX - enemy.x;
const dy = enemy.formationY - enemy.y;
enemy.x += dx * 0.1;
enemy.y += dy * 0.1;
// Add slight movement to make formation less static
enemy.x += Math.sin(frameCount * 0.05 + i) * 0.5;
enemy.y += Math.sin(frameCount * 0.03 + i) * 0.5;
// Random chance to dive with captured ship
if (Math.random() < enemy.diveChance * 0.5) {
enemy.state = 'diving';
enemy.diveTargetX = player.x;
enemy.diveTargetY = CANVAS_HEIGHT + 50;
enemy.diveProgress = 0;
}
} else if (enemy.state === 'waiting') {
// For challenging stage
if (enemy.entryDelay > 0) {
enemy.entryDelay--;
} else {
enemy.state = 'diving';
// Set random entry point from top or sides
const side = Math.floor(Math.random() * 3); // 0=top, 1=left, 2=right
if (side === 0) {
enemy.x = Math.random() * CANVAS_WIDTH;
enemy.y = -ENEMY_HEIGHT;
} else if (side === 1) {
enemy.x = -ENEMY_WIDTH;
enemy.y = Math.random() * CANVAS_HEIGHT * 0.5;
} else {
enemy.x = CANVAS_WIDTH;
enemy.y = Math.random() * CANVAS_HEIGHT * 0.5;
}
// Set random target point at bottom
enemy.diveTargetX = Math.random() * CANVAS_WIDTH;
enemy.diveTargetY = CANVAS_HEIGHT + ENEMY_HEIGHT;
enemy.diveProgress = 0;
}
}
// Check collision with player bullets
for (let j = player.bullets.length - 1; j >= 0; j--) {
const bullet = player.bullets[j];
if (bullet.x < enemy.x + enemy.width &&
bullet.x + bullet.width > enemy.x &&
bullet.y < enemy.y + enemy.height &&
bullet.y + bullet.height > enemy.y) {
// Remove bullet
player.bullets.splice(j, 1);
// If boss with captured ship, release the ship
if (enemy.hasCapturedShip) {
player.rescued();
enemy.hasCapturedShip = false;
}
// Create explosion
createExplosion(enemy.x + enemy.width / 2, enemy.y + enemy.height / 2, 30);
// Add score
score += enemy.type === 'boss' ? SCORE_BOSS : SCORE_REGULAR;
updateHUD();
// Remove enemy
enemies.splice(i, 1);
playSound('explosion');
break;
}
}
// Check collision with player ship
if (!player.isInvulnerable && !player.isDead &&
enemy.x < player.x + player.width &&
enemy.x + enemy.width > player.x &&
enemy.y < player.y + player.height &&
enemy.y + enemy.height > player.y) {
// Player hit
player.hit();
// Create explosion
createExplosion(enemy.x + enemy.width / 2, enemy.y + enemy.height / 2, 30);
// Remove enemy
enemies.splice(i, 1);