Skip to content

Commit

Permalink
Added in-game Playerlist (#21)
Browse files Browse the repository at this point in the history
Co-authored-by: Pierce Thompson <[email protected]>
  • Loading branch information
TheGamerH18 and Insprill authored Aug 8, 2023
1 parent a3415f7 commit 28fdbac
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
9 changes: 6 additions & 3 deletions Multiplayer.sln
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions Multiplayer/Components/Networking/NetworkLifecycle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class NetworkLifecycle : SingletonBehaviour<NetworkLifecycle>

public bool IsProcessingPacket => Client.IsProcessingPacket;

private PlayerListGUI playerList;
private NetworkStatsGui Stats;
private readonly ExecutionTimer tickTimer = new();
private readonly ExecutionTimer tickWatchdog = new(0.25f);
Expand Down Expand Up @@ -57,8 +58,10 @@ public bool IsHost()
protected override void Awake()
{
base.Awake();
playerList = gameObject.AddComponent<PlayerListGUI>();
Stats = gameObject.AddComponent<NetworkStatsGui>();
RegisterPackets();
WorldStreamingInit.LoadingFinished += () => { playerList.RegisterListeners(); };
Settings.OnSettingsUpdated += OnSettingsUpdated;
SceneManager.sceneLoaded += (scene, _) =>
{
Expand Down
7 changes: 7 additions & 0 deletions Multiplayer/Components/Networking/Player/NetworkedPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class NetworkedPlayer : MonoBehaviour

private AnimationHandler animationHandler;
private NameTag nameTag;
private int ping;

private string username;

Expand Down Expand Up @@ -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()
Expand Down
54 changes: 54 additions & 0 deletions Multiplayer/Components/Networking/PlayerListGUI.cs
Original file line number Diff line number Diff line change
@@ -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<string> GetPlayerList()
{
if (!NetworkLifecycle.Instance.IsClientRunning)
return new[] { "Not in game" };

IReadOnlyCollection<NetworkedPlayer> 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;
}
}

0 comments on commit 28fdbac

Please sign in to comment.