- 移除了.csproj文件 - 更新了.gitignore,添加了.editorconfig - 重构了IBoard和IPiece接口,引入了新的事件处理机制 - 优化了CCBoard、CCPiece等类的实现,使用新的事件驱动模型 - 删除了冗余代码,提高了代码的可读性和可维护性
80 lines
2.7 KiB
C#
80 lines
2.7 KiB
C#
// Chesspiece.cs
|
|
using Godot;
|
|
using ChineseChess;
|
|
using System.Linq;
|
|
using System;
|
|
|
|
using static IPiece;
|
|
public partial class ChessPiece : Sprite2D, IPieceOn {
|
|
[Export]
|
|
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;
|
|
|
|
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);
|
|
}
|
|
|
|
void IPieceOn.OnSelected(object _self, bool oldIsSelected) {
|
|
if (GetParent() is not ChessBoard chessBoard || _self is not CCPiece self) return;
|
|
if (self.IsSelected) {
|
|
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($"{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));
|
|
}
|
|
}
|
|
chessBoard.QueueRedraw();
|
|
}
|
|
|
|
Transform2D transToSeleted = new(
|
|
new Vector2(1.2f, 0),
|
|
new Vector2(0, 1.2f),
|
|
new Vector2(0, 0)
|
|
);
|
|
|
|
public ChessPiece() : this(new CCPiece()) {
|
|
}
|
|
|
|
public ChessPiece(CCPiece piece) {
|
|
PieceLabel = piece.CNName;
|
|
LabelColor = piece.TurnsSide == ChessCore.TurnsSideType.Red ? new Color("red") : new Color("black");
|
|
piece.On = this;
|
|
// Must Be Call for init Pos
|
|
piece.On.OnPos(piece, piece.Pos);
|
|
piece.Data.TryAdd("Godot", this);
|
|
}
|
|
|
|
// Called when the node enters the scene tree for the first time.
|
|
public override void _Ready() {
|
|
InitLabel();
|
|
}
|
|
|
|
private void InitLabel() {
|
|
Texture ??= (Texture2D)ResourceLoader.Load("res://Asserts/ChesspieceBase.tres");
|
|
textureSize = Texture.GetSize();
|
|
Vector2 labalPosition = new(
|
|
-textureSize.X / 2,
|
|
-textureSize.Y / 2);
|
|
labelOfChessName = new Label {
|
|
Text = PieceLabel,
|
|
HorizontalAlignment = HorizontalAlignment.Center,
|
|
VerticalAlignment = VerticalAlignment.Center,
|
|
Modulate = LabelColor,
|
|
Position = labalPosition,
|
|
Size = textureSize,
|
|
};
|
|
// labelOfChessName.SetAnchorsPreset(Control.LayoutPreset.FullRect);
|
|
AddChild(labelOfChessName);
|
|
}
|
|
} |