feat(chess): 初始化棋盘和玩家颜色,优化代码结构,重构多人游戏代码
- 在 ChessGame 中初始化棋盘,根据玩家颜色设置棋子 - 更新 UI 显示玩家颜色 - 调整棋盘位置和 UI 布局 - 重构部分代码以提高可读性和维护性
This commit is contained in:
@ -9,6 +9,7 @@ public partial class ChessBoard : Node2D {
|
||||
public delegate bool ChessMoveFunc(Vector2 toPos, Vector2 fromPos);
|
||||
// public Callable chessMoveFunc { get; set; }
|
||||
public event EventHandler<Vector2> OnMouseClicked;
|
||||
public event EventHandler<Vector2> OnPosClicked;
|
||||
|
||||
public override void _Ready() {
|
||||
board = new VirtualBoard(9, 10);
|
||||
@ -29,8 +30,6 @@ public partial class ChessBoard : Node2D {
|
||||
// board.OnMove += (sender, args) => {
|
||||
// // chessMoveFunc.Call(args.To, args.From);
|
||||
// };
|
||||
|
||||
InitChessBoard();
|
||||
}
|
||||
|
||||
public override void _Input(InputEvent @event) {
|
||||
@ -40,6 +39,8 @@ public partial class ChessBoard : Node2D {
|
||||
// HandleMouseClick(GetGlobalTransformWithCanvas().AffineInverse() * mouseButton.Position);
|
||||
|
||||
OnMouseClicked?.Invoke(this, GetLocalMousePosition());
|
||||
OnPosClicked?.Invoke(this, (PosTrans.transArrToPix.AffineInverse() *
|
||||
GetLocalMousePosition()).Round());
|
||||
}
|
||||
}
|
||||
|
||||
@ -49,32 +50,4 @@ public partial class ChessBoard : Node2D {
|
||||
// piece.Move(vector);
|
||||
board.SetPiecePos(piece, arrayPos);
|
||||
}
|
||||
|
||||
public void InitChessBoard() {
|
||||
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)
|
||||
});
|
||||
|
||||
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)
|
||||
});
|
||||
}
|
||||
|
||||
private void InitializePieces(string color, int baseY, (string label, int x, int y)[] positions) {
|
||||
foreach (var (label, x, y) in positions) {
|
||||
ChessPiece piece = new ChessPiece {
|
||||
PieceLabel = label,
|
||||
LabelColor = new Color(color)
|
||||
};
|
||||
InsertNode(piece, new Vector2(x, baseY + y));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ public partial class ChessPiece : Sprite2D {
|
||||
}
|
||||
}
|
||||
|
||||
Transform2D transToSeleted = new Transform2D(
|
||||
Transform2D transToSeleted = new(
|
||||
new Vector2(1.2f, 0),
|
||||
new Vector2(0, 1.2f),
|
||||
new Vector2(0, 0)
|
||||
|
Reference in New Issue
Block a user