Chessistics/Scripts/Pieces/PieceView.cs

121 lines
3.6 KiB
C#
Raw Normal View History

using Godot;
using Chessistics.Engine.Model;
using Chessistics.Scripts.Board;
namespace Chessistics.Scripts.Pieces;
public partial class PieceView : Node2D
{
private Sprite2D _sprite = null!;
private ColorRect _cargoIndicator = null!;
private Label _label = null!;
public int PieceId { get; private set; }
public PieceKind Kind { get; private set; }
public Coords StartCell { get; private set; }
public Coords EndCell { get; private set; }
private static readonly Color RookColor = new("#4A7AB5");
private static readonly Color BishopColor = new("#B54A8E");
private static readonly Color KnightColor = new("#B5824A");
private static readonly Color WoodCargoColor = new("#8B6914");
private static readonly Color StoneCargoColor = new("#808080");
public void Setup(int pieceId, PieceKind kind, Coords startCell, Coords endCell, BoardView boardView)
{
PieceId = pieceId;
Kind = kind;
StartCell = startCell;
EndCell = endCell;
Position = boardView.CoordsToPixel(startCell);
var color = kind switch
{
PieceKind.Rook => RookColor,
PieceKind.Bishop => BishopColor,
PieceKind.Knight => KnightColor,
_ => Colors.White
};
// Piece body (circle)
_sprite = new Sprite2D();
var texture = new GradientTexture2D
{
Width = 48,
Height = 48,
Fill = GradientTexture2D.FillEnum.Radial,
Gradient = new Gradient()
};
texture.Gradient.SetColor(0, color);
texture.Gradient.SetColor(1, color.Darkened(0.3f));
_sprite.Texture = texture;
AddChild(_sprite);
// Label
_label = new Label
{
Text = kind switch
{
PieceKind.Rook => "T",
PieceKind.Bishop => "F",
PieceKind.Knight => "C",
_ => "?"
},
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
Position = new Vector2(-8, -10)
};
_label.AddThemeFontSizeOverride("font_size", 16);
_label.AddThemeColorOverride("font_color", Colors.White);
AddChild(_label);
// Cargo indicator (hidden by default)
_cargoIndicator = new ColorRect
{
Size = new Vector2(14, 14),
Position = new Vector2(-7, -30),
Visible = false
};
AddChild(_cargoIndicator);
}
public void SetCargo(CargoType? cargo)
{
if (cargo == null)
{
_cargoIndicator.Visible = false;
return;
}
_cargoIndicator.Visible = true;
_cargoIndicator.Color = cargo.Value switch
{
CargoType.Wood => WoodCargoColor,
CargoType.Stone => StoneCargoColor,
_ => Colors.White
};
}
public void AnimateMoveTo(Vector2 target, float duration = 0.3f)
{
var tween = CreateTween();
if (Kind == PieceKind.Knight)
{
// Arc animation for knight
var mid = (Position + target) / 2 + new Vector2(0, -30);
tween.TweenMethod(Callable.From<float>(t =>
{
var a = Position.Lerp(mid, t);
var b = mid.Lerp(target, t);
Position = a.Lerp(b, t);
}), 0f, 1f, duration);
tween.Finished += () => Position = target;
}
else
{
tween.TweenProperty(this, "position", target, duration);
}
}
}