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 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; } [Export] public Player LocalPlayer { get; set; }
public MultiplayerManager MultiplayerManager { get; private set; } public MultiplayerManager MultiplayerManager { get; private set; }
public override void _EnterTree() 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_color = Color(1, 1, 1, 0.75)
shader_parameter/line_thickness = 2.0 shader_parameter/line_thickness = 2.0
[node name="Game" type="Node"] [node name="Game" type="Node" node_paths=PackedStringArray("LocalPlayer")]
script = ExtResource("1_uywdd") script = ExtResource("1_uywdd")
LocalPlayer = NodePath("Players/Local")
[node name="MultiplayerManager" type="Node" parent="." node_paths=PackedStringArray("LocalPlayer", "Players")] [node name="MultiplayerManager" type="Node" parent="." node_paths=PackedStringArray("LocalPlayer", "Players")]
script = ExtResource("1_7shyh") script = ExtResource("1_7shyh")

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

Loading…
Cancel
Save