From 58c7b9baf640522cee628201dcfc1f52e3208f73 Mon Sep 17 00:00:00 2001 From: _nebula <41904486+misternebula@users.noreply.github.com> Date: Tue, 19 Sep 2023 15:47:38 +0100 Subject: [PATCH] fix rafts --- .../Messages/RaftCarrierOnEntryMessage.cs | 62 +++++++++++++++++++ .../RaftSync/Patches/RaftPatches.cs | 29 +++++++++ QSB/EchoesOfTheEye/RaftSync/RaftManager.cs | 1 + .../TransformSync/RaftTransformSync.cs | 9 +++ .../RaftSync/WorldObjects/IQSBRaftCarrier.cs | 7 +++ .../RaftSync/WorldObjects/QSBDamRaftLift.cs | 5 ++ .../RaftSync/WorldObjects/QSBRaftCarrier.cs | 8 +++ .../RaftSync/WorldObjects/QSBRaftDock.cs | 3 +- 8 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 QSB/EchoesOfTheEye/RaftSync/Messages/RaftCarrierOnEntryMessage.cs create mode 100644 QSB/EchoesOfTheEye/RaftSync/WorldObjects/IQSBRaftCarrier.cs create mode 100644 QSB/EchoesOfTheEye/RaftSync/WorldObjects/QSBDamRaftLift.cs create mode 100644 QSB/EchoesOfTheEye/RaftSync/WorldObjects/QSBRaftCarrier.cs diff --git a/QSB/EchoesOfTheEye/RaftSync/Messages/RaftCarrierOnEntryMessage.cs b/QSB/EchoesOfTheEye/RaftSync/Messages/RaftCarrierOnEntryMessage.cs new file mode 100644 index 000000000..9230935bc --- /dev/null +++ b/QSB/EchoesOfTheEye/RaftSync/Messages/RaftCarrierOnEntryMessage.cs @@ -0,0 +1,62 @@ +using QSB.EchoesOfTheEye.RaftSync.WorldObjects; +using QSB.Messaging; +using QSB.WorldSync; +using UnityEngine; + +namespace QSB.EchoesOfTheEye.RaftSync.Messages; +internal class RaftCarrierOnEntryMessage : QSBWorldObjectMessage +{ + public RaftCarrierOnEntryMessage(QSBRaft raft) : base(raft.ObjectId) { } + + public override void OnReceiveRemote() + { + // TODO : work out if we can just call RaftCarrier.OnEntry with a right gameobject? tried it with _fluidDetector.gameObject and it didn't work + + var qsbRaft = Data.GetWorldObject(); + var attachedObj = WorldObject.AttachedObject as RaftCarrier; + + attachedObj._raft = qsbRaft.AttachedObject; + attachedObj._raft.OnArriveAtTarget += attachedObj.OnArriveAtTarget; + attachedObj.GetAlignDestination().localEulerAngles = Vector3.zero; + + var relativeDockForward = attachedObj.GetAlignDestination().InverseTransformDirection(attachedObj._raft.transform.forward); + relativeDockForward.y = 0f; + + var targetRaftRotation = OWMath.Angle(Vector3.forward, relativeDockForward, Vector3.up); + targetRaftRotation = OWMath.RoundToNearestMultiple(targetRaftRotation, 90f); + attachedObj.GetAlignDestination().localEulerAngles = new Vector3(0f, targetRaftRotation, 0f); + + var raftMovementDirection = attachedObj.GetAlignDestination().position - attachedObj._raft.GetBody().GetPosition(); + raftMovementDirection = Vector3.Project(raftMovementDirection, attachedObj._raft.transform.up); + + var targetPosition = attachedObj.GetAlignDestination().position - attachedObj.GetAlignDestination().up * raftMovementDirection.magnitude; + + attachedObj._raft.MoveToTarget(targetPosition, attachedObj.GetAlignDestination().rotation, attachedObj._raftAlignSpeed, false); + attachedObj._oneShotAudio.PlayOneShot(global::AudioType.Raft_Reel_Start, 1f); + attachedObj._loopingAudio.FadeIn(0.2f, false, false, 1f); + attachedObj._state = RaftCarrier.DockState.AligningBelow; + + if (WorldObject.AttachedObject is RaftDock dock) + { + if (dock._state == RaftCarrier.DockState.AligningBelow) + { + dock.enabled = true; + } + } + else if (WorldObject.AttachedObject is DamRaftLift lift) + { + if (lift._state == RaftCarrier.DockState.AligningBelow) + { + lift.enabled = true; + + foreach (var node in lift._liftNodes) + { + node.localEulerAngles = lift.GetAlignDestination().localEulerAngles; + } + + lift._nodeIndex = 1; + lift._raftDockLights.SetLightsActivation(true, false); + } + } + } +} diff --git a/QSB/EchoesOfTheEye/RaftSync/Patches/RaftPatches.cs b/QSB/EchoesOfTheEye/RaftSync/Patches/RaftPatches.cs index cff95f297..72a232209 100644 --- a/QSB/EchoesOfTheEye/RaftSync/Patches/RaftPatches.cs +++ b/QSB/EchoesOfTheEye/RaftSync/Patches/RaftPatches.cs @@ -42,6 +42,35 @@ private static void RaftDock_OnPressInteract(RaftDock __instance) return; } + if (!QSBWorldSync.AllObjectsReady) + { + return; + } + __instance.GetWorldObject().SendMessage(new RaftDockOnPressInteractMessage()); } + + [HarmonyPrefix] + [HarmonyPatch(typeof(RaftCarrier), nameof(RaftCarrier.OnEntry))] + private static bool RaftCarrier_OnEntry(RaftCarrier __instance, GameObject hitObj) + { + if (!QSBWorldSync.AllObjectsReady) + { + return true; + } + + if (hitObj.CompareTag("RaftDetector") && __instance._state == RaftCarrier.DockState.Ready) + { + var raft = hitObj.GetComponentInParent(); + var qsbRaft = raft.GetWorldObject(); + if (!qsbRaft.NetworkBehaviour.isOwned) + { + return false; + } + + __instance.GetWorldObject().SendMessage(new RaftCarrierOnEntryMessage(qsbRaft)); + } + + return true; + } } diff --git a/QSB/EchoesOfTheEye/RaftSync/RaftManager.cs b/QSB/EchoesOfTheEye/RaftSync/RaftManager.cs index 4dc784a01..83eee6957 100644 --- a/QSB/EchoesOfTheEye/RaftSync/RaftManager.cs +++ b/QSB/EchoesOfTheEye/RaftSync/RaftManager.cs @@ -14,5 +14,6 @@ public override async UniTask BuildWorldObjects(OWScene scene, CancellationToken { QSBWorldSync.Init(); QSBWorldSync.Init(); + QSBWorldSync.Init(); } } diff --git a/QSB/EchoesOfTheEye/RaftSync/TransformSync/RaftTransformSync.cs b/QSB/EchoesOfTheEye/RaftSync/TransformSync/RaftTransformSync.cs index 5b6b2f37b..55a8db4ee 100644 --- a/QSB/EchoesOfTheEye/RaftSync/TransformSync/RaftTransformSync.cs +++ b/QSB/EchoesOfTheEye/RaftSync/TransformSync/RaftTransformSync.cs @@ -80,6 +80,15 @@ protected override void Uninit() /// protected override void ApplyToAttached() { + if (_worldObject.AttachedObject is RaftController raft) + { + if (raft.IsDockingOrDocked()) + { + // don't sync position if we're docking, dock sequence moves the raft itself + return; + } + } + var targetPos = ReferenceTransform.FromRelPos(UseInterpolation ? SmoothPosition : transform.position); var targetRot = ReferenceTransform.FromRelRot(UseInterpolation ? SmoothRotation : transform.rotation); diff --git a/QSB/EchoesOfTheEye/RaftSync/WorldObjects/IQSBRaftCarrier.cs b/QSB/EchoesOfTheEye/RaftSync/WorldObjects/IQSBRaftCarrier.cs new file mode 100644 index 000000000..66975509a --- /dev/null +++ b/QSB/EchoesOfTheEye/RaftSync/WorldObjects/IQSBRaftCarrier.cs @@ -0,0 +1,7 @@ +using QSB.WorldSync; + +namespace QSB.EchoesOfTheEye.RaftSync.WorldObjects; + +internal interface IQSBRaftCarrier : IWorldObject +{ +} diff --git a/QSB/EchoesOfTheEye/RaftSync/WorldObjects/QSBDamRaftLift.cs b/QSB/EchoesOfTheEye/RaftSync/WorldObjects/QSBDamRaftLift.cs new file mode 100644 index 000000000..be7570f2f --- /dev/null +++ b/QSB/EchoesOfTheEye/RaftSync/WorldObjects/QSBDamRaftLift.cs @@ -0,0 +1,5 @@ +namespace QSB.EchoesOfTheEye.RaftSync.WorldObjects; + +internal class QSBDamRaftLift : QSBRaftCarrier +{ +} diff --git a/QSB/EchoesOfTheEye/RaftSync/WorldObjects/QSBRaftCarrier.cs b/QSB/EchoesOfTheEye/RaftSync/WorldObjects/QSBRaftCarrier.cs new file mode 100644 index 000000000..2d9dc8398 --- /dev/null +++ b/QSB/EchoesOfTheEye/RaftSync/WorldObjects/QSBRaftCarrier.cs @@ -0,0 +1,8 @@ +using QSB.WorldSync; + +namespace QSB.EchoesOfTheEye.RaftSync.WorldObjects; + +public class QSBRaftCarrier : WorldObject, IQSBRaftCarrier + where T : RaftCarrier +{ +} diff --git a/QSB/EchoesOfTheEye/RaftSync/WorldObjects/QSBRaftDock.cs b/QSB/EchoesOfTheEye/RaftSync/WorldObjects/QSBRaftDock.cs index 9b4761f3b..fb8380598 100644 --- a/QSB/EchoesOfTheEye/RaftSync/WorldObjects/QSBRaftDock.cs +++ b/QSB/EchoesOfTheEye/RaftSync/WorldObjects/QSBRaftDock.cs @@ -1,9 +1,8 @@ using QSB.ItemSync.WorldObjects; -using QSB.WorldSync; namespace QSB.EchoesOfTheEye.RaftSync.WorldObjects; -public class QSBRaftDock : WorldObject, IQSBDropTarget +public class QSBRaftDock : QSBRaftCarrier, IQSBDropTarget { IItemDropTarget IQSBDropTarget.AttachedObject => AttachedObject; }