Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
1705 lines (1578 loc) · 49.9 KB
/
Copy pathmain.js
File metadata and controls
1705 lines (1578 loc) · 49.9 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
/* =========================================================
GAME DATA
========================================================= */
const STRUCTURE_TYPES = [
{
type: "cone",
name: "視錐細胞",
desc: "明亮光線下感應色彩",
tags: ["強光", "色彩", "高解析度"],
},
{
type: "rod",
name: "視桿細胞",
desc: "微弱光線下感應明暗",
tags: ["弱光"],
},
{
type: "pupil_small",
name: "瞳孔(縮小)",
desc: "強光時收縮以減光",
tags: ["強光"],
},
{
type: "pupil_large",
name: "瞳孔(擴張)",
desc: "暗處時放大以增光",
tags: ["弱光"],
},
{
type: "lens_thick",
name: "晶體(較厚)",
desc: "近距離聚焦用(睫狀肌收縮)",
tags: ["近距"],
},
{
type: "lens_thin",
name: "晶體(較薄)",
desc: "遠距離聚焦用(睫狀肌舒張)",
tags: ["遠距"],
},
{
type: "ciliary_contract",
name: "睫狀肌(收縮)",
desc: "使晶體變厚聚焦近物",
tags: ["近距"],
},
{
type: "ciliary_relax",
name: "睫狀肌(舒張)",
desc: "使晶體變薄聚焦遠物",
tags: ["遠距"],
},
{
type: "macula",
name: "黃斑部",
desc: "中央窩高解析度辨色",
tags: ["高解析度"],
},
{
type: "nerve",
name: "視神經",
desc: "傳遞訊號至大腦(百搭卡)",
tags: ["wild"],
},
{
type: "concave",
name: "凹透鏡",
desc: "矯正近視用",
tags: ["lens"],
},
{
type: "convex",
name: "凸透鏡",
desc: "矯正遠視用",
tags: ["lens"],
},
];
const IDENTITY_CARDS = [
{
type: "healthy",
name: "健康明眸",
desc: "開局抽4張手牌,無特殊限制",
},
{
type: "myopia",
name: "近視患者",
desc: "手牌+1(開局抽5張);面對任何有[遠距]標籤的場景,必須連同「凹透鏡」一起出牌,否則出牌無效得0分",
},
{
type: "hyperopia",
name: "遠視患者",
desc: "手牌+1(開局抽5張);面對任何有[近距]標籤的場景,必須連同「凸透鏡」一起出牌,否則出牌無效得0分",
},
{
type: "night_blind",
name: "夜盲症患者",
desc: "手牌+1(開局抽5張);[強光]成功得分時免費多抽1張牌;[弱光]強制不能出牌",
},
];
const SCENE_CARDS = [
/* 重複標籤場景(需出 2 張相同標籤的卡牌才能得 3 分) */
{
type: "close_reading",
name: "近距閱讀書本",
icon: "📖",
tags: ["近距", "近距"],
correct: ["lens_thick", "ciliary_contract"],
},
{
type: "repair_watch",
name: "修理精密手錶",
icon: "⌚",
tags: ["近距", "近距"],
correct: ["lens_thick", "ciliary_contract"],
},
{
type: "distant_tower",
name: "遠方的旅遊塔",
icon: "🗼",
tags: ["遠距", "遠距"],
correct: ["lens_thin", "ciliary_relax"],
},
{
type: "distant_peak",
name: "眺望對岸山峰",
icon: "⛰️",
tags: ["遠距", "遠距"],
correct: ["lens_thin", "ciliary_relax"],
},
{
type: "sunlight",
name: "烈陽直射",
icon: "☀️",
tags: ["強光", "強光"],
correct: ["pupil_small", "cone"],
},
{
type: "snow_glare",
name: "雪地刺眼反光",
icon: "🏔️",
tags: ["強光", "強光"],
correct: ["pupil_small", "cone"],
},
{
type: "dark_alley",
name: "深夜無燈暗巷",
icon: "🌙",
tags: ["弱光", "弱光"],
correct: ["pupil_large", "rod"],
},
{
type: "blackout",
name: "停電的密室",
icon: "🕯️",
tags: ["弱光", "弱光"],
correct: ["pupil_large", "rod"],
},
/* 複合標籤場景(需出 2 張不同標籤的卡牌) */
{
type: "thread_needle",
name: "穿針引線",
icon: "🪡",
tags: ["近距", "高解析度"],
correct: ["lens_thick", "ciliary_contract", "macula"],
},
{
type: "microscope",
name: "顯微鏡看細胞",
icon: "🔬",
tags: ["近距", "高解析度"],
correct: ["lens_thick", "ciliary_contract", "macula"],
},
{
type: "dark_phone",
name: "暗處滑手機",
icon: "📱",
tags: ["近距", "弱光"],
correct: ["lens_thick", "ciliary_contract", "pupil_large", "rod"],
},
{
type: "color_canvas",
name: "繪製彩色畫布",
icon: "🎨",
tags: ["近距", "色彩"],
correct: ["lens_thick", "ciliary_contract", "cone"],
},
{
type: "sun_phone",
name: "烈日下看手機",
icon: "📲",
tags: ["近距", "強光"],
correct: ["lens_thick", "ciliary_contract", "pupil_small", "cone"],
},
{
type: "starry_sky",
name: "仰望滿天星空",
icon: "🌌",
tags: ["遠距", "弱光"],
correct: ["lens_thin", "ciliary_relax", "pupil_large", "rod"],
},
{
type: "far_sign",
name: "辨識遠方路標",
icon: "🪧",
tags: ["遠距", "高解析度"],
correct: ["lens_thin", "ciliary_relax", "macula", "cone"],
},
{
type: "fireworks",
name: "欣賞跨年煙火",
icon: "🎆",
tags: ["遠距", "色彩"],
correct: ["lens_thin", "ciliary_relax", "cone"],
},
{
type: "stage_light",
name: "舞台聚光燈打臉",
icon: "🎭",
tags: ["強光", "色彩"],
correct: ["pupil_small", "cone"],
},
{
type: "diamond_inspect",
name: "強光下檢查鑽石",
icon: "💎",
tags: ["強光", "高解析度"],
correct: ["pupil_small", "macula", "cone"],
},
{
type: "bush_chameleon",
name: "尋找草叢變色龍",
icon: "🦎",
tags: ["高解析度", "色彩"],
correct: ["macula", "cone"],
},
{
type: "dusk_color",
name: "黃昏微光看彩圖",
icon: "🌆",
tags: ["弱光", "色彩"],
correct: ["pupil_large", "rod", "cone"],
},
];
const SENSORY_CARDS = [
{ type: "color_test", name: "色盲測試", icon: "🔴🟢" },
{ type: "overlap_test", name: "重疊圖案分析", icon: "◯△□" },
];
/* Reshuffle discarded structure cards back into deck when empty */
function reshuffleStructureDeckIfEmpty() {
if (G.structureDeck.length === 0 && G.structureDiscard.length > 0) {
G.structureDeck = G.structureDiscard;
G.structureDiscard = [];
shuffle(G.structureDeck);
}
}
/* Card SVG icons */
const CARD_ICONS = {
cone: '<svg viewBox="0 0 100 80"><polygon points="18,68 33,14 48,68" fill="#e63946" opacity="0.85"/><polygon points="33,68 48,10 63,68" fill="#2a9d8f" opacity="0.85"/><polygon points="48,68 63,14 78,68" fill="#264653" opacity="0.85"/></svg>',
rod: '<svg viewBox="0 0 100 80"><rect x="38" y="8" width="24" height="52" rx="12" fill="#7c6f9c"/><ellipse cx="50" cy="10" rx="12" ry="6" fill="#b5a8d5"/><rect x="41" y="58" width="18" height="14" rx="4" fill="#574f7d"/></svg>',
pupil_small:
'<svg viewBox="0 0 100 60"><path d="M5,30 Q50,-2 95,30 Q50,62 5,30Z" fill="white" stroke="#555" stroke-width="2"/><circle cx="50" cy="30" r="17" fill="#8B6914"/><circle cx="50" cy="30" r="5" fill="black"/><ellipse cx="44" cy="24" rx="3" ry="2" fill="rgba(255,255,255,0.5)"/></svg>',
pupil_large:
'<svg viewBox="0 0 100 60"><path d="M5,30 Q50,-2 95,30 Q50,62 5,30Z" fill="white" stroke="#555" stroke-width="2"/><circle cx="50" cy="30" r="17" fill="#8B6914"/><circle cx="50" cy="30" r="13" fill="black"/><ellipse cx="42" cy="23" rx="3" ry="2" fill="rgba(255,255,255,0.35)"/></svg>',
lens_thick:
'<svg viewBox="0 0 100 80"><ellipse cx="50" cy="40" rx="20" ry="32" fill="rgba(135,206,250,0.35)" stroke="#4a90d9" stroke-width="2"/><line x1="10" y1="40" x2="30" y2="40" stroke="#4a90d9" stroke-width="1.5" stroke-dasharray="4,3"/><line x1="70" y1="40" x2="90" y2="40" stroke="#4a90d9" stroke-width="1.5" stroke-dasharray="4,3"/></svg>',
lens_thin:
'<svg viewBox="0 0 100 80"><ellipse cx="50" cy="40" rx="8" ry="32" fill="rgba(135,206,250,0.35)" stroke="#4a90d9" stroke-width="2"/><line x1="10" y1="40" x2="42" y2="40" stroke="#4a90d9" stroke-width="1.5" stroke-dasharray="4,3"/><line x1="58" y1="40" x2="90" y2="40" stroke="#4a90d9" stroke-width="1.5" stroke-dasharray="4,3"/></svg>',
nerve: '<svg viewBox="0 0 100 80"><path d="M12,42 L28,20 L44,52 L60,24 L72,44" fill="none" stroke="#e9c46a" stroke-width="3.5" stroke-linecap="round"/><circle cx="82" cy="38" r="13" fill="#f4a261" opacity="0.75"/><circle cx="82" cy="38" r="6" fill="#e76f51"/><circle cx="8" cy="42" r="5" fill="#2a9d8f"/></svg>',
ciliary_contract:
'<svg viewBox="0 0 100 80"><ellipse cx="50" cy="40" rx="20" ry="32" fill="rgba(135,206,250,0.3)" stroke="#4a90d9" stroke-width="2"/><path d="M10,20 Q30,10 10,60" fill="none" stroke="#e63946" stroke-width="2.5"/><path d="M90,20 Q70,10 90,60" fill="none" stroke="#e63946" stroke-width="2.5"/><text x="50" y="48" text-anchor="middle" font-size="14" font-weight="bold" fill="#e63946">收</text></svg>',
ciliary_relax:
'<svg viewBox="0 0 100 80"><ellipse cx="50" cy="40" rx="8" ry="32" fill="rgba(135,206,250,0.3)" stroke="#4a90d9" stroke-width="2"/><path d="M10,20 Q30,10 10,60" fill="none" stroke="#2a9d8f" stroke-width="2.5"/><path d="M90,20 Q70,10 90,60" fill="none" stroke="#2a9d8f" stroke-width="2.5"/><text x="50" y="48" text-anchor="middle" font-size="14" font-weight="bold" fill="#2a9d8f">舒</text></svg>',
macula: '<svg viewBox="0 0 100 80"><circle cx="50" cy="40" r="28" fill="#f4a261" opacity="0.3" stroke="#e76f51" stroke-width="2"/><circle cx="50" cy="40" r="12" fill="#e76f51" opacity="0.5"/><circle cx="50" cy="40" r="4" fill="#c1121f"/></svg>',
concave:
'<svg viewBox="0 0 100 80"><path d="M20,20 Q50,10 80,20 L80,60 Q50,70 20,60 Z" fill="rgba(135,206,250,0.25)" stroke="#457b9d" stroke-width="2"/><path d="M20,20 Q50,30 80,20" fill="none" stroke="#457b9d" stroke-width="2"/><path d="M20,60 Q50,50 80,60" fill="none" stroke="#457b9d" stroke-width="2"/></svg>',
convex: '<svg viewBox="0 0 100 80"><path d="M20,10 Q50,40 20,70 L80,70 Q50,40 80,10 Z" fill="rgba(231,111,81,0.25)" stroke="#e76f51" stroke-width="2"/><path d="M20,10 Q50,40 80,10" fill="none" stroke="#e76f51" stroke-width="2"/><path d="M20,70 Q50,40 80,70" fill="none" stroke="#e76f51" stroke-width="2"/></svg>',
};
/* Ishihara challenge presets */
const ISHIHARA_CHALLENGES = [
{ number: 7, options: [3, 5, 7, 9] },
{ number: 5, options: [2, 5, 8, 6] },
{ number: 12, options: [8, 12, 17, 21] },
{ number: 8, options: [3, 6, 8, 9] },
{ number: 3, options: [1, 3, 8, 5] },
];
/* Overlap challenge presets */
const OVERLAP_CHALLENGES = [
{
count: 3,
label: "圓形、三角形、正方形",
options: [2, 3, 4, 5],
},
{
count: 4,
label: "圓形、三角形、正方形、菱形",
options: [3, 4, 5, 6],
},
{
count: 5,
label: "圓形、三角形、正方形、菱形、五邊形",
options: [3, 4, 5, 6],
},
];
/* =========================================================
GAME STATE
========================================================= */
let uid = 0;
const G = {
phase: "menu",
currentPlayer: 0,
players: [],
structureDeck: [],
structureDiscard: [],
sceneDeck: [],
sceneDiscard: [],
sensoryDeck: [],
currentScene: null,
selectedCardsIdx: [],
/* 雙方暗蓋出牌用 */
coverState: "idle", // 'idle' | 'p1_cover' | 'p2_cover' | 'reveal'
coverCardsIdx: [[], []], // 雙方選中的卡牌索引
coverPass: [false, false], // 雙方是否選擇不出牌
identityDeck: [],
duel: {
buzzer: -1,
challenge: null,
sensoryCard: null,
secondChance: false,
canBuzz: false,
buzzed: false,
falseStart: null,
countdownActive: true,
questionStartTime: 0,
},
lastResult: null,
skipTurn: [-1, -1],
};
/* =========================================================
UTILITIES
========================================================= */
function genUID() {
return ++uid;
}
function shuffle(arr) {
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]];
}
return arr;
}
function pick(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}
function showScreen(id) {
document.querySelectorAll(".screen").forEach(function (s) {
s.classList.remove("active");
});
document.getElementById("screen-" + id).classList.add("active");
G.phase = id;
}
/* =========================================================
GAME INIT
========================================================= */
function startGame() {
G.structureDeck = [];
STRUCTURE_TYPES.forEach(function (t) {
/* 每種結構卡各2張(除了凹透鏡凸透鏡各1張) */
let count = t.type === "concave" || t.type === "convex" ? 1 : 2;
for (let ci = 0; ci < count; ci++) {
G.structureDeck.push(Object.assign({}, t, { uid: genUID() }));
}
});
shuffle(G.structureDeck);
G.sceneDeck = SCENE_CARDS.map(function (c) {
return Object.assign({}, c, { uid: genUID() });
});
shuffle(G.sceneDeck);
G.sensoryDeck = SENSORY_CARDS.map(function (c) {
return Object.assign({}, c, { uid: genUID() });
});
shuffle(G.sensoryDeck);
G.structureDiscard = [];
G.skipTurn = [-1, -1];
/* 身份卡 */
G.identityDeck = IDENTITY_CARDS.map(function (c) {
return Object.assign({}, c, { uid: genUID() });
});
shuffle(G.identityDeck);
G.players = [
{
name: "玩家一",
score: 0,
hand: [],
usedDuel: false,
identity: null,
skipNext: false,
},
{
name: "玩家二",
score: 0,
hand: [],
usedDuel: false,
identity: null,
skipNext: false,
},
];
/* 每人抽身份卡 */
G.players[0].identity = G.identityDeck.pop();
G.players[1].identity = G.identityDeck.pop();
/* 發手牌:健康明眸抽4張,其他角色抽5張(手牌+1) */
for (let pi = 0; pi < 2; pi++) {
let handSize = G.players[pi].identity.type === "healthy" ? 4 : 5;
for (let hi = 0; hi < handSize; hi++) {
if (G.structureDeck.length > 0) {
G.players[pi].hand.push(G.structureDeck.pop());
}
}
}
G.currentPlayer = 0;
G.selectedCardsIdx = [];
G.currentScene = null;
showIdentityScreen();
}
/* =========================================================
IDENTITY SCREEN
========================================================= */
function showIdentityScreen() {
let html =
'<h2 style="color:var(--optic-gold);font-size:1.3rem;margin-bottom:16px">🎭 身份卡</h2>';
G.players.forEach(function (p, i) {
html +=
'<div style="background:var(--bg-surface);border-radius:12px;padding:16px;margin-bottom:12px;border:1px solid rgba(196,155,61,0.15);text-align:center">' +
'<div style="font-size:0.9rem;color:var(--text-dim);margin-bottom:4px">' +
p.name +
"</div>" +
'<div style="font-size:1.3rem;font-weight:900;color:' +
(i === 0 ? "var(--p1-color)" : "var(--p2-color)") +
'">' +
p.identity.name +
"</div>" +
'<div style="font-size:0.8rem;color:var(--text-dim);margin-top:6px">' +
p.identity.desc +
"</div>" +
"</div>";
});
html +=
'<button class="btn btn-primary" onclick="showPassScreen()"><i class="fas fa-arrow-right"></i> 確認,開始遊戲</button>';
document.getElementById("pass-content").innerHTML = html;
showScreen("pass");
}
/* =========================================================
PASS DEVICE
========================================================= */
function showPassScreen() {
/* 檢查是否有人要跳過回合 */
let p = G.players[G.currentPlayer];
if (p.skipNext) {
p.skipNext = false;
G.currentPlayer = 1 - G.currentPlayer;
/* 遞迴但要防止無限循環 */
}
let html =
'<h2 style="color:var(--text-dim);font-size:1.1rem;">回合交替</h2>' +
'<div class="pass-scores" id="pass-scores-dynamic"></div>' +
'<div class="pass-hint">請將設備交給</div>' +
'<h1 class="pass-player-name" id="pass-player-name-dynamic"></h1>' +
'<button class="btn btn-primary" onclick="startTurn()"><i class="fas fa-eye"></i> 開始回合</button>';
document.getElementById("pass-content").innerHTML = html;
let scoresHtml = "";
G.players.forEach(function (p, i) {
let isCurrent = i === G.currentPlayer;
scoresHtml +=
'<div class="pass-score-item' +
(isCurrent ? " current" : "") +
'">' +
'<div class="pass-score-label">' +
p.name +
"</div>" +
'<div class="pass-score-num" style="color:' +
(i === 0 ? "var(--p1-color)" : "var(--p2-color)") +
'">' +
p.score +
"</div>" +
"</div>";
});
document.getElementById("pass-scores-dynamic").innerHTML = scoresHtml;
document.getElementById("pass-player-name-dynamic").textContent =
G.players[G.currentPlayer].name;
document.getElementById("pass-player-name-dynamic").style.color =
G.currentPlayer === 0 ? "var(--p1-color)" : "var(--p2-color)";
showScreen("pass");
}
function startTurn() {
G.selectedCardsIdx = [];
G.currentScene = null;
G.coverState = "idle";
G.coverCardsIdx = [[], []];
G.coverPass = [false, false];
/* 先顯示行動選擇(翻場景或發起決鬥) */
renderGameScreen("chooseAction");
showScreen("game");
}
/* =========================================================
GAME SCREEN RENDERING
========================================================= */
function renderGameScreen(subPhase) {
renderTopBar();
renderCenter(subPhase);
renderHandSection(subPhase);
}
function renderTopBar() {
let opp = G.players[1 - G.currentPlayer];
let oppIdx = 1 - G.currentPlayer;
let topHtml =
'<div class="player-bar-info">' +
'<div class="player-avatar" style="background:' +
(oppIdx === 0 ? "var(--p1-color)" : "var(--p2-color)") +
'">' +
(oppIdx + 1) +
"</div>" +
'<div><div class="player-name-bar">' +
opp.name +
"</div>" +
'<div class="player-hand-count"><i class="fas fa-clone"></i> ' +
opp.hand.length +
" 張手牌</div></div>" +
"</div>" +
'<div style="display:flex;align-items:center;gap:8px">' +
'<div class="player-score-bar"><i class="fas fa-star"></i> ' +
opp.score +
"</div>" +
'<div style="font-size:0.65rem;background:var(--bg-elevated);padding:2px 6px;border-radius:6px;color:var(--text-dim)">' +
opp.identity.name +
"</div>" +
"</div>";
document.getElementById("game-top-bar").innerHTML = topHtml;
}
function renderCenter(subPhase) {
let center = document.getElementById("game-center");
let p = G.players[G.currentPlayer];
if (subPhase === "chooseAction") {
let duelDisabled = p.usedDuel;
let canPlay = !p.skipNext;
center.innerHTML =
'<div style="font-size:0.75rem;background:var(--bg-elevated);padding:4px 12px;border-radius:20px;margin-bottom:4px;color:var(--optic-gold)">' +
'<i class="fas fa-id-card"></i> ' +
p.identity.name +
"</div>" +
'<div class="deck-row">' +
'<div class="deck-indicator"><div class="deck-icon structure"><i class="fas fa-eye"></i></div><div class="deck-count">結構 ' +
G.structureDeck.length +
"</div></div>" +
'<div class="deck-indicator"><div class="deck-icon scene"><i class="fas fa-image"></i></div><div class="deck-count">場景 ' +
G.sceneDeck.length +
"</div></div>" +
'<div class="deck-indicator"><div class="deck-icon sensory"><i class="fas fa-bolt"></i></div><div class="deck-count">應用 ' +
G.sensoryDeck.length +
"</div></div>" +
"</div>" +
'<div class="action-prompt">' +
'<div class="action-prompt-title">' +
p.name +
" 的回合</div>" +
'<div class="action-prompt-sub">選擇本回合的行動</div>' +
'<div class="action-buttons">' +
'<button class="btn btn-primary" onclick="drawSceneCard()"' +
(!canPlay ? " disabled" : "") +
'><i class="fas fa-image"></i> 翻開場景卡</button>' +
'<button class="btn btn-duel' +
(duelDisabled ? "" : "") +
'"' +
(duelDisabled || !canPlay ? " disabled" : "") +
' onclick="initiateDuel()">' +
'<i class="fas fa-bolt"></i> 發起決鬥' +
(duelDisabled
? '<br><span style="font-size:0.7rem;font-family:Noto Sans TC,sans-serif;font-weight:400">(已使用過)</span>'
: "") +
"</button>" +
"</div>" +
"</div>";
} else if (subPhase === "cover_p1" || subPhase === "cover_p2") {
/* 暗蓋出牌階段 */
let coverPlayerIdx = subPhase === "cover_p1" ? 0 : 1;
let coverPlayer = G.players[coverPlayerIdx];
let sc = G.currentScene;
let hasNightBlindWeak =
coverPlayer.identity.type === "night_blind" &&
sc.tags.indexOf("弱光") !== -1;
center.innerHTML =
'<div class="scene-area">' +
'<div class="scene-card-display">' +
'<div class="scene-label">場景卡</div>' +
'<div class="scene-icon">' +
sc.icon +
"</div>" +
'<div class="scene-name">' +
sc.name +
"</div>" +
"</div>" +
'<div style="font-size:0.9rem;font-weight:700;color:var(--optic-gold);text-align:center;margin-top:6px">' +
"🔒 " +
coverPlayer.name +
" 暗蓋出牌" +
"</div>" +
'<div style="font-size:0.75rem;color:var(--text-dim);text-align:center">' +
"請從手牌挑選要打出的卡牌,面朝下蓋在桌面(最多2張)<br>凹/凸透鏡不計入此限制<br>" +
(hasNightBlindWeak
? '<span style="color:var(--error)">🌙 夜盲症:弱光環境無法出牌,請按「不出牌」</span>'
: '<span style="color:var(--text-dim)">選好後按「確認暗蓋」</span>') +
"</div>" +
"</div>";
}
}
function renderHandSection(subPhase) {
let section = document.getElementById("hand-section");
/* cover_p1/p2: 顯示正在蓋牌的玩家的手牌 */
let coverPlayerIdx =
subPhase === "cover_p1"
? 0
: subPhase === "cover_p2"
? 1
: G.currentPlayer;
let p = G.players[coverPlayerIdx];
let identity = p.identity;
let sc = G.currentScene;
/* 夜盲症 + [弱光]:手牌不可選取,只能按不出牌 */
let nightBlindLock =
identity.type === "night_blind" &&
sc &&
sc.tags.indexOf("弱光") !== -1;
let canSelect =
(subPhase === "cover_p1" || subPhase === "cover_p2") &&
!nightBlindLock;
let cardsHtml = "";
p.hand.forEach(function (card, i) {
let sel = G.selectedCardsIdx.indexOf(i) !== -1 ? " selected" : "";
let dis = !canSelect ? " disabled" : "";
cardsHtml +=
'<div class="hand-card' +
sel +
dis +
'" onclick="selectCoverCard(' +
i +
"," +
coverPlayerIdx +
')" data-idx="' +
i +
'">' +
'<div class="hand-card-icon">' +
CARD_ICONS[card.type] +
"</div>" +
'<div class="hand-card-name">' +
card.name +
"</div>" +
"</div>";
});
let actionsHtml = "";
if (subPhase === "cover_p1" || subPhase === "cover_p2") {
let playDisabled =
G.selectedCardsIdx.length === 0 || nightBlindLock
? " disabled"
: "";
actionsHtml =
'<div class="hand-actions">' +
'<button class="btn btn-primary"' +
playDisabled +
' id="btn-confirm-cover" onclick="confirmCover(' +
coverPlayerIdx +
')"><i class="fas fa-check"></i> 確認暗蓋</button>' +
'<button class="btn btn-secondary" onclick="passCover(' +
coverPlayerIdx +
')"><i class="fas fa-minus"></i> 不出牌</button>' +
"</div>";
}
let identityTag =
'<span style="display:inline-block;font-size:0.7rem;color:var(--optic-gold);margin-left:6px"><i class="fas fa-id-card"></i> ' +
identity.name +
"</span>";
section.innerHTML =
'<div class="hand-label"><i class="fas fa-hand-paper"></i> ' +
p.name +
" 的手牌 (" +
p.hand.length +
")" +
identityTag +
"</div>" +
'<div class="hand-cards">' +
cardsHtml +
"</div>" +
actionsHtml;
/* Bottom bar */
let cur = G.players[G.currentPlayer];
let bottomInfo =
'<div style="display:flex;justify-content:space-between;align-items:center;margin-top:8px;padding-top:6px;border-top:1px solid rgba(196,155,61,0.08)">' +
'<div style="display:flex;align-items:center;gap:8px">' +
'<div class="player-avatar" style="background:' +
(G.currentPlayer === 0 ? "var(--p1-color)" : "var(--p2-color)") +
';width:28px;height:28px;font-size:0.7rem">' +
(G.currentPlayer + 1) +
"</div>" +
'<span style="font-size:0.85rem;font-weight:700">當前回合:' +
cur.name +
"</span>" +
"</div>" +
'<div class="player-score-bar" style="font-size:1rem"><i class="fas fa-star"></i> ' +
cur.score +
"</div>" +
"</div>";
section.innerHTML += bottomInfo;
}
/* =========================================================
CARD SELECTION & PLAY (雙人暗蓋同時出牌制)
========================================================= */
/* 蓋牌階段:選擇卡牌 */
function selectCoverCard(idx, playerIdx) {
if (G.phase !== "game") return;
if (!G.currentScene) return;
/* 確保是該玩家的蓋牌階段 */
if (
(G.coverState === "p1_cover" && playerIdx !== 0) ||
(G.coverState === "p2_cover" && playerIdx !== 1)
)
return;
/* 夜盲症 + [弱光]:禁止選牌 */
let p = G.players[playerIdx];
if (
p.identity.type === "night_blind" &&
G.currentScene.tags.indexOf("弱光") !== -1
)
return;
let pos = G.selectedCardsIdx.indexOf(idx);
if (pos !== -1) {
G.selectedCardsIdx.splice(pos, 1);
} else {
/* 最多2張(凹/凸透鏡不計入此限制,但我們在 UI 上仍顯示最多2張) */
let lensCount = 0;
G.selectedCardsIdx.forEach(function (si) {
if (G.players[playerIdx].hand[si].tags[0] === "lens") lensCount++;
});
let nonLensSelected = G.selectedCardsIdx.length - lensCount;
let isLens = G.players[playerIdx].hand[idx].tags[0] === "lens";
if (nonLensSelected >= 2 && !isLens) return;
G.selectedCardsIdx.push(idx);
}
let subPhase = G.coverState === "p1_cover" ? "cover_p1" : "cover_p2";
renderHandSection(subPhase);
}
/* 確認暗蓋:鎖定當前的蓋牌選擇 */
function confirmCover(playerIdx) {
/* 確保是該玩家的回合 */
if (
(G.coverState === "p1_cover" && playerIdx !== 0) ||
(G.coverState === "p2_cover" && playerIdx !== 1)
)
return;
/* 夜盲症 + [弱光]:不能確認暗蓋 */
let p = G.players[playerIdx];
if (
p.identity.type === "night_blind" &&
G.currentScene &&
G.currentScene.tags.indexOf("弱光") !== -1
)
return;
G.coverCardsIdx[playerIdx] = G.selectedCardsIdx.slice();
G.coverPass[playerIdx] = false;
advanceCoverPhase();
}
/* 不出牌:該玩家跳過此回合 */
function passCover(playerIdx) {
if (
(G.coverState === "p1_cover" && playerIdx !== 0) ||
(G.coverState === "p2_cover" && playerIdx !== 1)
)
return;
G.coverPass[playerIdx] = true;
G.coverCardsIdx[playerIdx] = [];
G.selectedCardsIdx = [];
advanceCoverPhase();
}
/* 推進蓋牌階段 */
function advanceCoverPhase() {
if (G.coverState === "p1_cover") {
/* 玩家1完成,短暫提示後換玩家2 */
let center = document.getElementById("game-center");
center.innerHTML =
'<div style="font-size:1.3rem;color:var(--optic-gold);text-align:center;padding:20px">🔒 玩家一已暗蓋完成<br><span style="font-size:1rem;color:var(--text-dim)">請將設備交給玩家二</span></div>' +
'<button class="btn btn-primary" onclick="startP2Cover()" style="margin-top:16px"><i class="fas fa-arrow-right"></i> 玩家二開始暗蓋</button>';
} else if (G.coverState === "p2_cover") {
/* 雙方都完成,進行同時結算 */
G.coverState = "reveal";
G.selectedCardsIdx = [];
doSimultaneousReveal();
}
}
function startP2Cover() {
G.coverState = "p2_cover";
G.selectedCardsIdx = [];
renderGameScreen("cover_p2");
}
/* 雙方同時翻牌結算 */
function doSimultaneousReveal() {
let sc = G.currentScene;
let results = [];
[0, 1].forEach(function (pi) {
let p = G.players[pi];
let result = {
player: pi,
score: 0,
correct: false,
passed: false,
msg: "",
};
if (G.coverPass[pi]) {
/* 不出牌:補1張 */
let drawn = null;
reshuffleStructureDeckIfEmpty();
if (G.structureDeck.length > 0) {
drawn = G.structureDeck.pop();
p.hand.push(drawn);
result.msg = "不出牌,補了1張牌";
} else {
result.msg = "不出牌(牌堆已空)";
}
result.passed = true;
results.push(result);
return;
}
let selectedCards = G.coverCardsIdx[pi].map(function (idx) {
return p.hand[idx];
});
result.cards = selectedCards;
/* 身份障礙檢查 */
if (p.identity.type === "myopia" && sc.tags.indexOf("遠距") !== -1) {
let hasConcave = selectedCards.some(function (c) {
return c.type === "concave";
});
if (!hasConcave) {
result.msg = "❌ 近視患者面對[遠距]場景需凹透鏡!";
results.push(result);
return;
}
}
if (
p.identity.type === "hyperopia" &&
sc.tags.indexOf("近距") !== -1
) {
let hasConvex = selectedCards.some(function (c) {
return c.type === "convex";
});
if (!hasConvex) {
result.msg = "❌ 遠視患者面對[近距]場景需凸透鏡!";
results.push(result);
return;
}
}
/* 檢查視神經百搭 */
let hasWild = selectedCards.some(function (c) {
return c.type === "nerve";
});
let playCards = selectedCards.filter(function (c) {
return c.tags[0] !== "lens" && c.type !== "nerve";
});
if (hasWild) {
let anyCorrect = playCards.some(function (c) {
return sc.correct.indexOf(c.type) !== -1;
});
if (anyCorrect && playCards.length > 0) {
/* 成功!得3分 */
removePlayedCards(p, G.coverCardsIdx[pi]);
p.score += 3;
result.score = 3;
result.correct = true;
result.msg = "視神經百搭!+3分";
if (
p.identity.type === "night_blind" &&
sc.tags.indexOf("強光") !== -1
) {
result.msg += doNightBlindBonus(p);
}
results.push(result);
return;
} else {
result.msg = "❌ 視神經需搭配1張正確卡";
results.push(result);
return;
}
}
/* 正常配對 */
let matchedTags = 0;
let usedTypes = [];
sc.tags.forEach(function (tag) {
for (let ci = 0; ci < playCards.length; ci++) {
if (usedTypes.indexOf(ci) !== -1) continue;
if (playCards[ci].tags.indexOf(tag) !== -1) {
matchedTags++;
usedTypes.push(ci);
break;
}
}
});
let score = matchedTags === 2 ? 3 : matchedTags === 1 ? 1 : 0;
if (score > 0) {
removePlayedCards(p, G.coverCardsIdx[pi]);
p.score += score;
result.score = score;
result.correct = true;
result.msg =
score === 3 ? "完美配對2個標籤!+3分" : "配對1個標籤!+1分";
if (
p.identity.type === "night_blind" &&
sc.tags.indexOf("強光") !== -1
) {
result.msg += doNightBlindBonus(p);
}
} else {
result.msg = "❌ 出牌與場景標籤不符(卡牌退回手牌)";
}
results.push(result);
});
showSimultaneousResult(sc, results);
}
/* 顯示雙方同時結算結果 */
function showSimultaneousResult(scene, results) {
let html =
'<h2 style="color:var(--optic-gold);font-size:1.2rem;margin-bottom:12px">⚔️ 雙方同時結算</h2>';
html +=
'<div style="display:flex;align-items:center;justify-content:center;gap:8px;margin-bottom:12px">' +
"<span>" +
scene.icon +
"</span> " +
scene.name +
"</div>";
/* 顯示分數浮標動畫 */
results.forEach(function (r) {
if (r.score > 0) {
showScorePopup("+" + r.score);
}
});