diff --git a/Multiplayer.sln b/Multiplayer.sln index 6ef3e07..cb95716 100644 --- a/Multiplayer.sln +++ b/Multiplayer.sln @@ -1,13 +1,16 @@ Microsoft Visual Studio Solution File, Format Version 12.00 -Project("{99d6f2a9-1021-4ea5-acb3-48cbd6fb8d09}") = "Multiplayer", "Multiplayer/Multiplayer.csproj", "{f712c7fb-eeae-4036-a938-356e022b0455}" +Project("{99D6F2A9-1021-4EA5-ACB3-48CBD6FB8D09}") = "Multiplayer", "Multiplayer/Multiplayer.csproj", "{F712C7FB-EEAE-4036-A938-356E022B0455}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Release|Any CPU = Release|Any CPU + Debug|Any CPU = Debug|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {f712c7fb-eeae-4036-a938-356e022b0455}.Release|Any CPU.ActiveCfg = Release|Any CPU - {f712c7fb-eeae-4036-a938-356e022b0455}.Release|Any CPU.Build.0 = Release|Any CPU + {F712C7FB-EEAE-4036-A938-356E022B0455}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F712C7FB-EEAE-4036-A938-356E022B0455}.Release|Any CPU.Build.0 = Release|Any CPU + {F712C7FB-EEAE-4036-A938-356E022B0455}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F712C7FB-EEAE-4036-A938-356E022B0455}.Debug|Any CPU.Build.0 = Debug|Any CPU EndGlobalSection EndGlobal diff --git a/Multiplayer/Components/Networking/NetworkLifecycle.cs b/Multiplayer/Components/Networking/NetworkLifecycle.cs index 2923f86..7c14288 100644 --- a/Multiplayer/Components/Networking/NetworkLifecycle.cs +++ b/Multiplayer/Components/Networking/NetworkLifecycle.cs @@ -30,6 +30,7 @@ public class NetworkLifecycle : SingletonBehaviour public bool IsProcessingPacket => Client.IsProcessingPacket; + private PlayerListGUI playerList; private NetworkStatsGui Stats; private readonly ExecutionTimer tickTimer = new(); private readonly ExecutionTimer tickWatchdog = new(0.25f); @@ -57,8 +58,10 @@ public bool IsHost() protected override void Awake() { base.Awake(); + playerList = gameObject.AddComponent(); Stats = gameObject.AddComponent(); RegisterPackets(); + WorldStreamingInit.LoadingFinished += () => { playerList.RegisterListeners(); }; Settings.OnSettingsUpdated += OnSettingsUpdated; SceneManager.sceneLoaded += (scene, _) => { diff --git a/Multiplayer/Components/Networking/Player/NetworkedPlayer.cs b/Multiplayer/Components/Networking/Player/NetworkedPlayer.cs index 6ddc8c7..fec0ea6 100644 --- a/Multiplayer/Components/Networking/Player/NetworkedPlayer.cs +++ b/Multiplayer/Components/Networking/Player/NetworkedPlayer.cs @@ -14,6 +14,7 @@ public class NetworkedPlayer : MonoBehaviour private AnimationHandler animationHandler; private NameTag nameTag; + private int ping; private string username; @@ -60,6 +61,12 @@ private void OnSettingsUpdated(Settings settings) public void SetPing(int ping) { nameTag.SetPing(ping); + this.ping = ping; + } + + public int GetPing() + { + return ping; } private void Update() diff --git a/Multiplayer/Components/Networking/PlayerListGUI.cs b/Multiplayer/Components/Networking/PlayerListGUI.cs new file mode 100644 index 0000000..9b2bfaa --- /dev/null +++ b/Multiplayer/Components/Networking/PlayerListGUI.cs @@ -0,0 +1,54 @@ +using System.Collections.Generic; +using Multiplayer.Components.Networking.Player; +using UnityEngine; + +namespace Multiplayer.Components.Networking; + +public class PlayerListGUI : MonoBehaviour +{ + private bool showPlayerList; + + public void RegisterListeners() + { + ScreenspaceMouse.Instance.ValueChanged += OnToggle; + } + + private void OnToggle(bool status) + { + showPlayerList = status; + } + + private void OnGUI() + { + if (!showPlayerList) + return; + + GUILayout.Window(157031520, new Rect(Screen.width / 2.0f - 125, 25, 250, 0), DrawPlayerList, "Online Players"); + } + + private static void DrawPlayerList(int windowId) + { + foreach (string player in GetPlayerList()) + GUILayout.Label(player); + } + + // todo: cache this? + private static IEnumerable GetPlayerList() + { + if (!NetworkLifecycle.Instance.IsClientRunning) + return new[] { "Not in game" }; + + IReadOnlyCollection players = NetworkLifecycle.Instance.Client.PlayerManager.Players; + string[] playerList = new string[players.Count + 1]; + int i = 0; + foreach (NetworkedPlayer player in players) + { + playerList[i] = $"{player.Username} ({player.GetPing().ToString()}ms)"; + i++; + } + + // The Player of the Client is not in the PlayerManager, so we need to add it separately + playerList[playerList.Length - 1] = $"{Multiplayer.Settings.Username} ({NetworkLifecycle.Instance.Client.Ping.ToString()}ms)"; + return playerList; + } +}