quantum-space-buddies/QSB/Anglerfish/TransformSync/AnglerTransformSync.cs

94 lines
3.1 KiB
C#
Raw Normal View History

2022-01-16 14:53:45 +00:00
using QSB.Anglerfish.WorldObjects;
2023-05-08 18:30:59 +00:00
using QSB.OwnershipSync;
2021-11-10 01:56:45 +00:00
using QSB.Syncs.Unsectored.Rigidbodies;
using QSB.Utility.LinkedWorldObject;
2021-11-10 01:56:45 +00:00
using QSB.WorldSync;
using UnityEngine;
2021-11-10 01:56:45 +00:00
2022-03-03 03:46:33 +00:00
namespace QSB.Anglerfish.TransformSync;
public class AnglerTransformSync : UnsectoredRigidbodySync, ILinkedNetworkBehaviour
2021-11-10 01:56:45 +00:00
{
2022-03-03 03:46:33 +00:00
protected override bool UseInterpolation => false;
protected override bool AllowInactiveAttachedObject => true; // since they deactivate when suspended
2022-01-15 05:29:57 +00:00
private QSBAngler _qsbAngler;
public void SetWorldObject(IWorldObject worldObject) => _qsbAngler = (QSBAngler)worldObject;
2022-01-15 05:29:57 +00:00
2022-03-03 03:46:33 +00:00
protected override OWRigidbody InitAttachedRigidbody()
=> _qsbAngler.AttachedObject._anglerBody;
2022-01-15 05:29:57 +00:00
2022-03-03 03:46:33 +00:00
public override void OnStartClient()
{
if (QSBCore.IsHost)
2022-01-15 05:29:57 +00:00
{
2023-05-08 18:41:43 +00:00
netIdentity.RegisterOwnerQueue();
2022-01-15 05:29:57 +00:00
}
2022-03-03 03:46:33 +00:00
base.OnStartClient();
}
2022-01-15 05:29:57 +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:29:57 +00:00
2022-03-03 03:46:33 +00:00
base.OnStopClient();
}
2022-03-03 03:46:33 +00:00
protected override float SendInterval => 1;
protected override bool UseReliableRpc => true;
2022-03-03 03:46:33 +00:00
protected override void Init()
{
base.Init();
SetReferenceTransform(_qsbAngler.AttachedObject._brambleBody.transform);
2022-03-03 03:46:33 +00:00
AttachedRigidbody.OnUnsuspendOWRigidbody += OnUnsuspend;
AttachedRigidbody.OnSuspendOWRigidbody += OnSuspend;
2023-05-08 18:41:43 +00:00
netIdentity.UpdateOwnerQueue(AttachedRigidbody.IsSuspended() ? OwnerQueueAction.Remove : OwnerQueueAction.Add);
2022-03-03 03:46:33 +00:00
}
protected override void Uninit()
{
base.Uninit();
AttachedRigidbody.OnUnsuspendOWRigidbody -= OnUnsuspend;
AttachedRigidbody.OnSuspendOWRigidbody -= OnSuspend;
}
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-03-03 03:46:33 +00:00
protected override void OnRenderObject()
{
if (!QSBCore.DebugSettings.DrawLines
|| !IsValid
|| !ReferenceTransform
|| !AttachedTransform.gameObject.activeInHierarchy)
2022-03-03 03:46:33 +00:00
{
return;
}
2022-01-15 05:29:57 +00:00
2022-03-03 03:46:33 +00:00
base.OnRenderObject();
Popcron.Gizmos.Sphere(AttachedRigidbody.GetPosition(), _qsbAngler.AttachedObject._arrivalDistance, Color.blue);
Popcron.Gizmos.Sphere(AttachedRigidbody.GetPosition(), _qsbAngler.AttachedObject._pursueDistance, Color.red);
Popcron.Gizmos.Sphere(AttachedRigidbody.GetPosition(), _qsbAngler.AttachedObject._escapeDistance, Color.yellow);
2022-03-03 03:46:33 +00:00
Popcron.Gizmos.Sphere(AttachedRigidbody.GetPosition()
+ AttachedRigidbody.transform.TransformDirection(_qsbAngler.AttachedObject._mouthOffset), 3, Color.grey);
2022-03-03 03:46:33 +00:00
if (_qsbAngler.TargetTransform)
2022-01-15 05:29:57 +00:00
{
Popcron.Gizmos.Line(_qsbAngler.TargetTransform.position, AttachedRigidbody.GetPosition(), Color.gray);
Popcron.Gizmos.Line(_qsbAngler.TargetTransform.position, _qsbAngler.TargetTransform.position + _qsbAngler.TargetVelocity, Color.green);
Popcron.Gizmos.Line(AttachedRigidbody.GetPosition(), _qsbAngler.AttachedObject._targetPos, Color.red);
Popcron.Gizmos.Sphere(_qsbAngler.AttachedObject._targetPos, 5, Color.red);
2021-11-10 01:56:45 +00:00
}
2022-03-03 03:46:33 +00:00
// Popcron.Gizmos.Line(AttachedObject.GetPosition(), _qsbAngler.AttachedObject.GetTargetPosition(), Color.white);
2021-11-10 01:56:45 +00:00
}
}