feat(chinese-chess): 实现中国象棋核心逻辑和基本功能
- 抽象出 ChessCore 类,包含游戏初始化、行棋逻辑、悔棋等功能 - 重构 Player 类,优化行棋和记录逻辑 - 更新 ChessBoard 和 ChessPiece 类,适应新逻辑 - 移除冗余代码,提高代码可读性和可维护性
This commit is contained in:
@ -3,18 +3,13 @@ using System;
|
||||
using Godot;
|
||||
|
||||
public partial class ChessBoard : Node2D {
|
||||
public VirtualBoard board = null;
|
||||
public Player playerSelf = null;
|
||||
|
||||
public delegate bool ChessMoveFunc(Vector2 toPos, Vector2 fromPos);
|
||||
// public Callable chessMoveFunc { get; set; }
|
||||
public event EventHandler<Vector2> OnMouseClicked;
|
||||
public event EventHandler<Vector2> OnPosClicked;
|
||||
|
||||
public override void _Ready() {
|
||||
board = new VirtualBoard(9, 10);
|
||||
playerSelf = new Player(board);
|
||||
}
|
||||
|
||||
public void LoadBoard(VirtualBoard board) {
|
||||
board.OnRemove += (sender, piece) => {
|
||||
if (piece.data != null) {
|
||||
RemoveChild(piece.data as Node);
|
||||
@ -22,32 +17,28 @@ public partial class ChessBoard : Node2D {
|
||||
};
|
||||
|
||||
board.OnInsert += (sender, piece) => {
|
||||
if (piece.data != null) {
|
||||
if (piece.data != null && piece.data is Node) {
|
||||
AddChild(piece.data as Node);
|
||||
} else {
|
||||
ChessPiece chessPiece = new(piece.name, piece);
|
||||
|
||||
if (piece.Pos().Y >= 5) {
|
||||
chessPiece.LabelColor = new Color("red");
|
||||
} else {
|
||||
chessPiece.LabelColor = new Color("black");
|
||||
}
|
||||
AddChild(chessPiece);
|
||||
}
|
||||
};
|
||||
|
||||
// board.OnMove += (sender, args) => {
|
||||
// // chessMoveFunc.Call(args.To, args.From);
|
||||
// };
|
||||
}
|
||||
|
||||
public override void _Input(InputEvent @event) {
|
||||
if (@event is InputEventMouseButton mouseEvent &&
|
||||
mouseEvent.Pressed &&
|
||||
mouseEvent.ButtonIndex == MouseButton.Left) {
|
||||
// HandleMouseClick(GetGlobalTransformWithCanvas().AffineInverse() * mouseButton.Position);
|
||||
|
||||
mouseEvent.Pressed &&
|
||||
mouseEvent.ButtonIndex == MouseButton.Left) {
|
||||
OnMouseClicked?.Invoke(this, GetLocalMousePosition());
|
||||
OnPosClicked?.Invoke(this, (PosTrans.transArrToPix.AffineInverse() *
|
||||
GetLocalMousePosition()).Round());
|
||||
}
|
||||
}
|
||||
|
||||
public void InsertNode(ChessPiece node, Vector2 arrayPos) {
|
||||
AddChild(node);
|
||||
VirtualPiece piece = node.GetVirtualPiece();
|
||||
// piece.Move(vector);
|
||||
board.SetPiecePos(piece, arrayPos);
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ public partial class ChessPiece : Sprite2D {
|
||||
public string PieceLabel { get; set; } = null;
|
||||
// 文字颜色(可导出以编辑器调整)
|
||||
[Export]
|
||||
public Color LabelColor { get; set; } = new Color("black");
|
||||
public Color LabelColor { get; set; } = new Color("white");
|
||||
private Vector2 textureSize;
|
||||
|
||||
private Label labelOfChessName;
|
||||
@ -37,12 +37,12 @@ public partial class ChessPiece : Sprite2D {
|
||||
new Vector2(0, 0)
|
||||
);
|
||||
|
||||
public ChessPiece() : this("", Vector2.Zero){
|
||||
public ChessPiece() : this("", new()){
|
||||
}
|
||||
|
||||
public ChessPiece(string name, Vector2 pos) {
|
||||
public ChessPiece(string name, VirtualPiece piece) {
|
||||
PieceLabel = name;
|
||||
piece = new VirtualPiece(name, pos);
|
||||
this.piece = piece;
|
||||
piece.OnMove += OnMove;
|
||||
piece.OnSelected += OnSelected;
|
||||
piece.data = this;
|
||||
|
Reference in New Issue
Block a user