108 lines
3.2 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 PositionMovedThreshold = 0.05f;
private const float AngleRotatedThreshold = 0.05f;
private const float VelocityChangeThreshold = 0.05f;
private const float AngVelocityChangeThreshold = 0.05f;
2022-01-14 20:59:42 -08:00
protected Vector3 _relativeVelocity;
protected Vector3 _relativeAngularVelocity;
2022-01-16 06:53:45 -08:00
private Vector3 _prevVelocity;
private Vector3 _prevAngularVelocity;
2022-01-14 20:59:42 -08:00
2022-01-16 06:53:45 -08:00
protected OWRigidbody AttachedRigidbody { 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
}
protected override void UpdatePrevData()
{
2022-01-16 04:51:37 -08:00
base.UpdatePrevData();
2022-01-14 20:59:42 -08:00
_prevVelocity = _relativeVelocity;
_prevAngularVelocity = _relativeAngularVelocity;
}
protected override void Serialize(NetworkWriter writer)
2022-01-14 20:59:42 -08:00
{
base.Serialize(writer);
2022-01-14 20:59:42 -08:00
writer.Write(_relativeVelocity);
writer.Write(_relativeAngularVelocity);
}
protected override void Deserialize(NetworkReader reader)
2022-01-14 20:59:42 -08:00
{
base.Deserialize(reader);
2022-01-16 04:51:37 -08:00
_relativeVelocity = reader.ReadVector3();
_relativeAngularVelocity = 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());
_relativeVelocity = ReferenceTransform.GetAttachedOWRigidbody().ToRelVel(AttachedRigidbody.GetVelocity(), AttachedRigidbody.GetPosition());
_relativeAngularVelocity = ReferenceTransform.GetAttachedOWRigidbody().ToRelAngVel(AttachedRigidbody.GetAngularVelocity());
}
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 = ReferenceTransform.GetAttachedOWRigidbody().FromRelVel(_relativeVelocity, targetPos);
var targetAngularVelocity = ReferenceTransform.GetAttachedOWRigidbody().FromRelAngVel(_relativeAngularVelocity);
AttachedRigidbody.SetVelocity(targetVelocity);
AttachedRigidbody.SetAngularVelocity(targetAngularVelocity);
}
protected override bool HasChanged()
{
2022-01-16 06:53:45 -08:00
if (Vector3.Distance(transform.position, _prevPosition) > PositionMovedThreshold)
{
return true;
}
2022-01-16 06:53:45 -08:00
if (Quaternion.Angle(transform.rotation, _prevRotation) > AngleRotatedThreshold)
{
return true;
}
2022-01-16 06:53:45 -08:00
if (Vector3.Distance(_relativeVelocity, _prevVelocity) > VelocityChangeThreshold)
{
return true;
}
2022-01-16 06:53:45 -08:00
if (Vector3.Distance(_relativeAngularVelocity, _prevAngularVelocity) > AngVelocityChangeThreshold)
{
return true;
}
return false;
}
}
}