81 lines
2.3 KiB
C#
81 lines
2.3 KiB
C#
// Chessboard.cs
|
|
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 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);
|
|
|
|
OnMouseClicked?.Invoke(this, GetLocalMousePosition());
|
|
}
|
|
}
|
|
|
|
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) {
|
|
ChessPiece piece = new ChessPiece {
|
|
PieceLabel = label,
|
|
LabelColor = new Color(color)
|
|
};
|
|
InsertNode(piece, new Vector2(x, baseY + y));
|
|
}
|
|
}
|
|
}
|