using System; using System.Collections.Generic; using Vector2I = Vector.Vector2I; public abstract class AbstractPiece : IPiece { private Vector2I pos; // 注意这个坐标的非像素坐标而是棋盘坐标 private bool isSelected = false; protected string name; private Dictionary data = new(); public Vector2I Pos { get => pos; set { if (pos != value) { var oldPos = pos; pos = value; OnPos?.Invoke(this, oldPos); } } } public bool IsSelected { get => isSelected; set { if (isSelected != value) { var oldIsSelected = isSelected; isSelected = value; OnSelected?.Invoke(this, oldIsSelected); } } } public string Name { get => name; set { if (name != value) { var oldName = name; name = value; OnName?.Invoke(this, oldName); } } } public Dictionary Data { get => data; set => data = value; } public event EventHandler OnPos; public event EventHandler OnMove; public event EventHandler OnSelected; public event EventHandler OnName; public virtual bool Move(Vector2I pos) { if (!CanMove(pos)) { return false; } Pos = pos; OnMove?.Invoke(this, pos); return true; } public abstract bool CanMove(Vector2I to); public override string ToString() { return $"{Name} at {pos}"; } public AbstractPiece(string name = "", Vector2I pos = null) { this.name = name; this.pos = pos ?? new(); } }