This commit is contained in:
JohnCorby 2022-08-15 22:38:03 -07:00
parent 6382c571a6
commit 830580a4be
2 changed files with 44 additions and 120 deletions

View File

@ -1,11 +1,8 @@
using HarmonyLib; using HarmonyLib;
using QSB.EchoesOfTheEye.LightSensorSync.Messages;
using QSB.EchoesOfTheEye.LightSensorSync.WorldObjects; using QSB.EchoesOfTheEye.LightSensorSync.WorldObjects;
using QSB.Messaging;
using QSB.Patches; using QSB.Patches;
using QSB.WorldSync; using QSB.WorldSync;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using UnityEngine; using UnityEngine;
/* /*
@ -20,63 +17,18 @@ internal class LightSensorPatches : QSBPatch
{ {
public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect; public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect;
[HarmonyPrefix]
[HarmonyPatch(nameof(SingleLightSensor.Start))]
private static bool Start(SingleLightSensor __instance)
{
if (__instance._lightDetector != null)
{
__instance._lightSources = new List<ILightSource>();
__instance._lightSourceMask = LightSourceType.VOLUME_ONLY;
if (__instance._detectFlashlight)
{
__instance._lightSourceMask |= LightSourceType.FLASHLIGHT;
}
if (__instance._detectProbe)
{
__instance._lightSourceMask |= LightSourceType.PROBE;
}
if (__instance._detectDreamLanterns)
{
__instance._lightSourceMask |= LightSourceType.DREAM_LANTERN;
}
if (__instance._detectSimpleLanterns)
{
__instance._lightSourceMask |= LightSourceType.SIMPLE_LANTERN;
}
__instance._lightDetector.OnLightVolumeEnter += __instance.OnLightSourceEnter;
__instance._lightDetector.OnLightVolumeExit += __instance.OnLightSourceExit;
}
else
{
Debug.LogError("LightSensor has no LightSourceDetector", __instance);
}
if (__instance._sector != null)
{
__instance.enabled = false;
__instance._lightDetector.GetShape().enabled = false;
// dont do _startIlluminated stuff here since its done in the worldobject
// player light sensors dont have it either so its okay
}
return false;
}
[HarmonyPrefix] [HarmonyPrefix]
[HarmonyPatch(nameof(SingleLightSensor.OnSectorOccupantsUpdated))] [HarmonyPatch(nameof(SingleLightSensor.OnSectorOccupantsUpdated))]
private static bool OnSectorOccupantsUpdated(SingleLightSensor __instance) private static bool OnSectorOccupantsUpdated(SingleLightSensor __instance)
{ {
if (LightSensorManager.IsPlayerLightSensor(__instance))
{
return true;
}
if (!QSBWorldSync.AllObjectsReady) if (!QSBWorldSync.AllObjectsReady)
{ {
return true; return true;
} }
// player light sensors dont have a sector, so no need to worry about them
var qsbLightSensor = __instance.GetWorldObject<QSBLightSensor>(); var qsbLightSensor = __instance.GetWorldObject<QSBLightSensor>();
var containsAnyOccupants = __instance._sector.ContainsAnyOccupants(DynamicOccupant.Player | DynamicOccupant.Probe); var containsAnyOccupants = __instance._sector.ContainsAnyOccupants(DynamicOccupant.Player | DynamicOccupant.Probe);
@ -95,15 +47,13 @@ internal class LightSensorPatches : QSBPatch
__instance._lightDetector.GetShape().enabled = false; __instance._lightDetector.GetShape().enabled = false;
if (!__instance._preserveStateWhileDisabled) if (!__instance._preserveStateWhileDisabled)
{ {
if (qsbLightSensor._locallyIlluminated) if (__instance._illuminated)
{ {
qsbLightSensor._locallyIlluminated = false; __instance.OnDetectDarkness.Invoke();
qsbLightSensor.OnDetectLocalDarkness?.Invoke();
qsbLightSensor.SendMessage(new SetIlluminatedMessage(false));
} }
__instance._illuminated = false;
} }
} }
return false; return false;
} }
@ -116,84 +66,32 @@ internal class LightSensorPatches : QSBPatch
[HarmonyPatch(nameof(SingleLightSensor.ManagedFixedUpdate))] [HarmonyPatch(nameof(SingleLightSensor.ManagedFixedUpdate))]
private static bool ManagedFixedUpdate(SingleLightSensor __instance) private static bool ManagedFixedUpdate(SingleLightSensor __instance)
{ {
if (LightSensorManager.IsPlayerLightSensor(__instance))
{
return true;
}
if (!QSBWorldSync.AllObjectsReady) if (!QSBWorldSync.AllObjectsReady)
{ {
return true; return true;
} }
var qsbLightSensor = __instance.GetWorldObject<QSBLightSensor>();
var isPlayerLightSensor = LightSensorManager.IsPlayerLightSensor(__instance);
var qsbPlayerLightSensor = isPlayerLightSensor ? __instance.GetComponent<QSBPlayerLightSensor>() : null;
var qsbLightSensor = isPlayerLightSensor ? null : __instance.GetWorldObject<QSBLightSensor>();
if (__instance._fixedUpdateFrameDelayCount > 0) if (__instance._fixedUpdateFrameDelayCount > 0)
{ {
__instance._fixedUpdateFrameDelayCount--; __instance._fixedUpdateFrameDelayCount--;
return false;
} }
if (__instance._illuminatingDreamLanternList != null)
{
_illuminatingDreamLanternList.Clear();
_illuminatingDreamLanternList.AddRange(__instance._illuminatingDreamLanternList);
}
var illuminated = __instance._illuminated; var illuminated = __instance._illuminated;
__instance.UpdateIllumination(); __instance.UpdateIllumination();
bool locallyIlluminated; if (!illuminated && __instance._illuminated)
if (isPlayerLightSensor)
{ {
locallyIlluminated = qsbPlayerLightSensor._locallyIlluminated; __instance.OnDetectLight.Invoke();
qsbPlayerLightSensor._locallyIlluminated = __instance._illuminated; return false;
} }
else if (illuminated && !__instance._illuminated)
{ {
locallyIlluminated = qsbLightSensor._locallyIlluminated; __instance.OnDetectDarkness.Invoke();
qsbLightSensor._locallyIlluminated = __instance._illuminated;
} }
__instance._illuminated = illuminated;
if (isPlayerLightSensor)
{
if (!locallyIlluminated && qsbPlayerLightSensor._locallyIlluminated)
{
qsbPlayerLightSensor._locallyIlluminated = true;
new PlayerSetIlluminatedMessage(qsbPlayerLightSensor.PlayerId, true).Send();
}
else if (locallyIlluminated && !qsbPlayerLightSensor._locallyIlluminated)
{
qsbPlayerLightSensor._locallyIlluminated = false;
new PlayerSetIlluminatedMessage(qsbPlayerLightSensor.PlayerId, false).Send();
}
}
else
{
if (!locallyIlluminated && qsbLightSensor._locallyIlluminated)
{
qsbLightSensor._locallyIlluminated = true;
qsbLightSensor.OnDetectLocalLight?.Invoke();
qsbLightSensor.SendMessage(new SetIlluminatedMessage(true));
}
else if (locallyIlluminated && !qsbLightSensor._locallyIlluminated)
{
qsbLightSensor._locallyIlluminated = false;
qsbLightSensor.OnDetectLocalDarkness?.Invoke();
qsbLightSensor.SendMessage(new SetIlluminatedMessage(false));
}
}
if (__instance._illuminatingDreamLanternList != null
&& !__instance._illuminatingDreamLanternList.SequenceEqual(_illuminatingDreamLanternList))
{
if (isPlayerLightSensor)
{
new PlayerIlluminatingLanternsMessage(qsbPlayerLightSensor.PlayerId, __instance._illuminatingDreamLanternList).Send();
}
else
{
qsbLightSensor.SendMessage(new IlluminatingLanternsMessage(__instance._illuminatingDreamLanternList));
}
}
return false; return false;
} }
@ -201,7 +99,18 @@ internal class LightSensorPatches : QSBPatch
[HarmonyPatch(nameof(SingleLightSensor.UpdateIllumination))] [HarmonyPatch(nameof(SingleLightSensor.UpdateIllumination))]
private static bool UpdateIllumination(SingleLightSensor __instance) private static bool UpdateIllumination(SingleLightSensor __instance)
{ {
if (LightSensorManager.IsPlayerLightSensor(__instance))
{
return true;
}
if (!QSBWorldSync.AllObjectsReady)
{
return true;
}
var qsbLightSensor = __instance.GetWorldObject<QSBLightSensor>();
__instance._illuminated = false; __instance._illuminated = false;
qsbLightSensor._locallyIlluminated = false;
__instance._illuminatingDreamLanternList?.Clear(); __instance._illuminatingDreamLanternList?.Clear();
if (__instance._lightSources == null || __instance._lightSources.Count == 0) if (__instance._lightSources == null || __instance._lightSources.Count == 0)
{ {

View File

@ -0,0 +1,15 @@
using HarmonyLib;
using QSB.Patches;
/*
* For those who come here,
* leave while you still can.
*/
namespace QSB.EchoesOfTheEye.LightSensorSync.Patches;
[HarmonyPatch(typeof(SingleLightSensor))]
internal class PlayerLightSensorPatches : QSBPatch
{
public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect;
}