361 lines
11 KiB
C#
Raw Normal View History

2021-11-10 09:18:57 +00:00
using HarmonyLib;
2021-11-09 20:35:55 -08:00
using QSB.Anglerfish.WorldObjects;
2021-11-10 00:48:29 -08:00
using QSB.Events;
2021-11-09 20:35:55 -08:00
using QSB.Patches;
using QSB.WorldSync;
2021-11-10 09:18:57 +00:00
using System;
2021-11-09 20:35:55 -08:00
using UnityEngine;
2021-11-09 17:56:45 -08:00
namespace QSB.Anglerfish.Patches
{
public class AnglerPatches : QSBPatch
{
public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect;
2021-11-09 20:35:55 -08:00
[HarmonyPrefix]
[HarmonyPatch(typeof(AnglerfishController), nameof(AnglerfishController.GetTargetPosition))]
public static bool GetTargetPosition(AnglerfishController __instance, ref Vector3 __result)
{
var qsbAngler = QSBWorldSync.GetWorldFromUnity<QSBAngler>(__instance);
2021-11-10 09:28:05 +00:00
if (qsbAngler == null || qsbAngler.TransformSync == null)
{
return false;
}
2021-11-10 09:28:05 +00:00
__result = qsbAngler.TargetTransform != null
? qsbAngler.TargetTransform.position
: __instance._brambleBody.transform.TransformPoint(__instance._localDisturbancePos);
return false;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(AnglerfishController), nameof(AnglerfishController.OnSectorOccupantRemoved))]
public static bool OnSectorOccupantRemoved(AnglerfishController __instance,
SectorDetector sectorDetector)
{
var qsbAngler = QSBWorldSync.GetWorldFromUnity<QSBAngler>(__instance);
2021-11-10 09:28:05 +00:00
if (qsbAngler == null || qsbAngler.TransformSync == null)
{
return false;
}
2021-11-10 09:28:05 +00:00
if (!(qsbAngler.TargetTransform != null) || !(sectorDetector.GetAttachedOWRigidbody().transform == qsbAngler.TargetTransform))
2021-11-10 09:18:57 +00:00
{
return false;
2021-11-10 09:18:57 +00:00
}
2021-11-10 09:28:05 +00:00
qsbAngler.TargetTransform = null;
return false;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(AnglerfishController), nameof(AnglerfishController.UpdateState))]
public static bool UpdateState(AnglerfishController __instance)
{
var qsbAngler = QSBWorldSync.GetWorldFromUnity<QSBAngler>(__instance);
2021-11-10 09:28:05 +00:00
if (qsbAngler == null || qsbAngler.TransformSync == null)
{
return false;
}
switch (__instance._currentState)
{
case AnglerfishController.AnglerState.Investigating:
if ((__instance._brambleBody.transform.TransformPoint(__instance._localDisturbancePos) - __instance._anglerBody.GetPosition()).sqrMagnitude >= __instance._arrivalDistance * (double)__instance._arrivalDistance)
2021-11-10 09:18:57 +00:00
{
break;
2021-11-10 09:18:57 +00:00
}
__instance.ChangeState(AnglerfishController.AnglerState.Lurking);
2021-11-10 00:48:29 -08:00
QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler);
break;
2021-11-10 09:28:05 +00:00
case AnglerfishController.AnglerState.Chasing:
2021-11-10 09:28:05 +00:00
if (qsbAngler.TargetTransform == null)
{
__instance.ChangeState(AnglerfishController.AnglerState.Lurking);
2021-11-10 00:48:29 -08:00
QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler);
break;
}
2021-11-10 09:28:05 +00:00
if ((qsbAngler.TargetTransform.position - __instance._anglerBody.GetPosition()).sqrMagnitude <= __instance._escapeDistance * (double)__instance._escapeDistance)
2021-11-10 09:18:57 +00:00
{
break;
2021-11-10 09:18:57 +00:00
}
2021-11-10 09:28:05 +00:00
qsbAngler.TargetTransform = null;
__instance.ChangeState(AnglerfishController.AnglerState.Lurking);
2021-11-10 00:48:29 -08:00
QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler);
break;
2021-11-10 09:28:05 +00:00
case AnglerfishController.AnglerState.Consuming:
if (__instance._consumeComplete)
2021-11-10 09:18:57 +00:00
{
break;
2021-11-10 09:18:57 +00:00
}
2021-11-10 09:28:05 +00:00
if (qsbAngler.TargetTransform == null)
{
__instance.ChangeState(AnglerfishController.AnglerState.Lurking);
2021-11-10 00:48:29 -08:00
QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler);
break;
}
2021-11-10 09:28:05 +00:00
var num = Time.time - __instance._consumeStartTime;
2021-11-10 09:28:05 +00:00
if (qsbAngler.TargetTransform.CompareTag("Player") && num > (double)__instance._consumeDeathDelay)
{
2021-11-10 01:39:00 -08:00
qsbAngler.TargetTransform = null;
__instance.ChangeState(AnglerfishController.AnglerState.Lurking);
QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler);
__instance.ApplyDrag(1f);
2021-11-10 01:39:00 -08:00
Locator.GetDeathManager().KillPlayer(DeathType.Digestion);
__instance._consumeComplete = true;
break;
}
2021-11-10 09:28:05 +00:00
if (!qsbAngler.TargetTransform.CompareTag("Ship"))
2021-11-10 09:18:57 +00:00
{
break;
2021-11-10 09:18:57 +00:00
}
if (num > (double)__instance._consumeShipCrushDelay)
2021-11-10 09:18:57 +00:00
{
2021-11-10 09:28:05 +00:00
qsbAngler.TargetTransform.GetComponentInChildren<ShipDamageController>().TriggerSystemFailure();
2021-11-10 09:18:57 +00:00
}
if (num <= (double)__instance._consumeDeathDelay)
2021-11-10 09:18:57 +00:00
{
break;
2021-11-10 09:18:57 +00:00
}
if (PlayerState.IsInsideShip())
2021-11-10 09:18:57 +00:00
{
2021-11-10 01:39:00 -08:00
qsbAngler.TargetTransform = null;
__instance.ChangeState(AnglerfishController.AnglerState.Lurking);
QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler);
__instance.ApplyDrag(1f);
2021-11-10 01:39:00 -08:00
Locator.GetDeathManager().KillPlayer(DeathType.Digestion);
2021-11-10 09:18:57 +00:00
}
__instance._consumeComplete = true;
break;
2021-11-10 09:28:05 +00:00
case AnglerfishController.AnglerState.Stunned:
__instance._stunTimer -= Time.deltaTime;
if (__instance._stunTimer > 0.0)
2021-11-10 09:18:57 +00:00
{
break;
2021-11-10 09:18:57 +00:00
}
2021-11-10 09:28:05 +00:00
if (qsbAngler.TargetTransform != null)
{
__instance.ChangeState(AnglerfishController.AnglerState.Chasing);
2021-11-10 00:48:29 -08:00
QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler);
break;
}
__instance.ChangeState(AnglerfishController.AnglerState.Lurking);
2021-11-10 00:48:29 -08:00
QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler);
break;
}
return false;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(AnglerfishController), nameof(AnglerfishController.UpdateMovement))]
public static bool UpdateMovement(AnglerfishController __instance)
{
var qsbAngler = QSBWorldSync.GetWorldFromUnity<QSBAngler>(__instance);
2021-11-10 09:28:05 +00:00
if (qsbAngler == null || qsbAngler.TransformSync == null)
{
return false;
}
qsbAngler.FixedUpdate();
if (__instance._anglerBody.GetVelocity().sqrMagnitude > (double)Mathf.Pow(__instance._chaseSpeed * 1.5f, 2f))
2021-11-10 09:18:57 +00:00
{
__instance.ApplyDrag(10f);
2021-11-10 09:18:57 +00:00
}
switch (__instance._currentState)
{
case AnglerfishController.AnglerState.Lurking:
__instance.ApplyDrag(1f);
break;
2021-11-10 09:28:05 +00:00
case AnglerfishController.AnglerState.Investigating:
var targetPos = __instance._brambleBody.transform.TransformPoint(__instance._localDisturbancePos);
__instance.RotateTowardsTarget(targetPos, __instance._turnSpeed, __instance._turnSpeed);
if (__instance._turningInPlace)
2021-11-10 09:18:57 +00:00
{
break;
2021-11-10 09:18:57 +00:00
}
__instance.MoveTowardsTarget(targetPos, __instance._investigateSpeed, __instance._acceleration);
break;
2021-11-10 09:28:05 +00:00
case AnglerfishController.AnglerState.Chasing:
var velocity = qsbAngler.TargetVelocity;
var normalized = velocity.normalized;
2021-11-10 09:28:05 +00:00
var from = __instance._anglerBody.GetPosition() + __instance.transform.TransformDirection(__instance._mouthOffset) - qsbAngler.TargetTransform.position;
var magnitude1 = velocity.magnitude;
var num1 = Vector3.Angle(from, normalized);
var a = magnitude1 * 2f;
var num2 = a;
if (num1 < 90.0)
{
var magnitude2 = (double)from.magnitude;
var num3 = (float)magnitude2 * Mathf.Sin(num1 * ((float)Math.PI / 180f));
var num4 = (float)magnitude2 * Mathf.Cos(num1 * ((float)Math.PI / 180f));
var magnitude3 = __instance._anglerBody.GetVelocity().magnitude;
var num5 = num4 / Mathf.Max(magnitude1, 0.0001f) / (num3 / Mathf.Max(magnitude3, 0.0001f));
if (num5 <= 1.0)
{
var t = Mathf.Clamp01(num5);
num2 = Mathf.Lerp(a, num4, t);
}
else
{
var num6 = Mathf.InverseLerp(1f, 4f, num5);
num2 = Mathf.Lerp(num4, 0.0f, num6 * num6);
}
}
2021-11-10 09:28:05 +00:00
2021-11-20 19:49:50 +00:00
__instance._targetPos = qsbAngler.TargetTransform.position + (normalized * num2);
__instance.RotateTowardsTarget(__instance._targetPos, __instance._turnSpeed, __instance._quickTurnSpeed);
if (__instance._turningInPlace)
2021-11-10 09:18:57 +00:00
{
break;
2021-11-10 09:18:57 +00:00
}
__instance.MoveTowardsTarget(__instance._targetPos, __instance._chaseSpeed, __instance._acceleration);
break;
2021-11-10 09:28:05 +00:00
case AnglerfishController.AnglerState.Consuming:
__instance.ApplyDrag(1f);
break;
2021-11-10 09:28:05 +00:00
case AnglerfishController.AnglerState.Stunned:
__instance.ApplyDrag(0.5f);
break;
}
return false;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(AnglerfishController), nameof(AnglerfishController.OnImpact))]
public static bool OnImpact(AnglerfishController __instance,
ImpactData impact)
{
var qsbAngler = QSBWorldSync.GetWorldFromUnity<QSBAngler>(__instance);
2021-11-10 09:28:05 +00:00
if (qsbAngler == null || qsbAngler.TransformSync == null)
{
return false;
}
var attachedOwRigidbody = impact.otherCollider.GetAttachedOWRigidbody();
2021-11-10 09:28:05 +00:00
if ((attachedOwRigidbody.CompareTag("Player") || attachedOwRigidbody.CompareTag("Ship"))
&& __instance._currentState != AnglerfishController.AnglerState.Consuming
&& __instance._currentState != AnglerfishController.AnglerState.Stunned)
{
2021-11-10 09:28:05 +00:00
qsbAngler.TargetTransform = attachedOwRigidbody.transform;
__instance.ChangeState(AnglerfishController.AnglerState.Chasing);
2021-11-10 00:48:29 -08:00
QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler);
}
return false;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(AnglerfishController), nameof(AnglerfishController.OnClosestAudibleNoise))]
public static bool OnClosestAudibleNoise(AnglerfishController __instance,
NoiseMaker noiseMaker)
{
var qsbAngler = QSBWorldSync.GetWorldFromUnity<QSBAngler>(__instance);
2021-11-10 09:28:05 +00:00
if (qsbAngler == null || qsbAngler.TransformSync == null)
{
return false;
}
2021-11-20 19:49:50 +00:00
if (__instance._currentState is AnglerfishController.AnglerState.Consuming
or AnglerfishController.AnglerState.Stunned)
2021-11-10 09:18:57 +00:00
{
return false;
2021-11-10 09:18:57 +00:00
}
if ((noiseMaker.GetNoiseOrigin() - __instance.transform.position).sqrMagnitude < __instance._pursueDistance * (double)__instance._pursueDistance)
{
2021-11-10 09:28:05 +00:00
if (!(qsbAngler.TargetTransform != noiseMaker.GetAttachedBody().transform))
2021-11-10 09:18:57 +00:00
{
return false;
2021-11-10 09:18:57 +00:00
}
2021-11-10 09:28:05 +00:00
qsbAngler.TargetTransform = noiseMaker.GetAttachedBody().transform;
if (__instance._currentState == AnglerfishController.AnglerState.Chasing)
2021-11-10 09:18:57 +00:00
{
return false;
2021-11-10 09:18:57 +00:00
}
__instance.ChangeState(AnglerfishController.AnglerState.Chasing);
2021-11-10 00:48:29 -08:00
QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler);
}
else
{
2021-11-20 19:49:50 +00:00
if (__instance._currentState is not AnglerfishController.AnglerState.Lurking
and not AnglerfishController.AnglerState.Investigating)
2021-11-10 09:18:57 +00:00
{
return false;
2021-11-10 09:18:57 +00:00
}
__instance._localDisturbancePos = __instance._brambleBody.transform.InverseTransformPoint(noiseMaker.GetNoiseOrigin());
if (__instance._currentState == AnglerfishController.AnglerState.Investigating)
2021-11-10 09:18:57 +00:00
{
return false;
2021-11-10 09:18:57 +00:00
}
__instance.ChangeState(AnglerfishController.AnglerState.Investigating);
2021-11-10 00:48:29 -08:00
QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler);
}
return false;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(AnglerfishController), nameof(AnglerfishController.OnCaughtObject))]
public static bool OnCaughtObject(AnglerfishController __instance,
OWRigidbody caughtBody)
{
var qsbAngler = QSBWorldSync.GetWorldFromUnity<QSBAngler>(__instance);
if (__instance._currentState == AnglerfishController.AnglerState.Consuming)
{
2021-11-10 09:28:05 +00:00
if (qsbAngler.TargetTransform.CompareTag("Player") || !caughtBody.CompareTag("Player"))
2021-11-10 09:18:57 +00:00
{
return false;
2021-11-10 09:18:57 +00:00
}
Locator.GetDeathManager().KillPlayer(DeathType.Digestion);
}
else
{
if (!caughtBody.CompareTag("Player") && !caughtBody.CompareTag("Ship"))
2021-11-10 09:18:57 +00:00
{
return false;
2021-11-10 09:18:57 +00:00
}
2021-11-10 09:28:05 +00:00
qsbAngler.TargetTransform = caughtBody.transform;
__instance._consumeStartTime = Time.time;
__instance.ChangeState(AnglerfishController.AnglerState.Consuming);
2021-11-10 00:48:29 -08:00
QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler);
}
2021-11-09 20:35:55 -08:00
return false;
}
2021-11-09 17:56:45 -08:00
}
}