feat(重构): 重构棋盘和棋子的逻辑
- 重构了 ChessBoard 和 ChessPiece 类的逻辑 - 添加了新版本的 ChineseChess 库 - 删除了旧版本的 VirtualBoard 和 VirtualPiece 类 - 更新了相关的场景和脚本
This commit is contained in:
@ -1,38 +1,60 @@
|
||||
// Chessboard.cs
|
||||
using System;
|
||||
using Godot;
|
||||
using ChineseChess;
|
||||
using Godot.Collections;
|
||||
|
||||
public partial class ChessBoard : Node2D {
|
||||
public event EventHandler<Vector2> OnMouseClicked;
|
||||
public event EventHandler<Vector2> OnPosClicked;
|
||||
|
||||
public Vector2 from = Vector2.Inf, to = Vector2.Inf;
|
||||
public Array<Vector2> canMovePos = new();
|
||||
|
||||
public override void _Ready() {
|
||||
}
|
||||
|
||||
public void LoadBoard(VirtualBoard board) {
|
||||
public void Clear() {
|
||||
from = to = Vector2.Inf;
|
||||
QueueRedraw();
|
||||
}
|
||||
|
||||
public void LoadBoard(CCBoard board) {
|
||||
board.OnRemove += (sender, piece) => {
|
||||
if (piece.data != null) {
|
||||
RemoveChild(piece.data as Node);
|
||||
if (piece?.Data.TryGetValue("Godot", out object node) ?? false) {
|
||||
RemoveChild(node as Node);
|
||||
}
|
||||
};
|
||||
|
||||
board.OnInsert += (sender, piece) => {
|
||||
if (piece.data != null && piece.data is Node) {
|
||||
AddChild(piece.data as Node);
|
||||
ChessPiece chessPiece = null;
|
||||
if (piece.Data.TryGetValue("Godot", out object node)) {
|
||||
chessPiece = node as ChessPiece;
|
||||
} 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);
|
||||
chessPiece = new((CCPiece)piece);
|
||||
}
|
||||
chessPiece.ShowBehindParent = true;
|
||||
AddChild(chessPiece);
|
||||
};
|
||||
|
||||
board.OnMove += (sender, vals) => {
|
||||
from = PosTrans.transArrToPix * new Vector2(vals.From.X, vals.From.Y);
|
||||
to = PosTrans.transArrToPix * new Vector2(vals.To.X, vals.To.Y);
|
||||
QueueRedraw();
|
||||
};
|
||||
}
|
||||
|
||||
public override void _Input(InputEvent @event) {
|
||||
|
||||
public override void _Draw() {
|
||||
Color color = new(0.7f, 0.7f, 0.7f, 0.5f);
|
||||
DrawLine(from, to, color, PosTrans.pixGripSize / 5);
|
||||
DrawCircle(from, PosTrans.pixGripSize / 2.2f, color);
|
||||
foreach (Vector2 pos in canMovePos) {
|
||||
DrawCircle(pos, PosTrans.pixGripSize / 2.2f,
|
||||
color.Inverted());
|
||||
}
|
||||
}
|
||||
|
||||
public override void _Input(InputEvent @event) {
|
||||
if (@event is InputEventMouseButton mouseEvent &&
|
||||
mouseEvent.Pressed &&
|
||||
mouseEvent.ButtonIndex == MouseButton.Left) {
|
||||
@ -41,4 +63,13 @@ public partial class ChessBoard : Node2D {
|
||||
GetLocalMousePosition()).Round());
|
||||
}
|
||||
}
|
||||
|
||||
public static class PosTrans {
|
||||
public static readonly int pixGripSize = 32;
|
||||
public static readonly Transform2D transArrToPix = new(
|
||||
new Vector2(pixGripSize, 0),
|
||||
new Vector2(0, pixGripSize),
|
||||
new Vector2(-4, -4.5f) * pixGripSize
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -1,34 +1,46 @@
|
||||
// Chesspiece.cs
|
||||
using Godot;
|
||||
using ChineseChess;
|
||||
using System.Linq;
|
||||
using System;
|
||||
|
||||
public partial class ChessPiece : Sprite2D {
|
||||
// 文字内容
|
||||
[Export]
|
||||
public string PieceLabel { get; set; } = null;
|
||||
// 文字颜色(可导出以编辑器调整)
|
||||
// Text Color (Can Export for Editor Adjust)
|
||||
[Export]
|
||||
public Color LabelColor { get; set; } = new Color("white");
|
||||
private Vector2 textureSize;
|
||||
|
||||
private Label labelOfChessName;
|
||||
private readonly VirtualPiece piece;
|
||||
private readonly IPiece piece;
|
||||
|
||||
public VirtualPiece GetVirtualPiece() {
|
||||
public IPiece GetVirtualPiece() {
|
||||
return piece;
|
||||
}
|
||||
|
||||
private void OnMove(Vector2 newPos) {
|
||||
Position = PosTrans.transArrToPix * new Vector2(newPos.X, newPos.Y);
|
||||
private void OnPos(object _self, Vector.Vector2I oldPos) {
|
||||
CCPiece self = (CCPiece)_self;
|
||||
Position = ChessBoard.PosTrans.transArrToPix * new Vector2(self.Pos.X, self.Pos.Y);
|
||||
}
|
||||
|
||||
public void OnSelected(bool isSelected) {
|
||||
if (isSelected) {
|
||||
GD.Print($"{piece.Pos()} is selected");
|
||||
public void OnSelected(object _self, bool oldIsSelected) {
|
||||
CCPiece self = (CCPiece)_self;
|
||||
ChessBoard chessBoard = GetParent() as ChessBoard;
|
||||
if (self.IsSelected) {
|
||||
GD.Print($"{piece.Pos} is selected");
|
||||
Transform *= transToSeleted;
|
||||
foreach (var item in self.CanMoveAllPos()) {
|
||||
chessBoard.canMovePos.Add(ChessBoard.PosTrans.transArrToPix * new Vector2(item.X, item.Y));
|
||||
}
|
||||
} else {
|
||||
GD.Print($"{piece.Pos()} is deselected");
|
||||
GD.Print($"{piece.Pos} is deselected");
|
||||
Transform *= transToSeleted.AffineInverse();
|
||||
foreach (var item in self.CanMoveAllPos()) {
|
||||
chessBoard.canMovePos.Remove(ChessBoard.PosTrans.transArrToPix * new Vector2(item.X, item.Y));
|
||||
}
|
||||
}
|
||||
chessBoard.QueueRedraw();
|
||||
}
|
||||
|
||||
Transform2D transToSeleted = new(
|
||||
@ -37,24 +49,26 @@ public partial class ChessPiece : Sprite2D {
|
||||
new Vector2(0, 0)
|
||||
);
|
||||
|
||||
public ChessPiece() : this("", new()){
|
||||
public ChessPiece() : this(new CCPiece()){
|
||||
}
|
||||
|
||||
public ChessPiece(string name, VirtualPiece piece) {
|
||||
PieceLabel = name;
|
||||
public ChessPiece(CCPiece piece) {
|
||||
PieceLabel = piece.CNName;
|
||||
this.piece = piece;
|
||||
piece.OnMove += OnMove;
|
||||
LabelColor = piece.TurnsSide == ChessCore.TurnsSideType.Red ? new Color("red") : new Color("black");
|
||||
piece.OnPos += OnPos;
|
||||
piece.OnSelected += OnSelected;
|
||||
piece.data = this;
|
||||
// Must Be Call for init Pos
|
||||
OnPos(piece, piece.Pos);
|
||||
piece.Data.TryAdd("Godot", this);
|
||||
}
|
||||
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
public override void _Ready() {
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
public override void _Ready() {
|
||||
InitLabel();
|
||||
}
|
||||
|
||||
private void InitLabel() {
|
||||
// this.Texture.ResourcePath = "res://Asserts/ChesspieceBase.tres";
|
||||
Texture ??= (Texture2D)ResourceLoader.Load("res://Asserts/ChesspieceBase.tres");
|
||||
textureSize = Texture.GetSize();
|
||||
Vector2 labalPosition = new(
|
||||
|
Reference in New Issue
Block a user