- 抽象出 ChessCore 类,包含游戏初始化、行棋逻辑、悔棋等功能 - 重构 Player 类,优化行棋和记录逻辑 - 更新 ChessBoard 和 ChessPiece 类,适应新逻辑 - 移除冗余代码,提高代码可读性和可维护性
45 lines
1.1 KiB
C#
45 lines
1.1 KiB
C#
// Chessboard.cs
|
|
using System;
|
|
using Godot;
|
|
|
|
public partial class ChessBoard : Node2D {
|
|
public event EventHandler<Vector2> OnMouseClicked;
|
|
public event EventHandler<Vector2> OnPosClicked;
|
|
|
|
public override void _Ready() {
|
|
}
|
|
|
|
public void LoadBoard(VirtualBoard board) {
|
|
board.OnRemove += (sender, piece) => {
|
|
if (piece.data != null) {
|
|
RemoveChild(piece.data as Node);
|
|
}
|
|
};
|
|
|
|
board.OnInsert += (sender, piece) => {
|
|
if (piece.data != null && piece.data is Node) {
|
|
AddChild(piece.data as Node);
|
|
} else {
|
|
ChessPiece chessPiece = new(piece.name, piece);
|
|
|
|
if (piece.Pos().Y >= 5) {
|
|
chessPiece.LabelColor = new Color("red");
|
|
} else {
|
|
chessPiece.LabelColor = new Color("black");
|
|
}
|
|
AddChild(chessPiece);
|
|
}
|
|
};
|
|
}
|
|
|
|
public override void _Input(InputEvent @event) {
|
|
if (@event is InputEventMouseButton mouseEvent &&
|
|
mouseEvent.Pressed &&
|
|
mouseEvent.ButtonIndex == MouseButton.Left) {
|
|
OnMouseClicked?.Invoke(this, GetLocalMousePosition());
|
|
OnPosClicked?.Invoke(this, (PosTrans.transArrToPix.AffineInverse() *
|
|
GetLocalMousePosition()).Round());
|
|
}
|
|
}
|
|
}
|