namespace LuckerParty; /// /// Listens to Network events and creates Client GameObjects /// 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 /// private GameObject _clientGroup; public void OnActive( Connection channel ) { // Set up the Client GameObject var gameObject = new GameObject( _clientGroup ) { Name = $"{channel.DisplayName} ({channel.SteamId})" }; _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 ) { Name = "Clients", NetworkMode = NetworkMode.Object }; } protected override void OnDestroy() { _clientGroup.Destroy(); } }