quantum-space-buddies/QSB/OrbSync/TransformSync/NomaiOrbTransformSync.cs

85 lines
2.3 KiB
C#
Raw Normal View History

2022-01-25 19:42:22 -08:00
using QSB.AuthoritySync;
2021-12-23 17:07:29 -08:00
using QSB.OrbSync.WorldObjects;
2021-10-20 17:29:49 +01:00
using QSB.Syncs.Unsectored.Transforms;
using QSB.Utility.LinkedWorldObject;
2021-04-28 10:02:16 +01:00
using QSB.WorldSync;
using UnityEngine;
2022-03-02 19:46:33 -08:00
namespace QSB.OrbSync.TransformSync;
public class NomaiOrbTransformSync : UnsectoredTransformSync, ILinkedNetworkBehaviour
2021-04-28 10:02:16 +01:00
{
2022-03-02 19:46:33 -08:00
/// <summary>
/// normally prints error when attached object is null.
/// this overrides it so that doesn't happen, since the orb can be destroyed.
/// </summary>
protected override bool CheckValid() => AttachedTransform && base.CheckValid();
2022-01-25 19:42:22 -08:00
2022-03-02 19:46:33 -08:00
protected override bool UseInterpolation => true;
protected override float DistanceChangeThreshold => 1f;
2022-01-14 21:25:13 -08:00
private QSBOrb _qsbOrb;
public void SetWorldObject(IWorldObject worldObject) => _qsbOrb = (QSBOrb)worldObject;
protected override Transform InitLocalTransform() => _qsbOrb.AttachedObject.transform;
protected override Transform InitRemoteTransform() => _qsbOrb.AttachedObject.transform;
2022-03-02 19:46:33 -08:00
public override void OnStartClient()
{
if (QSBCore.IsHost)
2022-01-14 21:25:13 -08:00
{
2022-03-02 19:46:33 -08:00
netIdentity.RegisterAuthQueue();
2022-01-14 21:25:13 -08:00
}
2022-03-02 19:46:33 -08:00
base.OnStartClient();
}
2022-01-14 21:25:13 -08:00
2022-03-02 19:46:33 -08:00
public override void OnStopClient()
{
if (QSBCore.IsHost)
{
netIdentity.UnregisterAuthQueue();
}
2022-01-14 21:25:13 -08:00
2022-03-02 19:46:33 -08:00
base.OnStopClient();
}
2022-01-14 21:25:13 -08:00
2022-03-02 19:46:33 -08:00
protected override void Init()
{
base.Init();
var body = AttachedTransform.GetAttachedOWRigidbody();
SetReferenceTransform(body.GetOrigParent());
2022-03-02 19:46:33 -08:00
body.OnUnsuspendOWRigidbody += OnUnsuspend;
body.OnSuspendOWRigidbody += OnSuspend;
netIdentity.UpdateAuthQueue(body.IsSuspended() ? AuthQueueAction.Remove : AuthQueueAction.Add);
}
protected override void Uninit()
{
base.Uninit();
2022-03-02 19:46:33 -08:00
// this is null sometimes on here, but not on other similar transforms syncs (like anglers)
// idk why, but whatever
if (AttachedTransform)
{
var body = AttachedTransform.GetAttachedOWRigidbody();
if (body)
2022-02-03 15:14:16 -08:00
{
2022-03-02 19:46:33 -08:00
body.OnUnsuspendOWRigidbody -= OnUnsuspend;
body.OnSuspendOWRigidbody -= OnSuspend;
2022-02-03 15:14:16 -08:00
}
2022-01-21 17:51:26 -08:00
}
2022-03-02 19:46:33 -08:00
}
2022-01-21 17:51:26 -08:00
2022-03-02 19:46:33 -08:00
private void OnUnsuspend(OWRigidbody suspendedBody) => netIdentity.UpdateAuthQueue(AuthQueueAction.Add);
private void OnSuspend(OWRigidbody suspendedBody) => netIdentity.UpdateAuthQueue(AuthQueueAction.Remove);
2022-02-13 04:15:43 -08:00
2022-03-02 19:46:33 -08:00
protected override void ApplyToAttached()
{
base.ApplyToAttached();
2022-02-13 16:57:27 -08:00
_qsbOrb.AttachedObject.SetTargetPosition(AttachedTransform.position);
2022-01-14 21:25:13 -08:00
}
2022-03-14 03:34:21 -07:00
}