totem: simplify

This commit is contained in:
JohnCorby 2022-04-02 02:26:44 -07:00
parent 630e13f831
commit e62f6d57fc
2 changed files with 38 additions and 72 deletions

View File

@ -61,29 +61,48 @@ public class AlarmTotemPatches : QSBPatch
/// </summary>
[HarmonyPrefix]
[HarmonyPatch(typeof(AlarmTotem), nameof(AlarmTotem.FixedUpdate))]
private static void FixedUpdate(AlarmTotem __instance)
{
if (!QSBWorldSync.AllObjectsReady)
{
return;
}
__instance.GetWorldObject<QSBAlarmTotem>().FixedUpdate();
}
/// <summary>
/// check for global visibility
/// </summary>
[HarmonyPrefix]
[HarmonyPatch(typeof(AlarmTotem), nameof(AlarmTotem.CheckPlayerVisible))]
private static bool CheckPlayerVisible(AlarmTotem __instance, ref bool __result)
private static bool FixedUpdate(AlarmTotem __instance)
{
if (!QSBWorldSync.AllObjectsReady)
{
return true;
}
__result = __instance.GetWorldObject<QSBAlarmTotem>().IsVisible();
var qsbAlarmTotem = __instance.GetWorldObject<QSBAlarmTotem>();
var isLocallyVisible = qsbAlarmTotem.IsLocallyVisible;
qsbAlarmTotem.IsLocallyVisible = __instance.CheckPlayerVisible();
if (qsbAlarmTotem.IsLocallyVisible && !isLocallyVisible)
{
qsbAlarmTotem.SendMessage(new SetVisibleMessage(true));
}
else if (isLocallyVisible && !qsbAlarmTotem.IsLocallyVisible)
{
qsbAlarmTotem.SendMessage(new SetVisibleMessage(false));
}
var isPlayerVisible = __instance._isPlayerVisible;
__instance._isPlayerVisible = qsbAlarmTotem.VisibleFor.Count > 0;
if (__instance._isPlayerVisible && !isPlayerVisible)
{
Locator.GetAlarmSequenceController().IncreaseAlarmCounter();
__instance._simTotemMaterials[0] = __instance._simAlarmMaterial;
__instance._simTotemRenderer.sharedMaterials = __instance._simTotemMaterials;
__instance._simVisionConeRenderer.SetColor(__instance._simAlarmColor);
if (__instance._isTutorialTotem)
{
GlobalMessenger.FireEvent("TutorialAlarmTotemTriggered");
}
}
else if (isPlayerVisible && !__instance._isPlayerVisible)
{
Locator.GetAlarmSequenceController().DecreaseAlarmCounter();
__instance._simTotemMaterials[0] = __instance._origSimEyeMaterial;
__instance._simTotemRenderer.sharedMaterials = __instance._simTotemMaterials;
__instance._simVisionConeRenderer.SetColor(__instance._simVisionConeRenderer.GetOriginalColor());
__instance._pulseLightController.FadeTo(0f, 0.5f);
}
return false;
}
}

View File

@ -2,7 +2,6 @@
using QSB.EchoesOfTheEye.AlarmTotemSync.Messages;
using QSB.Messaging;
using QSB.Player;
using QSB.Utility;
using QSB.WorldSync;
using System.Collections.Generic;
using System.Threading;
@ -12,6 +11,7 @@ namespace QSB.EchoesOfTheEye.AlarmTotemSync.WorldObjects;
public class QSBAlarmTotem : WorldObject<AlarmTotem>
{
public readonly List<uint> VisibleFor = new();
public bool IsLocallyVisible;
public override void SendInitialState(uint to)
{
@ -20,27 +20,15 @@ public class QSBAlarmTotem : WorldObject<AlarmTotem>
this.SendMessage(new VisibleForMessage(VisibleFor) { To = to });
}
public override async UniTask Init(CancellationToken ct)
{
public override async UniTask Init(CancellationToken ct) =>
QSBPlayerManager.OnRemovePlayer += OnPlayerLeave;
Delay.RunWhen(() => QSBWorldSync.AllObjectsReady, () =>
{
if (AttachedObject._isPlayerVisible)
{
this.SendMessage(new SetVisibleMessage(true));
}
});
}
public override void OnRemoval() =>
QSBPlayerManager.OnRemovePlayer -= OnPlayerLeave;
private void OnPlayerLeave(PlayerInfo player) =>
VisibleFor.QuickRemove(player.PlayerId);
public bool IsVisible() => VisibleFor.Count > 0;
public void SetVisible(uint playerId, bool visible)
{
if (visible)
@ -83,45 +71,4 @@ public class QSBAlarmTotem : WorldObject<AlarmTotem>
}
}
}
#region local visibility
private bool _isLocallyVisible;
public void FixedUpdate()
{
var isLocallyVisible = _isLocallyVisible;
_isLocallyVisible = CheckPlayerVisible();
if (_isLocallyVisible && !isLocallyVisible)
{
this.SendMessage(new SetVisibleMessage(true));
}
else if (isLocallyVisible && !_isLocallyVisible)
{
this.SendMessage(new SetVisibleMessage(false));
}
}
private bool CheckPlayerVisible()
{
if (!AttachedObject._isFaceOpen)
{
return false;
}
var lanternController = Locator.GetDreamWorldController().GetPlayerLantern().GetLanternController();
var playerLightSensor = Locator.GetPlayerLightSensor();
if (lanternController.IsHeldByPlayer() && !lanternController.IsConcealed() || playerLightSensor.IsIlluminated())
{
var position = Locator.GetPlayerCamera().transform.position;
if (AttachedObject.CheckPointInVisionCone(position) && !AttachedObject.CheckLineOccluded(AttachedObject._sightOrigin.position, position))
{
return true;
}
}
return false;
}
#endregion
}