QSBDreamLantern

This commit is contained in:
JohnCorby 2022-04-13 23:30:59 -07:00
parent 362b9afc39
commit df43552ed2
10 changed files with 147 additions and 125 deletions

View File

@ -1,7 +0,0 @@
namespace QSB.EchoesOfTheEye.DreamLantern;
public enum DreamLanternActionType
{
FOCUS,
CONCEAL
}

View File

@ -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<QSBDreamLantern, DreamLanternController>();
}

View File

@ -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;
}
}
}

View File

@ -0,0 +1,13 @@
using QSB.EchoesOfTheEye.DreamLantern.WorldObjects;
using QSB.Messaging;
using QSB.Patches;
namespace QSB.EchoesOfTheEye.DreamLantern.Messages;
internal class SetConcealedMessage : QSBWorldObjectMessage<QSBDreamLantern, bool>
{
public SetConcealedMessage(bool concealed) : base(concealed) { }
public override void OnReceiveRemote()
=> QSBPatch.RemoteCall(() => WorldObject.AttachedObject.SetConcealed(Data));
}

View File

@ -0,0 +1,13 @@
using QSB.EchoesOfTheEye.DreamLantern.WorldObjects;
using QSB.Messaging;
using QSB.Patches;
namespace QSB.EchoesOfTheEye.DreamLantern.Messages;
internal class SetFocusMessage : QSBWorldObjectMessage<QSBDreamLantern, float>
{
public SetFocusMessage(float focus) : base(focus) { }
public override void OnReceiveRemote()
=> QSBPatch.RemoteCall(() => WorldObject.AttachedObject.SetFocus(Data));
}

View File

@ -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<QSBDreamLanternItem, bool>
internal class SetLitMessage : QSBWorldObjectMessage<QSBDreamLantern, bool>
{
public DreamLanternLitMessage(bool lit) : base(lit) { }
public SetLitMessage(bool lit) : base(lit) { }
public override void OnReceiveRemote()
=> QSBPatch.RemoteCall(() => WorldObject.AttachedObject.SetLit(Data));

View File

@ -0,0 +1,13 @@
using QSB.EchoesOfTheEye.DreamLantern.WorldObjects;
using QSB.Messaging;
using QSB.Patches;
namespace QSB.EchoesOfTheEye.DreamLantern.Messages;
internal class SetRangeMessage : QSBWorldObjectMessage<QSBDreamLantern, (float minRange, float maxRange)>
{
public SetRangeMessage(float minRange, float maxRange) : base((minRange, maxRange)) { }
public override void OnReceiveRemote()
=> QSBPatch.RemoteCall(() => WorldObject.AttachedObject.SetRange(Data.minRange, Data.maxRange));
}

View File

@ -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<QSBDreamLanternItem>().SendMessage(new DreamLanternLitMessage(lit));
__instance.GetWorldObject<QSBDreamLantern>().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<QSBDreamLantern>().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<QSBDreamLantern>().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<QSBDreamLantern>().SendMessage(new SetRangeMessage(minRange, maxRange));
}
}

View File

@ -0,0 +1,16 @@
using QSB.EchoesOfTheEye.DreamLantern.Messages;
using QSB.Messaging;
using QSB.WorldSync;
namespace QSB.EchoesOfTheEye.DreamLantern.WorldObjects;
public class QSBDreamLantern : WorldObject<DreamLanternController>
{
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 });
}
}

View File

@ -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<DreamLanternItem>
{
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<DreamLanternItem> { }