refactor(重构): 重构了事件驱动的代码体系,使用全新命名和版本,以及测试套件的初试
- 移除了.csproj文件 - 更新了.gitignore,添加了.editorconfig - 重构了IBoard和IPiece接口,引入了新的事件处理机制 - 优化了CCBoard、CCPiece等类的实现,使用新的事件驱动模型 - 删除了冗余代码,提高了代码的可读性和可维护性
This commit is contained in:
@ -4,12 +4,14 @@ using Godot;
|
||||
using ChineseChess;
|
||||
using Godot.Collections;
|
||||
|
||||
public partial class ChessBoard : Node2D {
|
||||
public event EventHandler<Vector2> OnMouseClicked;
|
||||
public event EventHandler<Vector2> OnPosClicked;
|
||||
using static IBoard;
|
||||
public partial class ChessBoard : Node2D, ICCBoardOn {
|
||||
public event EventHandler<Vector2>? OnMouseClicked;
|
||||
public event EventHandler<Vector2>? OnPosClicked;
|
||||
public event EventHandler<int>? OnStepsChanged;
|
||||
|
||||
public Vector2 from = Vector2.Inf, to = Vector2.Inf;
|
||||
public Array<Vector2> canMovePos = new();
|
||||
public Array<Vector2> canMovePos = [];
|
||||
|
||||
public override void _Ready() {
|
||||
}
|
||||
@ -19,29 +21,39 @@ public partial class ChessBoard : Node2D {
|
||||
QueueRedraw();
|
||||
}
|
||||
|
||||
public void LoadBoard(CCBoard board) {
|
||||
board.OnRemove += (sender, piece) => {
|
||||
if (piece?.Data.TryGetValue("Godot", out object node) ?? false) {
|
||||
RemoveChild(node as Node);
|
||||
}
|
||||
};
|
||||
void IBoardOn.OnInsert(object self, IPiece piece) {
|
||||
ChessPiece? node = piece.On as ChessPiece;
|
||||
// throw new InvalidOperationException();
|
||||
node ??= new ChessPiece((CCPiece)piece);
|
||||
node.ShowBehindParent = true;
|
||||
AddChild(node);
|
||||
|
||||
board.OnInsert += (sender, piece) => {
|
||||
ChessPiece chessPiece = null;
|
||||
if (piece.Data.TryGetValue("Godot", out object node)) {
|
||||
chessPiece = node as ChessPiece;
|
||||
} else {
|
||||
chessPiece = new((CCPiece)piece);
|
||||
}
|
||||
chessPiece.ShowBehindParent = true;
|
||||
AddChild(chessPiece);
|
||||
};
|
||||
// ChessPiece chessPiece = null;
|
||||
// if (piece.Data.TryGetValue("Godot", out object node)) {
|
||||
// chessPiece = node as ChessPiece;
|
||||
// } else {
|
||||
// chessPiece = new((CCPiece)piece);
|
||||
// }
|
||||
// chessPiece.ShowBehindParent = true;
|
||||
// AddChild(chessPiece);
|
||||
}
|
||||
|
||||
board.OnMove += (sender, vals) => {
|
||||
from = PosTrans.transArrToPix * new Vector2(vals.From.X, vals.From.Y);
|
||||
to = PosTrans.transArrToPix * new Vector2(vals.To.X, vals.To.Y);
|
||||
QueueRedraw();
|
||||
};
|
||||
void IBoardOn.OnRemove(object self, IPiece piece) {
|
||||
if (piece.On is not ChessPiece node) {
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
RemoveChild(node);
|
||||
}
|
||||
|
||||
void IBoardOn.OnMove(object self, IBoardOn.MoveEventArgs vals) {
|
||||
from = PosTrans.transArrToPix * new Vector2(vals.From.X, vals.From.Y);
|
||||
to = PosTrans.transArrToPix * new Vector2(vals.To.X, vals.To.Y);
|
||||
QueueRedraw();
|
||||
}
|
||||
|
||||
void ICCBoardOn.OnSteps(object _self, int oldSteps) {
|
||||
if (_self is not CCBoard self) return;
|
||||
OnStepsChanged?.Invoke(self, self.Steps);
|
||||
}
|
||||
|
||||
public override void _Draw() {
|
||||
|
Reference in New Issue
Block a user