From f6c5ce685af1108be87d81ed463e9d634da1e634 Mon Sep 17 00:00:00 2001 From: copygirl Date: Tue, 26 Dec 2023 12:17:57 +0100 Subject: [PATCH] Add static Game.Instance property --- Game.cs | 13 ++++++++++++- game.tscn | 3 ++- ui/MultiplayerMenu.cs | 14 ++++++-------- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/Game.cs b/Game.cs index 778cb8d..66e2982 100644 --- a/Game.cs +++ b/Game.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"); + [Export] public Player LocalPlayer { get; set; } public MultiplayerManager MultiplayerManager { get; private set; } public override void _EnterTree() - => MultiplayerManager = GetNode("MultiplayerManager"); + { + // Set invariant culture so formatting is consistent. + CultureInfo.CurrentCulture = CultureInfo.InvariantCulture; + + MultiplayerManager = GetNode("MultiplayerManager"); + } } diff --git a/game.tscn b/game.tscn index 94badc6..522e1bd 100644 --- a/game.tscn +++ b/game.tscn @@ -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") diff --git a/ui/MultiplayerMenu.cs b/ui/MultiplayerMenu.cs index 4fff271..99f562a 100644 --- a/ui/MultiplayerMenu.cs +++ b/ui/MultiplayerMenu.cs @@ -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("/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)(() => { - 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); } }