124 lines
3.4 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-08-23 12:26:02 -07:00
var targetPos = ReferenceTransform.FromRelPos(UseInterpolation ? SmoothPosition : transform.position);
var targetRot = ReferenceTransform.FromRelRot(UseInterpolation ? SmoothRotation : transform.rotation);
2022-01-14 21:34:09 -08:00
if (ShouldMovePlayer)
2022-03-02 19:46:33 -08:00
{
if (Time.unscaledTime >= _lastSetPositionTime + ForcePositionAfterTime)
{
_lastSetPositionTime = Time.unscaledTime;
2022-01-14 21:34:09 -08:00
var playerBody = Locator.GetPlayerBody();
var relPos = AttachedTransform.ToRelPos(playerBody.GetPosition());
var relRot = AttachedTransform.ToRelRot(playerBody.GetRotation());
SetPosition(AttachedRigidbody, targetPos);
SetRotation(AttachedRigidbody, targetRot);
playerBody.SetPosition(AttachedTransform.FromRelPos(relPos));
playerBody.SetRotation(AttachedTransform.FromRelRot(relRot));
}
}
else
{
SetPosition(AttachedRigidbody, targetPos);
SetRotation(AttachedRigidbody, targetRot);
2022-03-02 19:46:33 -08:00
}
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);
}
#region copied from OWRigidbody
private static void SetPosition(OWRigidbody @this, Vector3 worldPosition)
{
@this._transform.position = worldPosition;
}
private static void SetRotation(OWRigidbody @this, Quaternion rotation)
2022-03-02 19:46:33 -08:00
{
@this._transform.rotation = rotation;
}
private static void SetVelocity(OWRigidbody @this, Vector3 newVelocity)
{
if (@this.RunningKinematicSimulation())
2022-03-02 19:46:33 -08:00
{
@this._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
{
@this._rigidbody.velocity = newVelocity + Locator.GetCenterOfTheUniverse().GetStaticFrameVelocity_Internal();
2022-01-14 21:34:09 -08:00
}
@this._lastVelocity = @this._currentVelocity;
@this._currentVelocity = newVelocity;
2022-01-14 21:34:09 -08:00
}
2022-03-02 19:46:33 -08:00
#endregion
2022-12-31 14:45:44 -08:00
private bool ShouldMovePlayer => Vector3.Distance(AttachedTransform.position, Locator.GetPlayerBody().GetPosition()) < 20;
protected override bool UseInterpolation => !ShouldMovePlayer;
2022-05-19 15:35:09 -07:00
}