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,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);
}