2021-01-31 16:14:17 +00:00
|
|
|
|
using OWML.Common;
|
2021-02-14 12:47:40 +00:00
|
|
|
|
using QSB.Events;
|
2021-01-05 15:56:14 +00:00
|
|
|
|
using QSB.Player;
|
2021-01-03 16:09:58 +00:00
|
|
|
|
using QSB.QuantumSync.WorldObjects;
|
2021-01-31 16:14:17 +00:00
|
|
|
|
using QSB.Utility;
|
2020-12-31 12:10:55 +00:00
|
|
|
|
using QSB.WorldSync;
|
2021-02-21 18:25:25 +00:00
|
|
|
|
using System.Collections.Generic;
|
2021-01-18 12:33:07 +00:00
|
|
|
|
using System.Linq;
|
2021-01-05 15:56:14 +00:00
|
|
|
|
using System.Reflection;
|
2020-12-22 21:39:53 +00:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace QSB.QuantumSync
|
|
|
|
|
{
|
|
|
|
|
internal class QuantumManager : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
public static QuantumManager Instance { get; private set; }
|
2021-01-18 12:33:07 +00:00
|
|
|
|
public QuantumShrine Shrine;
|
2021-02-09 11:01:21 +00:00
|
|
|
|
public bool IsReady;
|
2020-12-22 21:39:53 +00:00
|
|
|
|
|
|
|
|
|
public void Awake()
|
|
|
|
|
{
|
|
|
|
|
Instance = this;
|
2021-01-31 16:14:17 +00:00
|
|
|
|
QSBSceneManager.OnUniverseSceneLoaded += RebuildQuantumObjects;
|
2020-12-22 21:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-31 16:14:17 +00:00
|
|
|
|
public void OnDestroy() => QSBSceneManager.OnUniverseSceneLoaded -= RebuildQuantumObjects;
|
2020-12-23 22:43:05 +00:00
|
|
|
|
|
2021-01-31 16:14:17 +00:00
|
|
|
|
public void RebuildQuantumObjects(OWScene scene)
|
2020-12-22 21:39:53 +00:00
|
|
|
|
{
|
2021-02-01 15:44:21 +00:00
|
|
|
|
DebugLog.DebugWrite("Rebuilding quantum objects...", MessageType.Warning);
|
2021-02-18 15:36:11 +00:00
|
|
|
|
QSBWorldSync.Init<QSBSocketedQuantumObject, SocketedQuantumObject>();
|
|
|
|
|
QSBWorldSync.Init<QSBMultiStateQuantumObject, MultiStateQuantumObject>();
|
|
|
|
|
QSBWorldSync.Init<QSBQuantumSocket, QuantumSocket>();
|
|
|
|
|
QSBWorldSync.Init<QSBQuantumShuffleObject, QuantumShuffleObject>();
|
2021-02-26 14:19:42 +00:00
|
|
|
|
QSBWorldSync.Init<QSBQuantumMoon, QuantumMoon>();
|
2021-01-26 14:07:17 +00:00
|
|
|
|
if (scene == OWScene.SolarSystem)
|
|
|
|
|
{
|
|
|
|
|
Shrine = Resources.FindObjectsOfTypeAll<QuantumShrine>().First();
|
|
|
|
|
}
|
2021-02-09 11:01:21 +00:00
|
|
|
|
IsReady = true;
|
2020-12-22 21:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-14 12:47:40 +00:00
|
|
|
|
public void CheckExistingPlayers()
|
|
|
|
|
{
|
|
|
|
|
DebugLog.DebugWrite("Checking quantum objects for non-existent players...", MessageType.Info);
|
|
|
|
|
var quantumObjects = QSBWorldSync.GetWorldObjects<IQSBQuantumObject>().ToList();
|
|
|
|
|
for (var i = 0; i < quantumObjects.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
var obj = quantumObjects[i];
|
|
|
|
|
if (!QSBPlayerManager.PlayerExists(obj.ControllingPlayer))
|
|
|
|
|
{
|
|
|
|
|
var idToSend = obj.IsEnabled ? QSBPlayerManager.LocalPlayerId : 0u;
|
|
|
|
|
QSBEventManager.FireEvent(EventNames.QSBQuantumAuthority, i, idToSend);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-26 14:07:17 +00:00
|
|
|
|
public void OnRenderObject()
|
2021-01-03 16:09:58 +00:00
|
|
|
|
{
|
2021-01-31 16:14:17 +00:00
|
|
|
|
if (!QSBCore.HasWokenUp || !QSBCore.DebugMode || !QSBCore.ShowLinesInDebug)
|
2021-01-03 16:09:58 +00:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-01-18 12:33:07 +00:00
|
|
|
|
|
2021-01-26 14:07:17 +00:00
|
|
|
|
if (Shrine != null)
|
|
|
|
|
{
|
|
|
|
|
Popcron.Gizmos.Sphere(Shrine.transform.position, 10f, Color.magenta);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-16 12:10:14 +00:00
|
|
|
|
public static bool IsVisibleUsingCameraFrustum(ShapeVisibilityTracker tracker, bool ignoreLocalCamera)
|
2021-01-31 14:21:54 +00:00
|
|
|
|
{
|
|
|
|
|
return tracker.gameObject.activeInHierarchy
|
2021-02-22 10:51:51 +00:00
|
|
|
|
&& QSBPlayerManager.GetPlayersWithCameras(!ignoreLocalCamera)
|
2021-01-31 14:21:54 +00:00
|
|
|
|
.Any(x => (bool)tracker.GetType()
|
|
|
|
|
.GetMethod("IsInFrustum", BindingFlags.NonPublic | BindingFlags.Instance)
|
2021-02-22 10:51:51 +00:00
|
|
|
|
.Invoke(tracker, new object[] { x.Camera.GetFrustumPlanes() }));
|
2021-01-31 14:21:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-16 12:10:14 +00:00
|
|
|
|
public static bool IsVisible(ShapeVisibilityTracker tracker, bool ignoreLocalCamera)
|
2021-01-31 14:21:54 +00:00
|
|
|
|
{
|
|
|
|
|
return tracker.gameObject.activeInHierarchy
|
2021-02-16 12:10:14 +00:00
|
|
|
|
&& IsVisibleUsingCameraFrustum(tracker, ignoreLocalCamera)
|
2021-02-22 10:51:51 +00:00
|
|
|
|
&& QSBPlayerManager.GetPlayersWithCameras(!ignoreLocalCamera)
|
|
|
|
|
.Any(x => VisibilityOccluder.CanYouSee(tracker, x.Camera.mainCamera.transform.position));
|
2021-01-31 14:21:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-21 18:25:25 +00:00
|
|
|
|
public static IEnumerable<PlayerInfo> GetEntangledPlayers(QuantumObject obj)
|
2021-02-21 21:27:54 +00:00
|
|
|
|
{
|
|
|
|
|
var worldObj = GetObject(obj);
|
|
|
|
|
return QSBPlayerManager.PlayerList.Where(x => x.EntangledObject == worldObj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IQSBQuantumObject GetObject(QuantumObject unityObject)
|
2021-02-21 18:25:25 +00:00
|
|
|
|
{
|
|
|
|
|
IQSBQuantumObject worldObj = null;
|
2021-02-21 21:27:54 +00:00
|
|
|
|
if (unityObject.GetType() == typeof(SocketedQuantumObject) || unityObject.GetType() == typeof(QuantumShrine))
|
2021-02-21 18:25:25 +00:00
|
|
|
|
{
|
2021-02-24 10:45:25 +00:00
|
|
|
|
worldObj = QSBWorldSync.GetWorldFromUnity<QSBSocketedQuantumObject, SocketedQuantumObject>((SocketedQuantumObject)unityObject);
|
2021-02-21 18:25:25 +00:00
|
|
|
|
}
|
2021-02-21 21:27:54 +00:00
|
|
|
|
else if (unityObject.GetType() == typeof(MultiStateQuantumObject))
|
2021-02-21 18:25:25 +00:00
|
|
|
|
{
|
2021-02-24 10:45:25 +00:00
|
|
|
|
worldObj = QSBWorldSync.GetWorldFromUnity<QSBMultiStateQuantumObject, MultiStateQuantumObject>((MultiStateQuantumObject)unityObject);
|
2021-02-21 21:27:54 +00:00
|
|
|
|
}
|
|
|
|
|
else if (unityObject.GetType() == typeof(QuantumShuffleObject))
|
|
|
|
|
{
|
2021-02-24 10:45:25 +00:00
|
|
|
|
worldObj = QSBWorldSync.GetWorldFromUnity<QSBQuantumShuffleObject, QuantumShuffleObject>((QuantumShuffleObject)unityObject);
|
2021-02-21 18:25:25 +00:00
|
|
|
|
}
|
2021-02-26 14:19:42 +00:00
|
|
|
|
else if (unityObject.GetType() == typeof(QuantumMoon))
|
|
|
|
|
{
|
2021-02-26 22:24:49 +00:00
|
|
|
|
worldObj = QSBWorldSync.GetWorldFromUnity<QSBQuantumMoon, QuantumMoon>((QuantumMoon)unityObject);
|
2021-02-26 14:19:42 +00:00
|
|
|
|
}
|
2021-02-21 18:25:25 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2021-02-21 21:27:54 +00:00
|
|
|
|
DebugLog.ToConsole($"Warning - couldn't work out type of QuantumObject {unityObject.name}.", MessageType.Warning);
|
2021-02-21 18:25:25 +00:00
|
|
|
|
}
|
2021-02-21 21:27:54 +00:00
|
|
|
|
return worldObj;
|
2021-02-21 18:25:25 +00:00
|
|
|
|
}
|
2020-12-22 21:39:53 +00:00
|
|
|
|
}
|
2020-12-23 10:43:13 +00:00
|
|
|
|
}
|