-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1749 lines (1616 loc) · 56.1 KB
/
index.html
File metadata and controls
1749 lines (1616 loc) · 56.1 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>Missile Intercept Sim</title>
<style>
:root {
--bg: #08101f;
--panel: rgba(15, 23, 42, 0.92);
--panel2: rgba(30, 41, 59, 0.92);
--text: #eef6ff;
--muted: #a8b8d8;
--accent: #7dd3fc;
--good: #4ade80;
--bad: #fb7185;
--warn: #fbbf24;
}
* { box-sizing: border-box; }
body {
margin: 0;
font-family: Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
background: radial-gradient(circle at top, #0d1a35 0%, #08101f 55%, #060b15 100%);
color: var(--text);
}
.wrap {
max-width: 1240px;
margin: 0 auto;
padding: 16px;
}
h1 {
margin: 0 0 8px;
font-size: 1.55rem;
}
p.desc {
margin: 0 0 16px;
color: var(--muted);
line-height: 1.45;
}
.layout {
display: grid;
grid-template-columns: 330px 1fr;
gap: 16px;
}
.panel {
background: var(--panel);
border: 1px solid rgba(180, 200, 255, 0.12);
border-radius: 16px;
padding: 14px;
box-shadow: 0 14px 28px rgba(0,0,0,.28);
backdrop-filter: blur(6px);
}
.canvas-panel { padding: 10px; }
canvas {
width: 100%;
height: auto;
display: block;
background: linear-gradient(180deg, #244a85 0%, #4f86d1 24%, #89c1ff 48%, #dceffd 58%, #b8d8f0 72%, #6fa3c8 88%, #4a7aa8 100%);
border-radius: 12px;
border: 1px solid rgba(255,255,255,.18);
}
.control-card {
background: rgba(255,255,255,.04);
border: 1px solid rgba(255,255,255,.08);
border-radius: 14px;
padding: 12px;
margin-bottom: 14px;
}
.control-card h2 {
font-size: 1rem;
margin: 0 0 8px;
}
.small {
color: var(--muted);
font-size: .92rem;
line-height: 1.42;
}
.big-angle {
font-size: 2rem;
font-weight: 800;
color: var(--accent);
margin: 8px 0 6px;
font-variant-numeric: tabular-nums;
}
.key-row {
display: flex;
gap: 8px;
flex-wrap: wrap;
margin-top: 10px;
}
.key {
background: rgba(255,255,255,.08);
border: 1px solid rgba(255,255,255,.12);
border-bottom-width: 3px;
border-radius: 10px;
padding: 8px 10px;
min-width: 54px;
text-align: center;
font-weight: 800;
color: #e7f2ff;
}
.btns {
display: flex;
gap: 8px;
flex-wrap: wrap;
margin-bottom: 12px;
}
button {
border: 0;
background: var(--panel2);
color: var(--text);
padding: 10px 12px;
border-radius: 10px;
cursor: pointer;
font-weight: 800;
}
button.primary { background: #2563eb; }
button.good { background: #15803d; }
button:hover { filter: brightness(1.08); }
.stat {
display: grid;
grid-template-columns: 1fr auto;
gap: 8px;
padding: 8px 0;
border-bottom: 1px solid rgba(255,255,255,.08);
font-variant-numeric: tabular-nums;
}
.stat:last-child { border-bottom: 0; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.status {
margin-top: 12px;
padding: 10px 12px;
border-radius: 10px;
background: rgba(255,255,255,.06);
min-height: 52px;
line-height: 1.35;
}
.status.good { border: 1px solid rgba(74, 222, 128, .35); color: #dcfce7; }
.status.bad { border: 1px solid rgba(251, 113, 133, .35); color: #ffe4e6; }
.status.warn { border: 1px solid rgba(251, 191, 36, .35); color: #fef3c7; }
.footer-note {
margin-top: 12px;
color: var(--muted);
font-size: .85rem;
line-height: 1.45;
}
@media (max-width: 900px) {
.layout { grid-template-columns: 1fr; }
}
</style>
</head>
<body>
<div class="wrap">
<h1>Missile Intercept Sim</h1>
<p class="desc">
Random jets appear one at a time between <strong>10,000 and 40,000 ft</strong> and between <strong>Mach 0.6 and Mach 1.8</strong>.
Jets may deploy random <strong>chaff</strong> when your missile gets close. Your missile always flies in a straight line at constant <strong>Mach 2</strong>. Use the keyboard to aim and fire.
</p>
<div class="layout">
<div class="panel">
<div class="control-card">
<h2>Aim controls</h2>
<div class="big-angle" id="angleValue">60°</div>
<div class="small">Use <strong>↑</strong> and <strong>↓</strong> to change the missile angle at any time. Each tap moves <strong>1°</strong>; hold the key for <strong>5°</strong> steps. Press <strong>Space</strong> to fire.</div>
<div class="key-row">
<div class="key">↑</div>
<div class="key">↓</div>
<div class="key">Space</div>
</div>
</div>
<div class="btns" id="startBar">
<button id="startBtn" class="primary">Start Game</button>
</div>
<div>
<div class="stat"><span>Jet altitude</span><span id="planeAlt" class="mono">—</span></div>
<div class="stat"><span>Jet speed</span><span id="planeMach" class="mono">—</span></div>
<div class="stat"><span>Missile angle</span><span id="currentAngleStat" class="mono">—</span></div>
<div class="stat"><span>Time to jet overhead</span><span id="timeToOverhead" class="mono">—</span></div>
<div class="stat"><span>Missile state</span><span id="missileState" class="mono">Ready</span></div>
<div class="stat"><span>Score</span><span id="score" class="mono">0 / 0</span></div>
</div>
<div id="status" class="status warn">
Press <strong>Start game</strong> once to begin (the button then goes away). Aim with the arrow keys and fire with <strong>Space</strong>. After each engagement, a new jet arrives automatically after a short beat.
</div>
<div class="footer-note">
The jet always flies left to right and passes directly over the launcher. Missiles self-destruct at <strong>50,000 ft</strong>.
</div>
</div>
<div class="panel canvas-panel">
<canvas id="sim" width="900" height="560"></canvas>
</div>
</div>
</div>
<script>
(() => {
const SPEED_OF_SOUND_FTPS = 1116;
const MISSILE_SPEED = 2 * SPEED_OF_SOUND_FTPS;
const HIT_RADIUS_FT = 800;
const CHAFF_TRIGGER_RADIUS_FT = 4200;
const CHAFF_EFFECTIVENESS = 0.30;
const CHAFF_DECOY_DELAY_MS = 520;
const CHAFF_TIME_SCALE = 0.3;
const MISSILE_SELF_DESTRUCT_FT = 50000;
const MISSILE_TRAIL_MAX_POINTS = 90;
const PLANE_DRAW_SCALE = 1.1 * 0.7 * 1.1;
/** Nose extent in canvas px (local ~45 × draw scale) — tooltip hides before nose exits */
const PLANE_RIGHT_EXTENT_PX = Math.ceil(45 * PLANE_DRAW_SCALE);
/** Wall-clock delays before spawnPlane so explosions / wreck can play (ms). */
const RESPAWN_AFTER_HIT_MS = 3200;
const RESPAWN_AFTER_CHAFF_DECOY_MS = 1400;
const RESPAWN_AFTER_SELF_DESTRUCT_MS = 1400;
const RESPAWN_AFTER_MISSILE_OOB_MS = 1100;
const RESPAWN_AFTER_PLANE_ESCAPED_MS = 500;
const canvas = document.getElementById('sim');
const ctx = canvas.getContext('2d');
const angleValue = document.getElementById('angleValue');
const planeAltEl = document.getElementById('planeAlt');
const planeMachEl = document.getElementById('planeMach');
const timeToOverheadEl = document.getElementById('timeToOverhead');
const currentAngleStatEl = document.getElementById('currentAngleStat');
const missileStateEl = document.getElementById('missileState');
const scoreEl = document.getElementById('score');
const statusEl = document.getElementById('status');
const startBtn = document.getElementById('startBtn');
const startBar = document.getElementById('startBar');
const world = {
widthFt: 90000,
heightFt: 56000,
launcherX: 45000,
groundY: 0
};
let sessionStarted = false;
let roundActive = false;
let simTime = 0;
let lastFrame = performance.now();
let hits = 0;
let rounds = 0;
let plane = null;
let missile = null;
let outcomeResolved = false;
let angleDeg = 60;
let explosions = [];
let chaffBursts = [];
let chaffInterceptTooltipUntilMs = 0;
let chaffInterceptTooltipX = 0;
let chaffInterceptTooltipY = 0;
/** When `performance.now()` reaches this, call spawnPlane (wall clock, not sim time). */
let autoRespawnAtMs = null;
const SIM_SPEED_MULTIPLIER = 28;
function scheduleRespawn(delayMs) {
autoRespawnAtMs = performance.now() + delayMs;
}
function fmt(n, digits = 1) {
return Number(n).toFixed(digits);
}
function clamp(v, min, max) {
return Math.min(max, Math.max(min, v));
}
function planeIsSupersonic(p) {
return p != null && typeof p.mach === 'number' && p.mach >= 1;
}
function distancePointToSegment(px, py, x1, y1, x2, y2) {
const dx = x2 - x1;
const dy = y2 - y1;
const lenSq = dx * dx + dy * dy;
if (lenSq === 0) return Math.hypot(px - x1, py - y1);
let t = ((px - x1) * dx + (py - y1) * dy) / lenSq;
t = clamp(t, 0, 1);
const cx = x1 + t * dx;
const cy = y1 + t * dy;
return Math.hypot(px - cx, py - cy);
}
function relativeSegmentHitRadius(mPrevX, mPrevY, mX, mY, pPrevX, pPrevY, pX, pY, radius) {
const rx0 = mPrevX - pPrevX;
const ry0 = mPrevY - pPrevY;
const rx1 = mX - pX;
const ry1 = mY - pY;
return distancePointToSegment(0, 0, rx0, ry0, rx1, ry1) <= radius;
}
/** Fuselage hit zone: shorter behind center (tail, −X) vs nose (+X), feet */
const PLANE_HIT_TAIL_HALF_FT = 1400;
const PLANE_HIT_NOSE_HALF_FT = 4000;
function planeBodySegment(px, py) {
return {
x1: px - PLANE_HIT_TAIL_HALF_FT,
y1: py,
x2: px + PLANE_HIT_NOSE_HALF_FT,
y2: py
};
}
function distSqPointSeg(px, py, ax, ay, bx, by) {
const abx = bx - ax;
const aby = by - ay;
const apx = px - ax;
const apy = py - ay;
const abLenSq = abx * abx + aby * aby;
if (abLenSq === 0) return apx * apx + apy * apy;
let t = (apx * abx + apy * aby) / abLenSq;
t = clamp(t, 0, 1);
const cx = ax + t * abx;
const cy = ay + t * aby;
const dx = px - cx;
const dy = py - cy;
return dx * dx + dy * dy;
}
function distSegSeg(ax, ay, bx, by, cx, cy, dx, dy) {
function segPointDistSq(px, py, sx1, sy1, sx2, sy2) {
return distSqPointSeg(px, py, sx1, sy1, sx2, sy2);
}
let minSq = segPointDistSq(ax, ay, cx, cy, dx, dy);
minSq = Math.min(minSq, segPointDistSq(bx, by, cx, cy, dx, dy));
minSq = Math.min(minSq, segPointDistSq(cx, cy, ax, ay, bx, by));
minSq = Math.min(minSq, segPointDistSq(dx, dy, ax, ay, bx, by));
return Math.sqrt(minSq);
}
function missileHitsPlaneBody(mPrevX, mPrevY, mX, mY, pPrevX, pPrevY, pX, pY) {
const prevSeg = planeBodySegment(pPrevX, pPrevY);
const currSeg = planeBodySegment(pX, pY);
const r = HIT_RADIUS_FT;
if (Math.hypot(mX - pX, mY - pY) <= r) return true;
if (distSqPointSeg(mX, mY, currSeg.x1, currSeg.y1, currSeg.x2, currSeg.y2) <= r * r) return true;
if (distSqPointSeg(mPrevX, mPrevY, currSeg.x1, currSeg.y1, currSeg.x2, currSeg.y2) <= r * r) return true;
if (distSegSeg(mPrevX, mPrevY, mX, mY, currSeg.x1, currSeg.y1, currSeg.x2, currSeg.y2) <= r) return true;
if (distSegSeg(mPrevX, mPrevY, mX, mY, prevSeg.x1, prevSeg.y1, prevSeg.x2, prevSeg.y2) <= r) return true;
return relativeSegmentHitRadius(mPrevX, mPrevY, mX, mY, pPrevX, pPrevY, pX, pY, r);
}
function setStatus(text, kind = 'warn') {
statusEl.className = `status ${kind}`;
statusEl.innerHTML = text;
}
function updateAngleLabels() {
angleValue.textContent = `${Math.round(angleDeg)}°`;
currentAngleStatEl.textContent = `${Math.round(angleDeg)}°`;
}
function randomRange(min, max) {
return min + Math.random() * (max - min);
}
function spawnPlane() {
autoRespawnAtMs = null;
const altitude = Math.round(randomRange(10000, 40000) / 100) * 100;
const mach = Math.round(randomRange(0.6, 1.8) * 10) / 10;
const speed = mach * SPEED_OF_SOUND_FTPS;
const spawnX = 6000;
const distanceToOverhead = world.launcherX - spawnX;
const timeToOverheadFromSpawn = distanceToOverhead / speed;
plane = {
spawnX,
x: spawnX,
y: altitude,
mach,
speed,
altitude,
timeToOverheadFromSpawn,
/** When non-null (after a hit), HUD uses this instead of live countdown. */
timeToOverheadFrozen: null,
chaffUsed: false,
chaffWillDecoy: false,
chaffDecoyAtMs: 0,
destroyed: false,
crashing: false,
crashVx: 0,
crashVy: 0,
fireTimer: 0,
wreckAngle: 0,
wreckSpin: 0,
wreckRestAngle: 0,
prevX: spawnX,
prevY: altitude,
grounded: false
};
missile = {
active: false,
launched: false,
x: world.launcherX,
y: 0,
vx: 0,
vy: 0,
angleDeg,
trail: [],
launchTime: null,
flightDuration: null,
prevX: world.launcherX,
prevY: 0
};
simTime = 0;
outcomeResolved = false;
roundActive = true;
explosions = [];
chaffBursts = [];
chaffInterceptTooltipUntilMs = 0;
chaffInterceptTooltipX = 0;
chaffInterceptTooltipY = 0;
planeAltEl.textContent = `${plane.altitude.toLocaleString()} ft`;
planeMachEl.textContent = `Mach ${fmt(plane.mach, 1)}`;
timeToOverheadEl.textContent = `${fmt(plane.timeToOverheadFromSpawn, 1)} s`;
missileStateEl.textContent = 'Ready';
updateAngleLabels();
setStatus(
`New jet: <strong>${plane.altitude.toLocaleString()} ft</strong> at <strong>Mach ${fmt(plane.mach, 1)}</strong>. ` +
`Aim with <strong>↑</strong> and <strong>↓</strong>, then fire with <strong>Space</strong>.`,
'warn'
);
}
function deactivateMissile() {
if (missile && missile.launchTime != null) {
missile.flightDuration = simTime - missile.launchTime;
}
if (missile) missile.active = false;
}
function launchMissile() {
if (!plane || !missile || missile.launched || outcomeResolved) return;
const angleRad = angleDeg * Math.PI / 180;
missile.vx = MISSILE_SPEED * Math.cos(angleRad);
missile.vy = MISSILE_SPEED * Math.sin(angleRad);
missile.launched = true;
missile.active = true;
missile.angleDeg = angleDeg;
missile.x = world.launcherX;
missile.y = 0;
missile.launchTime = simTime;
missile.flightDuration = null;
missile.prevX = missile.x;
missile.prevY = missile.y;
missile.trail = [{ x: missile.x, y: missile.y }];
missileStateEl.textContent = `Launched @ ${Math.round(missile.angleDeg)}°`;
const rel = simTime - plane.timeToOverheadFromSpawn;
setStatus(
`Missile launched at <strong>${Math.round(missile.angleDeg)}°</strong> ` +
`and <strong>${rel >= 0 ? '+' : ''}${fmt(rel, 1)} s</strong> relative to jet overhead.`,
'warn'
);
}
function createExplosion(x, y, kind = 'small') {
const count = kind === 'big' ? 28 : (kind === 'tiny' ? 10 : 16);
const particles = [];
for (let i = 0; i < count; i++) {
const speed = (kind === 'big' ? 1600 : (kind === 'tiny' ? 560 : 900)) * (0.45 + Math.random());
const ang = Math.random() * Math.PI * 2;
particles.push({
x,
y,
vx: Math.cos(ang) * speed,
vy: Math.sin(ang) * speed,
size: (kind === 'big' ? 1500 : (kind === 'tiny' ? 520 : 900)) * (0.5 + Math.random()),
life: 0.85 + Math.random() * (kind === 'big' ? 0.7 : (kind === 'tiny' ? 0.18 : 0.35)),
maxLife: 0.85 + Math.random() * (kind === 'big' ? 0.7 : (kind === 'tiny' ? 0.18 : 0.35)),
hue: Math.random()
});
}
explosions.push({
x,
y,
age: 0,
life: kind === 'big' ? 1.2 : (kind === 'tiny' ? 0.52 : 0.75),
radius: kind === 'big' ? 3400 : (kind === 'tiny' ? 980 : 1800),
kind,
particles
});
}
function createChaffBurst(x, y) {
const particles = [];
for (let i = 0; i < 96; i++) {
const life = randomRange(3.2, 5.2);
particles.push({
x: x + randomRange(-170, 110),
y: y + randomRange(-40, 35),
vx: randomRange(-55, 22),
vy: randomRange(-290, -150),
life,
maxLife: life,
size: randomRange(240, 680)
});
}
chaffBursts.push({ age: 0, life: 5.6, particles });
}
function resolveRound(hit) {
if (outcomeResolved) return;
outcomeResolved = true;
if (!hit) roundActive = false;
rounds += 1;
if (hit) hits += 1;
scoreEl.textContent = `${hits} / ${rounds}`;
missileStateEl.textContent = hit ? 'Hit' : (missile?.launched ? 'Missed' : 'Expired');
if (hit) {
setStatus(
`Hit. Jet at <strong>${plane.altitude.toLocaleString()} ft</strong>, Mach <strong>${fmt(plane.mach, 1)}</strong>. ` +
`The jet is going down in flames.`,
'good'
);
} else {
setStatus(
`Miss. Jet was <strong>${plane.altitude.toLocaleString()} ft</strong> at Mach <strong>${fmt(plane.mach, 1)}</strong>. ` +
`Try a different launch angle or fire timing.`,
'bad'
);
}
}
function update(dt) {
if (!sessionStarted || !plane) return;
simTime += dt;
plane.prevX = plane.x;
plane.prevY = plane.y;
if (!plane.crashing && !plane.grounded) {
plane.x = plane.spawnX + plane.speed * simTime;
} else if (plane.crashing) {
plane.x += plane.crashVx * dt;
plane.y = Math.max(0, plane.y + plane.crashVy * dt);
plane.crashVy -= 620 * dt;
plane.crashVx *= (1 - 0.045 * dt);
plane.wreckAngle += plane.wreckSpin * dt;
plane.wreckSpin += 1.1 * dt;
plane.wreckSpin = Math.min(plane.wreckSpin, 3.2);
if (plane.wreckAngle > 1.35) {
plane.wreckAngle = 1.35;
plane.wreckSpin *= 0.65;
}
plane.fireTimer += dt;
if (plane.fireTimer > 0.07) {
plane.fireTimer = 0;
createExplosion(plane.x + randomRange(-400, 200), plane.y + randomRange(-200, 400), 'small');
}
if (plane.y <= 0) {
plane.y = 0;
plane.crashing = false;
plane.grounded = true;
plane.crashVx = 0;
plane.crashVy = 0;
plane.wreckSpin = 0;
plane.wreckRestAngle = 0.58 + randomRange(0, 0.12);
createExplosion(plane.x, randomRange(500, 1100), 'big');
createExplosion(plane.x + randomRange(-1400, -400), randomRange(400, 900), 'small');
createExplosion(plane.x + randomRange(400, 1400), randomRange(400, 900), 'small');
roundActive = false;
if (plane.destroyed) {
scheduleRespawn(RESPAWN_AFTER_HIT_MS);
}
}
} else if (plane.grounded) {
plane.y = 0;
plane.wreckAngle += (plane.wreckRestAngle - plane.wreckAngle) * Math.min(1, dt * 4.2);
plane.fireTimer += dt;
if (plane.fireTimer > 0.1) {
plane.fireTimer = 0;
createExplosion(
plane.x + randomRange(-900, 900),
randomRange(350, 950),
'small'
);
}
}
const tOver =
plane.timeToOverheadFrozen != null
? plane.timeToOverheadFrozen
: plane.timeToOverheadFromSpawn - simTime;
timeToOverheadEl.textContent = `${tOver >= 0 ? '' : '-'}${fmt(Math.abs(tOver), 1)} s`;
if (missile.active) {
missile.prevX = missile.x;
missile.prevY = missile.y;
missile.x += missile.vx * dt;
missile.y += missile.vy * dt;
missile.trail.push({ x: missile.x, y: missile.y });
if (missile.trail.length > MISSILE_TRAIL_MAX_POINTS) missile.trail.shift();
}
for (const ex of explosions) {
ex.age += dt;
for (const pt of ex.particles) {
pt.life -= dt;
pt.x += pt.vx * dt;
pt.y += pt.vy * dt;
pt.vx *= (1 - 0.9 * dt);
pt.vy *= (1 - 0.9 * dt);
}
ex.particles = ex.particles.filter(pt => pt.life > 0);
}
explosions = explosions.filter(ex => ex.age < ex.life || ex.particles.length);
for (const burst of chaffBursts) {
const cdt = dt * CHAFF_TIME_SCALE;
burst.age += cdt;
for (const pt of burst.particles) {
pt.life -= cdt;
pt.x += pt.vx * cdt;
pt.y += pt.vy * cdt;
pt.vx *= (1 - 0.13 * cdt);
pt.vy *= (1 - 0.05 * cdt);
pt.vy -= 120 * cdt;
}
burst.particles = burst.particles.filter(pt => pt.life > 0);
}
chaffBursts = chaffBursts.filter(burst => burst.age < burst.life || burst.particles.length);
if (plane.chaffWillDecoy) {
if (!missile.active) {
plane.chaffWillDecoy = false;
} else if (performance.now() >= plane.chaffDecoyAtMs) {
plane.chaffWillDecoy = false;
const stillInChaffEnvelope = Math.hypot(missile.x - plane.x, missile.y - plane.y) <= CHAFF_TRIGGER_RADIUS_FT * 1.15;
if (stillInChaffEnvelope) {
createChaffBurst(plane.x - 120, plane.y - 80);
deactivateMissile();
createExplosion(missile.x, missile.y, 'tiny');
chaffInterceptTooltipUntilMs = performance.now() + 2200;
missileStateEl.textContent = 'Decoyed';
setStatus('Jet deployed chaff. Missile lost lock, exploded, and this counts as a miss.', 'bad');
resolveRound(false);
scheduleRespawn(RESPAWN_AFTER_CHAFF_DECOY_MS);
} else {
setStatus('Jet deployed chaff, but the missile stayed on track.', 'warn');
}
}
}
const missileWouldHitNow = !plane.destroyed && missile.active && missileHitsPlaneBody(
missile.prevX, missile.prevY, missile.x, missile.y,
plane.prevX, plane.prevY, plane.x, plane.y
);
if (!plane.destroyed && missile.active && !plane.chaffUsed && !missileWouldHitNow) {
if (Math.hypot(missile.x - plane.x, missile.y - plane.y) <= CHAFF_TRIGGER_RADIUS_FT) {
plane.chaffUsed = true;
if (Math.random() < CHAFF_EFFECTIVENESS) {
createChaffBurst(plane.x - 120, plane.y - 80);
plane.chaffWillDecoy = true;
plane.chaffDecoyAtMs = performance.now() + CHAFF_DECOY_DELAY_MS;
chaffInterceptTooltipX = plane.x;
chaffInterceptTooltipY = plane.y;
chaffInterceptTooltipUntilMs = performance.now() + 2600;
setStatus('Jet deployed chaff. Missile tracking is being disrupted...', 'warn');
} else {
setStatus('Jet deployed chaff, but missile tracking held.', 'warn');
}
}
}
if (!plane.destroyed && missile.active && !plane.chaffWillDecoy) {
if (missileWouldHitNow) {
plane.chaffWillDecoy = false;
deactivateMissile();
plane.timeToOverheadFrozen = plane.timeToOverheadFromSpawn - simTime;
plane.destroyed = true;
plane.crashing = true;
plane.crashVx = plane.speed * 0.62;
plane.crashVy = -1150;
plane.wreckAngle = 0.28;
plane.wreckSpin = 1.35;
createExplosion(plane.x, plane.y, 'big');
createExplosion(plane.x - 1000, plane.y + 300, 'small');
resolveRound(true);
}
}
if (!outcomeResolved) {
if (!plane.grounded && plane.x > world.widthFt + 6000) {
resolveRound(false);
scheduleRespawn(RESPAWN_AFTER_PLANE_ESCAPED_MS);
} else if (missile.launched && missile.active && missile.y >= MISSILE_SELF_DESTRUCT_FT) {
deactivateMissile();
createExplosion(missile.x, missile.y, 'small');
missileStateEl.textContent = 'Self-destruct';
setStatus('Missile self-destructed at <strong>50,000 ft</strong>. Explosion triggered and this counts as a miss.', 'bad');
resolveRound(false);
scheduleRespawn(RESPAWN_AFTER_SELF_DESTRUCT_MS);
} else if (missile.launched && (
missile.x > world.widthFt + 6000 ||
missile.y < -1000
)) {
createExplosion(missile.x, Math.max(missile.y, 0), 'small');
deactivateMissile();
resolveRound(false);
scheduleRespawn(RESPAWN_AFTER_MISSILE_OOB_MS);
}
}
if (sessionStarted && autoRespawnAtMs != null && performance.now() >= autoRespawnAtMs) {
autoRespawnAtMs = null;
spawnPlane();
}
}
function toCanvasX(xFt) {
return (xFt / world.widthFt) * canvas.width;
}
function toCanvasY(yFt) {
const topPadFt = 2500;
return canvas.height - ((yFt + topPadFt) / (world.heightFt + topPadFt)) * (canvas.height - 24) - 10;
}
function drawSkyDecor() {
ctx.save();
ctx.globalAlpha = 0.18;
for (let i = 0; i < 4; i++) {
const x = 110 + i * 180;
const y = 90 + (i % 2) * 40;
ctx.beginPath();
ctx.ellipse(x, y, 42, 14, 0, 0, Math.PI * 2);
ctx.ellipse(x + 28, y - 6, 38, 16, 0, 0, Math.PI * 2);
ctx.ellipse(x + 56, y + 1, 34, 13, 0, 0, Math.PI * 2);
ctx.fillStyle = '#ffffff';
ctx.fill();
}
ctx.restore();
}
function drawGrid() {
ctx.save();
ctx.strokeStyle = 'rgba(255,255,255,0.13)';
ctx.lineWidth = 1;
for (let alt = 10000; alt <= 50000; alt += 5000) {
const y = toCanvasY(alt);
ctx.beginPath();
ctx.moveTo(0, y);
ctx.lineTo(canvas.width, y);
ctx.stroke();
ctx.fillStyle = 'rgba(0,0,0,0.52)';
ctx.font = '12px system-ui';
ctx.fillText(`${alt.toLocaleString()} ft`, 10, y - 6);
}
ctx.restore();
}
function drawGround() {
const groundY = toCanvasY(0);
ctx.save();
ctx.fillStyle = '#8f7349';
ctx.fillRect(0, groundY, canvas.width, canvas.height - groundY);
ctx.fillStyle = '#6f5a3c';
for (let i = 0; i < canvas.width; i += 26) {
ctx.fillRect(i, groundY + 26 + (i % 3), 14, 2);
}
ctx.strokeStyle = 'rgba(0,0,0,.25)';
ctx.beginPath();
ctx.moveTo(0, groundY);
ctx.lineTo(canvas.width, groundY);
ctx.stroke();
ctx.restore();
}
function drawLauncherScenery() {
const lx = toCanvasX(world.launcherX);
const gy = toCanvasY(0);
const w = canvas.width;
ctx.save();
ctx.fillStyle = '#8c8e93';
ctx.beginPath();
ctx.moveTo(0, gy + 18);
ctx.lineTo(0, gy - 42);
ctx.lineTo(w * 0.07, gy - 58);
ctx.lineTo(w * 0.14, gy - 28);
ctx.lineTo(w * 0.22, gy - 78);
ctx.lineTo(w * 0.3, gy - 48);
ctx.lineTo(w * 0.38, gy - 92);
ctx.lineTo(w * 0.46, gy - 36);
ctx.lineTo(w * 0.54, gy - 72);
ctx.lineTo(w * 0.62, gy - 44);
ctx.lineTo(w * 0.7, gy - 86);
ctx.lineTo(w * 0.78, gy - 32);
ctx.lineTo(w * 0.86, gy - 68);
ctx.lineTo(w * 0.93, gy - 50);
ctx.lineTo(w, gy - 38);
ctx.lineTo(w, gy + 18);
ctx.closePath();
ctx.fill();
ctx.fillStyle = '#6d6f74';
ctx.beginPath();
ctx.moveTo(0, gy + 16);
ctx.lineTo(0, gy - 28);
ctx.lineTo(w * 0.09, gy - 52);
ctx.lineTo(w * 0.18, gy - 22);
ctx.lineTo(w * 0.26, gy - 62);
ctx.lineTo(w * 0.35, gy - 18);
ctx.lineTo(w * 0.44, gy - 58);
ctx.lineTo(w * 0.52, gy - 34);
ctx.lineTo(w * 0.6, gy - 66);
ctx.lineTo(w * 0.68, gy - 26);
ctx.lineTo(w * 0.76, gy - 54);
ctx.lineTo(w * 0.84, gy - 20);
ctx.lineTo(w * 0.92, gy - 46);
ctx.lineTo(w, gy - 30);
ctx.lineTo(w, gy + 16);
ctx.closePath();
ctx.fill();
ctx.fillStyle = '#4a5d32';
ctx.beginPath();
ctx.moveTo(0, gy + 20);
ctx.quadraticCurveTo(w * 0.18, gy - 10, w * 0.32, gy + 6);
ctx.quadraticCurveTo(w * 0.42, gy - 6, lx - 78, gy + 5);
ctx.quadraticCurveTo(lx, gy + 9, lx + 78, gy + 6);
ctx.quadraticCurveTo(w * 0.62, gy - 8, w * 0.78, gy + 4);
ctx.quadraticCurveTo(w * 0.92, gy - 4, w, gy + 14);
ctx.lineTo(w, gy + 120);
ctx.lineTo(0, gy + 120);
ctx.closePath();
ctx.fill();
ctx.strokeStyle = 'rgba(55,95,42,0.5)';
ctx.lineWidth = 1.1;
for (let gx = 0; gx < w; gx += 5.5) {
const gTop = gy + 9 + ((gx * 11) % 6);
ctx.beginPath();
ctx.moveTo(gx, gTop);
ctx.quadraticCurveTo(gx + 1.5, gTop - 5, gx + 3, gTop - 9);
ctx.stroke();
}
function drawTree(tx, ty, s) {
ctx.fillStyle = '#4a3d2e';
ctx.fillRect(tx - 2 * s, ty, 4 * s, 12 * s);
ctx.fillStyle = '#2a5230';
ctx.beginPath();
ctx.moveTo(tx, ty - 26 * s);
ctx.lineTo(tx - 13 * s, ty - 1);
ctx.lineTo(tx + 13 * s, ty - 1);
ctx.closePath();
ctx.fill();
ctx.fillStyle = '#3d6e3f';
ctx.beginPath();
ctx.moveTo(tx, ty - 36 * s);
ctx.lineTo(tx - 9 * s, ty - 14 * s);
ctx.lineTo(tx + 9 * s, ty - 14 * s);
ctx.closePath();
ctx.fill();
}
for (let i = 0; ; i++) {
const gx = 26 + i * 112 + ((i * 37) % 34);
if (gx > w - 26) break;
const hMix = (i * 53 + (i * i) % 19) % 78;
const s = 0.34 + hMix * 0.011;
const tyOff = ((i * 41) % 11) - 4;
drawTree(gx, gy + 5 + tyOff, s);
}
ctx.restore();
}
function drawLauncher() {
const x = toCanvasX(world.launcherX);
const y = toCanvasY(0);
const angleRad = angleDeg * Math.PI / 180;
ctx.save();
ctx.translate(x, y);
ctx.scale(0.5, 0.5);
ctx.translate(-x, -y);
ctx.fillStyle = 'rgba(0,0,0,0.2)';
ctx.beginPath();
ctx.ellipse(x, y + 4, 44, 11, 0, 0, Math.PI * 2);
ctx.fill();
const hullGrad = ctx.createLinearGradient(x - 50, y - 20, x + 50, y + 4);
hullGrad.addColorStop(0, '#243044');
hullGrad.addColorStop(0.2, '#3d4f66');
hullGrad.addColorStop(0.45, '#4a5f78');
hullGrad.addColorStop(0.55, '#3d4f66');
hullGrad.addColorStop(0.8, '#2a3545');
hullGrad.addColorStop(1, '#1a222e');
ctx.fillStyle = hullGrad;
ctx.beginPath();
ctx.roundRect(x - 36, y - 18, 72, 20, 7);
ctx.fill();
ctx.strokeStyle = 'rgba(0,0,0,0.4)';
ctx.lineWidth = 2;
ctx.stroke();
ctx.strokeStyle = 'rgba(255,255,255,0.08)';
ctx.beginPath();
ctx.roundRect(x - 35, y - 17, 70, 18, 6);
ctx.stroke();
const wheelY = y - 1;
for (const wx of [x - 22, x, x + 22]) {
const wg = ctx.createRadialGradient(wx - 2.5, wheelY - 2.5, 0, wx, wheelY, 10);
wg.addColorStop(0, '#6b7788');
wg.addColorStop(0.55, '#3d4554');
wg.addColorStop(1, '#151a22');
ctx.fillStyle = wg;
ctx.beginPath();
ctx.arc(wx, wheelY, 9, 0, Math.PI * 2);
ctx.fill();
ctx.strokeStyle = 'rgba(0,0,0,0.45)';
ctx.lineWidth = 2;
ctx.stroke();
ctx.fillStyle = 'rgba(255,255,255,0.2)';
ctx.beginPath();
ctx.arc(wx - 2.5, wheelY - 2.5, 2.8, 0, Math.PI * 2);
ctx.fill();
}
const pedGrad = ctx.createLinearGradient(x - 18, y - 36, x + 18, y - 22);
pedGrad.addColorStop(0, '#6b7280');
pedGrad.addColorStop(0.5, '#4b5563');
pedGrad.addColorStop(1, '#374151');
ctx.fillStyle = pedGrad;
ctx.beginPath();
ctx.roundRect(x - 20, y - 38, 40, 18, 6);
ctx.fill();
ctx.strokeStyle = 'rgba(0,0,0,0.35)';
ctx.lineWidth = 2;
ctx.stroke();
ctx.translate(x, y - 28);
ctx.rotate(-angleRad);
const railL = 46;
const railH = 13;
const railGrad = ctx.createLinearGradient(0, -railH, 0, railH);
railGrad.addColorStop(0, '#9ca3af');
railGrad.addColorStop(0.35, '#e2e8f0');
railGrad.addColorStop(0.5, '#cbd5e1');
railGrad.addColorStop(0.65, '#94a3b8');
railGrad.addColorStop(1, '#64748b');
ctx.fillStyle = railGrad;
ctx.beginPath();
ctx.roundRect(2, -railH / 2, railL, railH, 3.5);
ctx.fill();
ctx.strokeStyle = 'rgba(30,41,59,0.55)';
ctx.lineWidth = 2;
ctx.stroke();
ctx.strokeStyle = 'rgba(0,0,0,0.18)';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(10, 0);
ctx.lineTo(railL - 4, 0);
ctx.stroke();
ctx.fillStyle = '#475569';
ctx.beginPath();
ctx.roundRect(railL + 1, -9, 8, 18, 2);
ctx.fill();
ctx.fillStyle = '#1e293b';
ctx.fillRect(railL + 6, -7, 4, 14);
ctx.fillStyle = 'rgba(255,255,255,0.15)';
ctx.fillRect(railL + 2, -7, 2, 14);
if (missile && !missile.launched) {
ctx.save();
ctx.translate(28, 0);
ctx.scale(0.5, 0.5);
ctx.translate(-28, 0);
const mg = ctx.createLinearGradient(14, 0, 48, 0);
mg.addColorStop(0, '#475569');
mg.addColorStop(0.4, '#94a3b8');
mg.addColorStop(0.75, '#cbd5e1');
mg.addColorStop(1, '#f8fafc');
ctx.fillStyle = mg;
ctx.beginPath();
ctx.moveTo(46, 0);