-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGame.cpp
More file actions
1132 lines (991 loc) · 45.1 KB
/
Game.cpp
File metadata and controls
1132 lines (991 loc) · 45.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
#include "Game.h"
#include "LanP2PNode.h"
#include "Button.h"
#include "GameClient.h"
#include "chess-game.h"
#include <algorithm>
#include <atomic>
#include <cmath>
#include <cstring>
#include <limits>
#include <string>
#include <thread>
#include <vector>
#include <raylib.h>
#include <raymath.h>
#include <rlgl.h>
/**
* @brief 匹配对手界面
*
* 显示可用的对等节点列表和待处理的匹配请求
* 提供以下功能:
* - 发现局域网内的其他玩家(10秒)
* - 向选中的玩家发送匹配请求
* - 接受或拒绝收到的匹配请求
* - 退出到主菜单
*
* @param client 游戏客户端引用
* @param node 局域网P2P节点引用
* @return true 匹配成功,false 用户退出
*/
bool SeekPeer(Client &client, lanp2p::LanP2PNode &node)
{
// ========== 创建UI按钮 ==========
Button discoverBtn(Rectangle{0.0f, 0.0f, 0.0f, 0.0f}, "Discover (10s)");
Button requestBtn(Rectangle{0.0f, 0.0f, 0.0f, 0.0f}, "Request Match");
Button acceptBtn(Rectangle{0.0f, 0.0f, 0.0f, 0.0f}, "Accept");
Button rejectBtn(Rectangle{0.0f, 0.0f, 0.0f, 0.0f}, "Reject");
Button exitBtn(Rectangle{0.0f, 0.0f, 0.0f, 0.0f}, "Exit");
// ========== 状态变量 ==========
int selectedPeer = -1; // 选中的对等节点索引(-1表示未选中)
int selectedPending = -1; // 选中的待处理请求索引
std::string status = "Idle"; // 状态栏文本
bool discoveryActive = false; // 是否正在发现节点
double discoveryEndTime = 0.0; // 发现结束时间
// ========== 主循环 ==========
while (!WindowShouldClose())
{
// 获取窗口尺寸
const int screenWidth = GetScreenWidth();
const int screenHeight = GetScreenHeight();
// 计算UI布局参数(响应式设计)
const float margin = std::max(16.0f, screenWidth * 0.02f); // 边距
const float spacing = std::max(8.0f, screenWidth * 0.01f); // 间距
float btnHeight = std::max(44.0f, screenHeight * 0.065f); // 按钮高度
float btnWidth = (screenWidth - margin * 2.0f - spacing * 4.0f) / 5.0f; // 按钮宽度
btnWidth = std::max(140.0f, btnWidth);
const int btnFontSize = static_cast<int>(std::max(18.0f, btnHeight * 0.5f));
const float topY = margin;
// 设置按钮位置和大小
discoverBtn.SetBounds(Rectangle{margin, topY, btnWidth, btnHeight});
requestBtn.SetBounds(Rectangle{margin + (btnWidth + spacing) * 1.0f, topY, btnWidth, btnHeight});
acceptBtn.SetBounds(Rectangle{margin + (btnWidth + spacing) * 2.0f, topY, btnWidth, btnHeight});
rejectBtn.SetBounds(Rectangle{margin + (btnWidth + spacing) * 3.0f, topY, btnWidth, btnHeight});
exitBtn.SetBounds(Rectangle{margin + (btnWidth + spacing) * 4.0f, topY, btnWidth, btnHeight});
// 设置按钮字体大小
discoverBtn.SetFontSize(btnFontSize);
requestBtn.SetFontSize(btnFontSize);
acceptBtn.SetFontSize(btnFontSize);
rejectBtn.SetFontSize(btnFontSize);
exitBtn.SetFontSize(btnFontSize);
// 如果已经匹配成功,退出循环
if (client.isInMatch())
break;
// 检查发现是否超时(10秒后自动停止)
if (discoveryActive && GetTime() >= discoveryEndTime)
{
node.stopUdpListen();
discoveryActive = false;
status = "Discovery stopped";
}
// 获取当前数据
const Vector2 mouse = GetMousePosition();
auto peers = client.getAvailablePeers(); // 可用节点列表
auto pending = client.getPendingRequestsSnapshot(); // 待处理请求列表
// 验证选中索引的有效性
if (selectedPeer >= static_cast<int>(peers.size()))
selectedPeer = -1;
if (selectedPending >= static_cast<int>(pending.size()))
selectedPending = -1;
// ========== 开始绘制 ==========
BeginDrawing();
ClearBackground(RAYWHITE);
// 绘制标题和信息
const float headerY = topY + btnHeight + margin;
DrawText("Match Panel", static_cast<int>(margin), static_cast<int>(headerY), 28, DARKGRAY);
DrawText(TextFormat("Local ID:%s TCP:%d Discovery:%d",
node.getNodeId().c_str(), node.getTcpPort(), node.getDiscoveryPort()),
static_cast<int>(margin), screenHeight - 70, 22, DARKGRAY);
DrawText(status.c_str(), static_cast<int>(margin), screenHeight - 40, 24, BLACK);
// ========== 处理按钮点击 ==========
// 发现按钮:开始10秒的节点发现
if (discoverBtn.Draw())
{
if (!discoveryActive)
{
node.startUdpListen();
discoveryActive = true;
discoveryEndTime = GetTime() + 10.0;
status = "Discovering (10s)";
}
}
// 请求匹配按钮:向选中的节点发送匹配请求
if (requestBtn.Draw())
{
if (selectedPeer >= 0 && selectedPeer < static_cast<int>(peers.size()))
status = client.requestMatch(peers[static_cast<size_t>(selectedPeer)]) ?
"Match request sent" : "Match request failed";
else
status = "Select a peer";
}
// 接受按钮:接受选中的匹配请求
if (acceptBtn.Draw())
{
if (selectedPending >= 0 && selectedPending < static_cast<int>(pending.size()))
status = client.respondToPendingRequest(pending[static_cast<size_t>(selectedPending)], true) ?
"Request accepted" : "Request expired";
else
status = "No request selected";
}
// 拒绝按钮:拒绝选中的匹配请求
if (rejectBtn.Draw())
{
if (selectedPending >= 0 && selectedPending < static_cast<int>(pending.size()))
status = client.respondToPendingRequest(pending[static_cast<size_t>(selectedPending)], false) ?
"Request rejected" : "Request expired";
else
status = "No request selected";
}
// 退出按钮:返回主菜单
if (exitBtn.Draw())
{
if (discoveryActive)
{
node.stopUdpListen();
discoveryActive = false;
}
return false;
}
// ========== 绘制可用节点列表 ==========
DrawText("Available peers", static_cast<int>(margin), 120, 24, BLACK);
float peerY = 152.0f;
for (size_t i = 0; i < peers.size(); ++i)
{
// 创建列表项矩形
Rectangle item{margin, peerY, static_cast<float>(screenWidth - margin * 2.0f), 40.0f};
bool hover = CheckCollisionPointRec(mouse, item);
// 根据状态选择颜色(选中/悬停/正常)
Color fill = (selectedPeer == static_cast<int>(i)) ? Fade(GREEN, 0.35f) : Fade(LIGHTGRAY, 0.35f);
if (hover)
fill = Fade(ORANGE, 0.35f);
// 绘制列表项
DrawRectangleRec(item, fill);
DrawRectangleLinesEx(item, 1.0f, DARKGRAY);
// 显示节点信息:序号 + 名称或ID + IP:端口
std::string label = std::to_string(i + 1) + ". " +
(peers[i].name.empty() ? peers[i].id : peers[i].name) +
" (" + peers[i].ip + ":" + std::to_string(peers[i].tcpPort) + ")";
DrawText(label.c_str(), static_cast<int>(item.x) + 8, static_cast<int>(item.y) + 8, 22, BLACK);
// 处理点击选中
if (hover && IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
selectedPeer = static_cast<int>(i);
peerY += 44.0f;
}
// ========== 绘制待处理请求列表 ==========
float pendingStart = peerY + 24.0f;
if (pendingStart < 340.0f)
pendingStart = 340.0f;
DrawText("Pending requests", static_cast<int>(margin), static_cast<int>(pendingStart), 24, BLACK);
float pendingY = pendingStart + 32.0f;
for (size_t i = 0; i < pending.size(); ++i)
{
// 创建列表项矩形
Rectangle item{margin, pendingY, static_cast<float>(screenWidth - margin * 2.0f), 40.0f};
bool hover = CheckCollisionPointRec(mouse, item);
// 根据状态选择颜色
Color fill = (selectedPending == static_cast<int>(i)) ? Fade(SKYBLUE, 0.35f) : Fade(LIGHTGRAY, 0.35f);
if (hover)
fill = Fade(ORANGE, 0.35f);
// 绘制列表项
DrawRectangleRec(item, fill);
DrawRectangleLinesEx(item, 1.0f, DARKGRAY);
// 显示请求信息:序号 + 请求者名称/ID + IP:端口 + 匹配ID前6位
std::string label = std::to_string(i + 1) + ". " +
(pending[i].peer.name.empty() ? pending[i].peer.id : pending[i].peer.name) +
" (" + pending[i].ip + ":" + std::to_string(pending[i].port) +
") id=" + pending[i].matchId.substr(0, 6);
DrawText(label.c_str(), static_cast<int>(item.x) + 8, static_cast<int>(item.y) + 8, 22, BLACK);
// 处理点击选中
if (hover && IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
selectedPending = static_cast<int>(i);
pendingY += 44.0f;
}
EndDrawing();
}
// 清理:如果发现仍在进行,停止它
if (discoveryActive)
{
node.stopUdpListen();
discoveryActive = false;
}
return client.isInMatch();
}
/**
* @brief 匿名命名空间:游戏内部使用的常量和结构体
*/
namespace
{
constexpr int BoardSize = 9; // 棋盘大小:9x9x9
/**
* @brief 球体实例结构体
*
* 用于表示棋盘上的一个格子位置(以球体显示)
*/
struct SphereInstance
{
Vector3 position; // 3D空间位置
Color color; // 颜色(包含透明度)
float distanceToCamera; // 到相机的距离(用于排序)
int i, j, k; // 棋盘逻辑坐标
};
/**
* @brief 角度转弧度
* @param degree 角度值
* @return 弧度值
*/
float d2r(float degree)
{
return degree * 3.1415926f / 180.0f;
}
} // namespace
/**
* @brief 主游戏循环函数
*
* 这是3D五子棋游戏的核心函数,提供完整的3D可视化和交互功能
*
* 主要功能:
* 1. 3D棋盘渲染(9x9x9网格,使用球体表示棋子)
* 2. 多种视角切换(3D透视、X/Y/Z轴正交视图)
* 3. 平面高亮与选择系统
* 4. 鼠标交互(拖拽旋转、滚轮缩放、点击落子)
* 5. 网络对战支持(通过Client对象)
* 6. 游戏结果显示
*
* 视角模式:
* - vmode=0: 3D透视视图(可自由旋转)
* - vmode=1: X轴正交视图(从X轴负方向看向原点)
* - vmode=2: Y轴正交视图(从Y轴负方向看向原点)
* - vmode=3: Z轴正交视图(从Z轴负方向看向原点)
*
* 高亮模式(仅在3D视图下有效):
* - hlmode=0: 无高亮
* - hlmode=1: 高亮Y-Z平面(固定X坐标)
* - hlmode=2: 高亮X-Z平面(固定Y坐标)
* - hlmode=3: 高亮X-Y平面(固定Z坐标)
*
* @param client 游戏客户端指针(nullptr表示本地双人模式)
* @return -1:用户关闭窗口,0:正常退出返回大厅
*/
int RunGame(Client* client)
{
int screenWidth = 1980;
int screenHeight = 1280;
SetWindowSize(screenWidth, screenHeight);
SetWindowTitle("3D Chess Online - Game");
SetTargetFPS(30);
// ========== 3D相机初始化 ==========
Camera3D camera = { 0 };
camera.position = Vector3{ 400.0f, 0.0f, 0.0f }; // 初始位置:X轴正方向400单位
camera.target = Vector3{ 0.0f, 0.0f, 0.0f }; // 目标:原点
camera.up = Vector3{ 0.0f, 1.0f, 0.0f }; // 上方向:Y轴正方向
camera.fovy = 70.0f; // 视场角(度)
camera.projection = CAMERA_PERSPECTIVE; // 投影模式:透视
SetTargetFPS(30);
// ========== 相机控制参数 ==========
float AngleTheta = 45.0f; // 水平旋转角度(绕Y轴,0°为Z轴正方向)
float AnglePhi = 45.0f; // 垂直旋转角度(-89°到89°)
float SphereDist = 10.0f; // 棋子之间的距离
float cameraDist = 400.0f; // 相机到原点的距离
float SphereRadius = 3.0f; // 棋子球体半径
float Axis_length = 200.0f; // 坐标轴长度
const float dragSensitivity = 0.3f; // 鼠标拖拽灵敏度
// ========== UI按钮创建 ==========
Button toggleButton(Rectangle{ 20.0f, 20.0f, 180.0f, 40.0f }, "Hide all spheres");
Button xButton(Rectangle{ 20.0f, 70.0f, 180.0f, 40.0f }, "X axis");
Button yButton(Rectangle{ 20.0f, 120.0f, 180.0f, 40.0f }, "Y axis");
Button zButton(Rectangle{ 20.0f, 170.0f, 180.0f, 40.0f }, "Z axis");
Button dButton(Rectangle{ 20.0f, 220.0f, 180.0f, 40.0f }, "3D");
Button HighlightButtonx(Rectangle{ 20.0f, 270.0f, 180.0f, 40.0f }, "Y-Z Highlight");
Button HighlightButtony(Rectangle{ 20.0f, 320.0f, 180.0f, 40.0f }, "X-Z Highlight");
Button HighlightButtonz(Rectangle{ 20.0f, 370.0f, 180.0f, 40.0f }, "X-Y Highlight");
// 创建数字按钮(0-9,用于选择平面)
std::vector<Button> NumberButton;
for (int i = 0; i <= 9; ++i)
{
NumberButton.push_back(Button(Rectangle{ 20.0f, (520.0f + i * 50.0f), 180.0f, 40.0f }, std::to_string(i)));
}
NumberButton[0].SetText("Reset"); // 0号按钮用于重置选择
// ========== 游戏状态变量 ==========
std::vector<SphereInstance> spheres; // 球体实例列表(每帧重建)
spheres.reserve(BoardSize * BoardSize * BoardSize);
bool showSpheres = true; // 是否显示球体
int vmode = 0; // 视角模式(0:3D, 1:X轴, 2:Y轴, 3:Z轴)
int hlmode = 0; // 高亮模式(0:无, 1:YZ平面, 2:XZ平面, 3:XY平面)
int number = 0; // 选中的平面编号(1-9,0表示未选中)
int gameStep = 1; // 当前回合数(从1开始)
// ========== 颜色配置 ==========
Color typeColor[4] = { GRAY, BLUE, RED, YELLOW };
typeColor[0].a = 65; // 空位(灰色,半透明)
// ========== 初始相机位置计算 ==========
camera.position.x = cameraDist * cosf(d2r(AnglePhi)) * sinf(d2r(AngleTheta));
camera.position.z = cameraDist * cosf(d2r(AnglePhi)) * cosf(d2r(AngleTheta));
camera.position.y = cameraDist * sinf(d2r(AnglePhi));
// UI文本
std::string BottomText = "3D View ";
std::string AddText;
bool hasLastOpponentMove = false;
int lastOpponentX = 0, lastOpponentY = 0, lastOpponentZ = 0;
// ========== 棋盘状态数组 ==========
// 0:空位, 1:蓝色棋子, 2:红色棋子
// 使用[BoardSize+2]是为了边界检测方便
short ColorBoard[BoardSize + 2][BoardSize + 2][BoardSize + 2];
memset(ColorBoard, 0, sizeof(ColorBoard));
// ========== 初始化网络对战 ==========
if (client)
{
client->initGameState();
}
// ========== 游戏结束相关 ==========
double gameOverTime = 0.0; // 游戏结束的时间戳
bool gameEnded = false; // 游戏是否已结束
int localWinner = 0; // 本地模式获胜者(0:未结束, 1:蓝色, 2:红色)
// ========== 主游戏循环 ==========
while (!WindowShouldClose())
{
screenWidth = GetScreenWidth();
screenHeight = GetScreenHeight();
if (client && !client->isGameRunning() && !gameEnded)
{
gameEnded = true;
gameOverTime = GetTime();
}
// ===== 游戏结束画面(显示3秒后返回大厅) =====
if (gameEnded)
{
if (GetTime() - gameOverTime > 3.0)
{
break; // 退出游戏循环
}
BeginDrawing();
ClearBackground(BLACK);
// 根据游戏结果显示不同信息
if (client)
{
// 在线模式
int result = client->getGameResult();
if (result == 1)
{
DrawText("YOU WIN!", screenWidth / 2 - 150, screenHeight / 2 - 50, 60, GREEN);
}
else if (result == 2)
{
DrawText("YOU LOSE!", screenWidth / 2 - 150, screenHeight / 2 - 50, 60, RED);
}
else
{
DrawText("GAME INTERRUPTED", screenWidth / 2 - 200, screenHeight / 2 - 50, 50, ORANGE);
}
}
else
{
// 本地模式
if (localWinner == 1)
{
DrawText("BLUE WINS!", screenWidth / 2 - 160, screenHeight / 2 - 50, 60, BLUE);
}
else if (localWinner == 2)
{
DrawText("RED WINS!", screenWidth / 2 - 160, screenHeight / 2 - 50, 60, RED);
}
}
DrawText("Returning to lobby in 3 seconds...", screenWidth / 2 - 200, screenHeight / 2 + 50, 24, WHITE);
EndDrawing();
continue;
}
// ===== 接收对手的落子 =====
if (client && !client->isMyTurn() && client->isGameRunning())
{
int x, y, z;
if (client->tryGetOpponentMove(x, y, z))
{
// 根据本方玩家决定对手颜色
int opponentColor = (client->getMyPlayer() == '1') ? 2 : 1;
ColorBoard[x][y][z] = opponentColor;
lastOpponentX = x;
lastOpponentY = y;
lastOpponentZ = z;
hasLastOpponentMove = true;
gameStep++;
}
}
// ===== 每帧初始化 =====
AddText.clear();
spheres.clear();
Axis_length = SphereDist * (BoardSize + 5.0f) / 2;
// ===== 键盘控制:调整球体半径 =====
if (IsKeyDown(KEY_O)) // O键:减小半径
{
if (SphereRadius > 1.0f)
{
SphereRadius -= 0.2f;
}
}
if (IsKeyDown(KEY_P)) // P键:增大半径
{
if (SphereRadius < SphereDist / 2)
{
SphereRadius += 0.2f;
}
}
// ===== 键盘和鼠标控制:3D视角旋转 =====
if (vmode == 0) // 仅在3D模式下允许旋转
{
// 方向键控制
if (IsKeyDown(KEY_UP))
{
AnglePhi += 2.0f;
if (AnglePhi > 89.0f) // 限制垂直角度
{
AnglePhi = 89.0f;
}
}
if (IsKeyDown(KEY_DOWN))
{
AnglePhi -= 2.0f;
if (AnglePhi < -89.0f)
{
AnglePhi = -89.0f;
}
}
if (IsKeyDown(KEY_LEFT))
{
AngleTheta -= 3.0f;
if (AngleTheta < 0.0f)
{
AngleTheta += 360.0f;
}
}
if (IsKeyDown(KEY_RIGHT))
{
AngleTheta += 3.0f;
if (AngleTheta > 360.0f)
{
AngleTheta -= 360.0f;
}
}
// 鼠标中键拖拽旋转
if (IsMouseButtonDown(MOUSE_BUTTON_MIDDLE))
{
Vector2 md = GetMouseDelta();
AngleTheta -= md.x * dragSensitivity;
AnglePhi += md.y * dragSensitivity;
// 限制角度范围
if (AnglePhi > 89.0f) AnglePhi = 89.0f;
if (AnglePhi < -89.0f) AnglePhi = -89.0f;
if (AngleTheta < 0.0f) AngleTheta += 360.0f;
if (AngleTheta >= 360.0f) AngleTheta -= 360.0f;
}
// 鼠标滚轮:调整球体间距
float deltasize = GetMouseWheelMove();
SphereDist += deltasize;
if (SphereDist < 2 * SphereRadius)
SphereDist = 2 * SphereRadius; // 防止球体重叠
}
// ===== 更新相机位置(球坐标 -> 笛卡尔坐标) =====
camera.position.x = cameraDist * cosf(d2r(AnglePhi)) * sinf(d2r(AngleTheta));
camera.position.z = cameraDist * cosf(d2r(AnglePhi)) * cosf(d2r(AngleTheta));
camera.position.y = cameraDist * sinf(d2r(AnglePhi));
// ========== 开始绘制 ==========
BeginDrawing();
ClearBackground(BLACK);
// ===== 3D场景绘制 =====
BeginMode3D(camera);
// 绘制坐标轴(Y轴:绿, X轴:红, Z轴:蓝)
DrawLine3D(Vector3{ 0, Axis_length, 0 }, Vector3{ 0, 0, 0 }, GREEN);
DrawLine3D(Vector3{ Axis_length, 0, 0 }, Vector3{ 0, 0, 0 }, RED);
DrawLine3D(Vector3{ 0, 0, Axis_length }, Vector3{ 0, 0, 0 }, BLUE);
if (showSpheres)
{
// ===== 准备球体渲染 =====
const bool highlightPlaneActive = (vmode == 0 && hlmode != 0 && number != 0);
const Vector2 mousePos = GetMousePosition();
float closestGreyDistSq = std::numeric_limits<float>::max();
std::size_t closestGreyIndex = std::numeric_limits<std::size_t>::max();
const float posDelta = SphereDist * (BoardSize + 1.0f) / 2.0f; // 偏移量(使棋盘居中)
Vector3 lastOpponentWorldPos{};
// ===== 遍历所有格子,创建球体实例 =====
for (int i = 1; i <= BoardSize; ++i)
for (int j = 1; j <= BoardSize; ++j)
for (int k = 1; k <= BoardSize; ++k)
{
// 获取基础颜色
Color color = typeColor[ColorBoard[i][j][k]];
color.a = 230; // 默认不透明度
// 判断是否在高亮平面外
bool isunhighlighted = (hlmode == 1 && number != i) ||
(hlmode == 2 && number != j) ||
(hlmode == 3 && number != k);
bool ishighlighted = (hlmode == 1 && number == i) ||
(hlmode == 2 && number == j) ||
(hlmode == 3 && number == k);
// ===== 应用透明度规则 =====
// 空位基础透明度:在选中的二维平面上设为较高透明度(可交互),否则非常透明
if(ColorBoard[i][j][k] == 0)
{
// 在二维视图的选中平面上,或3D高亮模式的高亮平面上
bool isOnSelectedPlane = (vmode != 0 && number != 0 &&
((vmode == 1 && number == i) ||
(vmode == 2 && number == j) ||
(vmode == 3 && number == k))) || ishighlighted;
// 3D高亮模式下空位更亮,二维视图保持原样
if (vmode == 0 && ishighlighted)
color.a = 180; // 3D高亮平面:180(非常明亮)
else
color.a = isOnSelectedPlane ? 100 : 40; // 选中平面:100(可交互),其他:40(很透明)
}
// 3D视图下的高亮模式:平面外的格子变暗
if (vmode == 0 && number != 0 && isunhighlighted && ColorBoard[i][j][k] != 0)
color.a = 150; // 已有棋子:半透明
// 正交视图下:只显示选中的平面
if (vmode != 0 && number != 0)
{
if (vmode == 1 && number != i)
color.a = 0; // 完全隐藏
if (vmode == 2 && number != j)
color.a = 0;
if (vmode == 3 && number != k)
color.a = 0;
}
// ===== 计算3D世界坐标 =====
Vector3 worldPos = Vector3{
SphereDist * i - posDelta,
SphereDist * j - posDelta,
SphereDist * k - posDelta
};
if (hasLastOpponentMove && client && i == lastOpponentX && j == lastOpponentY && k == lastOpponentZ)
lastOpponentWorldPos = worldPos;
// 投影到屏幕坐标
Vector2 screenPos = GetWorldToScreen(worldPos, camera);
// ===== 正交视图下的鼠标悬停检测 =====
if (number != 0 && vmode != 0 && color.a > 60 &&
CheckCollisionPointCircle(mousePos, screenPos, 45.0f))
{
color.a = 100; // 悬停时半透明
AddText = " --Mouse On (" + std::to_string(i) + "," +
std::to_string(j) + "," + std::to_string(k) + ")";
// 鼠标点击
if (ColorBoard[i][j][k] == 0 && IsMouseButtonDown(MOUSE_BUTTON_LEFT))
{
if (client)
{
// 在线对战模式,检查回合并落子
if (client->isMyTurn() && client->tryPlaceMyPiece(i, j, k))
{
int myColor = (client->getMyPlayer() == '1') ? 1 : 2;
ColorBoard[i][j][k] = myColor;
gameStep++;
}
}
else
{
// 本地双人模式,轮流落子
int playerColor = ((gameStep - 1) % 2 + 1);
ColorBoard[i][j][k] = playerColor;
// 检查是否获胜(需要转换为char类型)
int coords[3] = {i, j, k};
char playerChar = (playerColor == 1) ? '1' : '2';
// 创建临时char数组用于CheckWin
char* tempBoard = (char*)calloc(BoardSize * BoardSize * BoardSize, sizeof(char));
if (tempBoard)
{
// 将ColorBoard转换为char数组
for (int ti = 1; ti <= BoardSize; ++ti)
for (int tj = 1; tj <= BoardSize; ++tj)
for (int tk = 1; tk <= BoardSize; ++tk)
{
int idx = place(ti, tj, tk, BoardSize);
tempBoard[idx] = (ColorBoard[ti][tj][tk] == 1) ? '1' :
(ColorBoard[ti][tj][tk] == 2) ? '2' : 0;
}
// 检查获胜
if (CheckWin(BoardSize, tempBoard, coords, playerChar))
{
localWinner = playerColor;
gameEnded = true;
gameOverTime = GetTime();
}
free(tempBoard);
}
++gameStep;
}
}
}
// 完全透明的格子不添加到渲染列表
if (color.a == 0)
continue;
// 添加球体实例到列表
const std::size_t sphereIndex = spheres.size();
// 以紫色显示对手的最近落子
if (i == lastOpponentX && j == lastOpponentY && k == lastOpponentZ)
{
int oc = (client->getMyPlayer() == '1') ? 2 : 1;
color.r = (oc == 1) ? 0 : 122;
color.g = 122;
color.b = (oc == 2) ? 0 : 122;
}
spheres.push_back({ worldPos, color, Vector3Distance(camera.position, worldPos), i, j, k });
// ===== 3D高亮模式:寻找最近的空位 =====
if (highlightPlaneActive && ColorBoard[i][j][k] == 0)
{
const bool isOnHighlightPlane =
(hlmode == 1 && number == i) ||
(hlmode == 2 && number == j) ||
(hlmode == 3 && number == k);
if (isOnHighlightPlane)
{
float distSq = Vector2DistanceSqr(mousePos, screenPos);
if (distSq < closestGreyDistSq)
{
closestGreyDistSq = distSq;
closestGreyIndex = sphereIndex;
}
}
}
}
// ===== 高亮距离鼠标最近的空位(3D高亮模式) =====
if (highlightPlaneActive &&
closestGreyIndex != std::numeric_limits<std::size_t>::max() &&
closestGreyDistSq < 800) // 距离阈值:约28像素
{
SphereInstance& highlighted = spheres[closestGreyIndex];
Color highlightColor = YELLOW;
highlightColor.a = highlighted.color.a;
highlighted.color = highlightColor;
AddText = " --Hover (" + std::to_string(highlighted.i) + "," +
std::to_string(highlighted.j) + "," + std::to_string(highlighted.k) + ")";
// 点击落子
if (ColorBoard[highlighted.i][highlighted.j][highlighted.k] == 0 &&
IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
{
if (client)
{
if (client->isMyTurn() && client->tryPlaceMyPiece(highlighted.i, highlighted.j, highlighted.k))
{
int myColor = (client->getMyPlayer() == '1') ? 1 : 2;
ColorBoard[highlighted.i][highlighted.j][highlighted.k] = myColor;
gameStep++;
}
}
else
{
// 本地双人模式,轮流落子
int playerColor = ((gameStep - 1) % 2 + 1);
ColorBoard[highlighted.i][highlighted.j][highlighted.k] = playerColor;
// 检查是否获胜(需要转换为char类型)
int coords[3] = {highlighted.i, highlighted.j, highlighted.k};
char playerChar = (playerColor == 1) ? '1' : '2';
// 创建临时char数组用于CheckWin
char* tempBoard = (char*)calloc(BoardSize * BoardSize * BoardSize, sizeof(char));
if (tempBoard)
{
// 将ColorBoard转换为char数组
for (int ti = 1; ti <= BoardSize; ++ti)
for (int tj = 1; tj <= BoardSize; ++tj)
for (int tk = 1; tk <= BoardSize; ++tk)
{
int idx = place(ti, tj, tk, BoardSize);
tempBoard[idx] = (ColorBoard[ti][tj][tk] == 1) ? '1' :
(ColorBoard[ti][tj][tk] == 2) ? '2' : 0;
}
// 检查获胜
if (CheckWin(BoardSize, tempBoard, coords, playerChar))
{
localWinner = playerColor;
gameEnded = true;
gameOverTime = GetTime();
}
free(tempBoard);
}
++gameStep;
}
}
}
// ===== 渲染所有球体(按距离排序,实现正确的透明度混合) =====
if (!spheres.empty())
{
// 从远到近排序(画家算法)
std::sort(spheres.begin(), spheres.end(),
[](const SphereInstance& a, const SphereInstance& b)
{ return a.distanceToCamera > b.distanceToCamera; });
// 禁用深度写入(允许透明度混合)
rlDisableDepthMask();
for (const auto& sphere : spheres)
DrawSphere(sphere.position, SphereRadius, sphere.color);
rlEnableDepthMask();
}
// 同方相邻棋子连线(仅3D视图,使用细圆柱,含斜向邻接,且仅当连续>=3)
if (vmode == 0 && showSpheres)
{
const float lineRadius = SphereRadius * 0.12f;
for (int i = 1; i <= BoardSize; ++i)
for (int j = 1; j <= BoardSize; ++j)
for (int k = 1; k <= BoardSize; ++k)
{
int colorId = ColorBoard[i][j][k];
if (colorId == 0) continue;
// 遍历唯一方向(正半空间),检查是否为该方向连线的起点
for (int dx = -1; dx <= 1; ++dx)
for (int dy = -1; dy <= 1; ++dy)
for (int dz = -1; dz <= 1; ++dz)
{
if (dx == 0 && dy == 0 && dz == 0) continue;
if (dx < 0) continue;
if (dx == 0 && dy < 0) continue;
if (dx == 0 && dy == 0 && dz < 0) continue;
int prevI = i - dx, prevJ = j - dy, prevK = k - dz;
if (prevI >= 1 && prevI <= BoardSize && prevJ >= 1 && prevJ <= BoardSize && prevK >= 1 && prevK <= BoardSize)
{
if (ColorBoard[prevI][prevJ][prevK] == colorId)
continue; // 不是链条起点
}
// 前向统计长度
int len = 1;
int ni = i + dx, nj = j + dy, nk = k + dz;
while (ni >= 1 && ni <= BoardSize && nj >= 1 && nj <= BoardSize && nk >= 1 && nk <= BoardSize && ColorBoard[ni][nj][nk] == colorId)
{
++len;
ni += dx; nj += dy; nk += dz;
}
if (len < 3) continue; // 少于3个不连线
// 绘制链条圆柱
Vector3 posA{ SphereDist * i - posDelta,
SphereDist * j - posDelta,
SphereDist * k - posDelta };
Color lineColor = typeColor[colorId];
lineColor.a = 200;
double ThicknessFactor = (len==3)?0.15:0.3;
ni = i + dx; nj = j + dy; nk = k + dz;
for (int step = 1; step < len; ++step)
{
Vector3 posB{ SphereDist * ni - posDelta,
SphereDist * nj - posDelta,
SphereDist * nk - posDelta };
DrawCylinderEx(posA, posB, ThicknessFactor* SphereRadius, ThicknessFactor * SphereRadius, 8, lineColor);
posA = posB;
ni += dx; nj += dy; nk += dz;
}
}
}
}
}
EndMode3D();
int currentPlayer = ((gameStep - 1) % 2) + 1;
const float hudMargin = std::max(20.0f, screenWidth * 0.02f);
const float turnRadius = std::clamp(screenHeight * 0.03f, 20.0f, 36.0f);
const int turnFont = static_cast<int>(std::clamp(screenHeight * 0.025f, 16.0f, 28.0f));
const int stepFont = static_cast<int>(std::clamp(screenHeight * 0.04f, 30.0f, 52.0f));
const int bottomFont = static_cast<int>(std::clamp(screenHeight * 0.032f, 18.0f, 40.0f));
const int axisFont = static_cast<int>(std::clamp(screenHeight * 0.02f, 14.0f, 22.0f));
// ===== 当前玩家指示器(右上角圆圈) =====
Vector2 turnCircleCenter{ screenWidth - hudMargin - turnRadius, hudMargin + turnRadius };
DrawCircleV(turnCircleCenter, turnRadius, typeColor[currentPlayer]);
float turnTextX = std::max(hudMargin, turnCircleCenter.x - std::max(220.0f, screenWidth * 0.18f));
float turnTextY = hudMargin;
if (client)
{
// 显示当前回合归属
if (client->isMyTurn())
DrawText("Your Turn", static_cast<int>(turnTextX), static_cast<int>(turnTextY), turnFont, GREEN);
else
DrawText("Opponent's Turn", static_cast<int>(turnTextX), static_cast<int>(turnTextY), turnFont, ORANGE);
// 显示最近一次同步的时间(秒)
auto timeSinceSync = client->getTimeSinceLastSync();
std::string syncText = "Last Sync: " + std::to_string(timeSinceSync) + "s ago";
// 根据同步时间设置颜色(<6s:绿, <10s:黄, >=10s:红)
Color syncColor = (timeSinceSync < 6) ? GREEN : (timeSinceSync < 10 ? YELLOW : RED);
DrawText(syncText.c_str(), static_cast<int>(turnTextX), static_cast<int>(turnTextY) + 60, turnFont - 10, syncColor);
}
else
{
// 本地模式:显示当前玩家
if (currentPlayer == 1)
DrawText("BLUE's Turn", static_cast<int>(turnTextX), static_cast<int>(turnTextY), turnFont, WHITE);
else
DrawText("RED's Turn", static_cast<int>(turnTextX), static_cast<int>(turnTextY), turnFont, WHITE);
}
float stepX = screenWidth - hudMargin - stepFont * 1.5f;
float stepY = screenHeight - hudMargin - stepFont * 1.5f;
DrawText(std::to_string(gameStep).c_str(), static_cast<int>(stepX), static_cast<int>(stepY), stepFont, WHITE);
// ===== 绘制坐标轴标签 =====
Vector2 xPos = GetWorldToScreen(Vector3{ Axis_length, 0.0f, 0.0f }, camera);
Vector2 yPos = GetWorldToScreen(Vector3{ 0.0f, Axis_length, 0.0f }, camera);
Vector2 zPos = GetWorldToScreen(Vector3{ 0.0f, 0.0f, Axis_length }, camera);
DrawText("x", static_cast<int>(xPos.x), static_cast<int>(xPos.y), axisFont, RED);
DrawText("y", static_cast<int>(yPos.x), static_cast<int>(yPos.y), axisFont, GREEN);
DrawText("z", static_cast<int>(zPos.x), static_cast<int>(zPos.y), axisFont, BLUE);
if (vmode == 0)
{
BottomText = "3D View ";
if (hlmode != 0)
{
BottomText += " - Highlighting ";
if (hlmode == 1)
BottomText += "X";
if (hlmode == 2)
BottomText += "Y";
if (hlmode == 3)
BottomText += "Z";
}
if (number != 0)
BottomText += "=" + std::to_string(number) + " Plane";
}
else
{
if (vmode == 1)
BottomText = "X Axis View ";
if (vmode == 2)
BottomText = "Y Axis View ";
if (vmode == 3)
BottomText = "Z Axis View ";
if (number != 0)
{
if (vmode == 1)
BottomText += " X=" + std::to_string(number) + " Plane";
if (vmode == 2)
BottomText += " Y=" + std::to_string(number) + " Plane";
if (vmode == 3)
BottomText += " Z=" + std::to_string(number) + " Plane";
}
}
float bottomY = screenHeight - hudMargin - bottomFont - 4.0f;
// 绘制底部状态栏
if (showSpheres)
DrawText((BottomText + AddText).c_str(), static_cast<int>(hudMargin), static_cast<int>(bottomY), bottomFont, WHITE);
else
DrawText((BottomText + "(Hidden)").c_str(), static_cast<int>(hudMargin), static_cast<int>(bottomY), bottomFont, WHITE);
// ========== 处理UI按钮点击 ==========
// 切换显示/隐藏球体
if (toggleButton.Draw())
{
showSpheres = !showSpheres;
}
// 切换到3D视图
if (dButton.Draw())
{
hlmode = vmode; // 保存之前的高亮模式
vmode = 0;
camera.projection = CAMERA_PERSPECTIVE;
SphereRadius = 3.0f;
SphereDist = 10.0f;
AngleTheta = 45.0f;
AnglePhi = 45.0f;
cameraDist = 400.0f;
}
// 切换到X轴视图
if (xButton.Draw())
{