98 lines
3.0 KiB
C#
Raw Normal View History

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