86 lines
2.6 KiB
C#
86 lines
2.6 KiB
C#
using Godot;
|
|
using RPPackage;
|
|
|
|
public partial class Global : Node
|
|
{
|
|
public RPClientEDWS RPClient = new();
|
|
public string sessionId;
|
|
public string URL = "wss://game.zzyxyz.com/";
|
|
public Node CurrentScene { get; set; }
|
|
|
|
// Called when the node enters the scene tree for the first time.
|
|
public override void _Ready()
|
|
{
|
|
RPClient.OnRPCError += (string errCode, string type, string cmd, string errMsg) => {
|
|
GD.PrintErr($"errCode {errCode}, type/cmd {type}/{cmd}, errMsg {errMsg}");
|
|
};
|
|
RPClient.OnClose += (string eventName, object[] args) => {
|
|
SetProcess(false);
|
|
};
|
|
RPClient.OnOpen += (string eventName, object[] args) => {
|
|
RPClient.UserInit("undefined", "godot chessboard", () => {
|
|
return RPClient.RegionAdd("server");
|
|
});
|
|
};
|
|
Viewport root = GetTree().Root;
|
|
CurrentScene = root.GetChild(root.GetChildCount() - 1);
|
|
SetProcess(false);
|
|
}
|
|
|
|
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
public override void _Process(double delta)
|
|
{
|
|
RPClient.PollEx(delta);
|
|
}
|
|
|
|
public override void _Notification(int what)
|
|
{
|
|
if (what == NotificationWMCloseRequest) {
|
|
RPClient.Close();
|
|
GetTree().Quit(); // default behavior
|
|
}
|
|
}
|
|
|
|
public delegate void ChangeSceneCallback(Node newSence);
|
|
private static ChangeSceneCallback changeSceneCallback = null;
|
|
public void GotoScene(string path, ChangeSceneCallback callback = null)
|
|
{
|
|
// This function will usually be called from a signal callback,
|
|
// or some other function from the current scene.
|
|
// Deleting the current scene at this point is
|
|
// a bad idea, because it may still be executing code.
|
|
// This will result in a crash or unexpected behavior.
|
|
|
|
// The solution is to defer the load to a later time, when
|
|
// we can be sure that no code from the current scene is running:
|
|
if (callback != null) {
|
|
changeSceneCallback = callback;
|
|
}
|
|
Callable callbackWrapper = new(null, nameof(changeSceneCallback));
|
|
|
|
CallDeferred(MethodName.DeferredGotoScene, path, callbackWrapper);
|
|
changeSceneCallback = null;
|
|
}
|
|
|
|
public void DeferredGotoScene(string path, Callable onLoaded)
|
|
{
|
|
// It is now safe to remove the current scene.
|
|
CurrentScene.Free();
|
|
|
|
// Load a new scene.
|
|
var nextScene = GD.Load<PackedScene>(path);
|
|
|
|
// Instance the new scene.
|
|
CurrentScene = nextScene.Instantiate();
|
|
if (changeSceneCallback != null) {
|
|
onLoaded.Call(CurrentScene);
|
|
}
|
|
|
|
// Add it to the active scene, as child of root.
|
|
GetTree().Root.AddChild(CurrentScene);
|
|
|
|
// Optionally, to make it compatible with the SceneTree.change_scene_to_file() API.
|
|
GetTree().CurrentScene = CurrentScene;
|
|
}
|
|
}
|