79 lines
2.2 KiB
C#

using QSB.AuthoritySync;
using QSB.EchoesOfTheEye.RaftSync.WorldObjects;
using QSB.Syncs.Unsectored.Rigidbodies;
using QSB.Utility;
using QSB.Utility.LinkedWorldObject;
using QSB.WorldSync;
namespace QSB.EchoesOfTheEye.RaftSync.TransformSync;
public class RaftTransformSync : UnsectoredRigidbodySync, ILinkedNetworkBehaviour<QSBRaft>
{
protected override bool UseInterpolation => false;
public QSBRaft WorldObject { get; private set; }
public void LinkTo(IWorldObject worldObject) => WorldObject = (QSBRaft)worldObject;
protected override OWRigidbody InitAttachedRigidbody() => WorldObject.AttachedObject._raftBody;
public override void OnStartClient()
{
if (QSBCore.IsHost)
{
netIdentity.RegisterAuthQueue();
}
base.OnStartClient();
}
public override void OnStopClient()
{
if (QSBCore.IsHost)
{
netIdentity.UnregisterAuthQueue();
}
base.OnStopClient();
}
protected override void Init()
{
base.Init();
SetReferenceTransform(AttachedRigidbody.GetOrigParent());
AttachedRigidbody.OnUnsuspendOWRigidbody += OnUnsuspend;
AttachedRigidbody.OnSuspendOWRigidbody += OnSuspend;
netIdentity.UpdateAuthQueue(AttachedRigidbody.IsSuspended() ? AuthQueueAction.Remove : AuthQueueAction.Add);
}
protected override void Uninit()
{
base.Uninit();
AttachedRigidbody.OnUnsuspendOWRigidbody -= OnUnsuspend;
AttachedRigidbody.OnSuspendOWRigidbody -= OnSuspend;
}
private void OnUnsuspend(OWRigidbody suspendedBody) => netIdentity.UpdateAuthQueue(AuthQueueAction.Add);
private void OnSuspend(OWRigidbody suspendedBody) => netIdentity.UpdateAuthQueue(AuthQueueAction.Remove);
/// <summary>
/// replacement for base method
/// using SetPos/Rot instead of Move
/// </summary>
protected override void ApplyToAttached()
{
var targetPos = ReferenceTransform.FromRelPos(transform.position);
var targetRot = ReferenceTransform.FromRelRot(transform.rotation);
AttachedRigidbody.SetPosition(targetPos);
AttachedRigidbody.SetRotation(targetRot);
var targetVelocity = ReferenceRigidbody.FromRelVel(Velocity, targetPos);
var targetAngularVelocity = ReferenceRigidbody.FromRelAngVel(AngularVelocity);
AttachedRigidbody.SetVelocity(targetVelocity);
AttachedRigidbody.SetAngularVelocity(targetAngularVelocity);
}
}