update: 更新到godot4.4.1,并大量重构代码
This commit is contained in:
@ -5,7 +5,7 @@ using ChineseChess;
|
||||
|
||||
public partial class ChessGame : Node2D {
|
||||
ChessBoard board;
|
||||
Global global;
|
||||
GlobalManager global = GlobalManager.Instance;
|
||||
ConfirmationDialog dialog;
|
||||
private bool isSession = false;
|
||||
ChessCore Game;
|
||||
@ -15,15 +15,17 @@ public partial class ChessGame : Node2D {
|
||||
// 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();
|
||||
// ChessGameAPI api = new();
|
||||
// api.BindToLua(global.ModManager.lua);
|
||||
|
||||
// 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.Print("ChessGame uid:", global.RPClient.GetUserId(), " color:",global.GlobalData["player_color"]);
|
||||
// GD.Print("ChessGame uid:", global.RPClient.GetUserId(), " color:",global.GlobalData["player_color"]);
|
||||
|
||||
dialog = new ConfirmationDialog {
|
||||
DialogAutowrap = true,
|
||||
@ -52,12 +54,12 @@ public partial class ChessGame : Node2D {
|
||||
Vector.Vector2I vector = new(posX, posY);
|
||||
if (isSession) {
|
||||
Game.OnPosClicked(vector, ChessCore.PlayerSideType.Self);
|
||||
var res = global.RPClient.SendSessionToAll(global.sessionId, new Dictionary {
|
||||
{"type", "mouseClicked"},
|
||||
{"X", pos.X},
|
||||
{"Y", pos.Y},
|
||||
{"id", global.RPClient.GetUserId()}
|
||||
});
|
||||
// var res = global.RPClient.SendSessionToAll(global.sessionId, new Dictionary {
|
||||
// {"type", "mouseClicked"},
|
||||
// {"X", pos.X},
|
||||
// {"Y", pos.Y},
|
||||
// {"id", global.RPClient.GetUserId()}
|
||||
// });
|
||||
} else {
|
||||
Game.OnPosClicked(vector);
|
||||
}
|
||||
@ -66,13 +68,13 @@ public partial class ChessGame : Node2D {
|
||||
if (isSession) {
|
||||
GD.Print("ws is connected");
|
||||
|
||||
global.RPClient.OnPRCSessionExit += (cmd, code) => {
|
||||
GoHome();
|
||||
};
|
||||
// global.RPClient.OnPRCSessionExit += (cmd, code) => {
|
||||
// GoHome();
|
||||
// };
|
||||
|
||||
global.RPClient.OnPRCSessionRecv += (msg) => {
|
||||
SessionMsgHandle(msg["msg"].AsGodotDictionary());
|
||||
};
|
||||
// global.RPClient.OnPRCSessionRecv += (msg) => {
|
||||
// SessionMsgHandle(msg["msg"].AsGodotDictionary());
|
||||
// };
|
||||
}
|
||||
}
|
||||
|
||||
@ -80,50 +82,50 @@ public partial class ChessGame : Node2D {
|
||||
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());
|
||||
Vector.Vector2I vector = new((int)mouseClicked.X, (int)mouseClicked.Y);
|
||||
Game.OnPosClicked(vector, ChessCore.PlayerSideType.Opponent);
|
||||
break;
|
||||
case "undo":
|
||||
Game.Undo();
|
||||
break;
|
||||
case "reInit":
|
||||
Game.ReInit();
|
||||
break;
|
||||
}
|
||||
}
|
||||
// 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());
|
||||
// Vector.Vector2I vector = new((int)mouseClicked.X, (int)mouseClicked.Y);
|
||||
// Game.OnPosClicked(vector, 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()},
|
||||
});
|
||||
// global.RPClient.SendSessionToAll(global.sessionId, new Dictionary{
|
||||
// {"type", "over"},
|
||||
// {"id", global.RPClient.GetUserId()},
|
||||
// });
|
||||
}
|
||||
|
||||
public void GoHome() {
|
||||
if (global.RPClient.IsOnline()) {
|
||||
global.RPClient.ExitServer();
|
||||
}
|
||||
// if (global.RPClient.IsOnline()) {
|
||||
// global.RPClient.ExitServer();
|
||||
// }
|
||||
global.GotoScene("res://Main.tscn");
|
||||
}
|
||||
|
||||
@ -131,10 +133,10 @@ public partial class ChessGame : Node2D {
|
||||
GD.Print($"Undo {isSession}");
|
||||
|
||||
if (isSession) {
|
||||
global.RPClient.SendSessionToAll(global.sessionId, new Dictionary{
|
||||
{"type", "undo"},
|
||||
{"id", global.RPClient.GetUserId()},
|
||||
});
|
||||
// global.RPClient.SendSessionToAll(global.sessionId, new Dictionary{
|
||||
// {"type", "undo"},
|
||||
// {"id", global.RPClient.GetUserId()},
|
||||
// });
|
||||
} else {
|
||||
if (Game.board.Steps == 0) board.Clear();
|
||||
Game.Undo();
|
||||
@ -143,11 +145,11 @@ public partial class ChessGame : Node2D {
|
||||
|
||||
public void ReInit() {
|
||||
GD.PrintErr($"ReInit {isSession}");
|
||||
|
||||
|
||||
if (isSession) {
|
||||
global.RPClient.SendSessionToAll(global.sessionId, new Dictionary{
|
||||
{"type", "reInit"},
|
||||
});
|
||||
// global.RPClient.SendSessionToAll(global.sessionId, new Dictionary{
|
||||
// {"type", "reInit"},
|
||||
// });
|
||||
} else {
|
||||
Game.ReInit();
|
||||
board.Clear();
|
||||
|
Reference in New Issue
Block a user