quantum-space-buddies/QSB/OrbSync/Patches/OrbPatches.cs

135 lines
3.3 KiB
C#
Raw Normal View History

using HarmonyLib;
2022-02-13 12:15:43 +00:00
using QSB.AuthoritySync;
2021-12-23 09:50:09 +00:00
using QSB.Messaging;
2021-12-23 10:17:56 +00:00
using QSB.OrbSync.Messages;
2021-12-15 04:24:02 +00:00
using QSB.OrbSync.WorldObjects;
2020-12-14 19:31:31 +00:00
using QSB.Patches;
2021-12-15 04:24:02 +00:00
using QSB.WorldSync;
using UnityEngine;
2020-09-05 12:24:51 +00:00
2022-03-03 03:46:33 +00:00
namespace QSB.OrbSync.Patches;
[HarmonyPatch(typeof(NomaiInterfaceOrb))]
public class OrbPatches : QSBPatch
2020-09-05 12:24:51 +00:00
{
2022-03-03 03:46:33 +00:00
public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect;
2022-01-01 22:19:10 +00:00
2022-03-03 03:46:33 +00:00
[HarmonyPrefix]
[HarmonyPatch(nameof(NomaiInterfaceOrb.StartDragFromPosition))]
private static bool StartDragFromPosition(NomaiInterfaceOrb __instance, ref bool __result,
Vector3 manipPos)
{
if (!QSBWorldSync.AllObjectsReady)
{
2022-03-03 03:46:33 +00:00
return true;
}
2022-01-01 22:19:10 +00:00
2022-03-03 03:46:33 +00:00
if (__instance._orbBody.IsSuspended() || __instance._isBeingDragged)
{
__result = false;
return false;
}
2022-03-03 03:46:33 +00:00
if (__instance.RecentlyEnteredSlot())
{
__instance._loseFocusToStartDrag = true;
}
2022-02-14 00:57:27 +00:00
2022-03-03 03:46:33 +00:00
if (Vector3.Distance(manipPos, __instance.transform.position) < __instance._startDragDist)
{
if (!__instance._loseFocusToStartDrag)
{
2022-03-03 03:46:33 +00:00
__instance._isBeingDragged = true;
__instance._interactibleCollider.enabled = false;
if (__instance._orbAudio != null)
{
2022-03-03 03:46:33 +00:00
__instance._orbAudio.PlayStartDragClip();
}
2022-03-03 03:46:33 +00:00
var qsbOrb = __instance.GetWorldObject<QSBOrb>();
qsbOrb.SendMessage(new OrbDragMessage(true));
qsbOrb.TransformSync.netIdentity.UpdateAuthQueue(AuthQueueAction.Force);
}
2022-03-03 03:46:33 +00:00
}
else
{
__instance._loseFocusToStartDrag = false;
}
2021-12-14 05:18:16 +00:00
2022-03-03 03:46:33 +00:00
__result = __instance._isBeingDragged;
return false;
}
[HarmonyPrefix]
[HarmonyPatch(nameof(NomaiInterfaceOrb.CancelDrag))]
private static bool CancelDrag(NomaiInterfaceOrb __instance)
{
if (!QSBWorldSync.AllObjectsReady)
{
return true;
}
2022-01-01 22:19:10 +00:00
2022-03-03 03:46:33 +00:00
if (!__instance._isBeingDragged)
{
2022-03-03 03:46:33 +00:00
return false;
}
2022-02-14 00:57:27 +00:00
2022-03-03 03:46:33 +00:00
var qsbOrb = __instance.GetWorldObject<QSBOrb>();
if (!qsbOrb.TransformSync.hasAuthority)
{
return false;
}
2022-01-01 22:19:10 +00:00
2022-03-03 03:46:33 +00:00
qsbOrb.SendMessage(new OrbDragMessage(false));
return true;
}
2022-03-03 03:46:33 +00:00
[HarmonyPrefix]
[HarmonyPatch(nameof(NomaiInterfaceOrb.CheckSlotCollision))]
private static bool CheckSlotCollision(NomaiInterfaceOrb __instance,
bool playAudio)
{
if (!QSBWorldSync.AllObjectsReady)
{
return true;
2020-12-02 21:29:53 +00:00
}
2022-03-03 03:46:33 +00:00
var qsbOrb = __instance.GetWorldObject<QSBOrb>();
if (qsbOrb.TransformSync.hasAuthority)
{
2022-03-03 03:46:33 +00:00
if (__instance._occupiedSlot == null)
{
2022-03-03 03:46:33 +00:00
for (var slotIndex = 0; slotIndex < __instance._slots.Length; slotIndex++)
{
2022-03-03 03:46:33 +00:00
var slot = __instance._slots[slotIndex];
if (slot != null && slot.CheckOrbCollision(__instance))
{
2022-03-03 03:46:33 +00:00
__instance._occupiedSlot = slot;
__instance._enterSlotTime = Time.time;
if (slot.CancelsDragOnCollision())
{
2022-03-03 03:46:33 +00:00
__instance.CancelDrag();
2022-02-13 12:15:43 +00:00
}
2022-03-03 03:46:33 +00:00
if (playAudio && __instance._orbAudio != null && slot.GetPlayActivationAudio())
{
__instance._orbAudio.PlaySlotActivatedClip();
}
qsbOrb.SendMessage(new OrbSlotMessage(slotIndex, playAudio));
break;
}
}
}
2022-03-03 03:46:33 +00:00
else if ((!__instance._occupiedSlot.IsAttractive() || __instance._isBeingDragged) && !__instance._occupiedSlot.CheckOrbCollision(__instance))
{
__instance._occupiedSlot = null;
qsbOrb.SendMessage(new OrbSlotMessage(-1, playAudio));
}
}
2022-01-01 22:19:10 +00:00
2022-03-03 03:46:33 +00:00
__instance._owCollider.SetActivation(__instance._occupiedSlot == null || !__instance._occupiedSlot.IsAttractive() || __instance._isBeingDragged);
2022-03-03 03:46:33 +00:00
return false;
2020-12-02 21:29:53 +00:00
}
}