quantum-space-buddies/QSB/Syncs/Unsectored/Rigidbodies/UnsectoredRigidbodySync.cs

88 lines
2.7 KiB
C#
Raw Normal View History

2022-01-15 04:59:42 +00:00
using Mirror;
using QSB.Utility;
using UnityEngine;
2022-03-03 03:46:33 +00:00
namespace QSB.Syncs.Unsectored.Rigidbodies;
public abstract class UnsectoredRigidbodySync : BaseUnsectoredSync
{
2022-03-03 03:46:33 +00:00
private const float VelocityChangeThreshold = 0.05f;
private const float AngularVelocityChangeThreshold = 0.05f;
2022-01-15 04:59:42 +00:00
2022-03-03 03:46:33 +00:00
protected Vector3 Velocity;
protected Vector3 AngularVelocity;
private Vector3 _prevVelocity;
private Vector3 _prevAngularVelocity;
2022-01-15 04:59:42 +00:00
2022-03-03 03:46:33 +00:00
public OWRigidbody AttachedRigidbody { get; private set; }
public OWRigidbody ReferenceRigidbody { get; private set; }
2022-01-15 04:59:42 +00:00
2022-03-03 03:46:33 +00:00
protected abstract OWRigidbody InitAttachedRigidbody();
2022-01-15 04:59:42 +00:00
2022-03-03 03:46:33 +00:00
protected sealed override Transform InitAttachedTransform()
{
AttachedRigidbody = InitAttachedRigidbody();
return AttachedRigidbody ? AttachedRigidbody.transform : null;
}
2022-03-03 03:46:33 +00:00
public sealed override void SetReferenceTransform(Transform referenceTransform)
{
if (ReferenceTransform == referenceTransform)
{
2022-03-03 03:46:33 +00:00
return;
}
2022-01-15 04:59:42 +00:00
2022-03-03 03:46:33 +00:00
base.SetReferenceTransform(referenceTransform);
ReferenceRigidbody = ReferenceTransform ? ReferenceTransform.GetAttachedOWRigidbody() : null;
}
2022-01-15 04:59:42 +00:00
2022-03-03 03:46:33 +00:00
protected override bool HasChanged() =>
base.HasChanged() ||
Vector3.Distance(Velocity, _prevVelocity) > VelocityChangeThreshold ||
Vector3.Distance(AngularVelocity, _prevAngularVelocity) > AngularVelocityChangeThreshold;
2022-01-15 04:59:42 +00:00
2022-03-03 03:46:33 +00:00
protected override void Serialize(NetworkWriter writer)
{
writer.Write(Velocity);
writer.Write(AngularVelocity);
base.Serialize(writer);
}
2022-01-15 04:59:42 +00:00
2022-03-03 03:46:33 +00:00
protected override void UpdatePrevData()
{
base.UpdatePrevData();
_prevVelocity = Velocity;
_prevAngularVelocity = AngularVelocity;
}
2022-02-25 21:12:53 +00:00
2022-03-03 03:46:33 +00:00
protected override void Deserialize(NetworkReader reader)
{
Velocity = reader.ReadVector3();
AngularVelocity = reader.ReadVector3();
base.Deserialize(reader);
}
2022-01-15 04:59:42 +00:00
2022-03-03 03:46:33 +00:00
protected override void GetFromAttached()
{
var pos = AttachedRigidbody.GetPosition();
transform.position = ReferenceTransform.ToRelPos(pos);
2022-03-03 03:46:33 +00:00
transform.rotation = ReferenceTransform.ToRelRot(AttachedRigidbody.GetRotation());
Velocity = ReferenceRigidbody.ToRelVel(AttachedRigidbody.GetVelocity(), pos);
2022-03-03 03:46:33 +00:00
AngularVelocity = ReferenceRigidbody.ToRelAngVel(AttachedRigidbody.GetAngularVelocity());
}
2022-03-03 03:46:33 +00:00
protected override void ApplyToAttached()
{
var targetPos = ReferenceTransform.FromRelPos(UseInterpolation ? SmoothPosition : transform.position);
var targetRot = ReferenceTransform.FromRelRot(UseInterpolation ? SmoothRotation : transform.rotation);
2022-01-15 04:59:42 +00:00
2022-03-03 03:46:33 +00:00
AttachedRigidbody.MoveToPosition(targetPos);
AttachedRigidbody.MoveToRotation(targetRot);
2022-01-15 04:59:42 +00:00
2022-03-03 03:46:33 +00:00
var targetVelocity = ReferenceRigidbody.FromRelVel(Velocity, targetPos);
var targetAngularVelocity = ReferenceRigidbody.FromRelAngVel(AngularVelocity);
AttachedRigidbody.SetVelocity(targetVelocity);
AttachedRigidbody.SetAngularVelocity(targetAngularVelocity);
}
}