76 lines
2.1 KiB
C#
Raw Normal View History

2021-08-14 15:03:01 +01:00
using QSB.Syncs.Sectored.Rigidbodies;
2021-12-13 16:02:31 -08:00
using QSB.Utility;
2021-11-19 00:28:36 -08:00
using UnityEngine;
namespace QSB.ShipSync.TransformSync
{
public class ShipTransformSync : SectoredRigidbodySync
{
public static ShipTransformSync LocalInstance { get; private set; }
private float _lastSetPositionTime;
private const float ForcePositionAfterTime = 1;
2022-01-14 21:34:09 -08:00
protected override bool CheckReady() =>
base.CheckReady() &&
Locator.GetShipBody();
public override void OnStartClient()
2022-01-14 21:34:09 -08:00
{
base.OnStartClient();
LocalInstance = this;
2022-01-14 21:34:09 -08:00
}
protected override OWRigidbody InitAttachedRigidbody()
{
SectorDetector.Init(Locator.GetShipDetector().GetComponent<SectorDetector>());
return Locator.GetShipBody();
}
2022-01-14 21:34:09 -08:00
/// Dont do base... this is a replacement!
protected override void ApplyToAttached()
{
ApplyToSector();
if (!ReferenceTransform)
{
return;
}
2022-01-14 21:34:09 -08:00
var targetPos = ReferenceTransform.FromRelPos(transform.position);
2022-01-14 21:34:09 -08:00
if (Time.unscaledTime >= _lastSetPositionTime + ForcePositionAfterTime)
{
_lastSetPositionTime = Time.unscaledTime;
2022-01-14 21:34:09 -08:00
var targetRot = ReferenceTransform.FromRelRot(transform.rotation);
2022-01-14 21:34:09 -08:00
AttachedRigidbody.SetPosition(targetPos);
AttachedRigidbody.SetRotation(targetRot);
}
2022-01-14 21:34:09 -08:00
var targetVelocity = ReferenceRigidbody.FromRelVel(Velocity, targetPos);
var targetAngularVelocity = ReferenceRigidbody.FromRelAngVel(AngularVelocity);
SetVelocity(AttachedRigidbody, targetVelocity);
AttachedRigidbody.SetAngularVelocity(targetAngularVelocity);
2022-01-14 21:34:09 -08:00
}
/// use OWRigidbody version instead of ShipBody override
private static void SetVelocity(OWRigidbody rigidbody, Vector3 newVelocity)
2022-01-14 21:34:09 -08:00
{
if (rigidbody.RunningKinematicSimulation())
{
rigidbody._kinematicRigidbody.velocity = newVelocity + Locator.GetCenterOfTheUniverse().GetStaticFrameVelocity_Internal();
}
else
{
rigidbody._rigidbody.velocity = newVelocity + Locator.GetCenterOfTheUniverse().GetStaticFrameVelocity_Internal();
}
rigidbody._lastVelocity = rigidbody._currentVelocity;
rigidbody._currentVelocity = newVelocity;
2022-01-14 21:34:09 -08:00
}
protected override bool UseInterpolation => false;
2022-01-14 21:34:09 -08:00
}
}