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

81 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;
2021-04-28 10:02:16 +01:00
using QSB.WorldSync;
using System.Collections.Generic;
using UnityEngine;
namespace QSB.OrbSync.TransformSync
{
2022-01-14 22:39:41 -08:00
public class NomaiOrbTransformSync : UnsectoredTransformSync
2022-01-14 21:25:13 -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>
2022-01-25 19:42:22 -08:00
protected override bool CheckValid() => AttachedTransform && base.CheckValid();
2022-01-16 06:11:44 -08:00
protected override bool UseInterpolation => true;
2022-01-16 08:04:15 -08:00
protected override float DistanceLeeway => 1f;
2022-01-14 21:25:13 -08:00
protected override Transform InitLocalTransform() => _qsbOrb.AttachedObject.transform;
protected override Transform InitRemoteTransform() => _qsbOrb.AttachedObject.transform;
private QSBOrb _qsbOrb;
2022-01-14 22:28:44 -08:00
private static readonly List<NomaiOrbTransformSync> _instances = new();
2022-01-14 21:25:13 -08:00
2022-01-18 12:19:58 -08:00
public override void OnStartClient()
2022-01-14 21:25:13 -08:00
{
_instances.Add(this);
2022-01-18 12:19:58 -08:00
base.OnStartClient();
2022-01-14 21:25:13 -08:00
}
2022-01-18 15:06:45 -08:00
public override void OnStopClient()
2022-01-14 21:25:13 -08:00
{
_instances.Remove(this);
2022-01-18 15:06:45 -08:00
base.OnStopClient();
2022-01-14 21:25:13 -08:00
}
protected override void Init()
{
2022-01-25 19:42:22 -08:00
_qsbOrb = OrbManager.Orbs[_instances.IndexOf(this)].GetWorldObject<QSBOrb>();
2022-01-14 22:07:32 -08:00
_qsbOrb.TransformSync = this;
2022-01-14 21:25:13 -08:00
base.Init();
2022-01-25 19:42:22 -08:00
var body = AttachedTransform.GetAttachedOWRigidbody();
SetReferenceTransform(body.GetOrigParent());
2022-01-14 21:25:13 -08:00
if (QSBCore.IsHost)
{
netIdentity.RegisterAuthQueue();
}
2022-01-25 19:42:22 -08:00
body.OnUnsuspendOWRigidbody += OnUnsuspend;
body.OnSuspendOWRigidbody += OnSuspend;
netIdentity.SendAuthQueueMessage(body.IsSuspended() ? AuthQueueAction.Remove : AuthQueueAction.Add);
2022-01-14 21:25:13 -08:00
}
2022-01-21 17:51:26 -08:00
protected override void Uninit()
{
if (QSBCore.IsHost)
{
netIdentity.UnregisterAuthQueue();
}
2022-01-25 19:42:22 -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();
body.OnUnsuspendOWRigidbody -= OnUnsuspend;
body.OnSuspendOWRigidbody -= OnSuspend;
}
2022-01-21 17:51:26 -08:00
base.Uninit();
}
2022-01-14 21:25:13 -08:00
private void OnUnsuspend(OWRigidbody suspendedBody) => netIdentity.SendAuthQueueMessage(AuthQueueAction.Add);
private void OnSuspend(OWRigidbody suspendedBody) => netIdentity.SendAuthQueueMessage(AuthQueueAction.Remove);
}
2021-04-28 10:02:16 +01:00
}