Megacity demo game for UOS
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

50 行
2.0 KiB

using Unity.Burst;
using Unity.Entities;
using Unity.NetCode;
using Unity.NetCode.Extensions;
using static Unity.Entities.SystemAPI;
namespace Unity.MegaCity.Gameplay
{
/// <summary>
/// Verify if a player has been disconnected from the game.
/// </summary>
[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<PlayerConnectedElement>(myEntity);
m_ConnectedPlayers = state.GetEntityQuery(ComponentType.ReadOnly<NetworkStreamConnection>());
}
public void OnUpdate(ref SystemState state)
{
var playersConnected = GetSingletonBuffer<PlayerConnectedElement>();
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);
}
}
}
}
}
}