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

120 行
4.7 KiB

using Unity.Entities;
using Unity.MegaCity.Audio;
using Unity.MegaCity.UI;
using Unity.NetCode;
using Unity.Networking.Transport;
using UnityEngine;
namespace Unity.MegaCity.Gameplay
{
/// <summary>
/// This system is responsible for connecting to the server from the Main Menu.
/// </summary>
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]
public partial struct MainMenuSystem : ISystem, ISystemStartStop
{
private EntityQuery m_NetworkStreamConnectionQuery;
public void OnCreate(ref SystemState state)
{
m_NetworkStreamConnectionQuery = state.GetEntityQuery(ComponentType.ReadOnly<NetworkStreamConnection>());
}
public void OnStartRunning(ref SystemState state)
{
if (MainMenu.Instance == null)
return;
if (ModeBootstrap.Options.SkipMenu && TryConnect(ref state))
MultiverseRoomAPI.Instance.ConnectToServer();
}
public void OnStopRunning(ref SystemState state) { }
public void OnUpdate(ref SystemState state)
{
if (MainMenu.Instance == null)
return;
if (MultiverseRoomAPI.Instance.IsTryingToConnect)
{
var isConnected = IsConnectingOrConnected();
Debug.Log($"Is trying to connect via Button? : '{MultiverseRoomAPI.Instance.IsTryingToConnect}' \nIs the connection currently active? : {isConnected}'");
if (!isConnected)
{
ref var netStream = ref SystemAPI.GetSingletonRW<NetworkStreamDriver>().ValueRW;
isConnected = TryConnectingViaNetStream(state, netStream);
}
Debug.Log($"Has the connection been established successfully? : '{isConnected}'");
if (isConnected)
{
Enter(ref state);
}
else
{
MainMenu.Instance.ConnectionFailed();
}
}
}
private bool TryConnectingViaNetStream(SystemState state, NetworkStreamDriver netStream)
{
if (NetworkEndpoint.TryParse(MultiverseRoomAPI.Instance.IP, MultiverseRoomAPI.Instance.Port, out var networkEndpoint) && networkEndpoint.IsValid)
{
Debug.Log($"Connecting Manually ClientWorld '{state.World.Name}' to '{networkEndpoint}'...");
netStream.Connect(state.EntityManager, networkEndpoint);
return true;
}
else
{
Debug.LogError($"Play button press is not actionable, as invalid IP '{MultiverseRoomAPI.Instance.IP}' and/or Port '{MultiverseRoomAPI.Instance.Port}'!");
}
return false;
}
private void Enter(ref SystemState state)
{
if (!SystemAPI.TryGetSingleton<AudioSystemSettings>(out var audioManager))
LoadAudioSettings(state.EntityManager);
MainMenu.Instance.ConnectionSucceeded();
}
private bool IsConnectingOrConnected()
{
// NetcodeBootstrap may cause you to auto-connect
return m_NetworkStreamConnectionQuery.CalculateChunkCountWithoutFiltering() > 0;
}
private bool TryConnect(ref SystemState state)
{
if (IsConnectingOrConnected())
return true;
if (!NetworkEndpoint.TryParse(MultiverseRoomAPI.Instance.IP, MultiverseRoomAPI.Instance.Port, out var networkEndpoint))
{
Debug.LogError($"Unable to parse NetworkEndpoint from '{MultiverseRoomAPI.Instance.IP}:{MultiverseRoomAPI.Instance.Port}' Cannot connect!");
return false;
}
state.EntityManager.CreateSingleton(new NetworkStreamRequestConnect { Endpoint = networkEndpoint });
Debug.Log($"Connecting ClientWorld '{state.World.Name}' to '{networkEndpoint}'...");
return true;
}
private void LoadAudioSettings(EntityManager entityManager)
{
var audioSystemEntity = entityManager.CreateEntity(typeof(AudioSystemSettings));
var audioMaster = MainMenu.Instance.AudioMaster;
var systemSettings = new AudioSystemSettings
{
DebugMode = audioMaster.showDebugLines,
MaxDistance = audioMaster.maxDistance,
ClosestEmitterPerClipCount = audioMaster.closestEmitterPerClipCount,
};
entityManager.SetComponentData(audioSystemEntity, systemSettings);
}
}
}