From 2232e889ab5ca1a820de73e97769468dabf5da7f Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Thu, 23 Dec 2021 15:57:41 -0800 Subject: [PATCH] angler change state --- .../Events/AnglerChangeStateEvent.cs | 67 ----------------- .../Events/AnglerChangeStateMessage.cs | 26 ------- .../Messages/AnglerChangeStateMessage.cs | 74 +++++++++++++++++++ QSB/Anglerfish/Patches/AnglerPatches.cs | 26 ++++--- QSB/Events/EventNames.cs | 1 - 5 files changed, 88 insertions(+), 106 deletions(-) delete mode 100644 QSB/Anglerfish/Events/AnglerChangeStateEvent.cs delete mode 100644 QSB/Anglerfish/Events/AnglerChangeStateMessage.cs create mode 100644 QSB/Anglerfish/Messages/AnglerChangeStateMessage.cs diff --git a/QSB/Anglerfish/Events/AnglerChangeStateEvent.cs b/QSB/Anglerfish/Events/AnglerChangeStateEvent.cs deleted file mode 100644 index f5d0673f..00000000 --- a/QSB/Anglerfish/Events/AnglerChangeStateEvent.cs +++ /dev/null @@ -1,67 +0,0 @@ -using QSB.Anglerfish.WorldObjects; -using QSB.Events; -using QSB.Player; -using QSB.WorldSync; -using UnityEngine; - -namespace QSB.Anglerfish.Events -{ - public class AnglerChangeStateEvent : QSBEvent - { - public override bool RequireWorldObjectsReady => true; - - public override void SetupListener() - => GlobalMessenger.AddListener(EventNames.QSBAnglerChangeState, Handler); - - public override void CloseListener() - => GlobalMessenger.RemoveListener(EventNames.QSBAnglerChangeState, Handler); - - private void Handler(QSBAngler qsbAngler) => SendEvent(CreateMessage(qsbAngler)); - - private AnglerChangeStateMessage CreateMessage(QSBAngler qsbAngler) => new() - { - ObjectId = qsbAngler.ObjectId, - EnumValue = qsbAngler.AttachedObject._currentState, - TargetId = TargetToId(qsbAngler.TargetTransform), - LocalDisturbancePos = qsbAngler.AttachedObject._localDisturbancePos - }; - - public override void OnReceiveRemote(bool isHost, AnglerChangeStateMessage message) - { - var qsbAngler = QSBWorldSync.GetWorldFromId(message.ObjectId); - qsbAngler.TargetTransform = IdToTarget(message.TargetId); - qsbAngler.AttachedObject._localDisturbancePos = message.LocalDisturbancePos; - qsbAngler.AttachedObject.ChangeState(message.EnumValue); - } - - private static uint TargetToId(Transform transform) - { - if (transform == null) - { - return uint.MaxValue; - } - - if (transform == Locator.GetShipTransform()) - { - return uint.MaxValue - 1; - } - - return QSBPlayerManager.LocalPlayerId; - } - - private static Transform IdToTarget(uint id) - { - if (id == uint.MaxValue) - { - return null; - } - - if (id == uint.MaxValue - 1) - { - return Locator.GetShipTransform(); - } - - return QSBPlayerManager.GetPlayer(id).Body.transform; - } - } -} diff --git a/QSB/Anglerfish/Events/AnglerChangeStateMessage.cs b/QSB/Anglerfish/Events/AnglerChangeStateMessage.cs deleted file mode 100644 index 0615153d..00000000 --- a/QSB/Anglerfish/Events/AnglerChangeStateMessage.cs +++ /dev/null @@ -1,26 +0,0 @@ -using QSB.WorldSync.Events; -using QuantumUNET.Transport; -using UnityEngine; - -namespace QSB.Anglerfish.Events -{ - public class AnglerChangeStateMessage : EnumWorldObjectMessage - { - public uint TargetId; - public Vector3 LocalDisturbancePos; - - public override void Deserialize(QNetworkReader reader) - { - base.Deserialize(reader); - TargetId = reader.ReadUInt32(); - LocalDisturbancePos = reader.ReadVector3(); - } - - public override void Serialize(QNetworkWriter writer) - { - base.Serialize(writer); - writer.Write(TargetId); - writer.Write(LocalDisturbancePos); - } - } -} diff --git a/QSB/Anglerfish/Messages/AnglerChangeStateMessage.cs b/QSB/Anglerfish/Messages/AnglerChangeStateMessage.cs new file mode 100644 index 00000000..86ad8c35 --- /dev/null +++ b/QSB/Anglerfish/Messages/AnglerChangeStateMessage.cs @@ -0,0 +1,74 @@ +using QSB.Anglerfish.WorldObjects; +using QSB.Messaging; +using QSB.Player; +using QuantumUNET.Transport; +using UnityEngine; + +namespace QSB.Anglerfish.Messages +{ + public class AnglerChangeStateMessage : QSBEnumWorldObjectMessage + { + private uint TargetId; + private Vector3 LocalDisturbancePos; + + public AnglerChangeStateMessage(QSBAngler qsbAngler) + { + Value = qsbAngler.AttachedObject._currentState; + TargetId = TargetToId(qsbAngler.TargetTransform); + LocalDisturbancePos = qsbAngler.AttachedObject._localDisturbancePos; + } + + public AnglerChangeStateMessage() { } + + public override void Serialize(QNetworkWriter writer) + { + base.Serialize(writer); + writer.Write(TargetId); + writer.Write(LocalDisturbancePos); + } + + public override void Deserialize(QNetworkReader reader) + { + base.Deserialize(reader); + TargetId = reader.ReadUInt32(); + LocalDisturbancePos = reader.ReadVector3(); + } + + public override void OnReceiveRemote() + { + WorldObject.TargetTransform = IdToTarget(TargetId); + WorldObject.AttachedObject._localDisturbancePos = LocalDisturbancePos; + WorldObject.AttachedObject.ChangeState(Value); + } + + private static uint TargetToId(Transform transform) + { + if (transform == null) + { + return uint.MaxValue; + } + + if (transform == Locator.GetShipTransform()) + { + return uint.MaxValue - 1; + } + + return QSBPlayerManager.LocalPlayerId; + } + + private static Transform IdToTarget(uint id) + { + if (id == uint.MaxValue) + { + return null; + } + + if (id == uint.MaxValue - 1) + { + return Locator.GetShipTransform(); + } + + return QSBPlayerManager.GetPlayer(id).Body.transform; + } + } +} \ No newline at end of file diff --git a/QSB/Anglerfish/Patches/AnglerPatches.cs b/QSB/Anglerfish/Patches/AnglerPatches.cs index c492b9dc..a76f45fb 100644 --- a/QSB/Anglerfish/Patches/AnglerPatches.cs +++ b/QSB/Anglerfish/Patches/AnglerPatches.cs @@ -1,7 +1,9 @@ using HarmonyLib; +using QSB.Anglerfish.Messages; using QSB.Anglerfish.WorldObjects; using QSB.AuthoritySync; using QSB.Events; +using QSB.Messaging; using QSB.Patches; using QSB.Utility; using QSB.WorldSync; @@ -40,7 +42,7 @@ namespace QSB.Anglerfish.Patches if (qsbAngler.TargetTransform != null && sectorDetector.GetAttachedOWRigidbody().transform == qsbAngler.TargetTransform) { qsbAngler.TargetTransform = null; - QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler); + qsbAngler.SendMessage(new AnglerChangeStateMessage(qsbAngler)); } return false; @@ -62,7 +64,7 @@ namespace QSB.Anglerfish.Patches if ((__instance._brambleBody.transform.TransformPoint(__instance._localDisturbancePos) - __instance._anglerBody.GetPosition()).sqrMagnitude < __instance._arrivalDistance * __instance._arrivalDistance) { __instance.ChangeState(AnglerfishController.AnglerState.Lurking); - QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler); + qsbAngler.SendMessage(new AnglerChangeStateMessage(qsbAngler)); return false; } break; @@ -70,14 +72,14 @@ namespace QSB.Anglerfish.Patches if (qsbAngler.TargetTransform == null) { __instance.ChangeState(AnglerfishController.AnglerState.Lurking); - QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler); + qsbAngler.SendMessage(new AnglerChangeStateMessage(qsbAngler)); return false; } if ((qsbAngler.TargetTransform.position - __instance._anglerBody.GetPosition()).sqrMagnitude > __instance._escapeDistance * __instance._escapeDistance) { qsbAngler.TargetTransform = null; __instance.ChangeState(AnglerfishController.AnglerState.Lurking); - QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler); + qsbAngler.SendMessage(new AnglerChangeStateMessage(qsbAngler)); return false; } break; @@ -87,7 +89,7 @@ namespace QSB.Anglerfish.Patches if (qsbAngler.TargetTransform == null) { __instance.ChangeState(AnglerfishController.AnglerState.Lurking); - QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler); + qsbAngler.SendMessage(new AnglerChangeStateMessage(qsbAngler)); return false; } var num = Time.time - __instance._consumeStartTime; @@ -117,7 +119,7 @@ namespace QSB.Anglerfish.Patches else { qsbAngler.TargetTransform = null; - QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler); + qsbAngler.SendMessage(new AnglerChangeStateMessage(qsbAngler)); } break; case AnglerfishController.AnglerState.Stunned: @@ -127,11 +129,11 @@ namespace QSB.Anglerfish.Patches if (qsbAngler.TargetTransform != null) { __instance.ChangeState(AnglerfishController.AnglerState.Chasing); - QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler); + qsbAngler.SendMessage(new AnglerChangeStateMessage(qsbAngler)); return false; } __instance.ChangeState(AnglerfishController.AnglerState.Lurking); - QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler); + qsbAngler.SendMessage(new AnglerChangeStateMessage(qsbAngler)); } break; default: @@ -237,7 +239,7 @@ namespace QSB.Anglerfish.Patches { qsbAngler.TargetTransform = attachedOWRigidbody.transform; __instance.ChangeState(AnglerfishController.AnglerState.Chasing); - QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler); + qsbAngler.SendMessage(new AnglerChangeStateMessage(qsbAngler)); return false; } @@ -264,7 +266,7 @@ namespace QSB.Anglerfish.Patches { __instance.ChangeState(AnglerfishController.AnglerState.Chasing); } - QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler); + qsbAngler.SendMessage(new AnglerChangeStateMessage(qsbAngler)); return false; } } @@ -275,7 +277,7 @@ namespace QSB.Anglerfish.Patches { __instance.ChangeState(AnglerfishController.AnglerState.Investigating); } - QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler); + qsbAngler.SendMessage(new AnglerChangeStateMessage(qsbAngler)); } return false; @@ -301,7 +303,7 @@ namespace QSB.Anglerfish.Patches qsbAngler.TargetTransform = caughtBody.transform; __instance._consumeStartTime = Time.time; __instance.ChangeState(AnglerfishController.AnglerState.Consuming); - QSBEventManager.FireEvent(EventNames.QSBAnglerChangeState, qsbAngler); + qsbAngler.SendMessage(new AnglerChangeStateMessage(qsbAngler)); } return false; diff --git a/QSB/Events/EventNames.cs b/QSB/Events/EventNames.cs index b15e6fad..316d845f 100644 --- a/QSB/Events/EventNames.cs +++ b/QSB/Events/EventNames.cs @@ -68,7 +68,6 @@ public const string QSBEnterSatelliteCamera = nameof(QSBEnterSatelliteCamera); public const string QSBExitSatelliteCamera = nameof(QSBExitSatelliteCamera); public const string QSBSatelliteSnapshot = nameof(QSBSatelliteSnapshot); - public const string QSBAnglerChangeState = nameof(QSBAnglerChangeState); public const string QSBSatelliteRepairTick = nameof(QSBSatelliteRepairTick); public const string QSBSatelliteRepaired = nameof(QSBSatelliteRepairTick); public const string QSBJellyfishRising = nameof(QSBJellyfishRising);