Expand on NetworkManager.cs

This commit is contained in:
2024-12-22 21:01:57 -08:00
parent e6ef9447ff
commit 299752bd75

View File

@@ -5,20 +5,47 @@ namespace LuckerParty;
/// </summary> /// </summary>
public sealed class NetworkManager : Component, Component.INetworkListener public sealed class NetworkManager : Component, Component.INetworkListener
{ {
/// <summary>
/// Map from Connection to Client GameObject
/// </summary>
private readonly Dictionary<Connection, GameObject> _clientMap = new();
/// <summary> /// <summary>
/// A GameObject used for organizational grouping of Clients /// A GameObject used for organizational grouping of Clients
/// </summary> /// </summary>
[Property]
private GameObject ClientGroup { get; set; } private GameObject ClientGroup { get; set; }
public void OnActive( Connection channel ) public void OnActive( Connection channel )
{ {
ClientGroup ??= new GameObject( Scene.Root ); // Set up the Client GameObject
var gameObject = new GameObject( ClientGroup ) { Name = $"{channel.DisplayName} ({channel.SteamId})" }; var gameObject = new GameObject( ClientGroup ) { Name = $"{channel.DisplayName} ({channel.SteamId})" };
gameObject.NetworkSpawn( channel ); _clientMap.Add( channel, gameObject );
var client = gameObject.AddComponent<Client>(); var client = gameObject.AddComponent<Client>();
client.Connection = channel; client.Connection = channel;
// Spawn it on remote clients
gameObject.NetworkSpawn( channel );
}
public void OnDisconnected( Connection channel )
{
if ( !_clientMap.TryGetValue( channel, out var clientGameObject ) )
{
Log.Warning( $"Disconnected client {channel.SteamId} has no associated GameObject." );
return;
}
clientGameObject.Destroy();
_clientMap.Remove( channel );
}
protected override void OnAwake()
{
ClientGroup = new GameObject( Scene.Root );
}
protected override void OnDestroy()
{
ClientGroup.Destroy();
} }
} }