chinese_chess/Scripts/Src/AbstractPiece.cs
ZZY e16f76e11f refactor(重构): 重构了事件驱动的代码体系,使用全新命名和版本,以及测试套件的初试
- 移除了.csproj文件
- 更新了.gitignore,添加了.editorconfig
- 重构了IBoard和IPiece接口,引入了新的事件处理机制
- 优化了CCBoard、CCPiece等类的实现,使用新的事件驱动模型
- 删除了冗余代码,提高了代码的可读性和可维护性
2024-11-24 15:42:30 +08:00

68 lines
1.7 KiB
C#

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