diff --git a/QSB/EchoesOfTheEye/DreamLantern/DreamLanternActionType.cs b/QSB/EchoesOfTheEye/DreamLantern/DreamLanternActionType.cs deleted file mode 100644 index 96998bab..00000000 --- a/QSB/EchoesOfTheEye/DreamLantern/DreamLanternActionType.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace QSB.EchoesOfTheEye.DreamLantern; - -public enum DreamLanternActionType -{ - FOCUS, - CONCEAL -} diff --git a/QSB/EchoesOfTheEye/DreamLantern/DreamLanternManager.cs b/QSB/EchoesOfTheEye/DreamLantern/DreamLanternManager.cs new file mode 100644 index 00000000..700f0865 --- /dev/null +++ b/QSB/EchoesOfTheEye/DreamLantern/DreamLanternManager.cs @@ -0,0 +1,15 @@ +using Cysharp.Threading.Tasks; +using QSB.EchoesOfTheEye.DreamLantern.WorldObjects; +using QSB.WorldSync; +using System.Threading; + +namespace QSB.EchoesOfTheEye.DreamLantern; + +public class DreamLanternManager : WorldObjectManager +{ + public override WorldObjectScene WorldObjectScene => WorldObjectScene.SolarSystem; + public override bool DlcOnly => true; + + public override async UniTask BuildWorldObjects(OWScene scene, CancellationToken ct) => + QSBWorldSync.Init(); +} diff --git a/QSB/EchoesOfTheEye/DreamLantern/Messages/DreamLanternStateMessage.cs b/QSB/EchoesOfTheEye/DreamLantern/Messages/DreamLanternStateMessage.cs deleted file mode 100644 index 9ad9ca29..00000000 --- a/QSB/EchoesOfTheEye/DreamLantern/Messages/DreamLanternStateMessage.cs +++ /dev/null @@ -1,36 +0,0 @@ -using QSB.ItemSync.WorldObjects.Items; -using QSB.Messaging; -using QSB.Player; -using QSB.Utility; - -namespace QSB.EchoesOfTheEye.DreamLantern.Messages; - -internal class DreamLanternStateMessage : QSBMessage<(DreamLanternActionType Type, bool BoolValue, float FloatValue)> -{ - public DreamLanternStateMessage(DreamLanternActionType actionType, bool boolValue = false, float floatValue = 0f) - : base((actionType, boolValue, floatValue)) { } - - public override void OnReceiveRemote() - { - var heldItem = QSBPlayerManager.GetPlayer(From).HeldItem; - - if (heldItem is not QSBDreamLanternItem lantern) - { - DebugLog.ToConsole($"Error - Got DreamLanternStateMessage from player {From}, but they are not holding a QSBDreamLanternItem!", OWML.Common.MessageType.Error); - return; - } - - var controller = lantern.AttachedObject._lanternController; - - switch (Data.Type) - { - case DreamLanternActionType.CONCEAL: - DebugLog.DebugWrite($"CONCEAL {lantern.AttachedObject.name}"); - controller.SetConcealed(Data.BoolValue); - break; - case DreamLanternActionType.FOCUS: - controller.SetFocus(Data.FloatValue); - break; - } - } -} diff --git a/QSB/EchoesOfTheEye/DreamLantern/Messages/SetConcealedMessage.cs b/QSB/EchoesOfTheEye/DreamLantern/Messages/SetConcealedMessage.cs new file mode 100644 index 00000000..a06f1d59 --- /dev/null +++ b/QSB/EchoesOfTheEye/DreamLantern/Messages/SetConcealedMessage.cs @@ -0,0 +1,13 @@ +using QSB.EchoesOfTheEye.DreamLantern.WorldObjects; +using QSB.Messaging; +using QSB.Patches; + +namespace QSB.EchoesOfTheEye.DreamLantern.Messages; + +internal class SetConcealedMessage : QSBWorldObjectMessage +{ + public SetConcealedMessage(bool concealed) : base(concealed) { } + + public override void OnReceiveRemote() + => QSBPatch.RemoteCall(() => WorldObject.AttachedObject.SetConcealed(Data)); +} diff --git a/QSB/EchoesOfTheEye/DreamLantern/Messages/SetFocusMessage.cs b/QSB/EchoesOfTheEye/DreamLantern/Messages/SetFocusMessage.cs new file mode 100644 index 00000000..1fa7c5e3 --- /dev/null +++ b/QSB/EchoesOfTheEye/DreamLantern/Messages/SetFocusMessage.cs @@ -0,0 +1,13 @@ +using QSB.EchoesOfTheEye.DreamLantern.WorldObjects; +using QSB.Messaging; +using QSB.Patches; + +namespace QSB.EchoesOfTheEye.DreamLantern.Messages; + +internal class SetFocusMessage : QSBWorldObjectMessage +{ + public SetFocusMessage(float focus) : base(focus) { } + + public override void OnReceiveRemote() + => QSBPatch.RemoteCall(() => WorldObject.AttachedObject.SetFocus(Data)); +} diff --git a/QSB/EchoesOfTheEye/DreamLantern/Messages/DreamLanternLitMessage.cs b/QSB/EchoesOfTheEye/DreamLantern/Messages/SetLitMessage.cs similarity index 53% rename from QSB/EchoesOfTheEye/DreamLantern/Messages/DreamLanternLitMessage.cs rename to QSB/EchoesOfTheEye/DreamLantern/Messages/SetLitMessage.cs index 6e3e1d3d..48e4395a 100644 --- a/QSB/EchoesOfTheEye/DreamLantern/Messages/DreamLanternLitMessage.cs +++ b/QSB/EchoesOfTheEye/DreamLantern/Messages/SetLitMessage.cs @@ -1,12 +1,12 @@ -using QSB.ItemSync.WorldObjects.Items; +using QSB.EchoesOfTheEye.DreamLantern.WorldObjects; using QSB.Messaging; using QSB.Patches; namespace QSB.EchoesOfTheEye.DreamLantern.Messages; -internal class DreamLanternLitMessage : QSBWorldObjectMessage +internal class SetLitMessage : QSBWorldObjectMessage { - public DreamLanternLitMessage(bool lit) : base(lit) { } + public SetLitMessage(bool lit) : base(lit) { } public override void OnReceiveRemote() => QSBPatch.RemoteCall(() => WorldObject.AttachedObject.SetLit(Data)); diff --git a/QSB/EchoesOfTheEye/DreamLantern/Messages/SetRangeMessage.cs b/QSB/EchoesOfTheEye/DreamLantern/Messages/SetRangeMessage.cs new file mode 100644 index 00000000..093fde83 --- /dev/null +++ b/QSB/EchoesOfTheEye/DreamLantern/Messages/SetRangeMessage.cs @@ -0,0 +1,13 @@ +using QSB.EchoesOfTheEye.DreamLantern.WorldObjects; +using QSB.Messaging; +using QSB.Patches; + +namespace QSB.EchoesOfTheEye.DreamLantern.Messages; + +internal class SetRangeMessage : QSBWorldObjectMessage +{ + public SetRangeMessage(float minRange, float maxRange) : base((minRange, maxRange)) { } + + public override void OnReceiveRemote() + => QSBPatch.RemoteCall(() => WorldObject.AttachedObject.SetRange(Data.minRange, Data.maxRange)); +} diff --git a/QSB/EchoesOfTheEye/DreamLantern/Patches/DreamLanternPatches.cs b/QSB/EchoesOfTheEye/DreamLantern/Patches/DreamLanternPatches.cs index 9d5a962d..2b83b6d6 100644 --- a/QSB/EchoesOfTheEye/DreamLantern/Patches/DreamLanternPatches.cs +++ b/QSB/EchoesOfTheEye/DreamLantern/Patches/DreamLanternPatches.cs @@ -1,6 +1,6 @@ using HarmonyLib; using QSB.EchoesOfTheEye.DreamLantern.Messages; -using QSB.ItemSync.WorldObjects.Items; +using QSB.EchoesOfTheEye.DreamLantern.WorldObjects; using QSB.Messaging; using QSB.Patches; using QSB.WorldSync; @@ -13,73 +13,15 @@ internal class DreamLanternPatches : QSBPatch public override QSBPatchTypes Type => QSBPatchTypes.OnClientConnect; [HarmonyPrefix] - [HarmonyPatch(typeof(DreamLanternItem), nameof(DreamLanternItem.Update))] - public static bool UpdateReplacement(DreamLanternItem __instance) - { - var isHoldingItem = Locator.GetToolModeSwapper().IsInToolMode(ToolMode.Item); - - __instance._wasFocusing = __instance._focusing; - __instance._focusing = OWInput.IsPressed(InputLibrary.toolActionPrimary, InputMode.Character) && Time.time > __instance._forceUnfocusTime + 1f && isHoldingItem; - - var concealActionPressed = OWInput.IsPressed(InputLibrary.toolActionSecondary, InputMode.Character) && isHoldingItem; - if (concealActionPressed && !__instance._lanternController.IsConcealed()) - { - Locator.GetPlayerAudioController().OnArtifactConceal(); - __instance._lanternController.SetConcealed(true); - new DreamLanternStateMessage(DreamLanternActionType.CONCEAL, true).Send(); - } - else if (!concealActionPressed && __instance._lanternController.IsConcealed()) - { - Locator.GetPlayerAudioController().OnArtifactUnconceal(); - __instance._lanternController.SetConcealed(false); - new DreamLanternStateMessage(DreamLanternActionType.CONCEAL).Send(); - } - - if (__instance._focusing != __instance._wasFocusing) - { - if (__instance._focusing) - { - Locator.GetPlayerAudioController().OnArtifactFocus(); - } - else - { - Locator.GetPlayerAudioController().OnArtifactUnfocus(); - } - } - - __instance.UpdateFocus(); - - return false; - } - - [HarmonyPrefix] - [HarmonyPatch(typeof(DreamLanternController), nameof(DreamLanternController.MoveTowardFocus))] - public static bool UpdateFocusReplacement(DreamLanternController __instance, float targetFocus, float rate) - { - var value = Mathf.MoveTowards(__instance._focus, targetFocus, rate * Time.deltaTime); - - if (__instance._focus == value) - { - __instance.SetFocus(value); - return false; - } - - __instance.SetFocus(value); - new DreamLanternStateMessage(DreamLanternActionType.FOCUS, floatValue: value).Send(); - - return false; - } - - [HarmonyPrefix] - [HarmonyPatch(typeof(DreamLanternItem), nameof(DreamLanternItem.SetLit))] - public static void SetLit(DreamLanternItem __instance, bool lit) + [HarmonyPatch(typeof(DreamLanternController), nameof(DreamLanternController.SetLit))] + public static void SetLit(DreamLanternController __instance, bool lit) { if (Remote) { return; } - if (__instance._lanternController.IsLit() == lit) + if (__instance._lit == lit) { return; } @@ -89,6 +31,73 @@ internal class DreamLanternPatches : QSBPatch return; } - __instance.GetWorldObject().SendMessage(new DreamLanternLitMessage(lit)); + __instance.GetWorldObject().SendMessage(new SetLitMessage(lit)); + } + + [HarmonyPrefix] + [HarmonyPatch(typeof(DreamLanternController), nameof(DreamLanternController.SetConcealed))] + public static void SetConcealed(DreamLanternController __instance, bool concealed) + { + if (Remote) + { + return; + } + + if (__instance._concealed == concealed) + { + return; + } + + if (!QSBWorldSync.AllObjectsReady) + { + return; + } + + __instance.GetWorldObject().SendMessage(new SetConcealedMessage(concealed)); + } + + [HarmonyPrefix] + [HarmonyPatch(typeof(DreamLanternController), nameof(DreamLanternController.SetFocus))] + public static void SetFocus(DreamLanternController __instance, float focus) + { + if (Remote) + { + return; + } + + focus = Mathf.Clamp01(focus); + if (OWMath.ApproxEquals(__instance._focus, focus)) + { + return; + } + + if (!QSBWorldSync.AllObjectsReady) + { + return; + } + + __instance.GetWorldObject().SendMessage(new SetFocusMessage(focus)); + } + + [HarmonyPrefix] + [HarmonyPatch(typeof(DreamLanternController), nameof(DreamLanternController.SetRange))] + public static void SetRange(DreamLanternController __instance, float minRange, float maxRange) + { + if (Remote) + { + return; + } + + if (OWMath.ApproxEquals(__instance._minRange, minRange) && OWMath.ApproxEquals(__instance._maxRange, maxRange)) + { + return; + } + + if (!QSBWorldSync.AllObjectsReady) + { + return; + } + + __instance.GetWorldObject().SendMessage(new SetRangeMessage(minRange, maxRange)); } } diff --git a/QSB/EchoesOfTheEye/DreamLantern/WorldObjects/QSBDreamLantern.cs b/QSB/EchoesOfTheEye/DreamLantern/WorldObjects/QSBDreamLantern.cs new file mode 100644 index 00000000..a3b4a90f --- /dev/null +++ b/QSB/EchoesOfTheEye/DreamLantern/WorldObjects/QSBDreamLantern.cs @@ -0,0 +1,16 @@ +using QSB.EchoesOfTheEye.DreamLantern.Messages; +using QSB.Messaging; +using QSB.WorldSync; + +namespace QSB.EchoesOfTheEye.DreamLantern.WorldObjects; + +public class QSBDreamLantern : WorldObject +{ + public override void SendInitialState(uint to) + { + this.SendMessage(new SetLitMessage(AttachedObject._lit) { To = to }); + this.SendMessage(new SetConcealedMessage(AttachedObject._concealed) { To = to }); + this.SendMessage(new SetFocusMessage(AttachedObject._focus) { To = to }); + this.SendMessage(new SetRangeMessage(AttachedObject._minRange, AttachedObject._maxRange) { To = to }); + } +} diff --git a/QSB/ItemSync/WorldObjects/Items/QSBDreamLanternItem.cs b/QSB/ItemSync/WorldObjects/Items/QSBDreamLanternItem.cs index e6bb73af..5e7a263c 100644 --- a/QSB/ItemSync/WorldObjects/Items/QSBDreamLanternItem.cs +++ b/QSB/ItemSync/WorldObjects/Items/QSBDreamLanternItem.cs @@ -1,17 +1,3 @@ -using QSB.EchoesOfTheEye.DreamLantern.Messages; -using QSB.Messaging; +namespace QSB.ItemSync.WorldObjects.Items; -namespace QSB.ItemSync.WorldObjects.Items; - -public class QSBDreamLanternItem : QSBItem -{ - public override void SendInitialState(uint to) - { - base.SendInitialState(to); - - if (AttachedObject._lanternController != null) - { - this.SendMessage(new DreamLanternLitMessage(AttachedObject._lanternController.IsLit()) { To = to }); - } - } -} +public class QSBDreamLanternItem : QSBItem { }