From 82666dff2dbefc793e871dc56d2cf28b05228674 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Fri, 18 Mar 2022 04:16:48 -0700 Subject: [PATCH] light sensors: set enabled message, too lazy to do auth stuff --- .../Messages/SetEnabledMessage.cs | 10 +++++ .../Patches/LightSensorPatches.cs | 41 +++++++++++++++++++ .../WorldObjects/QSBLightSensor.cs | 34 ++++++++++++++- 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 QSB/EchoesOfTheEye/LightSensorSync/Messages/SetEnabledMessage.cs diff --git a/QSB/EchoesOfTheEye/LightSensorSync/Messages/SetEnabledMessage.cs b/QSB/EchoesOfTheEye/LightSensorSync/Messages/SetEnabledMessage.cs new file mode 100644 index 00000000..df03e1ca --- /dev/null +++ b/QSB/EchoesOfTheEye/LightSensorSync/Messages/SetEnabledMessage.cs @@ -0,0 +1,10 @@ +using QSB.EchoesOfTheEye.LightSensorSync.WorldObjects; +using QSB.Messaging; + +namespace QSB.EchoesOfTheEye.LightSensorSync.Messages; + +internal class SetEnabledMessage : QSBWorldObjectMessage +{ + public SetEnabledMessage(bool data) : base(data) { } + public override void OnReceiveRemote() => WorldObject.SetEnabled(Data); +} diff --git a/QSB/EchoesOfTheEye/LightSensorSync/Patches/LightSensorPatches.cs b/QSB/EchoesOfTheEye/LightSensorSync/Patches/LightSensorPatches.cs index 02fff681..079932a3 100644 --- a/QSB/EchoesOfTheEye/LightSensorSync/Patches/LightSensorPatches.cs +++ b/QSB/EchoesOfTheEye/LightSensorSync/Patches/LightSensorPatches.cs @@ -1,5 +1,7 @@ using HarmonyLib; +using QSB.EchoesOfTheEye.LightSensorSync.Messages; using QSB.EchoesOfTheEye.LightSensorSync.WorldObjects; +using QSB.Messaging; using QSB.Patches; using QSB.Tools.FlashlightTool; using QSB.Tools.ProbeTool; @@ -13,6 +15,45 @@ internal class LightSensorPatches : QSBPatch { public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect; + [HarmonyPrefix] + [HarmonyPatch(nameof(SingleLightSensor.OnSectorOccupantsUpdated))] + private static bool OnSectorOccupantsUpdated(SingleLightSensor __instance) + { + if (!QSBWorldSync.AllObjectsReady) + { + return true; + } + + var flag = __instance._sector.ContainsAnyOccupants(DynamicOccupant.Player | DynamicOccupant.Probe); + if (flag && !__instance.enabled) + { + __instance.enabled = true; + __instance.GetWorldObject().SendMessage(new SetEnabledMessage(true)); + __instance._lightDetector.GetShape().enabled = true; + if (__instance._preserveStateWhileDisabled) + { + __instance._fixedUpdateFrameDelayCount = 10; + } + } + else if (!flag && __instance.enabled) + { + __instance.enabled = false; + __instance.GetWorldObject().SendMessage(new SetEnabledMessage(false)); + __instance._lightDetector.GetShape().enabled = false; + if (!__instance._preserveStateWhileDisabled) + { + if (__instance._illuminated) + { + __instance.OnDetectDarkness.Invoke(); + } + + __instance._illuminated = false; + } + } + + return false; + } + [HarmonyPrefix] [HarmonyPatch(nameof(SingleLightSensor.UpdateIllumination))] private static bool UpdateIllumination(SingleLightSensor __instance) diff --git a/QSB/EchoesOfTheEye/LightSensorSync/WorldObjects/QSBLightSensor.cs b/QSB/EchoesOfTheEye/LightSensorSync/WorldObjects/QSBLightSensor.cs index 9c684700..52dc4535 100644 --- a/QSB/EchoesOfTheEye/LightSensorSync/WorldObjects/QSBLightSensor.cs +++ b/QSB/EchoesOfTheEye/LightSensorSync/WorldObjects/QSBLightSensor.cs @@ -1,4 +1,6 @@ -using QSB.WorldSync; +using QSB.EchoesOfTheEye.LightSensorSync.Messages; +using QSB.Messaging; +using QSB.WorldSync; using System; namespace QSB.EchoesOfTheEye.LightSensorSync.WorldObjects; @@ -10,5 +12,33 @@ internal class QSBLightSensor : WorldObject public Action OnDetectLocalLight; public Action OnDetectLocalDarkness; - public override void SendInitialState(uint to) { } + public override void SendInitialState(uint to) => + this.SendMessage(new SetEnabledMessage(AttachedObject.enabled)); + + public void SetEnabled(bool enabled) + { + if (enabled && !AttachedObject.enabled) + { + AttachedObject.enabled = true; + AttachedObject._lightDetector.GetShape().enabled = true; + if (AttachedObject._preserveStateWhileDisabled) + { + AttachedObject._fixedUpdateFrameDelayCount = 10; + } + } + else if (!enabled && AttachedObject.enabled) + { + AttachedObject.enabled = false; + AttachedObject._lightDetector.GetShape().enabled = false; + if (!AttachedObject._preserveStateWhileDisabled) + { + if (AttachedObject._illuminated) + { + AttachedObject.OnDetectDarkness.Invoke(); + } + + AttachedObject._illuminated = false; + } + } + } }