2021-02-28 15:06:11 +00:00
|
|
|
|
using OWML.Common;
|
2021-12-23 17:07:29 -08:00
|
|
|
|
using QSB.Messaging;
|
2021-01-26 14:07:17 +00:00
|
|
|
|
using QSB.Player;
|
2021-12-23 17:07:29 -08:00
|
|
|
|
using QSB.QuantumSync.Messages;
|
2021-02-19 09:40:46 +00:00
|
|
|
|
using QSB.Utility;
|
2021-01-26 14:07:17 +00:00
|
|
|
|
using QSB.WorldSync;
|
2021-02-27 14:23:35 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2021-10-18 09:02:48 +01:00
|
|
|
|
using UnityEngine;
|
2021-01-26 14:07:17 +00:00
|
|
|
|
|
|
|
|
|
namespace QSB.QuantumSync.WorldObjects
|
|
|
|
|
{
|
2021-01-29 15:30:40 +00:00
|
|
|
|
internal abstract class QSBQuantumObject<T> : WorldObject<T>, IQSBQuantumObject
|
2021-02-27 14:23:35 +00:00
|
|
|
|
where T : QuantumObject
|
2021-01-26 14:07:17 +00:00
|
|
|
|
{
|
2021-01-26 17:08:02 +00:00
|
|
|
|
public uint ControllingPlayer { get; set; }
|
2021-01-30 10:09:27 +00:00
|
|
|
|
public bool IsEnabled { get; set; }
|
2021-01-26 14:07:17 +00:00
|
|
|
|
|
2021-02-19 09:40:46 +00:00
|
|
|
|
public override void OnRemoval()
|
|
|
|
|
{
|
2021-02-27 14:23:35 +00:00
|
|
|
|
foreach (var shape in GetAttachedShapes())
|
|
|
|
|
{
|
2021-12-02 12:38:38 +00:00
|
|
|
|
shape.OnShapeActivated -= OnEnable;
|
|
|
|
|
shape.OnShapeDeactivated -= OnDisable;
|
2021-02-27 14:23:35 +00:00
|
|
|
|
}
|
2021-02-19 09:40:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-11 14:38:54 -08:00
|
|
|
|
public override void Init()
|
2021-02-19 09:40:46 +00:00
|
|
|
|
{
|
2021-10-18 09:02:48 +01:00
|
|
|
|
var debugBundle = QSBCore.DebugAssetBundle;
|
2021-11-24 22:16:56 +00:00
|
|
|
|
var sphere = debugBundle.LoadAsset<GameObject>("Assets/Prefabs/Sphere.prefab");
|
|
|
|
|
var cube = debugBundle.LoadAsset<GameObject>("Assets/Prefabs/Cube.prefab");
|
|
|
|
|
var capsule = debugBundle.LoadAsset<GameObject>("Assets/Prefabs/Capsule.prefab");
|
2021-10-18 09:02:48 +01:00
|
|
|
|
|
|
|
|
|
if (cube == null)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.DebugWrite($"CUBE IS NULL");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sphere == null)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.DebugWrite($"SPHERE IS NULL");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (capsule == null)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.DebugWrite($"CAPSULE IS NULL");
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-27 14:23:35 +00:00
|
|
|
|
foreach (var shape in GetAttachedShapes())
|
|
|
|
|
{
|
|
|
|
|
if (shape == null)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-10-18 09:02:48 +01:00
|
|
|
|
|
2021-11-25 22:44:15 +00:00
|
|
|
|
if (QSBCore.ShowQuantumVisibilityObjects)
|
2021-10-18 09:02:48 +01:00
|
|
|
|
{
|
2021-10-20 09:21:26 +01:00
|
|
|
|
if (shape is BoxShape boxShape)
|
|
|
|
|
{
|
|
|
|
|
var newCube = UnityEngine.Object.Instantiate(cube);
|
|
|
|
|
newCube.transform.parent = shape.transform;
|
|
|
|
|
newCube.transform.localPosition = Vector3.zero;
|
|
|
|
|
newCube.transform.localRotation = Quaternion.Euler(0, 0, 0);
|
|
|
|
|
newCube.transform.localScale = boxShape.size;
|
|
|
|
|
}
|
|
|
|
|
else if (shape is SphereShape sphereShape)
|
|
|
|
|
{
|
|
|
|
|
var newSphere = UnityEngine.Object.Instantiate(sphere);
|
|
|
|
|
newSphere.transform.parent = shape.transform;
|
|
|
|
|
newSphere.transform.localPosition = Vector3.zero;
|
|
|
|
|
newSphere.transform.localRotation = Quaternion.Euler(0, 0, 0);
|
|
|
|
|
newSphere.transform.localScale = Vector3.one * (sphereShape.radius * 2);
|
|
|
|
|
}
|
|
|
|
|
else if (shape is CapsuleShape capsuleShape)
|
|
|
|
|
{
|
|
|
|
|
var newCapsule = Object.Instantiate(capsule);
|
|
|
|
|
newCapsule.transform.parent = shape.transform;
|
|
|
|
|
newCapsule.transform.localPosition = Vector3.zero;
|
|
|
|
|
newCapsule.transform.localRotation = Quaternion.Euler(0, 0, 0);
|
|
|
|
|
newCapsule.transform.localScale = new Vector3(capsuleShape.radius * 2, capsuleShape.height, capsuleShape.radius * 2);
|
|
|
|
|
}
|
2021-10-18 09:02:48 +01:00
|
|
|
|
}
|
2021-03-17 17:04:57 +00:00
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2021-12-11 15:30:35 -08:00
|
|
|
|
StartDelayedReady();
|
2021-12-02 12:38:38 +00:00
|
|
|
|
QSBCore.UnityEvents.FireInNUpdates(LateInit, 5);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LateInit()
|
|
|
|
|
{
|
2021-12-11 15:30:35 -08:00
|
|
|
|
FinishDelayedReady();
|
2021-12-02 12:38:38 +00:00
|
|
|
|
foreach (var shape in GetAttachedShapes())
|
|
|
|
|
{
|
|
|
|
|
shape.OnShapeActivated += OnEnable;
|
|
|
|
|
shape.OnShapeDeactivated += OnDisable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var attachedShapes = GetAttachedShapes();
|
|
|
|
|
|
|
|
|
|
if (attachedShapes.Count == 0)
|
2021-03-17 17:04:57 +00:00
|
|
|
|
{
|
|
|
|
|
IsEnabled = false;
|
2021-12-02 12:38:38 +00:00
|
|
|
|
return;
|
2021-03-17 17:04:57 +00:00
|
|
|
|
}
|
2021-12-02 12:38:38 +00:00
|
|
|
|
|
|
|
|
|
if (attachedShapes.All(x => x.enabled && x.gameObject.activeInHierarchy && x.active))
|
2021-03-17 17:04:57 +00:00
|
|
|
|
{
|
|
|
|
|
IsEnabled = true;
|
2021-02-27 14:23:35 +00:00
|
|
|
|
}
|
2021-12-02 12:38:38 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ControllingPlayer = 0u;
|
|
|
|
|
IsEnabled = false;
|
|
|
|
|
}
|
2021-02-19 09:40:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-07 15:56:08 +00:00
|
|
|
|
public List<ShapeVisibilityTracker> GetVisibilityTrackers()
|
2021-12-02 12:38:38 +00:00
|
|
|
|
=> AttachedObject?._visibilityTrackers == null
|
|
|
|
|
? new()
|
|
|
|
|
: AttachedObject._visibilityTrackers.Select(x => (ShapeVisibilityTracker)x).ToList();
|
|
|
|
|
|
|
|
|
|
public List<Shape> GetAttachedShapes()
|
2021-02-27 14:23:35 +00:00
|
|
|
|
{
|
2021-02-28 15:06:11 +00:00
|
|
|
|
if (AttachedObject == null)
|
|
|
|
|
{
|
|
|
|
|
return new List<Shape>();
|
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2021-12-02 12:38:38 +00:00
|
|
|
|
var visibilityTrackers = AttachedObject._visibilityTrackers;
|
2021-02-27 14:23:35 +00:00
|
|
|
|
if (visibilityTrackers == null || visibilityTrackers.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
return new List<Shape>();
|
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2021-02-27 14:23:35 +00:00
|
|
|
|
if (visibilityTrackers.Any(x => x.GetType() == typeof(RendererVisibilityTracker)))
|
|
|
|
|
{
|
2021-02-28 15:06:11 +00:00
|
|
|
|
DebugLog.ToConsole($"Warning - {AttachedObject.name} has a RendererVisibilityTracker!", MessageType.Warning);
|
2021-02-27 14:23:35 +00:00
|
|
|
|
return new List<Shape>();
|
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2021-02-27 14:23:35 +00:00
|
|
|
|
var totalShapes = new List<Shape>();
|
2021-12-02 12:38:38 +00:00
|
|
|
|
foreach (ShapeVisibilityTracker tracker in visibilityTrackers)
|
2021-02-27 14:23:35 +00:00
|
|
|
|
{
|
2021-12-02 12:38:38 +00:00
|
|
|
|
if (tracker == null)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.ToConsole($"Warning - a ShapeVisibilityTracker in {LogName} is null!", MessageType.Warning);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var shapes = tracker._shapes;
|
2021-02-27 14:23:35 +00:00
|
|
|
|
totalShapes.AddRange(shapes);
|
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2021-02-27 14:23:35 +00:00
|
|
|
|
return totalShapes;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 17:04:57 +00:00
|
|
|
|
private void OnEnable(Shape s)
|
2021-01-26 14:07:17 +00:00
|
|
|
|
{
|
2021-12-02 12:38:38 +00:00
|
|
|
|
if (IsEnabled)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-30 10:09:27 +00:00
|
|
|
|
IsEnabled = true;
|
2021-12-04 09:51:27 +00:00
|
|
|
|
if (!WorldObjectManager.AllObjectsReady && !QSBCore.IsHost)
|
2021-02-14 09:43:36 +00:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2021-01-26 23:41:53 +00:00
|
|
|
|
if (ControllingPlayer != 0)
|
2021-01-26 14:07:17 +00:00
|
|
|
|
{
|
2021-01-26 23:41:53 +00:00
|
|
|
|
// controlled by another player, dont care that we activate it
|
2021-01-26 14:07:17 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2021-01-26 23:41:53 +00:00
|
|
|
|
// no one is controlling this object right now, request authority
|
2021-12-22 18:31:58 -08:00
|
|
|
|
((IQSBQuantumObject)this).SendMessage(new QuantumAuthorityMessage(QSBPlayerManager.LocalPlayerId));
|
2021-01-26 14:07:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 17:04:57 +00:00
|
|
|
|
private void OnDisable(Shape s)
|
2021-01-26 14:07:17 +00:00
|
|
|
|
{
|
2021-02-27 14:23:35 +00:00
|
|
|
|
if (!IsEnabled)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2021-04-28 16:28:02 +01:00
|
|
|
|
if (GetAttachedShapes().Any(x => x.isActiveAndEnabled))
|
2021-03-17 17:04:57 +00:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2021-01-30 10:09:27 +00:00
|
|
|
|
IsEnabled = false;
|
2021-12-04 09:51:27 +00:00
|
|
|
|
if (!WorldObjectManager.AllObjectsReady && !QSBCore.IsHost)
|
2021-02-14 09:43:36 +00:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2021-01-26 14:07:17 +00:00
|
|
|
|
if (ControllingPlayer != QSBPlayerManager.LocalPlayerId)
|
|
|
|
|
{
|
|
|
|
|
// not being controlled by us, don't care if we leave area
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2021-12-11 21:36:41 -08:00
|
|
|
|
var id = ObjectId;
|
2021-01-26 14:07:17 +00:00
|
|
|
|
// send event to other players that we're releasing authority
|
2021-12-22 18:31:58 -08:00
|
|
|
|
((IQSBQuantumObject)this).SendMessage(new QuantumAuthorityMessage(0u));
|
2021-01-26 14:07:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|