139 lines
4.1 KiB
C#
139 lines
4.1 KiB
C#
// Chessboard.cs
|
||
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 override void _Ready() {
|
||
board = new VirtualBoard(9, 10);
|
||
playerSelf = new Player(board);
|
||
|
||
board.OnRemove += (sender, piece) => {
|
||
if (piece.data != null) {
|
||
RemoveChild(piece.data as Node);
|
||
}
|
||
};
|
||
|
||
board.OnInsert += (sender, piece) => {
|
||
if (piece.data != null) {
|
||
AddChild(piece.data as Node);
|
||
}
|
||
};
|
||
|
||
// board.OnMove += (sender, args) => {
|
||
// // chessMoveFunc.Call(args.To, args.From);
|
||
// };
|
||
|
||
InitChessBoard();
|
||
}
|
||
|
||
public override void _Input(InputEvent @event) {
|
||
if (@event is InputEventMouseButton mouseEvent &&
|
||
mouseEvent.Pressed &&
|
||
mouseEvent.ButtonIndex == MouseButton.Left) {
|
||
// HandleMouseClick(GetGlobalTransformWithCanvas().AffineInverse() * mouseButton.Position);
|
||
HandleMouseClick(GetLocalMousePosition());
|
||
}
|
||
}
|
||
|
||
private void HandleMouseClick(Vector2 clickPosition) {
|
||
Vector2 clickBoardPos = (PosTrans.transArrToPix.AffineInverse() *
|
||
clickPosition).Round();
|
||
|
||
playerSelf.HandleBoardPosClick(clickBoardPos);
|
||
}
|
||
|
||
public void InsertNode(ChessPiece node, Vector2 arrayPos) {
|
||
AddChild(node);
|
||
VirtualPiece piece = node.GetVirtualPiece();
|
||
// piece.Move(vector);
|
||
board.SetPiecePos(piece, arrayPos);
|
||
}
|
||
|
||
public void InitChessBoard() {
|
||
InitializePieces("black", 0, new[] {
|
||
("车", 0, 0), ("马", 1, 0), ("象", 2, 0),
|
||
("士", 3, 0), ("将", 4, 0), ("士", 5, 0),
|
||
("象", 6, 0), ("马", 7, 0), ("车", 8, 0),
|
||
("炮", 1, 2), ("炮", 7, 2),
|
||
("卒", 0, 3), ("卒", 2, 3), ("卒", 4, 3), ("卒", 6, 3), ("卒", 8, 3)
|
||
});
|
||
|
||
InitializePieces("red", 9, new[] {
|
||
("车", 0, -0), ("马", 1, -0), ("象", 2, -0),
|
||
("士", 3, -0), ("将", 4, -0), ("士", 5, -0),
|
||
("象", 6, -0), ("马", 7, -0), ("车", 8, -0),
|
||
("炮", 1, -2), ("炮", 7, -2),
|
||
("卒", 0, -3), ("卒", 2, -3), ("卒", 4, -3), ("卒", 6, -3), ("卒", 8, -3)
|
||
});
|
||
}
|
||
|
||
private void InitializePieces(string color, int baseY, (string label, int x, int y)[] positions) {
|
||
foreach (var (label, x, y) in positions) {
|
||
InsertNode(new ChessPiece {
|
||
PieceLabel = label,
|
||
LabelColor = new Color(color)
|
||
}, new Vector2(x, baseY + y));
|
||
}
|
||
}
|
||
|
||
|
||
// using Godot;
|
||
|
||
// public enum ChessType
|
||
// {
|
||
// Carriage,
|
||
// Horse,
|
||
// Elephant,
|
||
// Advisor,
|
||
// General,
|
||
// Cannon,
|
||
// Pawn
|
||
// }
|
||
|
||
// public class VirtualBoard
|
||
// {
|
||
// // ...其他原有成员变量...
|
||
|
||
// public VirtualBoard(Node root = null)
|
||
// {
|
||
// BoardRoot = root ?? GetTree().CurrentScene; // 如果未提供root,默认为当前场景
|
||
// }
|
||
|
||
// public void InitChessBoard()
|
||
// {
|
||
// // 定义黑色和红色棋子的初始位置和类型
|
||
// var positions = new (Vector2 position, ChessType type)[]
|
||
// {
|
||
// // 黑方棋子初始化...
|
||
// // 示例省略具体位置和类型,你需要根据实际情况填写
|
||
// // (new Vector2(x, y), ChessType.Pawn),
|
||
|
||
// // 红方棋子初始化...
|
||
// // 同上
|
||
// };
|
||
|
||
// // 初始化棋子
|
||
// foreach (var (position, type) in positions)
|
||
// {
|
||
// var color = position.Y == 0 ? new Color("black") : new Color("red"); // 根据行判断颜色
|
||
// var piece = CreateChessPiece(type, color);
|
||
// InsertChess(piece, position);
|
||
// }
|
||
// }
|
||
|
||
// private ChessPiece CreateChessPiece(ChessType type, Color labelColor)
|
||
// {
|
||
// // 根据ChessType创建对应的棋子实例并设置Label和颜色
|
||
// // 这里需要你实现具体的逻辑,例如switch case或映射表来决定创建哪种棋子
|
||
// // 返回创建的棋子实例
|
||
// }
|
||
|
||
// // ...其他原有方法...
|
||
// }
|
||
}
|