77 lines
2.1 KiB
C#
77 lines
2.1 KiB
C#
// Chesspiece.cs
|
|
using Godot;
|
|
|
|
public partial class ChessPiece : Sprite2D {
|
|
// 文字内容
|
|
[Export]
|
|
public string PieceLabel { get; set; } = null;
|
|
// 文字颜色(可导出以编辑器调整)
|
|
[Export]
|
|
public Color LabelColor { get; set; } = new Color("black");
|
|
private Vector2 textureSize;
|
|
|
|
private Label labelOfChessName;
|
|
|
|
private Vector2 arrPos = new Vector2(); // 注意这个坐标的非像素坐标而是棋盘坐标
|
|
|
|
public void movePos(Vector2 newPos) {
|
|
this.arrPos = newPos;
|
|
this.Position = PosTrans.transArrToPix * newPos;
|
|
}
|
|
|
|
public Vector2 getPos() {
|
|
return arrPos;
|
|
}
|
|
|
|
// 是否被选中
|
|
public bool isSelected = false;
|
|
Transform2D transToSeleted = new Transform2D(
|
|
new Vector2(1.2f, 0),
|
|
new Vector2(0, 1.2f),
|
|
new Vector2(0, 0)
|
|
);
|
|
|
|
public void Selected() {
|
|
if (isSelected) {
|
|
return;
|
|
}
|
|
this.Transform *= transToSeleted;
|
|
this.isSelected = true;
|
|
}
|
|
|
|
public void DeSelected() {
|
|
if (!isSelected) {
|
|
return;
|
|
}
|
|
this.Transform *= transToSeleted.AffineInverse();
|
|
this.isSelected = false;
|
|
}
|
|
|
|
// Called when the node enters the scene tree for the first time.
|
|
public override void _Ready() {
|
|
InitLabel();
|
|
}
|
|
|
|
private void InitLabel() {
|
|
// this.Texture.ResourcePath = "res://Asserts/ChesspieceBase.tres";
|
|
this.Texture = (Texture2D)ResourceLoader.Load("res://Asserts/ChesspieceBase.tres");
|
|
textureSize = this.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);
|
|
}
|
|
|
|
interface IVarify {
|
|
bool CanMove(Vector2 newPosition);
|
|
}
|
|
} |