using CodingLand.Scripts.Logic; using Godot; namespace CodingLand.Scripts; public partial class Multiplayer : Node { [Export] public World World; private void GameFreeze() { GetTree().Paused = true; World.Hide(); } private void GameUnfreeze() { GetTree().Paused = false; World.Show(); } public override void _Ready() { GameFreeze(); if (DisplayServer.GetName() == "headless") { GD.Print("Starting server in headless mode..."); StartAsServer(4343); } } public bool StartAsServer(int port) { var peer = new ENetMultiplayerPeer(); peer.CreateServer(port); if (peer.GetConnectionStatus() == MultiplayerPeer.ConnectionStatus.Disconnected) { GD.PrintErr("Failed to start multiplayer server..."); OS.Alert("Failed to start multiplayer server..."); return false; } GD.Print("Running game as server..."); Multiplayer.MultiplayerPeer = peer; GameUnfreeze(); World.StartGame(); return true; } public bool StartAsClient(string addr, int port) { if (string.IsNullOrEmpty(addr)) return false; var peer = new ENetMultiplayerPeer(); peer.CreateClient(addr, port); if (peer.GetConnectionStatus() == MultiplayerPeer.ConnectionStatus.Disconnected) { var info = $"Unable to connect multiplayer server {addr}:{port}"; GD.PrintErr(info); OS.Alert(info); return false; } GD.Print("Running game as client..."); Multiplayer.MultiplayerPeer = peer; GameUnfreeze(); World.StartGame(); return true; } }