chinese_chess/Scripts/Src/AbstractPiece.cs
ZZY 9c619784af feat(重构): 重构棋盘和棋子的逻辑
- 重构了 ChessBoard 和 ChessPiece 类的逻辑
- 添加了新版本的 ChineseChess 库
- 删除了旧版本的 VirtualBoard 和 VirtualPiece 类
- 更新了相关的场景和脚本
2024-11-22 20:01:40 +08:00

74 lines
1.8 KiB
C#

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<string, object> 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<string, object> Data {
get => data;
set => data = value;
}
public event EventHandler<Vector2I> OnPos;
public event EventHandler<Vector2I> OnMove;
public event EventHandler<bool> OnSelected;
public event EventHandler<string> 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();
}
}