feat: 重构棋盘逻辑和游戏流程,将引擎版本提升至4.3
- 重构了 VirtualBoard 类,优化了棋盘操作和事件处理 - 重写了 ChessPiece 类,增加了事件处理和虚拟棋子逻辑 - 更新了 ChessGame 控制器,增加了结束回合功能和对话框 - 调整了场景布局和资源引用 - 清理了冗余代码和注释
This commit is contained in:
@ -11,42 +11,38 @@ public partial class ChessPiece : Sprite2D {
|
||||
private Vector2 textureSize;
|
||||
|
||||
private Label labelOfChessName;
|
||||
private VirtualPiece piece;
|
||||
|
||||
private Vector2 arrPos = new Vector2(); // 注意这个坐标的非像素坐标而是棋盘坐标
|
||||
|
||||
public void movePos(Vector2 newPos) {
|
||||
this.arrPos = newPos;
|
||||
this.Position = PosTrans.transArrToPix * newPos;
|
||||
public VirtualPiece GetVirtualPiece() {
|
||||
return piece;
|
||||
}
|
||||
|
||||
public Vector2 getPos() {
|
||||
return arrPos;
|
||||
private void OnMove(Vector2 newPos) {
|
||||
Position = PosTrans.transArrToPix * new Vector2(newPos.X, newPos.Y);
|
||||
}
|
||||
|
||||
public void OnSelected(bool isSelected) {
|
||||
if (isSelected) {
|
||||
GD.Print($"{piece.Pos()} is selected");
|
||||
Transform *= transToSeleted;
|
||||
} else {
|
||||
GD.Print($"{piece.Pos()} is deselected");
|
||||
Transform *= transToSeleted.AffineInverse();
|
||||
}
|
||||
}
|
||||
|
||||
// 是否被选中
|
||||
public bool isSelected = false;
|
||||
Transform2D transToSeleted = new Transform2D(
|
||||
new Vector2(1.2f, 0),
|
||||
new Vector2(0, 1.2f),
|
||||
new Vector2(0, 0)
|
||||
);
|
||||
|
||||
public void Selected() {
|
||||
if (isSelected) {
|
||||
return;
|
||||
}
|
||||
GD.Print($"{arrPos} is selected");
|
||||
this.Transform *= transToSeleted;
|
||||
this.isSelected = true;
|
||||
}
|
||||
|
||||
public void DeSelected() {
|
||||
if (!isSelected) {
|
||||
return;
|
||||
}
|
||||
GD.Print($"{arrPos} is deselected");
|
||||
this.Transform *= transToSeleted.AffineInverse();
|
||||
this.isSelected = false;
|
||||
public ChessPiece(string name = "", Vector2 pos = new Vector2()) {
|
||||
PieceLabel = name;
|
||||
piece = new VirtualPiece(name, pos);
|
||||
piece.OnMove += OnMove;
|
||||
piece.OnSelected += OnSelected;
|
||||
piece.data = this;
|
||||
}
|
||||
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
@ -56,8 +52,8 @@ public partial class ChessPiece : Sprite2D {
|
||||
|
||||
private void InitLabel() {
|
||||
// this.Texture.ResourcePath = "res://Asserts/ChesspieceBase.tres";
|
||||
this.Texture ??= (Texture2D)ResourceLoader.Load("res://Asserts/ChesspieceBase.tres");
|
||||
textureSize = this.Texture.GetSize();
|
||||
Texture ??= (Texture2D)ResourceLoader.Load("res://Asserts/ChesspieceBase.tres");
|
||||
textureSize = Texture.GetSize();
|
||||
Vector2 labalPosition = new(
|
||||
-textureSize.X / 2,
|
||||
-textureSize.Y / 2);
|
||||
@ -72,8 +68,4 @@ public partial class ChessPiece : Sprite2D {
|
||||
// labelOfChessName.SetAnchorsPreset(Control.LayoutPreset.FullRect);
|
||||
AddChild(labelOfChessName);
|
||||
}
|
||||
|
||||
interface IVarify {
|
||||
bool CanMove(Vector2 newPosition);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user