ZZY 8ee9732a6f feat(chinese-chess): 实现中国象棋核心逻辑和基本功能
- 抽象出 ChessCore 类,包含游戏初始化、行棋逻辑、悔棋等功能
- 重构 Player 类,优化行棋和记录逻辑
- 更新 ChessBoard 和 ChessPiece 类,适应新逻辑
- 移除冗余代码,提高代码可读性和可维护性
2024-11-07 20:48:08 +08:00

156 lines
4.0 KiB
C#

using Godot;
using Godot.Collections;
using ChineseChess;
public partial class ChessGame : Node2D {
ChessBoard board;
Global global;
ConfirmationDialog dialog;
private bool isSession = false;
ChessCore Game;
ChessCore.TurnsSideType sideSelf;
ChessCore.TurnsSideType sideOpposite;
// Called when the node enters the scene tree for the first time.
public override void _Ready() {
// Init.Call();
global = GetNode<Global>("/root/Global");
board = GetNode<ChessBoard>("Chessboard");
isSession = global.RPClient.IsOnline();
GetNode<LineEdit>("Control/VBoxContainer/MarginContainer3/HFlowContainer/LineEdit")
.Text = global.GlobalData["player_color"].AsString();
LineEdit turnSideEdit = GetNode<LineEdit>("Control/VBoxContainer/MarginContainer3/HFlowContainer/LineEdit2");
turnSideEdit.Text = "red";
GD.PrintErr("ChessGame ", global.RPClient.GetUserId(), ":",global.GlobalData["player_color"]);
dialog = new ConfirmationDialog {
DialogAutowrap = true,
MinSize = new Vector2I(400, 200),
Position = new Vector2I(200, 400),
};
AddChild(dialog);
if (global.GlobalData["player_color"].AsString() == "red") {
sideSelf = ChessCore.TurnsSideType.Red;
sideOpposite = ChessCore.TurnsSideType.Black;
} else {
sideSelf = ChessCore.TurnsSideType.Black;
sideOpposite = ChessCore.TurnsSideType.Red;
}
if (isSession) {
Game = new(ChessCore.Mode.MultiMode, sideSelf);
} else {
Game = new(ChessCore.Mode.SingleMode, sideSelf);
}
board.LoadBoard(Game.board);
Game.InitGame();
Game.board.OnMove += (sender, e) => {
turnSideEdit.Text = Game.GetTurnsType() == ChessCore.TurnsSideType.Red ? "red" : "black";
};
board.OnPosClicked += (sender, pos) => {
if (isSession) {
Game.OnPosClicked(pos, ChessCore.PlayerSideType.Self);
var res = global.RPClient.SendSessionToAll(global.sessionId, new Dictionary {
{"type", "mouseClicked"},
{"X", pos.X},
{"Y", pos.Y},
{"id", global.RPClient.GetUserId()}
});
} else {
Game.OnPosClicked(pos);
}
};
if (isSession) {
GD.Print("ws is connected");
global.RPClient.OnPRCSessionExit += (cmd, code) => {
GoHome();
};
global.RPClient.OnPRCSessionRecv += (msg) => {
SessionMsgHandle(msg["msg"].AsGodotDictionary());
};
}
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta) {
}
private void SessionMsgHandle(Dictionary msg) {
GD.PrintErr($"session msg: {msg}");
switch (msg["type"].AsString()) {
case "over":
if (global.RPClient.GetUserId() == msg["id"].AsString()) {
break;
}
dialog.Title = "Opponent Finished";
dialog.DialogText = "Turn On You\n";
dialog.Visible = true;
break;
case "mouseClicked":
if (msg["id"].ToString() == global.RPClient.GetUserId()) {
break;
}
Vector2 mouseClicked = new(GD.StrToVar(msg["X"].ToString()).AsInt32(),
GD.StrToVar(msg["Y"].ToString()).AsInt32());
Game.OnPosClicked(mouseClicked, ChessCore.PlayerSideType.Opponent);
break;
case "undo":
Game.Undo();
break;
case "reInit":
Game.ReInit();
break;
}
}
private void BtnOver() {
GD.Print($"BtnOver {isSession}");
if (isSession == false) {
return;
}
global.RPClient.SendSessionToAll(global.sessionId, new Dictionary{
{"type", "over"},
{"id", global.RPClient.GetUserId()},
});
}
public void GoHome() {
if (global.RPClient.IsOnline()) {
global.RPClient.ExitServer();
}
global.GotoScene("res://Main.tscn");
}
public void Undo() {
GD.Print($"Undo {isSession}");
if (isSession) {
global.RPClient.SendSessionToAll(global.sessionId, new Dictionary{
{"type", "undo"},
{"id", global.RPClient.GetUserId()},
});
} else {
Game.Undo();
}
}
public void ReInit() {
GD.PrintErr($"ReInit {isSession}");
if (isSession) {
global.RPClient.SendSessionToAll(global.sessionId, new Dictionary{
{"type", "reInit"},
});
} else {
Game.ReInit();
}
}
}