87 lines
2.6 KiB
C#
Raw Normal View History

2022-02-26 00:02:14 -08:00
using QSB.AuthoritySync;
2022-02-25 00:21:21 -08:00
using QSB.EchoesOfTheEye.RaftSync.WorldObjects;
using QSB.Syncs.Unsectored.Rigidbodies;
2022-02-25 16:01:36 -08:00
using QSB.Utility;
2022-02-25 00:21:21 -08:00
using QSB.WorldSync;
using System.Collections.Generic;
namespace QSB.EchoesOfTheEye.RaftSync.TransformSync
2022-02-25 00:21:21 -08:00
{
public class RaftTransformSync : UnsectoredRigidbodySync
{
protected override bool UseInterpolation => false;
2022-02-25 00:21:21 -08:00
private QSBRaft _qsbRaft;
private static readonly List<RaftTransformSync> _instances = new();
2022-02-25 00:21:21 -08:00
protected override OWRigidbody InitAttachedRigidbody() => _qsbRaft.AttachedObject._raftBody;
2022-02-25 00:21:21 -08:00
public override void OnStartClient()
2022-02-25 00:21:21 -08:00
{
_instances.Add(this);
if (QSBCore.IsHost)
{
netIdentity.RegisterAuthQueue();
}
2022-02-25 00:21:21 -08:00
base.OnStartClient();
}
2022-02-25 00:21:21 -08:00
public override void OnStopClient()
2022-02-25 00:21:21 -08:00
{
_instances.Remove(this);
if (QSBCore.IsHost)
{
netIdentity.UnregisterAuthQueue();
}
2022-02-25 00:21:21 -08:00
base.OnStopClient();
}
2022-02-25 00:21:21 -08:00
protected override void Init()
{
_qsbRaft = RaftManager.Rafts[_instances.IndexOf(this)].GetWorldObject<QSBRaft>();
_qsbRaft.TransformSync = this;
2022-02-25 00:21:21 -08:00
base.Init();
SetReferenceTransform(AttachedRigidbody.GetOrigParent());
2022-02-25 00:21:21 -08:00
AttachedRigidbody.OnUnsuspendOWRigidbody += OnUnsuspend;
AttachedRigidbody.OnSuspendOWRigidbody += OnSuspend;
netIdentity.UpdateAuthQueue(AttachedRigidbody.IsSuspended() ? AuthQueueAction.Remove : AuthQueueAction.Add);
}
2022-02-25 00:21:21 -08:00
protected override void Uninit()
{
base.Uninit();
2022-02-25 00:21:21 -08:00
AttachedRigidbody.OnUnsuspendOWRigidbody -= OnUnsuspend;
AttachedRigidbody.OnSuspendOWRigidbody -= OnSuspend;
}
2022-02-25 00:21:21 -08:00
private void OnUnsuspend(OWRigidbody suspendedBody) => netIdentity.UpdateAuthQueue(AuthQueueAction.Add);
private void OnSuspend(OWRigidbody suspendedBody) => netIdentity.UpdateAuthQueue(AuthQueueAction.Remove);
2022-02-25 16:01:36 -08:00
public override void OnStartAuthority() => DebugLog.DebugWrite($"{this} - authority = true");
public override void OnStopAuthority() => DebugLog.DebugWrite($"{this} - authority = false");
2022-02-25 16:01:36 -08:00
/// <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);
2022-02-25 16:01:36 -08:00
AttachedRigidbody.SetPosition(targetPos);
AttachedRigidbody.SetRotation(targetRot);
2022-02-25 16:01:36 -08:00
var targetVelocity = ReferenceRigidbody.FromRelVel(Velocity, targetPos);
var targetAngularVelocity = ReferenceRigidbody.FromRelAngVel(AngularVelocity);
2022-02-25 16:01:36 -08:00
AttachedRigidbody.SetVelocity(targetVelocity);
AttachedRigidbody.SetAngularVelocity(targetAngularVelocity);
}
2022-02-25 16:01:36 -08:00
}
}