Add static Game.Instance property

main
copygirl 11 months ago
parent 58c668d7a2
commit f6c5ce685a
  1. 13
      Game.cs
  2. 3
      game.tscn
  3. 14
      ui/MultiplayerMenu.cs

@ -1,9 +1,20 @@
using System.Globalization;
public partial class Game : Node
{
static Game _game;
public static Game Instance => _game
??= ((SceneTree)Engine.GetMainLoop()).Root.GetNode<Game>("Game");
[Export] public Player LocalPlayer { get; set; }
public MultiplayerManager MultiplayerManager { get; private set; }
public override void _EnterTree()
=> MultiplayerManager = GetNode<MultiplayerManager>("MultiplayerManager");
{
// Set invariant culture so formatting is consistent.
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
MultiplayerManager = GetNode<MultiplayerManager>("MultiplayerManager");
}
}

@ -15,8 +15,9 @@ shader = ExtResource("4_gacvj")
shader_parameter/line_color = Color(1, 1, 1, 0.75)
shader_parameter/line_thickness = 2.0
[node name="Game" type="Node"]
[node name="Game" type="Node" node_paths=PackedStringArray("LocalPlayer")]
script = ExtResource("1_uywdd")
LocalPlayer = NodePath("Players/Local")
[node name="MultiplayerManager" type="Node" parent="." node_paths=PackedStringArray("LocalPlayer", "Players")]
script = ExtResource("1_7shyh")

@ -30,18 +30,16 @@ public partial class MultiplayerMenu : MarginContainer
[Export] public Label PlayersLabel { get; set; }
[Export] public Button DisconnectButton { get; set; }
Game _game;
public override void _Ready()
{
_game = GetNode<Game>("/root/Game");
PortDisplay.AddThemeColorOverride("font_uneditable_color", PortDisplay.GetThemeColor("font_color"));
Multiplayer.ConnectedToServer += () => UpdateStatus(Status.Connected);
Multiplayer.ConnectionFailed += () => UpdateStatus(Status.ConnectionFailed);
Multiplayer.ServerDisconnected += () => UpdateStatus(Status.Disconnected);
_game.MultiplayerManager.PlayerJoined += (_) => UpdatePlayerCount();
_game.MultiplayerManager.PlayerLeft += (_) => UpdatePlayerCount();
Game.Instance.MultiplayerManager.PlayerJoined += _ => UpdatePlayerCount();
Game.Instance.MultiplayerManager.PlayerLeft += _ => UpdatePlayerCount();
}
void UpdateStatus(Status status)
@ -81,7 +79,7 @@ public partial class MultiplayerMenu : MarginContainer
< Status.Connecting => "Singleplayer",
Status.Connecting => "??? Players",
> Status.Connecting => ((Func<string>)(() => {
var players = _game.MultiplayerManager.Players.GetChildCount();
var players = Game.Instance.MultiplayerManager.Players.GetChildCount();
return $"{players} {(players != 1 ? "Players" : "Player")}";
}))(),
};
@ -100,14 +98,14 @@ public partial class MultiplayerMenu : MarginContainer
var address = AddressInput.Text;
if (address == "") address = AddressInput.PlaceholderText;
var port = (ushort)RoundToInt(PortInput.Value);
_game.MultiplayerManager.Connect(address, port);
Game.Instance.MultiplayerManager.Connect(address, port);
UpdateStatus(Status.Connecting);
}
public void OnHostPressed()
{
var port = (ushort)RoundToInt(PortInput.Value);
if (_game.MultiplayerManager.CreateServer(port)) {
if (Game.Instance.MultiplayerManager.CreateServer(port)) {
PortDisplay.Text = port.ToString();
UpdateStatus(Status.Hosting);
} else
@ -116,7 +114,7 @@ public partial class MultiplayerMenu : MarginContainer
public void OnDisconnectPressed()
{
_game.MultiplayerManager.Disconnect();
Game.Instance.MultiplayerManager.Disconnect();
UpdateStatus(Status.Disconnected);
}
}

Loading…
Cancel
Save