54 lines
1.4 KiB
C#
54 lines
1.4 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 event EventHandler<Vector2> OnPosClicked;
|
|
|
|
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);
|
|
// };
|
|
}
|
|
|
|
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());
|
|
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);
|
|
}
|
|
}
|