// Chesspiece.cs using Godot; using ChineseChess; using System.Linq; using System; public partial class ChessPiece : Sprite2D { [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; private readonly IPiece piece; public IPiece GetVirtualPiece() { return piece; } private void OnPos(object _self, Vector.Vector2I oldPos) { CCPiece self = (CCPiece)_self; 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; if (self.IsSelected) { GD.Print($"{piece.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"); 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; this.piece = piece; LabelColor = piece.TurnsSide == ChessCore.TurnsSideType.Red ? new Color("red") : new Color("black"); piece.OnPos += OnPos; piece.OnSelected += OnSelected; // Must Be Call for init Pos 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); } }