AceField/Scripts/Launcher.cs

83 lines
1.5 KiB
C#
Raw Normal View History

2024-08-07 15:01:57 +00:00
using AceField.Scripts.Logic;
2024-08-05 08:30:50 +00:00
using Godot;
2024-08-07 15:01:57 +00:00
namespace AceField.Scripts;
2024-08-05 08:30:50 +00:00
2024-08-05 15:54:22 +00:00
public partial class Launcher : Node
2024-08-05 08:30:50 +00:00
{
2024-08-05 11:08:28 +00:00
[Export] public World World;
2024-08-05 08:30:50 +00:00
private void GameFreeze()
{
GetTree().Paused = true;
2024-08-05 11:08:28 +00:00
World.Hide();
2024-08-05 08:30:50 +00:00
}
private void GameUnfreeze()
{
GetTree().Paused = false;
2024-08-05 11:08:28 +00:00
World.Show();
2024-08-05 08:30:50 +00:00
}
public override void _Ready()
{
GameFreeze();
if (DisplayServer.GetName() == "headless")
{
GD.Print("Starting server in headless mode...");
StartAsServer(4343);
}
}
2024-08-05 15:54:22 +00:00
public void StartAsSingle()
{
GameUnfreeze();
World.StartGame();
}
2024-08-05 08:30:50 +00:00
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;
}
2024-08-05 11:08:28 +00:00
GD.Print("Running game as server...");
2024-08-05 08:30:50 +00:00
Multiplayer.MultiplayerPeer = peer;
GameUnfreeze();
2024-08-05 11:08:28 +00:00
World.StartGame();
2024-08-05 08:30:50 +00:00
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;
}
2024-08-05 11:08:28 +00:00
GD.Print("Running game as client...");
2024-08-05 08:30:50 +00:00
Multiplayer.MultiplayerPeer = peer;
GameUnfreeze();
2024-08-05 11:08:28 +00:00
World.StartGame();
2024-08-05 08:30:50 +00:00
return true;
}
}