using Unity.Burst; using Unity.Entities; using Unity.NetCode; using Unity.NetCode.Extensions; using static Unity.Entities.SystemAPI; namespace Unity.MegaCity.Gameplay { /// /// Verify if a player has been disconnected from the game. /// [BurstCompile] [WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)] public partial struct VerifyPlayerDisconnectedSystem : ISystem { private EntityQuery m_ConnectedPlayers; public void OnCreate(ref SystemState state) { var myEntity = state.EntityManager.CreateEntity(); state.EntityManager.AddBuffer(myEntity); m_ConnectedPlayers = state.GetEntityQuery(ComponentType.ReadOnly()); } public void OnUpdate(ref SystemState state) { var playersConnected = GetSingletonBuffer(); for (int i = 0; i < playersConnected.Length; i++) { var player = playersConnected[i]; if (!state.EntityManager.Exists(player.Value)) { var playerId = player.PlayerId.ToString(); UnityEngine.Debug.Log($"Attention: Player {player.Name}({playerId}) has been disconnected from the game." + $"\nConnected Players: {m_ConnectedPlayers.CalculateEntityCount()}"); playersConnected.RemoveAt(i); // if there is no player connected, auto shut down. if(m_ConnectedPlayers.CalculateEntityCount() == 0) { int ttlInSeconds = 10; UnityEngine.Debug.Log($"Attention: There is no player connected. The game will be over in {ttlInSeconds}s."); MultiverseSDKWrapper.Instance.ShutdownMultiverseInTTL(ttlInSeconds); } } } } } }