125 lines
3.7 KiB
C#
Raw Normal View History

2021-01-31 16:14:17 +00:00
using OWML.Common;
2021-12-22 14:46:37 -08:00
using QSB.Messaging;
2021-01-05 15:56:14 +00:00
using QSB.Player;
2021-12-23 17:07:29 -08:00
using QSB.QuantumSync.Messages;
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 15:32:47 +01: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;
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 override WorldObjectType WorldObjectType => WorldObjectType.Both;
public static QuantumShrine Shrine { get; private set; }
2020-12-22 21:39:53 +00:00
public void Awake() => QSBPlayerManager.OnRemovePlayer += PlayerLeave;
2020-12-23 22:43:05 +00:00
public override void BuildWorldObjects(OWScene scene)
2020-12-22 21:39:53 +00:00
{
DebugLog.DebugWrite("Building quantum objects...", MessageType.Info);
QSBWorldSync.Init<QSBQuantumState, QuantumState>();
QSBWorldSync.Init<QSBSocketedQuantumObject, SocketedQuantumObject>();
QSBWorldSync.Init<QSBMultiStateQuantumObject, MultiStateQuantumObject>();
QSBWorldSync.Init<QSBQuantumSocket, QuantumSocket>();
QSBWorldSync.Init<QSBQuantumShuffleObject, QuantumShuffleObject>();
QSBWorldSync.Init<QSBQuantumMoon, QuantumMoon>();
QSBWorldSync.Init<QSBEyeProxyQuantumMoon, EyeProxyQuantumMoon>();
2022-01-07 21:24:39 -08:00
QSBWorldSync.Init<QSBQuantumSkeletonTower, QuantumSkeletonTower>();
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
}
public void PlayerLeave(PlayerInfo player)
2021-02-14 12:47:40 +00:00
{
2021-08-08 19:53:55 +01:00
if (!QSBCore.IsHost)
2021-03-23 13:18:29 +00:00
{
return;
}
2021-06-18 22:38:32 +01:00
2021-12-22 14:46:37 -08:00
foreach (var obj in QSBWorldSync.GetWorldObjects<IQSBQuantumObject>())
2021-02-14 12:47:40 +00:00
{
if (obj.ControllingPlayer == player.PlayerId)
2021-02-14 12:47:40 +00:00
{
obj.SendMessage(new QuantumAuthorityMessage(obj.IsEnabled ? QSBPlayerManager.LocalPlayerId : 0u));
2021-02-14 12:47:40 +00:00
}
}
}
2021-01-26 14:07:17 +00:00
public void OnRenderObject()
2021-01-03 16:09:58 +00:00
{
if (!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-05-19 21:43:29 +01:00
public static Tuple<bool, List<PlayerInfo>> IsVisibleUsingCameraFrustum(ShapeVisibilityTracker tracker, bool ignoreLocalCamera)
{
if (!QSBWorldSync.AllObjectsReady)
2021-07-11 16:18:47 +01: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 11:24:13 +01:00
DebugLog.ToConsole($"Warning - Could not find any players with cameras!", MessageType.Warning);
2021-07-11 16:18:47 +01:00
return new Tuple<bool, List<PlayerInfo>>(false, new List<PlayerInfo>());
2021-03-25 20:56:26 +00:00
}
2021-06-18 22:38:32 +01:00
2021-03-25 20:56:26 +00:00
if (!tracker.gameObject.activeInHierarchy)
{
2021-07-11 16:18:47 +01:00
return new Tuple<bool, List<PlayerInfo>>(false, new List<PlayerInfo>());
2021-03-25 20:56:26 +00:00
}
2021-06-18 22:38:32 +01:00
2021-05-19 21:43:29 +01: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 22:38:32 +01:00
2021-12-26 20:42:07 -08:00
var isInFrustum = tracker.IsInFrustum(player.Camera.GetFrustumPlanes());
2021-03-25 20:56:26 +00:00
if (isInFrustum)
{
2021-05-19 21:43:29 +01:00
playersWhoCanSee.Add(player);
foundPlayers = true;
2021-03-25 20:56:26 +00:00
}
}
2021-05-19 21:43:29 +01:00
return new Tuple<bool, List<PlayerInfo>>(foundPlayers, playersWhoCanSee);
}
2021-07-12 22:02:50 +01:00
public static bool IsVisible(ShapeVisibilityTracker tracker, bool ignoreLocalCamera) => tracker.gameObject.activeInHierarchy
&& IsVisibleUsingCameraFrustum(tracker, ignoreLocalCamera).Item1
&& QSBPlayerManager.GetPlayersWithCameras(!ignoreLocalCamera)
.Any(x => VisibilityOccluder.CanYouSee(tracker, x.Camera.mainCamera.transform.position));
public static IEnumerable<PlayerInfo> GetEntangledPlayers(QuantumObject obj)
{
if (!QSBWorldSync.AllObjectsReady)
{
return Enumerable.Empty<PlayerInfo>();
}
var worldObj = obj.GetWorldObject<IQSBQuantumObject>();
return QSBPlayerManager.PlayerList.Where(x => x.EntangledObject == worldObj);
}
2020-12-22 21:39:53 +00:00
}
2021-12-22 18:37:24 -08:00
}