105 lines
3.0 KiB
C#
Raw Normal View History

2021-11-10 09:18:57 +00:00
using QSB.Anglerfish.WorldObjects;
2021-11-09 17:56:45 -08:00
using QSB.Syncs.Unsectored.Rigidbodies;
using QSB.WorldSync;
using QuantumUNET.Transport;
2021-11-25 15:32:34 +00:00
using System.Collections.Generic;
using UnityEngine;
2021-11-09 17:56:45 -08:00
namespace QSB.Anglerfish.TransformSync
{
public class AnglerTransformSync : UnsectoredRigidbodySync
{
public override bool IsReady => WorldObjectManager.AllObjectsAdded;
public override bool UseInterpolation => false;
2021-11-09 17:56:45 -08:00
2021-11-09 18:38:19 -08:00
private QSBAngler _qsbAngler;
2021-11-20 19:49:50 +00:00
private static readonly List<AnglerTransformSync> _instances = new();
2021-11-10 01:20:16 -08:00
2021-11-10 09:28:05 +00:00
protected override OWRigidbody GetRigidbody()
=> _qsbAngler.AttachedObject._anglerBody;
2021-11-09 17:56:45 -08:00
public override void Start()
{
_instances.Add(this);
base.Start();
}
2021-11-10 01:20:16 -08:00
2021-11-09 17:56:45 -08:00
protected override void OnDestroy()
{
_instances.Remove(this);
base.OnDestroy();
}
public override float GetNetworkSendInterval() => 1;
2021-11-09 17:56:45 -08:00
protected override void Init()
{
2021-11-09 18:38:19 -08:00
_qsbAngler = QSBWorldSync.GetWorldFromId<QSBAngler>(_instances.IndexOf(this));
2021-11-10 09:28:05 +00:00
_qsbAngler.TransformSync = this;
2021-11-09 17:56:45 -08:00
base.Init();
SetReferenceTransform(_qsbAngler.AttachedObject._brambleBody.transform);
}
private bool _shouldUpdate;
public override void DeserializeTransform(QNetworkReader reader, bool initialState)
{
base.DeserializeTransform(reader, initialState);
if (!WorldObjectManager.AllObjectsReady || HasAuthority)
{
return;
}
_shouldUpdate = true;
}
protected override bool UpdateTransform()
{
if (HasAuthority)
{
return base.UpdateTransform();
}
if (!_shouldUpdate)
{
return false;
}
2021-11-25 15:38:05 +00:00
_shouldUpdate = false;
return base.UpdateTransform();
}
protected override void OnRenderObject()
{
if (!WorldObjectManager.AllObjectsReady
|| !QSBCore.ShowLinesInDebug
|| !IsReady
2021-12-01 01:44:12 -08:00
|| ReferenceTransform == null
|| ((OWRigidbody)AttachedObject).IsSuspended())
{
return;
}
base.OnRenderObject();
2021-12-01 01:44:12 -08:00
Popcron.Gizmos.Sphere(AttachedObject.transform.position, _qsbAngler.AttachedObject._arrivalDistance, Color.blue);
Popcron.Gizmos.Sphere(AttachedObject.transform.position, _qsbAngler.AttachedObject._pursueDistance, Color.red);
Popcron.Gizmos.Sphere(AttachedObject.transform.position, _qsbAngler.AttachedObject._escapeDistance, Color.yellow);
Popcron.Gizmos.Sphere(AttachedObject.transform.position
+ AttachedObject.transform.TransformDirection(_qsbAngler.AttachedObject._mouthOffset), 3, Color.grey);
if (_qsbAngler.TargetTransform != null)
{
Popcron.Gizmos.Line(_qsbAngler.TargetTransform.position, ((OWRigidbody)AttachedObject).GetPosition(), Color.gray);
Popcron.Gizmos.Line(_qsbAngler.TargetTransform.position, _qsbAngler.TargetTransform.position + _qsbAngler.TargetVelocity, Color.green);
Popcron.Gizmos.Line(((OWRigidbody)AttachedObject).GetPosition(), _qsbAngler.AttachedObject._targetPos, Color.red);
Popcron.Gizmos.Sphere(_qsbAngler.AttachedObject._targetPos, 5, Color.red);
}
// Popcron.Gizmos.Line(AttachedObject.transform.position, _qsbAngler.AttachedObject.GetTargetPosition(), Color.white);
2021-11-09 17:56:45 -08:00
}
}
}