85 lines
2.0 KiB
C#
Raw Normal View History

2021-05-15 14:10:51 +01:00
using QSB.SectorSync;
2021-08-14 15:03:01 +01:00
using QSB.Syncs.Sectored.Rigidbodies;
2021-07-11 16:18:47 +01:00
using QSB.WorldSync;
2021-04-11 17:05:02 +01:00
namespace QSB.ShipSync.TransformSync
{
public class ShipTransformSync : SectoredRigidbodySync
2020-12-02 21:29:53 +00:00
{
2021-05-10 14:30:38 +01:00
public static ShipTransformSync LocalInstance { get; private set; }
2020-12-02 21:29:53 +00:00
2021-08-22 16:42:57 +01:00
private const int ForcePositionAfterUpdates = 50;
private int _updateCount;
2021-05-10 14:32:47 +01:00
public override bool IsReady
2021-07-17 09:51:47 +01:00
=> Locator.GetShipBody() != null;
2021-05-10 14:30:38 +01:00
2021-05-10 20:26:44 +01:00
public override void Start()
{
base.Start();
LocalInstance = this;
}
2020-12-02 21:29:53 +00:00
protected override OWRigidbody GetRigidbody()
2021-03-09 19:45:00 +00:00
{
2021-08-22 16:42:57 +01:00
QSBCore.UnityEvents.RunWhen(() => WorldObjectManager.AllReady, () => SectorSync.Init(Locator.GetShipDetector().GetComponent<SectorDetector>(), TargetType.Ship));
return Locator.GetShipBody();
2021-05-10 20:26:44 +01:00
}
2021-05-15 14:10:51 +01:00
2021-08-22 16:42:57 +01:00
private void ForcePosition()
{
2021-09-13 19:22:48 +01:00
if (ReferenceTransform == null)
{
return;
}
2021-08-22 16:42:57 +01:00
var targetPos = _intermediaryTransform.GetTargetPosition_Unparented();
var targetRot = _intermediaryTransform.GetTargetRotation_Unparented();
(AttachedObject as OWRigidbody).SetPosition(targetPos);
(AttachedObject as OWRigidbody).SetRotation(targetRot);
}
2021-07-11 16:18:47 +01:00
protected override bool UpdateTransform()
2021-05-15 14:10:51 +01:00
{
2021-09-13 19:22:48 +01:00
if (!UpdateSectors())
{
return false;
}
2021-09-13 19:22:48 +01:00
// Dont do base... this is a replacement!
2021-08-22 16:42:57 +01:00
if (HasAuthority)
2021-05-15 14:10:51 +01:00
{
2021-09-13 19:22:48 +01:00
SetValuesToSync();
return true;
2021-05-15 14:10:51 +01:00
}
2021-08-22 16:42:57 +01:00
_updateCount++;
if (_updateCount >= ForcePositionAfterUpdates)
2021-05-15 14:10:51 +01:00
{
2021-08-22 16:42:57 +01:00
_updateCount = 0;
ForcePosition();
2021-05-15 14:10:51 +01:00
}
if (ReferenceTransform == null)
{
return true;
}
2021-08-22 16:42:57 +01:00
var targetPos = _intermediaryTransform.GetTargetPosition_Unparented();
var targetVelocity = ReferenceTransform.GetAttachedOWRigidbody().GetPointVelocity(targetPos) + _relativeVelocity;
var targetAngularVelocity = ReferenceTransform.GetAttachedOWRigidbody().GetAngularVelocity() + _relativeAngularVelocity;
2021-05-15 14:10:51 +01:00
2021-08-22 16:42:57 +01:00
SetVelocity(AttachedObject as OWRigidbody, targetVelocity);
(AttachedObject as OWRigidbody).SetAngularVelocity(targetAngularVelocity);
return true;
}
2021-05-15 14:10:51 +01:00
public override bool UseInterpolation => true;
protected override float DistanceLeeway => 20f;
2020-12-02 21:29:53 +00:00
}
2020-12-03 08:28:05 +00:00
}