ZZY 6daf09b300 feat(chess): 初始化棋盘和玩家颜色,优化代码结构,重构多人游戏代码
- 在 ChessGame 中初始化棋盘,根据玩家颜色设置棋子
- 更新 UI 显示玩家颜色
- 调整棋盘位置和 UI 布局
- 重构部分代码以提高可读性和维护性
2024-11-07 15:28:03 +08:00

54 lines
1.4 KiB
C#

// Chessboard.cs
using System;
using Godot;
public partial class ChessBoard : Node2D {
public VirtualBoard board = null;
public Player playerSelf = null;
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);
playerSelf = new Player(board);
board.OnRemove += (sender, piece) => {
if (piece.data != null) {
RemoveChild(piece.data as Node);
}
};
board.OnInsert += (sender, piece) => {
if (piece.data != null) {
AddChild(piece.data as Node);
}
};
// board.OnMove += (sender, args) => {
// // chessMoveFunc.Call(args.To, args.From);
// };
}
public override void _Input(InputEvent @event) {
if (@event is InputEventMouseButton mouseEvent &&
mouseEvent.Pressed &&
mouseEvent.ButtonIndex == MouseButton.Left) {
// HandleMouseClick(GetGlobalTransformWithCanvas().AffineInverse() * mouseButton.Position);
OnMouseClicked?.Invoke(this, GetLocalMousePosition());
OnPosClicked?.Invoke(this, (PosTrans.transArrToPix.AffineInverse() *
GetLocalMousePosition()).Round());
}
}
public void InsertNode(ChessPiece node, Vector2 arrayPos) {
AddChild(node);
VirtualPiece piece = node.GetVirtualPiece();
// piece.Move(vector);
board.SetPiecePos(piece, arrayPos);
}
}