234 lines
5.0 KiB
C#
Raw Normal View History

2022-01-28 20:49:07 -08:00
using Cysharp.Threading.Tasks;
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;
using System.Collections.Generic;
using System.Linq;
2022-01-28 20:49:07 -08:00
using System.Threading;
using UnityEngine;
2021-01-26 14:07:17 +00:00
2022-03-02 19:46:33 -08:00
namespace QSB.QuantumSync.WorldObjects;
internal abstract class QSBQuantumObject<T> : WorldObject<T>, IQSBQuantumObject
where T : QuantumObject
2021-01-26 14:07:17 +00:00
{
2022-03-02 19:46:33 -08:00
/// <summary>
/// whether the controlling player is always the host <br/>
/// also means this object is considered always enabled
/// </summary>
protected virtual bool HostControls => false;
public uint ControllingPlayer { get; set; }
public bool IsEnabled { get; private set; }
public override void OnRemoval()
{
2022-03-02 19:46:33 -08:00
if (HostControls)
{
return;
}
2022-03-02 19:46:33 -08:00
foreach (var shape in GetAttachedShapes())
{
2022-03-02 19:46:33 -08:00
shape.OnShapeActivated -= OnEnable;
shape.OnShapeDeactivated -= OnDisable;
}
}
2021-02-19 09:40:46 +00:00
2022-03-02 19:46:33 -08:00
public override async UniTask Init(CancellationToken ct)
{
await UniTask.DelayFrame(5, cancellationToken: ct);
if (HostControls)
{
// first player is the host
ControllingPlayer = QSBPlayerManager.PlayerList[0].PlayerId;
IsEnabled = true;
return;
}
2022-03-02 19:46:33 -08:00
var attachedShapes = GetAttachedShapes();
if (attachedShapes.Count == 0)
{
2022-03-02 19:46:33 -08:00
IsEnabled = false;
return;
}
2022-03-02 19:46:33 -08:00
foreach (var shape in attachedShapes)
{
shape.OnShapeActivated += OnEnable;
shape.OnShapeDeactivated += OnDisable;
}
2021-12-02 12:38:38 +00:00
2022-03-02 19:46:33 -08:00
if (attachedShapes.All(x => x.enabled && x.gameObject.activeInHierarchy && x.active))
{
IsEnabled = true;
}
else
{
ControllingPlayer = 0u;
IsEnabled = false;
}
}
2022-01-06 14:57:53 -08:00
2022-03-02 19:46:33 -08:00
public override void SendInitialState(uint to) =>
((IQSBQuantumObject)this).SendMessage(new QuantumAuthorityMessage(ControllingPlayer) { To = to });
2021-02-19 09:40:46 +00:00
2022-03-02 19:46:33 -08:00
public List<Shape> GetAttachedShapes()
{
if (AttachedObject == null)
{
return new List<Shape>();
}
2022-01-21 15:13:16 -08:00
2022-03-02 19:46:33 -08:00
var visibilityTrackers = AttachedObject._visibilityTrackers;
if (visibilityTrackers == null || visibilityTrackers.Length == 0)
{
return new List<Shape>();
}
2021-06-18 22:38:32 +01:00
2022-03-02 19:46:33 -08:00
if (visibilityTrackers.Any(x => x.GetType() == typeof(RendererVisibilityTracker)))
{
2022-03-02 19:46:33 -08:00
DebugLog.ToConsole($"Warning - {AttachedObject.name} has a RendererVisibilityTracker!", MessageType.Warning);
return new List<Shape>();
}
2021-06-18 22:38:32 +01:00
2022-03-02 19:46:33 -08:00
var totalShapes = new List<Shape>();
foreach (ShapeVisibilityTracker tracker in visibilityTrackers)
{
if (tracker == null)
{
2022-03-02 19:46:33 -08:00
DebugLog.ToConsole($"Warning - a ShapeVisibilityTracker in {this} is null!", MessageType.Warning);
continue;
}
2021-06-18 22:38:32 +01:00
2022-03-02 19:46:33 -08:00
// if the tracker is not active, this won't have been set, so just do it ourselves
tracker._shapes ??= tracker.GetComponents<Shape>();
totalShapes.AddRange(tracker._shapes.Where(x => x != null));
}
2022-03-02 19:46:33 -08:00
return totalShapes;
}
public void SetIsQuantum(bool isQuantum) => AttachedObject._isQuantum = isQuantum;
2022-03-07 20:34:35 +00:00
public VisibilityObject GetVisibilityObject() => AttachedObject;
2022-03-02 19:46:33 -08:00
private void OnEnable(Shape s)
{
if (IsEnabled)
{
return;
}
2021-12-02 12:38:38 +00:00
2022-03-02 19:46:33 -08:00
IsEnabled = true;
if (!QSBWorldSync.AllObjectsReady && !QSBCore.IsHost)
{
return;
}
2022-03-02 19:46:33 -08:00
if (ControllingPlayer != 0)
{
2022-03-02 19:46:33 -08:00
// controlled by another player, dont care that we activate it
return;
}
2021-06-18 22:38:32 +01:00
2022-03-02 19:46:33 -08:00
// no one is controlling this object right now, request authority
((IQSBQuantumObject)this).SendMessage(new QuantumAuthorityMessage(QSBPlayerManager.LocalPlayerId));
}
private void OnDisable(Shape s) =>
// we wait a frame here in case the shapes get disabled as we switch from 1 visibility tracker to another
Delay.RunNextFrame(() =>
{
if (!IsEnabled)
2021-01-26 14:07:17 +00:00
{
return;
}
2021-06-18 22:38:32 +01:00
2022-03-02 19:46:33 -08:00
if (GetAttachedShapes().Any(x => x.isActiveAndEnabled))
2022-01-07 20:46:58 +00:00
{
return;
}
2022-03-02 19:46:33 -08:00
IsEnabled = false;
if (!QSBWorldSync.AllObjectsReady && !QSBCore.IsHost)
2022-01-07 20:46:58 +00:00
{
return;
}
2022-03-02 19:46:33 -08:00
if (ControllingPlayer != QSBPlayerManager.LocalPlayerId)
{
2022-03-02 19:46:33 -08:00
// not being controlled by us, don't care if we leave area
return;
}
2022-03-02 19:46:33 -08:00
// send event to other players that we're releasing authority
((IQSBQuantumObject)this).SendMessage(new QuantumAuthorityMessage(0u));
});
2022-03-02 19:46:33 -08:00
public override void DisplayLines()
{
if (AttachedObject == null)
{
return;
}
2022-01-07 20:46:58 +00:00
2022-03-02 19:46:33 -08:00
var localPlayer = QSBPlayerManager.LocalPlayer;
2022-03-02 19:46:33 -08:00
if (localPlayer == null)
{
return;
}
var body = localPlayer.Body;
2022-01-07 20:46:58 +00:00
2022-03-02 19:46:33 -08:00
if (body == null)
{
return;
}
2022-03-02 19:46:33 -08:00
if (ControllingPlayer == 0)
{
if (IsEnabled)
{
2022-03-02 19:46:33 -08:00
Popcron.Gizmos.Line(AttachedObject.transform.position,
body.transform.position,
Color.magenta * 0.25f);
}
2022-01-07 20:46:58 +00:00
2022-03-02 19:46:33 -08:00
return;
}
2022-03-02 19:46:33 -08:00
var player = QSBPlayerManager.GetPlayer(ControllingPlayer);
2022-03-02 19:46:33 -08:00
if (player == null)
{
return;
}
var playerBody = player.Body;
if (playerBody == null)
{
return;
}
2022-03-02 19:46:33 -08:00
Popcron.Gizmos.Line(AttachedObject.transform.position,
playerBody.transform.position,
Color.magenta);
2021-01-26 14:07:17 +00:00
}
2022-03-07 20:34:35 +00:00
public override string ReturnLabel()
{
var label = $"{this}\r\n";
foreach (var tracker in AttachedObject._visibilityTrackers)
{
label += $"{tracker.name}:\r\n IsVisible:{tracker.IsVisible()}\r\n VisibleUsingCamera:{tracker.IsVisibleUsingCameraFrustum()}\r\n";
}
return label;
}
}