ZZY 7e2ca3948d feat: 重构棋盘逻辑和游戏流程,将引擎版本提升至4.3
- 重构了 VirtualBoard 类,优化了棋盘操作和事件处理
- 重写了 ChessPiece 类,增加了事件处理和虚拟棋子逻辑
- 更新了 ChessGame 控制器,增加了结束回合功能和对话框
- 调整了场景布局和资源引用
- 清理了冗余代码和注释
2024-11-02 21:18:53 +08:00

146 lines
3.7 KiB
C#

using Godot;
using Godot.Collections;
public partial class ChessGame : Node2D
{
ChessBoard board;
Global global;
ConfirmationDialog dialog;
private bool isSession = false;
private Vector2 from;
private Vector2 to;
// 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");
dialog = new ConfirmationDialog {
DialogAutowrap = true,
MinSize = new Vector2I(400, 200),
Position = new Vector2I(200, 400),
};
AddChild(dialog);
// GetNode<Button>("Undo").Connect("pressed", Callable.From(board.Undo));
// GetNode<Button>("ReInit").Connect("pressed", Callable.From(board.ReInit));
// GetNode<Button>("Home").Connect("pressed", Callable.From(this.GoHome));
if (!global.RPClient.GetIsConnected()) {
return;
}
isSession = true;
GD.Print("ws is connected");
global.RPClient.OnPRCSessionExit += (cmd, code) => {
GoHome();
};
board.Set("Hello", Callable.From(() => {GD.PrintErr("hello");}));
board.Set("chessMoveFunc", Callable.From((Vector2 newPos, Vector2 fromPos) => {
if (from.X == fromPos.X && from.Y == fromPos.Y && to.X == newPos.X && to.Y == newPos.Y) {
return true;
}
from = fromPos;
to = newPos;
var res = global.RPClient.SendSessionToAll(global.sessionId, new Dictionary{
{"type", "move"},
{"from", fromPos},
{"to", newPos},
{"fromX", fromPos.X},
{"fromY", fromPos.Y},
{"toX", newPos.X},
{"toY", newPos.Y},
});
GD.Print($"chessMoveFunc Callback {fromPos} -> {newPos} {res}");
return false;
}));
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 "move":
Vector2 _to = new(GD.StrToVar(msg["toX"].ToString()).AsInt32(),
GD.StrToVar(msg["toY"].ToString()).AsInt32());
Vector2 _from = new(GD.StrToVar(msg["fromX"].ToString()).AsInt32(),
GD.StrToVar(msg["fromY"].ToString()).AsInt32());
if (_to.X == to.X && _to.Y == to.Y && _from.X == from.X && _from.Y == from.Y) {
return;
}
to = _to;
from = _from;
board.playerSelf.MoveAndRecord(to, from);
break;
case "undo":
_Undo();
break;
case "reInit":
_ReInit();
break;
}
}
private void _Undo() {
board.playerSelf.Undo();
}
private void _ReInit() {
board.playerSelf.ReInit();
board.InitChessBoard();
}
private void BtnOver() {
GD.PrintErr($"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.PrintErr($"Undo {isSession}");
if (isSession == false) {
_Undo();
}
global.RPClient.SendSessionToAll(global.sessionId, new Dictionary{
{"type", "undo"},
});
}
public void ReInit() {
GD.PrintErr($"ReInit {isSession}");
if (isSession == false) {
_ReInit();
}
global.RPClient.SendSessionToAll(global.sessionId, new Dictionary{
{"type", "reInit"},
});
}
}