quantum-space-buddies/QSB/JellyfishSync/TransformSync/JellyfishTransformSync.cs

144 lines
3.9 KiB
C#
Raw Normal View History

2021-12-01 09:46:43 +00:00
using System.Collections.Generic;
using QSB.JellyfishSync.WorldObjects;
using QSB.Syncs;
2021-12-01 09:10:38 +00:00
using QSB.Syncs.Unsectored.Rigidbodies;
using QSB.Utility;
2021-12-01 09:10:38 +00:00
using QSB.WorldSync;
using QuantumUNET.Transport;
2021-12-01 09:44:12 +00:00
using UnityEngine;
2021-12-01 09:10:38 +00:00
namespace QSB.JellyfishSync.TransformSync
{
public class JellyfishTransformSync : UnsectoredRigidbodySync
{
public override bool IsReady => QSBCore.WorldObjectsReady;
public override bool UseInterpolation => false;
private QSBJellyfish _qsbJellyfish;
2021-12-01 09:46:43 +00:00
private static readonly List<JellyfishTransformSync> _instances = new();
2021-12-01 09:10:38 +00:00
protected override OWRigidbody GetRigidbody()
=> _qsbJellyfish.AttachedObject._jellyfishBody;
public override void Start()
{
2021-12-01 09:46:43 +00:00
_instances.Add(this);
2021-12-01 09:10:38 +00:00
base.Start();
}
protected override void OnDestroy()
{
2021-12-01 09:46:43 +00:00
_instances.Remove(this);
2021-12-01 09:10:38 +00:00
base.OnDestroy();
}
2021-12-01 09:44:12 +00:00
public override float GetNetworkSendInterval() => 10;
2021-12-01 09:10:38 +00:00
protected override void Init()
{
2021-12-01 09:46:43 +00:00
_qsbJellyfish = QSBWorldSync.GetWorldFromId<QSBJellyfish>(_instances.IndexOf(this));
2021-12-01 09:10:38 +00:00
_qsbJellyfish.TransformSync = this;
base.Init();
SetReferenceTransform(_qsbJellyfish.AttachedObject._planetBody.transform);
}
private bool _shouldUpdate;
public override void DeserializeTransform(QNetworkReader reader, bool initialState)
{
base.DeserializeTransform(reader, initialState);
_shouldUpdate = true;
}
/// replacement using SetPosition/Rotation instead of Move
2021-12-01 09:10:38 +00:00
protected override bool UpdateTransform()
{
if (HasAuthority)
{
_qsbJellyfish.Align = true;
SetValuesToSync();
return true;
2021-12-01 09:10:38 +00:00
}
if (!_shouldUpdate)
{
return false;
}
_shouldUpdate = false;
var targetPos = ReferenceTransform.DecodePos(transform.position);
var targetRot = ReferenceTransform.DecodeRot(transform.rotation);
if (targetPos == Vector3.zero || transform.position == Vector3.zero)
{
return false;
}
var positionToSet = targetPos;
var rotationToSet = targetRot;
if (UseInterpolation)
{
positionToSet = SmartSmoothDamp(AttachedObject.transform.position, targetPos);
rotationToSet = QuaternionHelper.SmoothDamp(AttachedObject.transform.rotation, targetRot, ref _rotationSmoothVelocity, SmoothTime);
}
var hasMoved = CustomHasMoved(
transform.position,
_localPrevPosition,
transform.rotation,
_localPrevRotation,
_relativeVelocity,
_localPrevVelocity,
_relativeAngularVelocity,
_localPrevAngularVelocity);
_localPrevPosition = transform.position;
_localPrevRotation = transform.rotation;
_localPrevVelocity = _relativeVelocity;
_localPrevAngularVelocity = _relativeAngularVelocity;
if (!hasMoved)
{
return true;
}
_qsbJellyfish.Align = false;
((OWRigidbody)AttachedObject).SetPosition(positionToSet);
((OWRigidbody)AttachedObject).SetRotation(rotationToSet);
var targetVelocity = ReferenceTransform.GetAttachedOWRigidbody().DecodeVel(_relativeVelocity, targetPos);
var targetAngularVelocity = ReferenceTransform.GetAttachedOWRigidbody().DecodeAngVel(_relativeAngularVelocity);
((OWRigidbody)AttachedObject).SetVelocity(targetVelocity);
((OWRigidbody)AttachedObject).SetAngularVelocity(targetAngularVelocity);
return true;
2021-12-01 09:10:38 +00:00
}
2021-12-01 09:44:12 +00:00
protected override void OnRenderObject()
{
if (!QSBCore.WorldObjectsReady
|| !QSBCore.ShowLinesInDebug
|| !IsReady
|| ReferenceTransform == null
|| ((OWRigidbody)AttachedObject).IsSuspended())
{
return;
}
base.OnRenderObject();
2021-12-01 09:44:12 +00:00
var jellyfish = _qsbJellyfish.AttachedObject;
var position = ReferenceTransform.position;
var dir = Vector3.Normalize(jellyfish.transform.position - position);
// Popcron.Gizmos.Line(position + dir * jellyfish._lowerLimit, position + dir * jellyfish._upperLimit, Color.magenta);
2021-12-01 09:44:12 +00:00
Popcron.Gizmos.Sphere(position + dir * jellyfish._lowerLimit, 10f, Color.magenta);
Popcron.Gizmos.Sphere(position + dir * jellyfish._upperLimit, 10f, Color.magenta);
}
2021-12-01 09:10:38 +00:00
}
}