From 299752bd75f229db794b79105d8b8af3ba8903c7 Mon Sep 17 00:00:00 2001 From: para Date: Sun, 22 Dec 2024 21:01:57 -0800 Subject: [PATCH] Expand on NetworkManager.cs --- code/NetworkManager.cs | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/code/NetworkManager.cs b/code/NetworkManager.cs index f9649d0..f657a01 100644 --- a/code/NetworkManager.cs +++ b/code/NetworkManager.cs @@ -5,20 +5,47 @@ namespace LuckerParty; /// public sealed class NetworkManager : Component, Component.INetworkListener { + /// + /// Map from Connection to Client GameObject + /// + private readonly Dictionary _clientMap = new(); + /// /// A GameObject used for organizational grouping of Clients /// - [Property] private GameObject ClientGroup { get; set; } 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})" }; - gameObject.NetworkSpawn( channel ); - + _clientMap.Add( channel, gameObject ); var client = gameObject.AddComponent(); 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(); } }