feat(chess): 初始化棋盘和玩家颜色,优化代码结构,重构多人游戏代码
- 在 ChessGame 中初始化棋盘,根据玩家颜色设置棋子 - 更新 UI 显示玩家颜色 - 调整棋盘位置和 UI 布局 - 重构部分代码以提高可读性和维护性
This commit is contained in:
@ -1,8 +1,8 @@
|
||||
using System.Collections;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
public partial class ChessGame : Node2D
|
||||
{
|
||||
public partial class ChessGame : Node2D {
|
||||
ChessBoard board;
|
||||
Global global;
|
||||
ConfirmationDialog dialog;
|
||||
@ -17,17 +17,14 @@ public partial class ChessGame : Node2D
|
||||
// Init.Call();
|
||||
global = GetNode<Global>("/root/Global");
|
||||
board = GetNode<ChessBoard>("Chessboard");
|
||||
isSession = global.RPClient.IsOnline();
|
||||
|
||||
playerSelf = new Player(board.board, Player.PlayerType.Human);
|
||||
playerOpponent = new Player(board.board, Player.PlayerType.Human);
|
||||
|
||||
board.OnMouseClicked += (sender, clickPosition) => {
|
||||
Vector2 clickBoardPos = (PosTrans.transArrToPix.AffineInverse() *
|
||||
clickPosition).Round();
|
||||
|
||||
playerSelf.HandleBoardPosClick(clickBoardPos);
|
||||
};
|
||||
|
||||
InitChessBoard();
|
||||
GetNode<LineEdit>("Control/VBoxContainer/MarginContainer3/HFlowContainer/LineEdit")
|
||||
.Text = global.GlobalData["player_color"].AsString();
|
||||
GD.PrintErr("ChessGame ", global.RPClient.GetUserId(), ":",global.GlobalData["player_color"]);
|
||||
|
||||
dialog = new ConfirmationDialog {
|
||||
DialogAutowrap = true,
|
||||
@ -36,37 +33,76 @@ public partial class ChessGame : Node2D
|
||||
};
|
||||
AddChild(dialog);
|
||||
|
||||
if (!global.RPClient.GetIsConnected()) {
|
||||
return;
|
||||
board.OnPosClicked += (sender, pos) => {
|
||||
if (isSession) {
|
||||
var res = global.RPClient.SendSessionToAll(global.sessionId, new Dictionary {
|
||||
{"type", "mouseClicked"},
|
||||
{"X", pos.X},
|
||||
{"Y", pos.Y},
|
||||
{"id", global.RPClient.GetUserId()}
|
||||
});
|
||||
playerSelf.HandleBoardPosClick(pos);
|
||||
} else {
|
||||
playerSelf.HandleBoardPosClick(pos);
|
||||
playerSelf.SetAllowedPieces(null);
|
||||
}
|
||||
};
|
||||
|
||||
if (isSession) {
|
||||
GD.Print("ws is connected");
|
||||
|
||||
global.RPClient.OnPRCSessionExit += (cmd, code) => {
|
||||
GoHome();
|
||||
};
|
||||
|
||||
global.RPClient.OnPRCSessionRecv += (msg) => {
|
||||
SessionMsgHandle(msg["msg"].AsGodotDictionary());
|
||||
};
|
||||
}
|
||||
isSession = true;
|
||||
GD.Print("ws is connected");
|
||||
|
||||
global.RPClient.OnPRCSessionExit += (cmd, code) => {
|
||||
GoHome();
|
||||
};
|
||||
|
||||
board.OnMouseClicked += (sender, clickPosition) => {
|
||||
Vector2 clickBoardPos = (PosTrans.transArrToPix.AffineInverse() *
|
||||
clickPosition).Round();
|
||||
|
||||
var res = global.RPClient.SendSessionToAll(global.sessionId, new Dictionary {
|
||||
{"type", "mouseClicked"},
|
||||
{"X", clickBoardPos.X},
|
||||
{"Y", clickBoardPos.Y},
|
||||
});
|
||||
// GD.Print($"chessMoveFunc Callback {fromPos} -> {newPos} {res}");
|
||||
};
|
||||
|
||||
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) {
|
||||
}
|
||||
|
||||
public void InitChessBoard() {
|
||||
ArrayList black = InitializePieces("black", 0, new[] {
|
||||
("车", 0, 0), ("马", 1, 0), ("象", 2, 0),
|
||||
("士", 3, 0), ("将", 4, 0), ("士", 5, 0),
|
||||
("象", 6, 0), ("马", 7, 0), ("车", 8, 0),
|
||||
("炮", 1, 2), ("炮", 7, 2),
|
||||
("卒", 0, 3), ("卒", 2, 3), ("卒", 4, 3), ("卒", 6, 3), ("卒", 8, 3)
|
||||
});
|
||||
|
||||
ArrayList red = InitializePieces("red", 9, new[] {
|
||||
("车", 0, -0), ("马", 1, -0), ("象", 2, -0),
|
||||
("士", 3, -0), ("将", 4, -0), ("士", 5, -0),
|
||||
("象", 6, -0), ("马", 7, -0), ("车", 8, -0),
|
||||
("炮", 1, -2), ("炮", 7, -2),
|
||||
("卒", 0, -3), ("卒", 2, -3), ("卒", 4, -3), ("卒", 6, -3), ("卒", 8, -3)
|
||||
});
|
||||
if (global.GlobalData["player_color"].AsString() == "red") {
|
||||
playerSelf.SetAllowedPieces(red);
|
||||
playerOpponent.SetAllowedPieces(black);
|
||||
} else {
|
||||
playerSelf.SetAllowedPieces(black);
|
||||
playerOpponent.SetAllowedPieces(red);
|
||||
}
|
||||
}
|
||||
|
||||
private ArrayList InitializePieces(string color, int baseY, (string label, int x, int y)[] positions) {
|
||||
ArrayList list = new();
|
||||
foreach (var (label, x, y) in positions) {
|
||||
ChessPiece piece = new ChessPiece {
|
||||
PieceLabel = label,
|
||||
LabelColor = new Color(color)
|
||||
};
|
||||
list.Add(piece.GetVirtualPiece());
|
||||
board.InsertNode(piece, new Vector2(x, baseY + y));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private void SessionMsgHandle(Dictionary msg) {
|
||||
GD.PrintErr($"session msg: {msg}");
|
||||
switch (msg["type"].AsString()) {
|
||||
@ -79,12 +115,18 @@ public partial class ChessGame : Node2D
|
||||
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());
|
||||
playerOpponent.HandleBoardPosClick(mouseClicked);
|
||||
break;
|
||||
case "undo":
|
||||
_Undo();
|
||||
if (msg["id"].ToString() == global.RPClient.GetUserId()) {
|
||||
break;
|
||||
}
|
||||
playerOpponent.Undo();
|
||||
break;
|
||||
case "reInit":
|
||||
_ReInit();
|
||||
@ -92,13 +134,9 @@ public partial class ChessGame : Node2D
|
||||
}
|
||||
}
|
||||
|
||||
private void _Undo() {
|
||||
playerSelf.Undo();
|
||||
}
|
||||
|
||||
private void _ReInit() {
|
||||
playerSelf.ReInit();
|
||||
board.InitChessBoard();
|
||||
InitChessBoard();
|
||||
}
|
||||
|
||||
private void BtnOver() {
|
||||
@ -123,11 +161,13 @@ public partial class ChessGame : Node2D
|
||||
GD.Print($"Undo {isSession}");
|
||||
|
||||
if (isSession) {
|
||||
playerSelf.Undo();
|
||||
global.RPClient.SendSessionToAll(global.sessionId, new Dictionary{
|
||||
{"type", "undo"},
|
||||
{"id", global.RPClient.GetUserId()},
|
||||
});
|
||||
} else {
|
||||
_Undo();
|
||||
playerSelf.Undo();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -10,6 +10,7 @@ public partial class GameLobby : Control {
|
||||
|
||||
string URL;
|
||||
string userName;
|
||||
bool isAcceptedPart = false;
|
||||
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
public override void _Ready() {
|
||||
@ -26,8 +27,11 @@ public partial class GameLobby : Control {
|
||||
global.RPClient.SessionAckCreate(dialog.GetMeta("sessionId").ToString(), false);
|
||||
};
|
||||
dialog.Confirmed += () => {
|
||||
if (dialog.Title == "Session Created")
|
||||
global.RPClient.SessionAckCreate(dialog.GetMeta("sessionId").ToString(), true);
|
||||
if (dialog.Title == "Session Created") {
|
||||
// FIXME
|
||||
isAcceptedPart = true;
|
||||
global.RPClient.SessionAckCreate(dialog.GetMeta("sessionId").ToString(), true);
|
||||
}
|
||||
};
|
||||
|
||||
global.RPClient.OnOpen += (string eventName, object[] args) => {
|
||||
@ -56,7 +60,14 @@ public partial class GameLobby : Control {
|
||||
dialog.Visible = true;
|
||||
} else {
|
||||
if (res) {
|
||||
// TODO FIXME
|
||||
global.sessionId = sessionId;
|
||||
if (!isAcceptedPart) {
|
||||
global.GlobalData["player_color"] = "red";
|
||||
} else {
|
||||
global.GlobalData["player_color"] = "black";
|
||||
}
|
||||
// GD.PrintErr("sessionId: ", reqUserId, "color: ", global.GlobalData["player_color"].AsString());
|
||||
global.GotoScene("res://Scenes/ChessGame.tscn");
|
||||
} else {
|
||||
dialog.Title = "Failed";
|
||||
|
@ -33,9 +33,13 @@ public partial class Setting : Control
|
||||
}
|
||||
|
||||
private void OnBack() {
|
||||
global.SaveConfig();
|
||||
global.GotoScene("res://Main.tscn", null);
|
||||
}
|
||||
|
||||
private void OnSave() {
|
||||
global.SaveConfig();
|
||||
// OS.Alert("Saved", "Setting");
|
||||
}
|
||||
|
||||
void OnNameChanged(string Value)
|
||||
{
|
||||
|
Reference in New Issue
Block a user