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

159 lines
4.7 KiB
C#
Raw Normal View History

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;
using QSB.WorldSync;
2021-10-12 14:32:47 +00:00
using System;
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
{
2021-03-23 13:18:29 +00:00
internal class QuantumManager : WorldObjectManager
2020-12-22 21:39:53 +00:00
{
public static QuantumShrine Shrine { get; private set; }
2020-12-22 21:39:53 +00:00
public static QuantumManager Instance { get; private set; }
2021-03-23 13:18:29 +00:00
public override void Awake()
2020-12-22 21:39:53 +00:00
{
2021-03-23 13:18:29 +00:00
base.Awake();
2020-12-22 21:39:53 +00:00
Instance = this;
2021-03-23 13:18:29 +00:00
QSBPlayerManager.OnRemovePlayer += PlayerLeave;
2020-12-22 21:39:53 +00:00
}
2021-03-23 13:18:29 +00:00
public override void OnDestroy()
{
base.OnDestroy();
QSBPlayerManager.OnRemovePlayer -= PlayerLeave;
}
2020-12-23 22:43:05 +00:00
2021-03-23 13:18:29 +00:00
protected override void RebuildWorldObjects(OWScene scene)
2020-12-22 21:39:53 +00:00
{
2021-08-12 13:18:35 +00:00
DebugLog.DebugWrite("Rebuilding quantum objects...", MessageType.Info);
2021-03-18 13:35:11 +00:00
QSBWorldSync.Init<QSBQuantumState, QuantumState>();
2021-03-18 11:09:19 +00:00
QSBWorldSync.Init<QSBSocketedQuantumObject, SocketedQuantumObject>();
QSBWorldSync.Init<QSBMultiStateQuantumObject, MultiStateQuantumObject>();
2021-02-18 15:36:11 +00:00
QSBWorldSync.Init<QSBQuantumSocket, QuantumSocket>();
QSBWorldSync.Init<QSBQuantumShuffleObject, QuantumShuffleObject>();
2021-02-26 14:19:42 +00:00
QSBWorldSync.Init<QSBQuantumMoon, QuantumMoon>();
2021-03-18 11:09:19 +00:00
QSBWorldSync.Init<QSBEyeProxyQuantumMoon, EyeProxyQuantumMoon>();
2021-01-26 14:07:17 +00:00
if (scene == OWScene.SolarSystem)
{
Shrine = QSBWorldSync.GetUnityObjects<QuantumShrine>().First();
2021-01-26 14:07:17 +00:00
}
2020-12-22 21:39:53 +00:00
}
2021-03-23 13:18:29 +00:00
public void PlayerLeave(uint playerId)
2021-02-14 12:47:40 +00:00
{
2021-08-08 18:53:55 +00:00
if (!QSBCore.IsHost)
2021-03-23 13:18:29 +00:00
{
return;
}
2021-06-18 21:38:32 +00:00
2021-02-14 12:47:40 +00:00
var quantumObjects = QSBWorldSync.GetWorldObjects<IQSBQuantumObject>().ToList();
for (var i = 0; i < quantumObjects.Count; i++)
{
var obj = quantumObjects[i];
2021-03-23 13:18:29 +00:00
if (obj.ControllingPlayer == playerId)
2021-02-14 12:47:40 +00:00
{
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
{
if (!WorldObjectManager.AllObjectsReady || !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-11-07 09:52:03 +00:00
foreach (var quantumObject in QSBWorldSync.GetWorldObjects<IQSBQuantumObject>())
{
if (quantumObject.ControllingPlayer == 0)
{
2021-12-02 12:38:38 +00:00
if (quantumObject.IsEnabled)
{
Popcron.Gizmos.Line(quantumObject.ReturnObject().transform.position,
2021-12-02 12:38:38 +00:00
QSBPlayerManager.LocalPlayer.Body.transform.position,
Color.magenta * 0.25f);
}
2021-11-07 09:52:03 +00:00
continue;
}
Popcron.Gizmos.Line(quantumObject.ReturnObject().transform.position,
2021-11-07 09:52:03 +00:00
QSBPlayerManager.GetPlayer(quantumObject.ControllingPlayer).Body.transform.position,
Color.magenta);
}
2021-01-26 14:07:17 +00:00
}
2021-05-19 20:43:29 +00:00
public static Tuple<bool, List<PlayerInfo>> IsVisibleUsingCameraFrustum(ShapeVisibilityTracker tracker, bool ignoreLocalCamera)
{
if (!AllObjectsReady)
2021-07-11 15:18:47 +00:00
{
return new Tuple<bool, List<PlayerInfo>>(false, new List<PlayerInfo>());
}
2021-03-25 20:56:26 +00:00
var playersWithCameras = QSBPlayerManager.GetPlayersWithCameras(!ignoreLocalCamera);
if (playersWithCameras.Count == 0)
{
2021-09-02 10:24:13 +00:00
DebugLog.ToConsole($"Warning - Could not find any players with cameras!", MessageType.Warning);
2021-07-11 15:18:47 +00:00
return new Tuple<bool, List<PlayerInfo>>(false, new List<PlayerInfo>());
2021-03-25 20:56:26 +00:00
}
2021-06-18 21:38:32 +00:00
2021-03-25 20:56:26 +00:00
if (!tracker.gameObject.activeInHierarchy)
{
2021-07-11 15:18:47 +00:00
return new Tuple<bool, List<PlayerInfo>>(false, new List<PlayerInfo>());
2021-03-25 20:56:26 +00:00
}
2021-06-18 21:38:32 +00:00
2021-03-25 20:56:26 +00:00
var frustumMethod = tracker.GetType().GetMethod("IsInFrustum", BindingFlags.NonPublic | BindingFlags.Instance);
2021-05-19 20:43:29 +00:00
var playersWhoCanSee = new List<PlayerInfo>();
var foundPlayers = false;
2021-03-25 20:56:26 +00:00
foreach (var player in playersWithCameras)
{
if (player.Camera == null)
{
DebugLog.ToConsole($"Warning - Camera is null for id:{player.PlayerId}!", MessageType.Warning);
continue;
}
2021-06-18 21:38:32 +00:00
2021-03-25 20:56:26 +00:00
var isInFrustum = (bool)frustumMethod.Invoke(tracker, new object[] { player.Camera.GetFrustumPlanes() });
if (isInFrustum)
{
2021-05-19 20:43:29 +00:00
playersWhoCanSee.Add(player);
foundPlayers = true;
2021-03-25 20:56:26 +00:00
}
}
2021-05-19 20:43:29 +00:00
return new Tuple<bool, List<PlayerInfo>>(foundPlayers, playersWhoCanSee);
}
2021-07-12 21:02:50 +00:00
public static bool IsVisible(ShapeVisibilityTracker tracker, bool ignoreLocalCamera) => tracker.gameObject.activeInHierarchy
2021-10-12 14:32:47 +00:00
&& IsVisibleUsingCameraFrustum(tracker, ignoreLocalCamera).Item1
2021-02-22 10:51:51 +00:00
&& QSBPlayerManager.GetPlayersWithCameras(!ignoreLocalCamera)
.Any(x => VisibilityOccluder.CanYouSee(tracker, x.Camera.mainCamera.transform.position));
2021-02-21 18:25:25 +00:00
public static IEnumerable<PlayerInfo> GetEntangledPlayers(QuantumObject obj)
2021-02-21 21:27:54 +00:00
{
if (!WorldObjectManager.AllObjectsReady)
{
return Enumerable.Empty<PlayerInfo>();
}
2021-06-18 21:38:32 +00:00
var worldObj = QSBWorldSync.GetWorldFromUnity<IQSBQuantumObject>(obj);
2021-02-21 21:27:54 +00:00
return QSBPlayerManager.PlayerList.Where(x => x.EntangledObject == worldObj);
}
2020-12-22 21:39:53 +00:00
}
}