refactor(重构): 重构了事件驱动的代码体系,使用全新命名和版本,以及测试套件的初试

- 移除了.csproj文件
- 更新了.gitignore,添加了.editorconfig
- 重构了IBoard和IPiece接口,引入了新的事件处理机制
- 优化了CCBoard、CCPiece等类的实现,使用新的事件驱动模型
- 删除了冗余代码,提高了代码的可读性和可维护性
This commit is contained in:
ZZY
2024-11-24 15:42:30 +08:00
parent 327c2df94d
commit e16f76e11f
18 changed files with 416 additions and 274 deletions

View File

@ -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() {

View File

@ -4,37 +4,31 @@ using ChineseChess;
using System.Linq;
using System;
public partial class ChessPiece : Sprite2D {
using static IPiece;
public partial class ChessPiece : Sprite2D, IPieceOn {
[Export]
public string PieceLabel { get; set; } = null;
public string? PieceLabel { get; set; } = null;
// Text Color (Can Export for Editor Adjust)
[Export]
public Color LabelColor { get; set; } = new Color("white");
private Vector2 textureSize;
private Label? labelOfChessName;
private Label labelOfChessName;
private readonly IPiece piece;
public IPiece GetVirtualPiece() {
return piece;
}
private void OnPos(object _self, Vector.Vector2I oldPos) {
CCPiece self = (CCPiece)_self;
void IPieceOn.OnPos(object _self, Vector.Vector2I oldPos) {
if (_self is not CCPiece self) return;
Position = ChessBoard.PosTrans.transArrToPix * new Vector2(self.Pos.X, self.Pos.Y);
}
public void OnSelected(object _self, bool oldIsSelected) {
CCPiece self = (CCPiece)_self;
ChessBoard chessBoard = GetParent() as ChessBoard;
void IPieceOn.OnSelected(object _self, bool oldIsSelected) {
if (GetParent() is not ChessBoard chessBoard || _self is not CCPiece self) return;
if (self.IsSelected) {
GD.Print($"{piece.Pos} is selected");
GD.Print($"{self.Pos} is selected");
Transform *= transToSeleted;
foreach (var item in self.CanMoveAllPos()) {
chessBoard.canMovePos.Add(ChessBoard.PosTrans.transArrToPix * new Vector2(item.X, item.Y));
}
} else {
GD.Print($"{piece.Pos} is deselected");
GD.Print($"{self.Pos} is deselected");
Transform *= transToSeleted.AffineInverse();
foreach (var item in self.CanMoveAllPos()) {
chessBoard.canMovePos.Remove(ChessBoard.PosTrans.transArrToPix * new Vector2(item.X, item.Y));
@ -49,17 +43,15 @@ public partial class ChessPiece : Sprite2D {
new Vector2(0, 0)
);
public ChessPiece() : this(new CCPiece()){
public ChessPiece() : this(new CCPiece()) {
}
public ChessPiece(CCPiece piece) {
PieceLabel = piece.CNName;
this.piece = piece;
LabelColor = piece.TurnsSide == ChessCore.TurnsSideType.Red ? new Color("red") : new Color("black");
piece.OnPos += OnPos;
piece.OnSelected += OnSelected;
piece.On = this;
// Must Be Call for init Pos
OnPos(piece, piece.Pos);
piece.On.OnPos(piece, piece.Pos);
piece.Data.TryAdd("Godot", this);
}