105 lines
2.6 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-12-13 16:02:31 -08:00
using QSB.Utility;
2021-07-11 16:18:47 +01:00
using QSB.WorldSync;
2021-11-19 00:28:36 -08:00
using UnityEngine;
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-12-10 22:13:39 +00:00
public override bool IsPlayerObject => false;
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
{
QSBCore.UnityEvents.RunWhen(() => WorldObjectManager.AllObjectsReady, () => 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-12-13 13:58:37 -08:00
if (ReferenceTransform == null || transform.position == Vector3.zero)
2021-09-13 19:22:48 +01:00
{
return;
}
2021-12-13 16:02:31 -08:00
var targetPos = ReferenceTransform.FromRelPos(transform.position);
var targetRot = ReferenceTransform.FromRelRot(transform.rotation);
2021-08-22 16:42:57 +01:00
2021-12-13 13:26:54 -08:00
AttachedObject.SetPosition(targetPos);
AttachedObject.SetRotation(targetRot);
2021-08-22 16:42:57 +01:00
}
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
}
2021-12-13 13:58:37 -08:00
if (ReferenceTransform == null || transform.position == Vector3.zero)
{
2021-12-13 13:58:37 -08:00
return false;
}
2021-12-13 16:02:31 -08:00
var targetPos = ReferenceTransform.FromRelPos(transform.position);
2021-08-22 16:42:57 +01:00
2021-12-13 16:02:31 -08:00
var targetVelocity = ReferenceTransform.GetAttachedOWRigidbody().FromRelVel(_relativeVelocity, targetPos);
var targetAngularVelocity = ReferenceTransform.GetAttachedOWRigidbody().FromRelAngVel(_relativeAngularVelocity);
2021-05-15 14:10:51 +01:00
2021-12-13 13:26:54 -08:00
SetVelocity(AttachedObject, targetVelocity);
AttachedObject.SetAngularVelocity(targetAngularVelocity);
2021-08-22 16:42:57 +01:00
return true;
}
2021-05-15 14:10:51 +01:00
2021-11-19 00:28:36 -08:00
/// use OWRigidbody version instead of ShipBody override
private void SetVelocity(OWRigidbody rigidbody, Vector3 newVelocity)
{
if (rigidbody.RunningKinematicSimulation())
{
rigidbody._kinematicRigidbody.velocity = newVelocity + Locator.GetCenterOfTheUniverse().GetStaticFrameVelocity_Internal();
}
else
{
rigidbody._rigidbody.velocity = newVelocity + Locator.GetCenterOfTheUniverse().GetStaticFrameVelocity_Internal();
}
2021-11-25 15:38:05 +00:00
2021-11-19 00:28:36 -08:00
rigidbody._lastVelocity = rigidbody._currentVelocity;
rigidbody._currentVelocity = newVelocity;
}
2021-12-13 13:58:37 -08:00
public override bool UseInterpolation => false;
2020-12-02 21:29:53 +00:00
}
}