103 lines
2.8 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;
2022-03-02 19:46:33 -08:00
namespace QSB.ShipSync.TransformSync;
public class ShipTransformSync : SectoredRigidbodySync
{
2022-03-02 19:46:33 -08:00
public static ShipTransformSync LocalInstance { get; private set; }
2022-05-19 14:34:49 +01:00
public ShipThrusterVariableSyncer ThrusterVariableSyncer { get; private set; }
2022-03-02 19:46:33 -08:00
private float _lastSetPositionTime;
private const float ForcePositionAfterTime = 1;
2022-01-14 21:34:09 -08:00
2022-05-19 15:35:09 -07:00
/// <summary>
/// normally prints error when attached object is null.
/// this overrides it so that doesn't happen, since the ship can be destroyed.
/// </summary>
protected override bool CheckValid()
=> AttachedTransform
&& base.CheckValid();
2022-03-02 19:46:33 -08:00
protected override bool CheckReady() =>
base.CheckReady() &&
Locator.GetShipBody();
2022-03-02 19:46:33 -08:00
public override void OnStartClient()
{
base.OnStartClient();
LocalInstance = this;
}
protected override OWRigidbody InitAttachedRigidbody()
{
SectorDetector.Init(Locator.GetShipDetector().GetComponent<SectorDetector>());
return Locator.GetShipBody();
}
2022-01-14 21:34:09 -08:00
2022-05-17 22:28:49 +01:00
protected override void Init()
{
base.Init();
2022-05-19 14:34:49 +01:00
ThrusterVariableSyncer = this.GetRequiredComponent<ShipThrusterVariableSyncer>();
ThrusterVariableSyncer.Init();
2022-05-17 22:28:49 +01:00
ShipThrusterManager.CreateShipVFX();
}
2022-03-02 19:46:33 -08:00
/// Dont do base... this is a replacement!
protected override void ApplyToAttached()
{
ApplyToSector();
if (!ReferenceTransform)
{
2022-03-02 19:46:33 -08:00
return;
}
2022-01-14 21:34:09 -08:00
2022-03-02 19:46:33 -08:00
var targetPos = ReferenceTransform.FromRelPos(transform.position);
var targetRot = ReferenceTransform.FromRelRot(transform.rotation);
2022-01-14 21:34:09 -08:00
if (PlayerState.IsInsideShip())
2022-03-02 19:46:33 -08:00
{
if (Time.unscaledTime >= _lastSetPositionTime + ForcePositionAfterTime)
{
_lastSetPositionTime = Time.unscaledTime;
2022-01-14 21:34:09 -08:00
AttachedRigidbody.SetPosition(targetPos);
AttachedRigidbody.SetRotation(targetRot);
}
}
else
{
2022-03-02 19:46:33 -08:00
AttachedRigidbody.SetPosition(targetPos);
AttachedRigidbody.SetRotation(targetRot);
}
2022-01-14 21:34:09 -08:00
2022-03-02 19:46:33 -08:00
var targetVelocity = ReferenceRigidbody.FromRelVel(Velocity, targetPos);
var targetAngularVelocity = ReferenceRigidbody.FromRelAngVel(AngularVelocity);
2022-01-14 21:34:09 -08:00
2022-03-02 19:46:33 -08:00
SetVelocity(AttachedRigidbody, targetVelocity);
AttachedRigidbody.SetAngularVelocity(targetAngularVelocity);
}
2022-03-02 19:46:33 -08:00
/// use OWRigidbody version instead of ShipBody override
private static void SetVelocity(OWRigidbody rigidbody, Vector3 newVelocity)
{
if (rigidbody.RunningKinematicSimulation())
{
rigidbody._kinematicRigidbody.velocity = newVelocity + Locator.GetCenterOfTheUniverse().GetStaticFrameVelocity_Internal();
2022-01-14 21:34:09 -08:00
}
2022-03-02 19:46:33 -08:00
else
2022-01-14 21:34:09 -08:00
{
2022-03-02 19:46:33 -08:00
rigidbody._rigidbody.velocity = newVelocity + Locator.GetCenterOfTheUniverse().GetStaticFrameVelocity_Internal();
2022-01-14 21:34:09 -08:00
}
2022-03-02 19:46:33 -08:00
rigidbody._lastVelocity = rigidbody._currentVelocity;
rigidbody._currentVelocity = newVelocity;
2022-01-14 21:34:09 -08:00
}
2022-03-02 19:46:33 -08:00
2022-08-22 20:45:24 -07:00
protected override bool UseInterpolation => !PlayerState.IsInsideShip(); /*TODO: test that this doesnt NRE*/
2022-05-19 15:35:09 -07:00
}