185 lines
6.3 KiB
C#
Raw Normal View History

2021-11-08 20:55:53 +00:00
using HarmonyLib;
2022-02-26 00:02:14 -08:00
using QSB.EchoesOfTheEye.LightSensorSync.WorldObjects;
2021-11-08 20:55:53 +00:00
using QSB.Patches;
using QSB.Tools.FlashlightTool;
2022-03-07 15:02:10 -08:00
using QSB.Tools.ProbeTool;
2022-02-26 00:02:14 -08:00
using QSB.WorldSync;
2021-11-08 20:55:53 +00:00
using UnityEngine;
2022-03-02 19:46:33 -08:00
namespace QSB.EchoesOfTheEye.LightSensorSync.Patches;
[HarmonyPatch(typeof(SingleLightSensor))]
2022-03-02 19:46:33 -08:00
internal class LightSensorPatches : QSBPatch
2021-11-08 20:55:53 +00:00
{
2022-03-02 19:46:33 -08:00
public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect;
[HarmonyPrefix]
[HarmonyPatch(nameof(SingleLightSensor.UpdateIllumination))]
private static bool UpdateIllumination(SingleLightSensor __instance)
2021-11-08 20:55:53 +00:00
{
2022-03-02 19:46:33 -08:00
if (!QSBWorldSync.AllObjectsReady)
{
return true;
}
2022-02-26 00:02:14 -08:00
2022-03-02 19:46:33 -08:00
var qsbLightSensor = __instance.GetWorldObject<QSBLightSensor>();
var illuminatedByLocal = qsbLightSensor.IlluminatedByLocal;
qsbLightSensor.IlluminatedByLocal = false;
2022-03-02 19:46:33 -08:00
__instance._illuminated = false;
__instance._illuminatingDreamLanternList?.Clear();
if (__instance._lightSources == null || __instance._lightSources.Count == 0)
{
if (illuminatedByLocal)
{
qsbLightSensor.OnDetectLocalDarkness?.Invoke();
}
return false;
2022-03-02 19:46:33 -08:00
}
2021-11-08 20:55:53 +00:00
2022-03-02 19:46:33 -08:00
var vector = __instance.transform.TransformPoint(__instance._localSensorOffset);
var sensorWorldDir = Vector3.zero;
if (__instance._directionalSensor)
{
sensorWorldDir = __instance.transform.TransformDirection(__instance._localDirection).normalized;
}
2021-11-08 20:55:53 +00:00
foreach (var lightSource in __instance._lightSources)
2022-03-02 19:46:33 -08:00
{
if ((__instance._lightSourceMask & lightSource.GetLightSourceType()) == lightSource.GetLightSourceType() &&
2022-03-07 15:02:10 -08:00
lightSource.CheckIlluminationAtPoint(vector, __instance._sensorRadius, __instance._maxDistance))
2021-11-08 20:55:53 +00:00
{
var lightSourceType = lightSource.GetLightSourceType();
2022-03-02 19:46:33 -08:00
switch (lightSourceType)
2021-11-08 20:55:53 +00:00
{
2022-03-02 19:46:33 -08:00
case LightSourceType.UNDEFINED:
{
var owlight = lightSource as OWLight2;
var occludableLight = owlight.GetLight().shadows != LightShadows.None && owlight.GetLight().shadowStrength > 0.5f;
if (owlight.CheckIlluminationAtPoint(vector, __instance._sensorRadius, __instance._maxDistance)
2022-03-07 15:02:10 -08:00
&& !__instance.CheckOcclusion(owlight.transform.position, vector, sensorWorldDir, occludableLight))
2021-11-08 20:55:53 +00:00
{
2022-03-02 19:46:33 -08:00
__instance._illuminated = true;
qsbLightSensor.IlluminatedByLocal = true;
2022-03-02 19:46:33 -08:00
}
2021-11-08 20:55:53 +00:00
2022-03-02 19:46:33 -08:00
break;
}
case LightSourceType.FLASHLIGHT:
{
2022-03-07 15:02:10 -08:00
if (lightSource is Flashlight)
2022-03-02 19:46:33 -08:00
{
2022-03-07 15:02:10 -08:00
var position = Locator.GetPlayerCamera().transform.position;
var to = __instance.transform.position - position;
if (Vector3.Angle(Locator.GetPlayerCamera().transform.forward, to) <= __instance._maxSpotHalfAngle
&& !__instance.CheckOcclusion(position, vector, sensorWorldDir))
{
__instance._illuminated = true;
qsbLightSensor.IlluminatedByLocal = true;
}
}
else if (lightSource is QSBFlashlight qsbFlashlight)
{
var playerCamera = qsbFlashlight.Player.Camera;
2022-03-07 15:08:33 -08:00
2022-03-07 15:02:10 -08:00
var position = playerCamera.transform.position;
var to = __instance.transform.position - position;
if (Vector3.Angle(playerCamera.transform.forward, to) <= __instance._maxSpotHalfAngle
&& !__instance.CheckOcclusion(position, vector, sensorWorldDir))
{
__instance._illuminated = true;
}
}
2022-03-02 19:46:33 -08:00
2021-11-08 20:55:53 +00:00
break;
2022-03-02 19:46:33 -08:00
}
2022-03-07 15:02:10 -08:00
case LightSourceType.PROBE:
2022-03-02 19:46:33 -08:00
{
2022-03-07 15:02:10 -08:00
if (lightSource is SurveyorProbe probe)
2021-11-08 20:55:53 +00:00
{
2022-03-07 15:02:10 -08:00
if (probe != null
&& probe.IsLaunched()
&& !probe.IsRetrieving()
&& probe.CheckIlluminationAtPoint(vector, __instance._sensorRadius, __instance._maxDistance)
&& !__instance.CheckOcclusion(probe.GetLightSourcePosition(), vector, sensorWorldDir))
{
__instance._illuminated = true;
qsbLightSensor.IlluminatedByLocal = true;
}
}
else if (lightSource is QSBProbe qsbProbe)
{
if (qsbProbe != null
&& qsbProbe.IsLaunched()
&& !qsbProbe.IsRetrieving()
&& qsbProbe.CheckIlluminationAtPoint(vector, __instance._sensorRadius, __instance._maxDistance)
&& !__instance.CheckOcclusion(qsbProbe.GetLightSourcePosition(), vector, sensorWorldDir))
{
__instance._illuminated = true;
}
}
2022-03-02 19:46:33 -08:00
break;
}
case LightSourceType.FLASHLIGHT | LightSourceType.PROBE:
case LightSourceType.FLASHLIGHT | LightSourceType.DREAM_LANTERN:
case LightSourceType.PROBE | LightSourceType.DREAM_LANTERN:
case LightSourceType.FLASHLIGHT | LightSourceType.PROBE | LightSourceType.DREAM_LANTERN:
break;
case LightSourceType.DREAM_LANTERN:
{
var dreamLanternController = lightSource as DreamLanternController;
2022-03-02 19:46:33 -08:00
if (dreamLanternController.IsLit()
2022-03-07 15:02:10 -08:00
&& dreamLanternController.IsFocused(__instance._lanternFocusThreshold)
&& dreamLanternController.CheckIlluminationAtPoint(vector, __instance._sensorRadius, __instance._maxDistance)
&& !__instance.CheckOcclusion(dreamLanternController.GetLightPosition(), vector, sensorWorldDir))
2021-11-08 20:55:53 +00:00
{
2022-03-02 19:46:33 -08:00
__instance._illuminatingDreamLanternList.Add(dreamLanternController);
__instance._illuminated = true;
qsbLightSensor.IlluminatedByLocal = true; // todo remote dream lanterns
2021-11-08 20:55:53 +00:00
}
2021-11-25 15:38:05 +00:00
2021-11-08 20:55:53 +00:00
break;
2022-03-02 19:46:33 -08:00
}
case LightSourceType.SIMPLE_LANTERN:
foreach (var owlight in lightSource.GetLights())
2022-03-02 19:46:33 -08:00
{
var occludableLight = owlight.GetLight().shadows != LightShadows.None && owlight.GetLight().shadowStrength > 0.5f;
2022-03-02 19:46:33 -08:00
var maxDistance = Mathf.Min(__instance._maxSimpleLanternDistance, __instance._maxDistance);
if (owlight.CheckIlluminationAtPoint(vector, __instance._sensorRadius, maxDistance)
2022-03-07 15:02:10 -08:00
&& !__instance.CheckOcclusion(owlight.transform.position, vector, sensorWorldDir, occludableLight))
2021-11-08 20:55:53 +00:00
{
__instance._illuminated = true;
2022-03-07 15:13:17 -08:00
qsbLightSensor.IlluminatedByLocal = true; // todo remote dream lanterns
2022-03-02 19:46:33 -08:00
break;
2021-11-08 20:55:53 +00:00
}
2022-03-02 19:46:33 -08:00
}
2021-11-25 15:38:05 +00:00
2022-03-02 19:46:33 -08:00
break;
default:
if (lightSourceType == LightSourceType.VOLUME_ONLY)
{
__instance._illuminated = true;
qsbLightSensor.IlluminatedByLocal = true;
2022-03-02 19:46:33 -08:00
}
break;
2021-11-08 20:55:53 +00:00
}
}
}
2022-03-02 19:46:33 -08:00
if (qsbLightSensor.IlluminatedByLocal && !illuminatedByLocal)
{
qsbLightSensor.OnDetectLocalLight?.Invoke();
}
else if (!qsbLightSensor.IlluminatedByLocal && illuminatedByLocal)
{
qsbLightSensor.OnDetectLocalDarkness?.Invoke();
}
2022-03-02 19:46:33 -08:00
return false;
2021-11-08 20:55:53 +00:00
}
}