light sensors: set enabled message, too lazy to do auth stuff

This commit is contained in:
JohnCorby 2022-03-18 04:16:48 -07:00
parent 8dbd884403
commit 82666dff2d
3 changed files with 83 additions and 2 deletions

View File

@ -0,0 +1,10 @@
using QSB.EchoesOfTheEye.LightSensorSync.WorldObjects;
using QSB.Messaging;
namespace QSB.EchoesOfTheEye.LightSensorSync.Messages;
internal class SetEnabledMessage : QSBWorldObjectMessage<QSBLightSensor, bool>
{
public SetEnabledMessage(bool data) : base(data) { }
public override void OnReceiveRemote() => WorldObject.SetEnabled(Data);
}

View File

@ -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<QSBLightSensor>().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<QSBLightSensor>().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)

View File

@ -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<SingleLightSensor>
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;
}
}
}
}