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

98 lines
3.2 KiB
C#
Raw Normal View History

2022-01-16 14:53:45 +00:00
using QSB.Anglerfish.WorldObjects;
2021-12-24 01:07:29 +00:00
using QSB.AuthoritySync;
2021-11-10 01:56:45 +00:00
using QSB.Syncs.Unsectored.Rigidbodies;
using QSB.WorldSync;
2021-11-25 15:32:34 +00:00
using System.Collections.Generic;
using UnityEngine;
2021-11-10 01:56:45 +00:00
namespace QSB.Anglerfish.TransformSync;
public class AnglerTransformSync : UnsectoredRigidbodySync
2021-11-10 01:56:45 +00:00
{
protected override bool UseInterpolation => false;
2022-02-25 22:50:37 +00:00
protected override bool AllowInactiveAttachedObject => true; // since they deactivate when suspended
2022-01-15 05:29:57 +00:00
private QSBAngler _qsbAngler;
private static readonly List<AnglerTransformSync> _instances = new();
2022-01-15 05:29:57 +00:00
protected override OWRigidbody InitAttachedRigidbody()
=> _qsbAngler.AttachedObject._anglerBody;
2022-01-15 05:29:57 +00:00
public override void OnStartClient()
{
_instances.Add(this);
if (QSBCore.IsHost)
2022-01-15 05:29:57 +00:00
{
netIdentity.RegisterAuthQueue();
2022-01-15 05:29:57 +00:00
}
base.OnStartClient();
}
public override void OnStopClient()
{
_instances.Remove(this);
if (QSBCore.IsHost)
{
netIdentity.UnregisterAuthQueue();
2022-01-15 05:29:57 +00:00
}
base.OnStopClient();
}
2022-01-15 05:29:57 +00:00
protected override float SendInterval => 1;
protected override bool UseReliableRpc => true;
2022-01-15 05:29:57 +00:00
protected override void Init()
{
_qsbAngler = AnglerManager.Anglers[_instances.IndexOf(this)].GetWorldObject<QSBAngler>();
_qsbAngler.TransformSync = this;
2022-01-15 05:29:57 +00:00
base.Init();
SetReferenceTransform(_qsbAngler.AttachedObject._brambleBody.transform);
2022-01-15 05:29:57 +00:00
AttachedRigidbody.OnUnsuspendOWRigidbody += OnUnsuspend;
AttachedRigidbody.OnSuspendOWRigidbody += OnSuspend;
netIdentity.UpdateAuthQueue(AttachedRigidbody.IsSuspended() ? AuthQueueAction.Remove : AuthQueueAction.Add);
}
protected override void Uninit()
{
base.Uninit();
AttachedRigidbody.OnUnsuspendOWRigidbody -= OnUnsuspend;
AttachedRigidbody.OnSuspendOWRigidbody -= OnSuspend;
}
private void OnUnsuspend(OWRigidbody suspendedBody) => netIdentity.UpdateAuthQueue(AuthQueueAction.Add);
private void OnSuspend(OWRigidbody suspendedBody) => netIdentity.UpdateAuthQueue(AuthQueueAction.Remove);
protected override void OnRenderObject()
{
if (!QSBCore.DebugSettings.DrawLines
|| !IsValid
2022-02-25 22:50:37 +00:00
|| !ReferenceTransform
|| !AttachedTransform.gameObject.activeInHierarchy)
{
return;
2022-01-22 01:51:26 +00:00
}
base.OnRenderObject();
2022-01-15 05:29:57 +00:00
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);
Popcron.Gizmos.Sphere(AttachedRigidbody.GetPosition()
+ AttachedRigidbody.transform.TransformDirection(_qsbAngler.AttachedObject._mouthOffset), 3, Color.grey);
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
}
// Popcron.Gizmos.Line(AttachedObject.GetPosition(), _qsbAngler.AttachedObject.GetTargetPosition(), Color.white);
2021-11-10 01:56:45 +00:00
}
}