quantum-space-buddies/QSB/QuantumSync/QuantumManager.cs

140 lines
5.2 KiB
C#
Raw Normal View History

2021-01-03 16:09:58 +00:00
using OWML.Utils;
2021-01-05 15:56:14 +00:00
using QSB.Player;
2021-01-03 16:09:58 +00:00
using QSB.QuantumSync.WorldObjects;
using QSB.WorldSync;
2020-12-22 21:39:53 +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; }
private List<SocketedQuantumObject> _socketedQuantumObjects;
2020-12-23 22:43:05 +00:00
private List<MultiStateQuantumObject> _multiStateQuantumObjects;
2020-12-22 21:39:53 +00:00
private List<QuantumSocket> _quantumSockets;
2021-01-01 10:17:35 +00:00
private List<QuantumShuffleObject> _quantumShuffleObjects;
2021-01-18 12:33:07 +00:00
public QuantumShrine Shrine;
2020-12-22 21:39:53 +00:00
public void Awake()
{
Instance = this;
QSBSceneManager.OnSceneLoaded += OnSceneLoaded;
}
2020-12-23 22:43:05 +00:00
public void OnDestroy() => QSBSceneManager.OnSceneLoaded -= OnSceneLoaded;
2020-12-22 21:39:53 +00:00
private void OnSceneLoaded(OWScene scene, bool isInUniverse)
{
_socketedQuantumObjects = QSBWorldSync.Init<QSBSocketedQuantumObject, SocketedQuantumObject>();
_multiStateQuantumObjects = QSBWorldSync.Init<QSBMultiStateQuantumObject, MultiStateQuantumObject>();
2020-12-24 15:57:25 +00:00
_quantumSockets = QSBWorldSync.Init<QSBQuantumSocket, QuantumSocket>();
2021-01-01 10:17:35 +00:00
_quantumShuffleObjects = QSBWorldSync.Init<QSBQuantumShuffleObject, QuantumShuffleObject>();
2021-01-26 14:07:17 +00:00
if (scene == OWScene.SolarSystem)
{
Shrine = Resources.FindObjectsOfTypeAll<QuantumShrine>().First();
}
2020-12-22 21:39:53 +00:00
}
2021-01-26 14:07:17 +00:00
public void OnRenderObject()
2021-01-03 16:09:58 +00:00
{
2021-01-05 15:56:14 +00:00
if (!QSBCore.HasWokenUp || !QSBCore.DebugMode)
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);
}
}
public void OnGUI()
{
if (!QSBCore.HasWokenUp || !QSBCore.DebugMode)
{
return;
}
2021-01-18 12:33:07 +00:00
GUI.Label(new Rect(220, 10, 200f, 20f), $"QM Visible : {Locator.GetQuantumMoon().IsVisible()}");
2021-01-05 15:56:14 +00:00
var offset = 40f;
var tracker = Locator.GetQuantumMoon().GetValue<ShapeVisibilityTracker>("_visibilityTracker");
foreach (var camera in QSBPlayerManager.GetPlayerCameras())
2021-01-03 16:09:58 +00:00
{
2021-01-18 12:33:07 +00:00
GUI.Label(new Rect(220, offset, 200f, 20f), $"- {camera.name} : {tracker.GetType().GetMethod("IsInFrustum", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(tracker, new object[] { camera.GetFrustumPlanes() })}");
offset += 30f;
}
// Used for diagnosing specific socketed objects. Just set <index> to be the correct index.
/*
var index = 110;
var socketedObject = QSBWorldSync.GetWorldObject<QSBSocketedQuantumObject>(index);
GUI.Label(new Rect(220, offset, 200f, 20f), $"{index} Controller : {socketedObject.ControllingPlayer}");
offset += 30f;
GUI.Label(new Rect(220, offset, 200f, 20f), $"{index} Visible : {socketedObject.AttachedObject.IsVisible()}");
offset += 30f;
GUI.Label(new Rect(220, offset, 200f, 20f), $"{index} Locked : {socketedObject.AttachedObject.IsLocked()}");
offset += 30f;
GUI.Label(new Rect(220, offset, 200f, 20f), $"{index} Illuminated : {socketedObject.AttachedObject.IsIlluminated()}");
offset += 30f;
var socketedTrackers = socketedObject.AttachedObject.GetComponentsInChildren<ShapeVisibilityTracker>();
if (socketedTrackers == null || socketedTrackers.Length == 0)
{
GUI.Label(new Rect(220, offset, 200f, 20f), $"- List is null or empty.");
return;
}
if (socketedTrackers.Any(x => x is null))
{
GUI.Label(new Rect(220, offset, 200f, 20f), $"- Uses a null.");
return;
}
foreach (var camera in QSBPlayerManager.GetPlayerCameras())
{
GUI.Label(new Rect(220, offset, 200f, 20f), $"- {camera.name} : {socketedTrackers.Any(x => (bool)x.GetType().GetMethod("IsInFrustum", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(x, new object[] { camera.GetFrustumPlanes() }))}");
offset += 30f;
}
*/
2021-01-18 12:33:07 +00:00
offset = 10f;
GUI.Label(new Rect(440, offset, 200f, 20f), $"Players in QM :");
offset += 30f;
foreach (var player in QSBPlayerManager.PlayerList.Where(x => x.IsInMoon))
{
GUI.Label(new Rect(440, offset, 200f, 20f), $"- {player.PlayerId}");
offset += 30f;
}
GUI.Label(new Rect(440, offset, 200f, 20f), $"Players in Shrine :");
offset += 30f;
foreach (var player in QSBPlayerManager.PlayerList.Where(x => x.IsInShrine))
{
GUI.Label(new Rect(440, offset, 200f, 20f), $"- {player.PlayerId}");
2021-01-05 15:56:14 +00:00
offset += 30f;
2021-01-03 16:09:58 +00:00
}
}
public static bool IsVisibleUsingCameraFrustum(ShapeVisibilityTracker tracker, bool skipVisibilityCheck)
{
return tracker.gameObject.activeInHierarchy
&& QSBPlayerManager.GetPlayerCameras(!skipVisibilityCheck)
.Any(x => (bool)tracker.GetType()
.GetMethod("IsInFrustum", BindingFlags.NonPublic | BindingFlags.Instance)
.Invoke(tracker, new object[] { x.GetFrustumPlanes() }));
}
public static bool IsVisible(ShapeVisibilityTracker tracker, bool skipVisibilityCheck)
{
return tracker.gameObject.activeInHierarchy
&& IsVisibleUsingCameraFrustum(tracker, skipVisibilityCheck)
&& QSBPlayerManager.GetPlayerCameras(!skipVisibilityCheck)
.Any(x => VisibilityOccluder.CanYouSee(tracker, x.mainCamera.transform.position));
}
2020-12-22 21:39:53 +00:00
public int GetId(SocketedQuantumObject obj) => _socketedQuantumObjects.IndexOf(obj);
2020-12-23 22:43:05 +00:00
public int GetId(MultiStateQuantumObject obj) => _multiStateQuantumObjects.IndexOf(obj);
2020-12-22 21:39:53 +00:00
public int GetId(QuantumSocket obj) => _quantumSockets.IndexOf(obj);
2021-01-01 10:17:35 +00:00
public int GetId(QuantumShuffleObject obj) => _quantumShuffleObjects.IndexOf(obj);
2020-12-22 21:39:53 +00:00
}
2020-12-23 10:43:13 +00:00
}