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

77 lines
2.1 KiB
C#
Raw Normal View History

2023-05-08 18:30:59 +00:00
using QSB.OrbSync.WorldObjects;
using QSB.OwnershipSync;
2021-10-20 16:29:49 +00:00
using QSB.Syncs.Unsectored.Transforms;
using QSB.Utility.LinkedWorldObject;
2021-04-28 09:02:16 +00:00
using QSB.WorldSync;
using UnityEngine;
2022-03-03 03:46:33 +00:00
namespace QSB.OrbSync.TransformSync;
public class NomaiOrbTransformSync : UnsectoredTransformSync, ILinkedNetworkBehaviour
2021-04-28 09:02:16 +00:00
{
2022-03-03 03:46:33 +00: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-26 03:42:22 +00:00
2022-03-03 03:46:33 +00:00
protected override bool UseInterpolation => true;
protected override float DistanceChangeThreshold => 1f;
2022-01-15 05:25:13 +00: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-03 03:46:33 +00:00
public override void OnStartClient()
{
if (QSBCore.IsHost)
2022-01-15 05:25:13 +00:00
{
2023-05-08 18:41:43 +00:00
netIdentity.RegisterOwnerQueue();
2022-01-15 05:25:13 +00:00
}
2022-03-03 03:46:33 +00:00
base.OnStartClient();
}
2022-01-15 05:25:13 +00:00
2022-03-03 03:46:33 +00:00
public override void OnStopClient()
{
if (QSBCore.IsHost)
{
2023-05-08 18:41:43 +00:00
netIdentity.UnregisterOwnerQueue();
}
2022-01-15 05:25:13 +00:00
2022-03-03 03:46:33 +00:00
base.OnStopClient();
}
2022-01-15 05:25:13 +00:00
2022-03-03 03:46:33 +00:00
protected override void Init()
{
base.Init();
var body = AttachedTransform.GetAttachedOWRigidbody();
SetReferenceTransform(body.GetOrigParent());
2022-03-03 03:46:33 +00:00
body.OnUnsuspendOWRigidbody += OnUnsuspend;
body.OnSuspendOWRigidbody += OnSuspend;
2023-05-08 18:41:43 +00:00
netIdentity.UpdateOwnerQueue(body.IsSuspended() ? OwnerQueueAction.Remove : OwnerQueueAction.Add);
2022-03-03 03:46:33 +00:00
}
protected override void Uninit()
{
base.Uninit();
var body = AttachedTransform.GetAttachedOWRigidbody();
body.OnUnsuspendOWRigidbody -= OnUnsuspend;
body.OnSuspendOWRigidbody -= OnSuspend;
2022-03-03 03:46:33 +00:00
}
2022-01-22 01:51:26 +00:00
2023-05-08 18:41:43 +00:00
private void OnUnsuspend(OWRigidbody suspendedBody) => netIdentity.UpdateOwnerQueue(OwnerQueueAction.Add);
private void OnSuspend(OWRigidbody suspendedBody) => netIdentity.UpdateOwnerQueue(OwnerQueueAction.Remove);
2022-02-13 12:15:43 +00:00
2022-03-03 03:46:33 +00:00
protected override void ApplyToAttached()
{
base.ApplyToAttached();
2022-02-14 00:57:27 +00:00
_qsbOrb.AttachedObject.SetTargetPosition(AttachedTransform.position);
2022-01-15 05:25:13 +00:00
}
2022-03-14 10:34:21 +00:00
}