ownership stuff

This commit is contained in:
JohnCorby 2022-11-17 01:49:06 -08:00
parent bb6a2eb432
commit d92a711582
5 changed files with 57 additions and 116 deletions

View File

@ -1,12 +0,0 @@
using QSB.EchoesOfTheEye.AlarmTotemSync.WorldObjects;
using QSB.Messaging;
namespace QSB.EchoesOfTheEye.AlarmTotemSync.Messages;
public class TotemEnabledMessage : QSBWorldObjectMessage<QSBAlarmTotem, bool>
{
public TotemEnabledMessage(bool enabled) : base(enabled) { }
public override void OnReceiveRemote() =>
WorldObject.SetEnabled(Data);
}

View File

@ -1,16 +0,0 @@
using QSB.EchoesOfTheEye.AlarmTotemSync.WorldObjects;
using QSB.Messaging;
using System.Collections.Generic;
namespace QSB.EchoesOfTheEye.AlarmTotemSync.Messages;
public class TotemVisibleForMessage : QSBWorldObjectMessage<QSBAlarmTotem, List<uint>>
{
public TotemVisibleForMessage(List<uint> visibleFor) : base(visibleFor) { }
public override void OnReceiveRemote()
{
WorldObject.VisibleFor.Clear();
WorldObject.VisibleFor.AddRange(Data);
}
}

View File

@ -1,11 +0,0 @@
using QSB.EchoesOfTheEye.AlarmTotemSync.WorldObjects;
using QSB.Messaging;
namespace QSB.EchoesOfTheEye.AlarmTotemSync.Messages;
public class TotemVisibleMessage : QSBWorldObjectMessage<QSBAlarmTotem, bool>
{
public TotemVisibleMessage(bool visible) : base(visible) { }
public override void OnReceiveLocal() => OnReceiveRemote();
public override void OnReceiveRemote() => WorldObject.SetVisible(From, Data);
}

View File

@ -1,9 +1,11 @@
using HarmonyLib;
using QSB.AuthoritySync;
using QSB.EchoesOfTheEye.AlarmTotemSync.Messages;
using QSB.EchoesOfTheEye.AlarmTotemSync.WorldObjects;
using QSB.Messaging;
using QSB.Patches;
using QSB.Player;
using QSB.Utility;
using QSB.WorldSync;
using UnityEngine;
@ -13,6 +15,60 @@ public class AlarmTotemPatches : QSBPatch
{
public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect;
[HarmonyPrefix]
[HarmonyPatch(typeof(AlarmTotem), nameof(AlarmTotem.OnSectorOccupantAdded))]
private static bool OnSectorOccupantAdded(AlarmTotem __instance, SectorDetector sectorDetector)
{
if (!QSBWorldSync.AllObjectsReady)
{
return true;
}
var qsbAlarmTotem = __instance.GetWorldObject<QSBAlarmTotem>();
if (sectorDetector.GetOccupantType() == DynamicOccupant.Player)
{
__instance.enabled = true;
qsbAlarmTotem.RequestOwnership();
}
return false;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(AlarmTotem), nameof(AlarmTotem.OnSectorOccupantRemoved))]
private static bool OnSectorOccupantRemoved(AlarmTotem __instance, SectorDetector sectorDetector)
{
if (!QSBWorldSync.AllObjectsReady)
{
return true;
}
var qsbAlarmTotem = __instance.GetWorldObject<QSBAlarmTotem>();
if (sectorDetector.GetOccupantType() == DynamicOccupant.Player)
{
__instance.enabled = false;
qsbAlarmTotem.ReleaseOwnership();
Delay.RunFramesLater(10, () =>
{
// no one else took ownership, so we can safely turn stuff off
// ie turn off when no one else is there
if (qsbAlarmTotem.Owner == 0)
{
__instance._pulseLightController.SetIntensity(0f);
__instance._simTotemMaterials[0] = __instance._origSimEyeMaterial;
__instance._simTotemRenderer.sharedMaterials = __instance._simTotemMaterials;
__instance._simVisionConeRenderer.SetColor(__instance._simVisionConeRenderer.GetOriginalColor());
if (__instance._isPlayerVisible)
{
__instance._isPlayerVisible = false;
__instance._secondsConcealed = 0f;
Locator.GetAlarmSequenceController().DecreaseAlarmCounter();
}
}
});
}
return false;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(AlarmTotem), nameof(AlarmTotem.CheckPlayerVisible))]
private static bool CheckPlayerVisible(AlarmTotem __instance, out bool __result)

View File

@ -1,11 +1,4 @@
using Cysharp.Threading.Tasks;
using QSB.AuthoritySync;
using QSB.EchoesOfTheEye.AlarmTotemSync.Messages;
using QSB.Messaging;
using QSB.Player;
using QSB.WorldSync;
using System.Collections.Generic;
using System.Threading;
using QSB.AuthoritySync;
namespace QSB.EchoesOfTheEye.AlarmTotemSync.WorldObjects;
@ -15,73 +8,4 @@ namespace QSB.EchoesOfTheEye.AlarmTotemSync.WorldObjects;
public class QSBAlarmTotem : AuthWorldObject<AlarmTotem>
{
public override bool CanOwn => AttachedObject.enabled;
public readonly List<uint> VisibleFor = new();
public bool IsLocallyVisible;
public override void SendInitialState(uint to)
{
this.SendMessage(new TotemEnabledMessage(AttachedObject.enabled) { To = to });
this.SendMessage(new TotemVisibleForMessage(VisibleFor) { To = to });
}
public override async UniTask Init(CancellationToken ct) =>
QSBPlayerManager.OnRemovePlayer += OnPlayerLeave;
public override void OnRemoval() =>
QSBPlayerManager.OnRemovePlayer -= OnPlayerLeave;
private void OnPlayerLeave(PlayerInfo player) =>
VisibleFor.QuickRemove(player.PlayerId);
public void SetVisible(uint playerId, bool visible)
{
if (visible)
{
VisibleFor.SafeAdd(playerId);
}
else
{
VisibleFor.QuickRemove(playerId);
}
}
public void SetEnabled(bool enabled)
{
if (AttachedObject.enabled == enabled)
{
return;
}
if (!enabled &&
AttachedObject._sector &&
AttachedObject._sector.ContainsOccupant(DynamicOccupant.Player))
{
// local player is in sector, do not disable
return;
}
AttachedObject.enabled = enabled;
if (!enabled)
{
AttachedObject._simTotemMaterials[0] = AttachedObject._origSimEyeMaterial;
AttachedObject._simTotemRenderer.sharedMaterials = AttachedObject._simTotemMaterials;
AttachedObject._simVisionConeRenderer.SetColor(AttachedObject._simVisionConeRenderer.GetOriginalColor());
AttachedObject._pulseLightController.SetIntensity(0f);
/*
if (AttachedObject._isPlayerVisible)
{
AttachedObject._isPlayerVisible = false;
Locator.GetAlarmSequenceController().DecreaseAlarmCounter();
}
*/
if (IsLocallyVisible)
{
IsLocallyVisible = false;
this.SendMessage(new TotemVisibleMessage(false));
}
}
}
}