- 更新了多个文件的代码结构和类型定义,提高了代码的健壮性和可维护性 - 优化了事件处理、棋子管理和移动逻辑,为后续功能扩展打下坚实基础 - 修复了一些潜在的空指针异常问题,提高了程序的稳定性 - 修复部分bug
74 lines
1.8 KiB
C#
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();
|
|
}
|
|
}
|