102 lines
2.6 KiB
C#
102 lines
2.6 KiB
C#
using Godot;
|
|
using Godot.Collections;
|
|
|
|
public partial class ChessGame : Node2D
|
|
{
|
|
ChessBoard board;
|
|
Global global;
|
|
private bool isSession = false;
|
|
|
|
// Called when the node enters the scene tree for the first time.
|
|
public override void _Ready()
|
|
{
|
|
// Init.Call();
|
|
global = GetNode<Global>("/root/Global");
|
|
board = GetNode<ChessBoard>("Chessboard");
|
|
// GetNode<Button>("Undo").Connect("pressed", Callable.From(board.Undo));
|
|
// GetNode<Button>("ReInit").Connect("pressed", Callable.From(board.ReInit));
|
|
// GetNode<Button>("Home").Connect("pressed", Callable.From(this.GoHome));
|
|
|
|
if (!global.RPClient.GetIsConnected()) {
|
|
return;
|
|
}
|
|
|
|
isSession = true;
|
|
GD.Print("ws is connected");
|
|
global.RPClient.OnPRCSessionExit += (cmd, code) => {
|
|
GoHome();
|
|
};
|
|
board.Set("Hello", Callable.From(() => {GD.PrintErr("hello");}));
|
|
board.Set("chessMoveFunc", Callable.From((Vector2 newPos, Vector2 fromPos) => {
|
|
var res = global.RPClient.SendSessionToAll(global.sessionId, new Dictionary{
|
|
{"type", "move"},
|
|
{"from", fromPos},
|
|
{"to", newPos},
|
|
{"fromX", fromPos.X},
|
|
{"fromY", fromPos.Y},
|
|
{"toX", newPos.X},
|
|
{"toY", newPos.Y},
|
|
});
|
|
GD.Print($"chessMoveFunc Callback {fromPos} -> {newPos} {res}");
|
|
return false;
|
|
}));
|
|
global.RPClient.OnPRCSessionRecv += (msg) => {
|
|
SessionMsgHandle(msg["msg"].AsGodotDictionary());
|
|
};
|
|
}
|
|
|
|
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
public override void _Process(double delta) {
|
|
}
|
|
|
|
private void SessionMsgHandle(Dictionary msg) {
|
|
// GD.PrintErr($"session msg: {msg}");
|
|
switch (msg["type"].AsString()) {
|
|
case "move":
|
|
Vector2 to = new(GD.StrToVar(msg["toX"].ToString()).AsInt32(),
|
|
GD.StrToVar(msg["toY"].ToString()).AsInt32());
|
|
|
|
Vector2 from = new(GD.StrToVar(msg["fromX"].ToString()).AsInt32(),
|
|
GD.StrToVar(msg["fromY"].ToString()).AsInt32());
|
|
board.MoveAndRecord(to, from);
|
|
break;
|
|
case "undo":
|
|
board.Undo();
|
|
break;
|
|
case "reInit":
|
|
board.ReInit();
|
|
break;
|
|
}
|
|
}
|
|
|
|
public void GoHome() {
|
|
if (global.RPClient.IsOnline()) {
|
|
global.RPClient.ExitServer();
|
|
}
|
|
global.GotoScene("res://Main.tscn");
|
|
}
|
|
|
|
public void Undo() {
|
|
GD.PrintErr($"Undo {isSession}");
|
|
if (isSession == false) {
|
|
GD.PrintErr($"Undo ??");
|
|
board.Undo();
|
|
return;
|
|
}
|
|
global.RPClient.SendSessionToAll(global.sessionId, new Dictionary{
|
|
{"type", "undo"},
|
|
});
|
|
}
|
|
|
|
public void ReInit() {
|
|
GD.PrintErr($"ReInit {isSession}");
|
|
if (isSession == false) {
|
|
board.ReInit();
|
|
return;
|
|
}
|
|
global.RPClient.SendSessionToAll(global.sessionId, new Dictionary{
|
|
{"type", "reInit"},
|
|
});
|
|
}
|
|
}
|