AceField/Scripts/Launcher.cs

90 lines
1.7 KiB
C#
Raw Permalink Normal View History

2024-08-07 15:01:57 +00:00
using AceField.Scripts.Logic;
2024-08-09 06:14:51 +00:00
using AceField.Scripts.UI;
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-09 06:14:51 +00:00
[Export] public BootScreen BootMenu;
2024-08-08 09:10:48 +00:00
[Export] public CanvasLayer Overlay;
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-08 09:10:48 +00:00
Overlay.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-08 09:10:48 +00:00
Overlay.Show();
2024-08-09 06:14:51 +00:00
BootMenu.StartGame -= GameUnfreeze;
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-09 06:14:51 +00:00
public void StartAsSingle()
2024-08-05 15:54:22 +00:00
{
GameUnfreeze();
}
2024-08-09 06:14:51 +00:00
public bool StartAsServer(int port)
2024-08-05 08:30:50 +00:00
{
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;
2024-08-09 06:14:51 +00:00
BootMenu.StartGame += GameUnfreeze;
BootMenu.Show();
2024-08-05 08:30:50 +00:00
return true;
}
2024-08-09 06:14:51 +00:00
public bool StartAsClient(string addr, int port)
2024-08-05 08:30:50 +00:00
{
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;
2024-08-09 06:14:51 +00:00
BootMenu.StartGame += GameUnfreeze;
BootMenu.Show();
2024-08-05 08:30:50 +00:00
return true;
}
}