-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
2672 lines (2664 loc) · 111 KB
/
index.html
File metadata and controls
2672 lines (2664 loc) · 111 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="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover">
<title>starship-sim : Starship Simulation</title>
<style>
html, body {
width: 100%;
height: 100%;
overflow: hidden;
}
html {
min-height: -webkit-fill-available;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
user-select: none;
-webkit-user-select: none;
-webkit-touch-callout: none;
}
body {
background: #000;
font-family: 'Courier New', monospace;
overflow: hidden;
width: 100%;
height: 100vh;
min-height: 100vh;
height: 100dvh;
min-height: 100dvh;
min-height: -webkit-fill-available;
position: fixed;
inset: 0;
padding-top: env(safe-area-inset-top, 0px);
padding-right: env(safe-area-inset-right, 0px);
padding-bottom: env(safe-area-inset-bottom, 0px);
padding-left: env(safe-area-inset-left, 0px);
}
canvas {
display: block;
background: #001a33;
width: 100%;
height: 100%;
touch-action: none;
}
#info {
position: absolute;
bottom: 8px;
left: 8px;
color: #0f0;
font-size: 9px;
line-height: 1.25;
background: rgba(0, 0, 0, 0.45);
padding: 6px 7px;
border: 1px solid #0f0;
z-index: 10;
max-width: 150px;
}
#github-link {
position: absolute;
display: inline-flex;
align-items: center;
gap: 4px;
padding: 4px 6px;
background: rgba(0, 0, 0, 0.45);
border: 1px solid #0f0;
color: #0f0;
text-decoration: none;
font-size: 10px;
line-height: 1;
font-family: 'Courier New', monospace;
z-index: 10;
}
#github-link:hover {
background: rgba(0, 100, 150, 0.9);
box-shadow: 0 0 10px rgba(0, 255, 0, 0.5);
}
.github-icon {
width: 12px;
height: 12px;
display: block;
}
#test-buttons {
position: absolute;
display: flex;
flex-direction: column;
gap: 6px;
z-index: 10;
}
.test-btn {
padding: 6px 10px;
background: rgba(0, 50, 100, 0.7);
color: #0f0;
border: 1px solid #0f0;
cursor: pointer;
font-size: 10px;
line-height: 1.1;
font-family: 'Courier New', monospace;
transition: all 0.2s;
}
.test-btn:hover {
background: rgba(0, 100, 150, 0.9);
box-shadow: 0 0 10px rgba(0, 255, 0, 0.5);
}
#bottom-controls {
position: absolute;
display: flex;
flex-direction: column;
align-items: flex-end;
gap: 8px;
width: fit-content;
max-width: calc(100vw - 16px);
z-index: 10;
}
#action-controls {
display: flex;
flex-wrap: nowrap;
align-items: center;
justify-content: flex-end;
gap: 6px;
width: fit-content;
max-width: 100%;
}
#wreckage-toggle {
padding: 6px 10px;
background: rgba(0, 50, 100, 0.7);
color: #0f0;
border: 1px solid #0f0;
cursor: pointer;
font-size: 10px;
line-height: 1.2;
white-space: nowrap;
font-family: 'Courier New', monospace;
transition: all 0.2s;
}
#wreckage-toggle:hover {
background: rgba(0, 100, 150, 0.9);
box-shadow: 0 0 10px rgba(0, 255, 0, 0.5);
}
#wreckage-toggle.active {
background: rgba(200, 100, 0, 0.8);
border: 1px solid rgba(255, 200, 0, 0.8);
color: rgba(255, 200, 0, 0.9);
box-shadow: 0 0 10px rgba(255, 150, 0, 0.6);
}
#beacon-controls {
display: flex;
flex-wrap: nowrap;
justify-content: flex-end;
gap: 4px;
width: fit-content;
}
.beacon-team-btn {
display: inline-flex;
align-items: center;
gap: 4px;
padding: 6px 8px;
background: rgba(0, 50, 100, 0.7);
color: #0f0;
border: 1px solid #0f0;
cursor: pointer;
font-size: 12px;
font-weight: bold;
line-height: 1;
font-family: 'Courier New', monospace;
transition: all 0.2s;
}
.beacon-icon {
width: 12px;
height: 12px;
fill: none;
stroke: currentColor;
stroke-width: 1.2;
stroke-linecap: round;
stroke-linejoin: round;
}
.beacon-label {
min-width: 0.7em;
text-align: center;
}
.beacon-team-btn:hover {
background: rgba(0, 100, 150, 0.9);
box-shadow: 0 0 10px rgba(0, 255, 0, 0.5);
}
.beacon-team-btn.active {
background: rgba(100, 200, 255, 0.8);
border: 2px solid rgba(153, 221, 255, 0.9);
color: rgba(51, 181, 255, 1);
box-shadow: 0 0 10px rgba(153, 221, 255, 0.6);
}
#zoom-controls {
position: absolute;
right: 8px;
bottom: 8px;
width: fit-content;
max-width: calc(100vw - 16px);
padding: 5px 8px;
background: transparent;
color: #0f0;
font-size: 10px;
line-height: 1.1;
font-family: 'Courier New', monospace;
z-index: 10;
}
#zoom-stepper {
display: flex;
align-items: center;
justify-content: center;
gap: 10px;
}
#zoom-value {
min-width: 38px;
text-align: center;
font-weight: bold;
font-size: 16px;
line-height: 1;
}
#zoom-out, #zoom-in {
-webkit-appearance: none;
appearance: none;
touch-action: manipulation;
width: 38px;
height: 38px;
padding: 0;
display: inline-flex;
align-items: center;
justify-content: center;
border: none;
background: transparent;
box-shadow: none;
color: #0f0;
font-family: 'Courier New', monospace;
cursor: pointer;
}
.zoom-icon {
width: 34px;
height: 34px;
fill: none;
stroke: currentColor;
stroke-width: 1.6;
stroke-linecap: round;
stroke-linejoin: round;
}
#zoom-out:disabled, #zoom-in:disabled {
opacity: 0.35;
cursor: default;
}
#zoom-out:hover:not(:disabled), #zoom-in:hover:not(:disabled) {
color: #66ff66;
}
#zoom-out:focus, #zoom-in:focus {
outline: none;
box-shadow: none;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<a
id="github-link"
href="https://github.com/igapyon/starship-sim"
target="_blank"
rel="noopener noreferrer"
aria-label="GitHub repository"
>
<svg aria-hidden="true" viewBox="0 0 16 16" class="github-icon" fill="currentColor">
<path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0 0 16 8c0-4.42-3.58-8-8-8Z"/>
</svg>
<span>GitHub</span>
</a>
<!-- 左下:情報パネル(チームA:水色の情報表示) -->
<div id="info"></div>
<div id="test-buttons">
<button class="test-btn" data-config="mixed1c8vsmixed1c8">L1D2C8</button>
<button class="test-btn" data-config="corvette8vsdestroyer2">C8vsD2</button>
<button class="test-btn" data-config="lightcruiser1vsdestroyer2">L1vsD2</button>
<button class="test-btn" data-config="mass_d1c8">Mass D1C8</button>
<button class="test-btn" data-config="threeway_l1d4c32">L1D4C16</button>
</div>
<div id="zoom-controls">
<div id="zoom-stepper">
<button id="zoom-out" type="button" aria-label="Zoom out">
<svg class="zoom-icon" viewBox="0 0 16 16" aria-hidden="true">
<circle cx="6.5" cy="6.5" r="4.5"></circle>
<line x1="10.2" y1="10.2" x2="14" y2="14"></line>
<line x1="4.2" y1="6.5" x2="8.8" y2="6.5"></line>
</svg>
</button>
<span id="zoom-value">1x</span>
<button id="zoom-in" type="button" aria-label="Zoom in">
<svg class="zoom-icon" viewBox="0 0 16 16" aria-hidden="true">
<circle cx="6.5" cy="6.5" r="4.5"></circle>
<line x1="10.2" y1="10.2" x2="14" y2="14"></line>
<line x1="4.2" y1="6.5" x2="8.8" y2="6.5"></line>
<line x1="6.5" y1="4.2" x2="6.5" y2="8.8"></line>
</svg>
</button>
</div>
</div>
<div id="bottom-controls">
<div id="action-controls">
<!-- ビーコン配置チーム選択 -->
<div id="beacon-controls">
<button class="beacon-team-btn active" data-team="A" aria-label="Beacon team A">
<svg class="beacon-icon" viewBox="0 0 16 16" aria-hidden="true">
<circle cx="8" cy="8" r="1.5"></circle>
<path d="M8 4.5a3.5 3.5 0 0 1 0 7"></path>
<path d="M8 2a6 6 0 0 1 0 12"></path>
<path d="M8 4.5a3.5 3.5 0 0 0 0 7"></path>
<path d="M8 2a6 6 0 0 0 0 12"></path>
</svg>
<span class="beacon-label">A</span>
</button>
<button class="beacon-team-btn" data-team="B" aria-label="Beacon team B">
<svg class="beacon-icon" viewBox="0 0 16 16" aria-hidden="true">
<circle cx="8" cy="8" r="1.5"></circle>
<path d="M8 4.5a3.5 3.5 0 0 1 0 7"></path>
<path d="M8 2a6 6 0 0 1 0 12"></path>
<path d="M8 4.5a3.5 3.5 0 0 0 0 7"></path>
<path d="M8 2a6 6 0 0 0 0 12"></path>
</svg>
<span class="beacon-label">B</span>
</button>
<button class="beacon-team-btn" data-team="C" aria-label="Beacon team C">
<svg class="beacon-icon" viewBox="0 0 16 16" aria-hidden="true">
<circle cx="8" cy="8" r="1.5"></circle>
<path d="M8 4.5a3.5 3.5 0 0 1 0 7"></path>
<path d="M8 2a6 6 0 0 1 0 12"></path>
<path d="M8 4.5a3.5 3.5 0 0 0 0 7"></path>
<path d="M8 2a6 6 0 0 0 0 12"></path>
</svg>
<span class="beacon-label">C</span>
</button>
</div>
<!-- 残骸攻撃トグルボタン -->
<button id="wreckage-toggle">残骸を無視する</button>
</div>
</div>
<script>
// @ts-nocheck
const WORLD_SIZE = 720;
const MIN_ZOOM = 0.5;
const AUTO_FOCUS_DELAY_MS = 3000;
const SHIP_PICK_TOLERANCE = 8;
const ZOOM_STEPS = [0.4, 0.5, 0.625, 0.75, 0.875, 1, 1.25, 1.5, 1.75, 2, 3, 4];
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const infoDiv = document.getElementById('info');
const githubLink = document.getElementById('github-link');
const testButtons = document.getElementById('test-buttons');
const bottomControls = document.getElementById('bottom-controls');
const zoomControls = document.getElementById('zoom-controls');
const zoomOutButton = document.getElementById('zoom-out');
const zoomInButton = document.getElementById('zoom-in');
const zoomValueLabel = document.getElementById('zoom-value');
let hasInitializedZoom = false;
let hasUserAdjustedZoom = false;
const renderState = {
dpr: 1,
viewportWidth: 0,
viewportHeight: 0,
worldScale: 1,
offsetX: 0,
offsetY: 0,
zoomLevels: [MIN_ZOOM, 1],
zoom: 1
};
const followState = {
selectedShip: null,
preferredTeamId: null,
lastKnownX: WORLD_SIZE / 2,
lastKnownY: WORLD_SIZE / 2,
pendingAutoFocusAt: 0,
forceCenterOnNextFrame: false
};
function getAllShips() {
if (typeof TEAM_IDS === 'undefined' || typeof teamShips === 'undefined')
return [];
return TEAM_IDS.flatMap((teamId) => teamShips[teamId] || []);
}
function getShipTeamId(ship) {
if (!ship || typeof TEAM_IDS === 'undefined' || typeof teamShips === 'undefined')
return null;
for (const teamId of TEAM_IDS) {
const ships = teamShips[teamId] || [];
if (ships.includes(ship))
return teamId;
}
if (typeof TEAM_COLORS !== 'undefined') {
for (const teamId of TEAM_IDS) {
if (TEAM_COLORS[teamId] === ship.color)
return teamId;
}
}
return null;
}
function getPreferredAliveShip(preferredTeamId) {
const hasPropulsion = (ship) => ship && ship.isAlive() && ship.engines && ship.engines.length > 0;
if (preferredTeamId && typeof teamShips !== 'undefined') {
const preferredShips = teamShips[preferredTeamId] || [];
for (const ship of preferredShips) {
if (hasPropulsion(ship))
return ship;
}
}
// 同陣営に候補がいない場合でも、まずは推進力ありを優先
const allShips = getAllShips();
for (const ship of allShips) {
if (hasPropulsion(ship))
return ship;
}
return null;
}
function selectFollowShip(ship, forceCenter = true) {
followState.selectedShip = ship || null;
if (ship) {
followState.lastKnownX = ship.x;
followState.lastKnownY = ship.y;
const teamId = getShipTeamId(ship);
if (teamId)
followState.preferredTeamId = teamId;
}
followState.pendingAutoFocusAt = 0;
followState.forceCenterOnNextFrame = forceCenter;
}
function isShipFollowable(ship) {
if (!ship || !ship.isAlive())
return false;
if (!ship.engines || ship.engines.length === 0)
return false;
return getAllShips().includes(ship);
}
function isWorldFullyVisible() {
const worldPixelSize = WORLD_SIZE * renderState.zoom;
return worldPixelSize <= renderState.viewportWidth && worldPixelSize <= renderState.viewportHeight;
}
function isShipFollowModeActive() {
const beaconTeam = getSelectedBeaconTeamSafe();
if (typeof beaconTeam === 'undefined')
return false;
return beaconTeam === null && !isWorldFullyVisible();
}
function getSelectedBeaconTeamSafe() {
try {
return selectedBeaconTeam;
}
catch {
return undefined;
}
}
function getFocusPoint() {
const followModeActive = isShipFollowModeActive();
if (!followModeActive) {
followState.pendingAutoFocusAt = 0;
return null;
}
// 追尾モード開始時に選択がない場合は、生存船先頭へ自動フォーカスする
if (!followState.selectedShip) {
const nextShip = getPreferredAliveShip(followState.preferredTeamId);
if (nextShip) {
selectFollowShip(nextShip, true);
return { x: nextShip.x, y: nextShip.y };
}
}
if (isShipFollowable(followState.selectedShip)) {
followState.pendingAutoFocusAt = 0;
followState.lastKnownX = followState.selectedShip.x;
followState.lastKnownY = followState.selectedShip.y;
const selectedTeamId = getShipTeamId(followState.selectedShip);
if (selectedTeamId)
followState.preferredTeamId = selectedTeamId;
return { x: followState.selectedShip.x, y: followState.selectedShip.y };
}
if (followState.selectedShip) {
followState.lastKnownX = followState.selectedShip.x;
followState.lastKnownY = followState.selectedShip.y;
}
const now = performance.now();
if (followState.pendingAutoFocusAt === 0) {
followState.pendingAutoFocusAt = now + AUTO_FOCUS_DELAY_MS;
return { x: followState.lastKnownX, y: followState.lastKnownY };
}
if (now < followState.pendingAutoFocusAt) {
return { x: followState.lastKnownX, y: followState.lastKnownY };
}
const nextShip = getPreferredAliveShip(followState.preferredTeamId);
if (nextShip) {
selectFollowShip(nextShip, true);
return { x: nextShip.x, y: nextShip.y };
}
followState.pendingAutoFocusAt = 0;
followState.selectedShip = null;
return { x: followState.lastKnownX, y: followState.lastKnownY };
}
function computeCenteredOffset(viewportSize, worldPixelSize, targetWorldPos) {
const desiredOffset = Math.floor(viewportSize / 2 - targetWorldPos * renderState.worldScale);
const minOffset = Math.floor(viewportSize - worldPixelSize);
if (minOffset <= 0) {
return clamp(desiredOffset, minOffset, 0);
}
return Math.floor((viewportSize - worldPixelSize) / 2);
}
function formatZoomLabel(zoom) {
if (!Number.isFinite(zoom) || zoom <= 0)
return '100%';
return `${Math.max(1, Math.round(zoom * 100))}%`;
}
function updateZoomControls() {
const maxFittableZoom = Math.min(renderState.viewportWidth, renderState.viewportHeight) / WORLD_SIZE;
const alwaysMinZoom = 0.4;
const alwaysMaxZoom = 2;
const selectableMaxZoom = Math.max(maxFittableZoom, alwaysMaxZoom);
const levels = ZOOM_STEPS.filter((zoom) => (zoom >= alwaysMinZoom && zoom <= alwaysMaxZoom) ||
(zoom > alwaysMaxZoom && zoom <= selectableMaxZoom + 0.000001));
if (levels.length === 0)
levels.push(MIN_ZOOM);
if (levels.length === 1) {
const nextLevel = ZOOM_STEPS.find((zoom) => zoom > levels[0]);
if (nextLevel !== undefined)
levels.push(nextLevel);
}
renderState.zoomLevels = levels;
if (!Number.isFinite(renderState.zoom) || renderState.zoom <= 0) {
renderState.zoom = 1;
}
const currentIndex = levels.indexOf(renderState.zoom);
if (currentIndex === -1) {
let nearest = levels[0];
let bestDelta = Math.abs(renderState.zoom - nearest);
for (const level of levels) {
const delta = Math.abs(renderState.zoom - level);
if (delta < bestDelta) {
nearest = level;
bestDelta = delta;
}
}
renderState.zoom = nearest;
}
const nextIndex = levels.indexOf(renderState.zoom);
zoomOutButton.disabled = nextIndex <= 0;
zoomInButton.disabled = nextIndex >= levels.length - 1;
zoomValueLabel.textContent = formatZoomLabel(renderState.zoom);
}
function getInitialFitZoom() {
const maxFittableZoom = Math.min(renderState.viewportWidth, renderState.viewportHeight) / WORLD_SIZE;
const fitLevels = ZOOM_STEPS.filter((zoom) => zoom <= maxFittableZoom + 0.000001);
if (fitLevels.length > 0)
return fitLevels[fitLevels.length - 1];
// 画面が極端に小さい場合は最小ズームで開始
return ZOOM_STEPS[0];
}
function recomputeRenderState() {
const worldPixelSize = WORLD_SIZE * renderState.zoom;
renderState.worldScale = renderState.zoom;
const focusPoint = getFocusPoint();
if (!focusPoint) {
renderState.offsetX = Math.floor((renderState.viewportWidth - worldPixelSize) / 2);
renderState.offsetY = Math.floor((renderState.viewportHeight - worldPixelSize) / 2);
return;
}
const focusX = clamp(focusPoint.x, 0, WORLD_SIZE);
const focusY = clamp(focusPoint.y, 0, WORLD_SIZE);
if (followState.forceCenterOnNextFrame) {
renderState.offsetX = computeCenteredOffset(renderState.viewportWidth, worldPixelSize, focusX);
renderState.offsetY = computeCenteredOffset(renderState.viewportHeight, worldPixelSize, focusY);
followState.forceCenterOnNextFrame = false;
return;
}
// 追尾モード中は選択船を常時センタリング(全体が収まる場合はcomputeCenteredOffset内で固定表示)
renderState.offsetX = computeCenteredOffset(renderState.viewportWidth, worldPixelSize, focusX);
renderState.offsetY = computeCenteredOffset(renderState.viewportHeight, worldPixelSize, focusY);
}
function resetFollowSelectionState() {
followState.selectedShip = null;
followState.preferredTeamId = null;
followState.lastKnownX = WORLD_SIZE / 2;
followState.lastKnownY = WORLD_SIZE / 2;
followState.pendingAutoFocusAt = 0;
followState.forceCenterOnNextFrame = false;
recomputeRenderState();
requestAnimationFrame(layoutUi);
}
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function layoutUi() {
const margin = 8;
const worldPixelSize = WORLD_SIZE * renderState.worldScale;
const worldLeft = renderState.offsetX;
const worldTop = renderState.offsetY;
const worldRight = worldLeft + worldPixelSize;
const worldBottom = worldTop + worldPixelSize;
const leftGutter = worldLeft - margin;
const rightGutter = renderState.viewportWidth - worldRight - margin;
const topGutter = worldTop - margin;
const bottomGutter = renderState.viewportHeight - worldBottom - margin;
function placeTopLeft(element) {
const width = element.offsetWidth;
const height = element.offsetHeight;
let left;
let top;
if (leftGutter >= width) {
left = worldLeft - width - margin;
top = worldTop;
}
else if (topGutter >= height) {
left = worldLeft;
top = worldTop - height - margin;
}
else {
left = margin;
top = margin;
}
element.style.left = `${Math.round(clamp(left, margin, renderState.viewportWidth - width - margin))}px`;
element.style.top = `${Math.round(clamp(top, margin, renderState.viewportHeight - height - margin))}px`;
}
function placeTopRight(element) {
const width = element.offsetWidth;
const height = element.offsetHeight;
let left;
let top;
if (rightGutter >= width) {
left = worldRight + margin;
top = worldTop;
}
else if (topGutter >= height) {
left = worldRight - width;
top = worldTop - height - margin;
}
else {
left = renderState.viewportWidth - width - margin;
top = margin;
}
element.style.left = `${Math.round(clamp(left, margin, renderState.viewportWidth - width - margin))}px`;
element.style.top = `${Math.round(clamp(top, margin, renderState.viewportHeight - height - margin))}px`;
}
function placeBottomRight(element) {
const width = element.offsetWidth;
const height = element.offsetHeight;
let left;
let top;
if (rightGutter >= width) {
left = worldRight + margin;
// 右ガター配置時も、ズームは上下に揺れないよう常に下寄せを優先する
top = renderState.viewportHeight - height - margin;
}
else if (bottomGutter >= height) {
left = worldRight - width;
top = worldBottom + margin;
}
else {
left = renderState.viewportWidth - width - margin;
top = renderState.viewportHeight - height - margin;
}
element.style.left = `${Math.round(clamp(left, margin, renderState.viewportWidth - width - margin))}px`;
element.style.top = `${Math.round(clamp(top, margin, renderState.viewportHeight - height - margin))}px`;
}
function placeBottomLeft(element) {
const width = element.offsetWidth;
const height = element.offsetHeight;
let left;
let top;
if (leftGutter >= width) {
left = worldLeft - width - margin;
top = worldBottom - height;
}
else if (bottomGutter >= height) {
left = worldLeft;
top = worldBottom + margin;
}
else {
left = margin;
top = renderState.viewportHeight - height - margin;
}
element.style.left = `${Math.round(clamp(left, margin, renderState.viewportWidth - width - margin))}px`;
element.style.top = `${Math.round(clamp(top, margin, renderState.viewportHeight - height - margin))}px`;
}
infoDiv.style.bottom = 'auto';
infoDiv.style.right = 'auto';
githubLink.style.bottom = 'auto';
githubLink.style.right = 'auto';
placeTopLeft(testButtons);
placeTopRight(bottomControls);
placeBottomRight(zoomControls);
placeBottomLeft(infoDiv);
const githubGap = 6;
const githubLeft = infoDiv.offsetLeft;
const githubTop = clamp(infoDiv.offsetTop - githubLink.offsetHeight - githubGap, margin, renderState.viewportHeight - githubLink.offsetHeight - margin);
githubLink.style.left = `${Math.round(githubLeft)}px`;
githubLink.style.top = `${Math.round(githubTop)}px`;
}
function resizeCanvas() {
const viewport = window.visualViewport;
renderState.dpr = window.devicePixelRatio || 1;
const innerWidth = Math.floor(window.innerWidth);
const innerHeight = Math.floor(window.innerHeight);
const clientWidth = Math.floor(document.documentElement ? document.documentElement.clientWidth : 0);
const clientHeight = Math.floor(document.documentElement ? document.documentElement.clientHeight : 0);
const screenWidth = Math.floor(window.screen ? window.screen.width : 0);
const screenHeight = Math.floor(window.screen ? window.screen.height : 0);
const viewportWidth = Math.floor(viewport ? viewport.width : innerWidth);
const viewportHeight = Math.floor(viewport ? viewport.height : innerHeight);
function pickDimension(primary, secondary, tertiary, screenValue) {
const fallback = Math.max(primary, secondary, tertiary, screenValue, 1);
const suspiciousByScreen = screenValue > 0 && primary < screenValue * 0.25;
const suspiciousBySecondaryLarge = secondary > 0 && primary > secondary * 1.3;
const suspiciousByTertiaryLarge = tertiary > 0 && primary > tertiary * 1.3;
if (primary <= 0 || suspiciousByScreen || suspiciousBySecondaryLarge || suspiciousByTertiaryLarge) {
if (secondary > 0 && !(screenValue > 0 && secondary < screenValue * 0.25))
return secondary;
if (tertiary > 0 && !(screenValue > 0 && tertiary < screenValue * 0.25))
return tertiary;
if (screenValue > 0)
return screenValue;
return fallback;
}
return primary;
}
renderState.viewportWidth = pickDimension(viewportWidth, innerWidth, clientWidth, screenWidth);
renderState.viewportHeight = pickDimension(viewportHeight, innerHeight, clientHeight, screenHeight);
canvas.width = Math.floor(renderState.viewportWidth * renderState.dpr);
canvas.height = Math.floor(renderState.viewportHeight * renderState.dpr);
canvas.style.width = `${renderState.viewportWidth}px`;
canvas.style.height = `${renderState.viewportHeight}px`;
updateZoomControls();
if (!hasInitializedZoom) {
renderState.zoom = getInitialFitZoom();
hasInitializedZoom = true;
updateZoomControls();
}
else if (!hasUserAdjustedZoom) {
// 初回の手動操作前は、リサイズ時も画面に収まる倍率へ追従
renderState.zoom = getInitialFitZoom();
updateZoomControls();
}
recomputeRenderState();
layoutUi();
requestAnimationFrame(layoutUi);
}
resizeCanvas();
window.addEventListener('resize', resizeCanvas);
window.addEventListener('orientationchange', resizeCanvas);
window.addEventListener('pageshow', resizeCanvas);
window.addEventListener('focus', resizeCanvas);
document.addEventListener('visibilitychange', () => {
if (!document.hidden)
resizeCanvas();
});
if (window.visualViewport) {
window.visualViewport.addEventListener('resize', resizeCanvas);
window.visualViewport.addEventListener('scroll', resizeCanvas);
}
// iOS/WebView は初回表示直後に viewport 値が遅れて安定するため、短時間だけ再計測する
[0, 50, 150, 400, 1000].forEach((delay) => {
setTimeout(resizeCanvas, delay);
});
function screenToWorld(screenX, screenY) {
const worldX = (screenX - renderState.offsetX) / renderState.worldScale;
const worldY = (screenY - renderState.offsetY) / renderState.worldScale;
if (worldX < 0 || worldX > WORLD_SIZE || worldY < 0 || worldY > WORLD_SIZE)
return null;
return { x: worldX, y: worldY };
}
// ========================================
// イベントハンドラ
// ========================================
function handleMove(e) {
const rect = canvas.getBoundingClientRect();
let screenX;
let screenY;
if (e.touches) {
screenX = e.touches[0].clientX - rect.left;
screenY = e.touches[0].clientY - rect.top;
}
else if (e.changedTouches) {
screenX = e.changedTouches[0].clientX - rect.left;
screenY = e.changedTouches[0].clientY - rect.top;
}
else {
screenX = e.clientX - rect.left;
screenY = e.clientY - rect.top;
}
const worldPoint = screenToWorld(screenX, screenY);
if (!worldPoint)
return;
mouseX = worldPoint.x;
mouseY = worldPoint.y;
}
document.addEventListener('mousemove', handleMove);
canvas.addEventListener('touchmove', (e) => {
e.preventDefault();
handleMove(e);
}, { passive: false });
// テストボタンのイベントハンドラ
document.querySelectorAll('.test-btn').forEach(btn => {
btn.addEventListener('click', () => {
const config = btn.dataset.config;
initializeGame(config);
});
});
// 残骸攻撃トグルボタンのイベントハンドラ
document.getElementById('wreckage-toggle').addEventListener('click', () => {
attackWrecks = !attackWrecks;
const toggle = document.getElementById('wreckage-toggle');
if (attackWrecks) {
toggle.classList.add('active');
toggle.textContent = '残骸を攻撃する';
}
else {
toggle.classList.remove('active');
toggle.textContent = '残骸を無視する';
}
});
// ビーコンチームボタンのイベントハンドラ
const beaconTeamButtons = document.querySelectorAll('.beacon-team-btn');
function updateBeaconTeamSelection(teamId) {
const currentBeaconTeam = getSelectedBeaconTeamSafe();
const previousBeaconTeam = currentBeaconTeam;
if (currentBeaconTeam === teamId) {
selectedBeaconTeam = null;
}
else {
selectedBeaconTeam = teamId;
}
beaconTeamButtons.forEach((button) => {
if (button.dataset.team === teamId)
button.classList.add('active');
else
button.classList.remove('active');
});
if (selectedBeaconTeam === null) {
beaconTeamButtons.forEach((button) => button.classList.remove('active'));
// Beaconモード解除時は、記憶している追尾対象に1回だけ再センタリングする
if (previousBeaconTeam !== null && followState.selectedShip) {
followState.forceCenterOnNextFrame = true;
followState.pendingAutoFocusAt = 0;
}
}
recomputeRenderState();
layoutUi();
}
beaconTeamButtons.forEach((button) => {
button.addEventListener('click', () => {
updateBeaconTeamSelection(button.dataset.team);
});
});
function pickShipAt(worldX, worldY) {
const allShips = getAllShips();
let selected = null;
let closestDistance = Infinity;
for (const ship of allShips) {
if (!ship || !ship.isAlive())
continue;
const dx = ship.x - worldX;
const dy = ship.y - worldY;
const distance = Math.hypot(dx, dy);
const pickRadius = ship.getCollisionRadius() + SHIP_PICK_TOLERANCE;
if (distance <= pickRadius && distance < closestDistance) {
selected = ship;
closestDistance = distance;
}
}
return selected;
}
function placeBeaconFromEvent(e) {
const rect = canvas.getBoundingClientRect();
let screenX;
let screenY;
if (e.touches) {
screenX = e.touches[0].clientX - rect.left;
screenY = e.touches[0].clientY - rect.top;
}
else if (e.changedTouches) {
screenX = e.changedTouches[0].clientX - rect.left;
screenY = e.changedTouches[0].clientY - rect.top;
}
else {
screenX = e.clientX - rect.left;
screenY = e.clientY - rect.top;
}
const worldPoint = screenToWorld(screenX, screenY);
if (!worldPoint)
return;
const beaconTeam = getSelectedBeaconTeamSafe();
if (typeof beaconTeam !== 'undefined' && beaconTeam) {
// ビーコン配置モード
detectionBeacons.push(new DetectionBeacon(worldPoint.x, worldPoint.y, beaconTeam));
return;
}
// 追従モード(A/B/C すべてOFF)ではクリック/タップで船を選択
const selectedShip = pickShipAt(worldPoint.x, worldPoint.y);
if (!selectedShip)
return;
selectFollowShip(selectedShip, true);
recomputeRenderState();
layoutUi();
}
// キャンバスクリック/タップでビーコン配置
canvas.addEventListener('click', (e) => {
placeBeaconFromEvent(e);
});
canvas.addEventListener('touchstart', (e) => {
e.preventDefault();
placeBeaconFromEvent(e);
}, { passive: false });
function shiftZoom(direction) {
const index = renderState.zoomLevels.indexOf(renderState.zoom);
const targetIndex = index + direction;
if (targetIndex < 0 || targetIndex >= renderState.zoomLevels.length)
return;
const nextZoom = renderState.zoomLevels[targetIndex];
if (!nextZoom)
return;
hasInitializedZoom = true;
hasUserAdjustedZoom = true;
renderState.zoom = nextZoom;
updateZoomControls();
recomputeRenderState();
layoutUi();
}
function bindPress(button, handler) {
if (window.PointerEvent) {
button.addEventListener('pointerdown', (e) => {
e.preventDefault();
handler();
});
return;
}
let touchTriggered = false;
button.addEventListener('touchstart', (e) => {
e.preventDefault();
touchTriggered = true;
handler();
}, { passive: false });
button.addEventListener('click', (e) => {
if (touchTriggered) {
touchTriggered = false;
return;
}
e.preventDefault();
handler();
});
}
bindPress(zoomOutButton, () => shiftZoom(-1));
bindPress(zoomInButton, () => shiftZoom(1));
document.addEventListener('contextmenu', (e) => e.preventDefault());
// @ts-nocheck
// ========================================
// チーム定義
// ========================================
const TEAM_DEFINITIONS = {
A: {
id: 'A',
name: 'チームA',
alias: '水色チーム',
colorName: '水色',
color: '#33b5ff',
reactsToMouse: true,
enemies: ['B', 'C']
},
B: {
id: 'B',
name: 'チームB',
alias: '桃色チーム',
colorName: '桃色',
color: '#ff5fa2',
reactsToMouse: false,
enemies: ['A', 'C']
},
C: {
id: 'C',
name: 'チームC',
alias: '黄色チーム',
colorName: '黄色',
color: '#ffb300',
reactsToMouse: false,
enemies: ['A', 'B']
}
};
const TEAM_IDS = Object.keys(TEAM_DEFINITIONS);
const TEAM_COLORS = {
A: TEAM_DEFINITIONS.A.color,
B: TEAM_DEFINITIONS.B.color,
C: TEAM_DEFINITIONS.C.color
};
function formatTeamLabel(teamId) {
const team = TEAM_DEFINITIONS[teamId];
return `${team.name}(${team.colorName})`;
}
// @ts-nocheck
// ========================================
// コンポーネントカタログ(データ定義)
// ========================================
const COMPONENT_CATALOG = {
// 船体パーツ定義