use sector instead of shapes

This commit is contained in:
_nebula 2023-01-21 13:43:47 +00:00
parent 7b69b649cb
commit ee00128835
3 changed files with 32 additions and 66 deletions

View File

@ -1,21 +0,0 @@
using HarmonyLib;
using QSB.Patches;
using QSB.Utility;
namespace QSB.QuantumSync.Patches.Common.Visibility;
[HarmonyPatch(typeof(Shape))]
internal class VisibilityShapePatches : QSBPatch
{
public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect;
[HarmonyPostfix]
[HarmonyPatch(nameof(Shape.OnEnable))]
public static void OnEnable(Shape __instance)
=> __instance.RaiseEvent(nameof(__instance.OnShapeActivated), __instance);
[HarmonyPostfix]
[HarmonyPatch(nameof(Shape.OnDisable))]
public static void OnDisable(Shape __instance)
=> __instance.RaiseEvent(nameof(__instance.OnShapeDeactivated), __instance);
}

View File

@ -38,11 +38,11 @@ internal abstract class QSBQuantumObject<T> : WorldObject<T>, IQSBQuantumObject
return;
}
foreach (var shape in GetAttachedShapes())
/*foreach (var shape in GetAttachedShapes())
{
shape.OnShapeActivated -= OnEnable;
shape.OnShapeDeactivated -= OnDisable;
}
}*/
}
public override async UniTask Init(CancellationToken ct)
@ -64,11 +64,13 @@ internal abstract class QSBQuantumObject<T> : WorldObject<T>, IQSBQuantumObject
return;
}
foreach (var shape in attachedShapes)
/*foreach (var shape in attachedShapes)
{
shape.OnShapeActivated += OnEnable;
shape.OnShapeDeactivated += OnDisable;
}
}*/
AttachedObject._sector.OnSectorOccupantsUpdated += OnSectorOccupantsUpdated;
if (attachedShapes.All(x => x.enabled && x.gameObject.activeInHierarchy && x.active))
{
@ -81,6 +83,21 @@ internal abstract class QSBQuantumObject<T> : WorldObject<T>, IQSBQuantumObject
}
}
private void OnSectorOccupantsUpdated()
{
var playerInSector = AttachedObject._sector.ContainsAnyOccupants(DynamicOccupant.Player);
DebugLog.DebugWrite($"{ObjectId} {AttachedObject.name} SectorOccupantsUpdated : playerInSector:{playerInSector}");
if (!IsEnabled && playerInSector && ControllingPlayer == 0)
{
RequestAuthority();
}
else if (IsEnabled && !playerInSector && ControllingPlayer == QSBPlayerManager.LocalPlayerId)
{
ReleaseAuthority();
}
}
public override void SendInitialState(uint to) =>
((IQSBQuantumObject)this).SendMessage(new QuantumAuthorityMessage(ControllingPlayer) { To = to });
@ -124,58 +141,29 @@ internal abstract class QSBQuantumObject<T> : WorldObject<T>, IQSBQuantumObject
public VisibilityObject GetVisibilityObject() => AttachedObject;
private void OnEnable(Shape s)
private void RequestAuthority()
{
if (IsEnabled)
{
return;
}
IsEnabled = true;
if (!QSBWorldSync.AllObjectsReady && !QSBCore.IsHost)
{
return;
}
if (ControllingPlayer != 0)
{
// controlled by another player, dont care that we activate it
return;
}
// 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(() =>
private void ReleaseAuthority()
{
IsEnabled = false;
if (!QSBWorldSync.AllObjectsReady && !QSBCore.IsHost)
{
if (!IsEnabled)
{
return;
}
return;
}
if (GetAttachedShapes().Any(x => x.isActiveAndEnabled))
{
return;
}
IsEnabled = false;
if (!QSBWorldSync.AllObjectsReady && !QSBCore.IsHost)
{
return;
}
if (ControllingPlayer != QSBPlayerManager.LocalPlayerId)
{
// not being controlled by us, don't care if we leave area
return;
}
// send event to other players that we're releasing authority
((IQSBQuantumObject)this).SendMessage(new QuantumAuthorityMessage(0u));
});
// send event to other players that we're releasing authority
((IQSBQuantumObject)this).SendMessage(new QuantumAuthorityMessage(0u));
}
public void OnTakeProbeSnapshot(PlayerInfo player, ProbeCamera.ID cameraId)
{

View File

@ -2,6 +2,7 @@
using QSB.EchoesOfTheEye.Ghosts.WorldObjects;
using QSB.Player;
using QSB.Player.TransformSync;
using QSB.QuantumSync.WorldObjects;
using QSB.ShipSync;
using QSB.ShipSync.TransformSync;
using QSB.ShipSync.WorldObjects;
@ -296,7 +297,6 @@ internal class DebugGUI : MonoBehaviour, IAddComponentOnStart
}
}
/*
#region Column4 - Quantum Object Possesion
foreach (var player in QSBPlayerManager.PlayerList)
@ -335,7 +335,6 @@ internal class DebugGUI : MonoBehaviour, IAddComponentOnStart
}
#endregion
*/
}
private static void DrawWorldObjectLabels()