using Mirror; using QSB.AuthoritySync; using QSB.EchoesOfTheEye.RaftSync.WorldObjects; using QSB.Syncs.Unsectored.Rigidbodies; using QSB.Utility; using QSB.WorldSync; using System.Collections.Generic; namespace QSB.EchoesOfTheEye.RaftSync.TransformSync; public class RaftTransformSync : UnsectoredRigidbodySync { protected override bool UseInterpolation => false; private QSBRaft _qsbRaft; private static readonly List _instances = new(); protected override OWRigidbody InitAttachedRigidbody() => _qsbRaft.AttachedObject._raftBody; public override void OnStartClient() { _instances.Add(this); if (QSBCore.IsHost) { netIdentity.RegisterAuthQueue(); } base.OnStartClient(); } public override void OnStopClient() { _instances.Remove(this); if (QSBCore.IsHost) { netIdentity.UnregisterAuthQueue(); } base.OnStopClient(); } protected override void Init() { _qsbRaft = RaftManager.Rafts[_instances.IndexOf(this)].GetWorldObject(); _qsbRaft.TransformSync = this; 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); public override void OnStartAuthority() => DebugLog.DebugWrite($"{this} - start authority"); public override void OnStopAuthority() => DebugLog.DebugWrite($"{this} - stop authority"); protected override void Deserialize(NetworkReader reader) { DebugLog.DebugWrite($"{this} - deserialize"); base.Deserialize(reader); } /// /// replacement for base method /// using SetPos/Rot instead of Move /// protected override void ApplyToAttached() { var targetPos = ReferenceTransform.FromRelPos(UseInterpolation ? SmoothPosition : transform.position); var targetRot = ReferenceTransform.FromRelRot(UseInterpolation ? SmoothRotation : 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); } }