Skip to content

Commit

Permalink
fix rafts
Browse files Browse the repository at this point in the history
  • Loading branch information
misternebula committed Sep 19, 2023
1 parent 958dd40 commit 58c7b9b
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 2 deletions.
62 changes: 62 additions & 0 deletions QSB/EchoesOfTheEye/RaftSync/Messages/RaftCarrierOnEntryMessage.cs
Original file line number Diff line number Diff line change
@@ -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<IQSBRaftCarrier, int>
{
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<QSBRaft>();
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);
}
}
}
}
29 changes: 29 additions & 0 deletions QSB/EchoesOfTheEye/RaftSync/Patches/RaftPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,35 @@ private static void RaftDock_OnPressInteract(RaftDock __instance)
return;
}

if (!QSBWorldSync.AllObjectsReady)
{
return;
}

__instance.GetWorldObject<QSBRaftDock>().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<RaftController>();
var qsbRaft = raft.GetWorldObject<QSBRaft>();
if (!qsbRaft.NetworkBehaviour.isOwned)
{
return false;
}

__instance.GetWorldObject<IQSBRaftCarrier>().SendMessage(new RaftCarrierOnEntryMessage(qsbRaft));
}

return true;
}
}
1 change: 1 addition & 0 deletions QSB/EchoesOfTheEye/RaftSync/RaftManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public override async UniTask BuildWorldObjects(OWScene scene, CancellationToken
{
QSBWorldSync.Init<QSBRaft, RaftController>();
QSBWorldSync.Init<QSBRaftDock, RaftDock>();
QSBWorldSync.Init<QSBDamRaftLift, DamRaftLift>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ protected override void Uninit()
/// </summary>
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);

Expand Down
7 changes: 7 additions & 0 deletions QSB/EchoesOfTheEye/RaftSync/WorldObjects/IQSBRaftCarrier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using QSB.WorldSync;

namespace QSB.EchoesOfTheEye.RaftSync.WorldObjects;

internal interface IQSBRaftCarrier : IWorldObject
{
}
5 changes: 5 additions & 0 deletions QSB/EchoesOfTheEye/RaftSync/WorldObjects/QSBDamRaftLift.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace QSB.EchoesOfTheEye.RaftSync.WorldObjects;

internal class QSBDamRaftLift : QSBRaftCarrier<DamRaftLift>
{
}
8 changes: 8 additions & 0 deletions QSB/EchoesOfTheEye/RaftSync/WorldObjects/QSBRaftCarrier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using QSB.WorldSync;

namespace QSB.EchoesOfTheEye.RaftSync.WorldObjects;

public class QSBRaftCarrier<T> : WorldObject<T>, IQSBRaftCarrier
where T : RaftCarrier
{
}
3 changes: 1 addition & 2 deletions QSB/EchoesOfTheEye/RaftSync/WorldObjects/QSBRaftDock.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using QSB.ItemSync.WorldObjects;
using QSB.WorldSync;

namespace QSB.EchoesOfTheEye.RaftSync.WorldObjects;

public class QSBRaftDock : WorldObject<RaftDock>, IQSBDropTarget
public class QSBRaftDock : QSBRaftCarrier<RaftDock>, IQSBDropTarget
{
IItemDropTarget IQSBDropTarget.AttachedObject => AttachedObject;
}

0 comments on commit 58c7b9b

Please sign in to comment.