split up UpdateTransform + refactor

This commit is contained in:
JohnCorby 2022-01-16 06:53:45 -08:00
parent 1f4c06561b
commit 7f1c6ea6c8
13 changed files with 141 additions and 389 deletions

View File

@ -1,5 +1,4 @@
using Mirror;
using QSB.Anglerfish.WorldObjects;
using QSB.Anglerfish.WorldObjects;
using QSB.AuthoritySync;
using QSB.Syncs.Unsectored.Rigidbodies;
using QSB.WorldSync;
@ -12,7 +11,7 @@ namespace QSB.Anglerfish.TransformSync
{
protected override bool IsReady => WorldObjectManager.AllObjectsAdded;
protected override bool UseInterpolation => false;
protected override bool IsPlayerObject => false;
protected override bool OnlyApplyOnDeserialize => true;
private QSBAngler _qsbAngler;
private static readonly List<AnglerTransformSync> _instances = new();
@ -64,36 +63,6 @@ namespace QSB.Anglerfish.TransformSync
private void OnUnsuspend(OWRigidbody suspendedBody) => netIdentity.SendAuthQueueMessage(AuthQueueAction.Add);
private void OnSuspend(OWRigidbody suspendedBody) => netIdentity.SendAuthQueueMessage(AuthQueueAction.Remove);
private bool _shouldUpdate;
protected override void Deserialize(NetworkReader reader)
{
base.Deserialize(reader);
if (!WorldObjectManager.AllObjectsReady)
{
return;
}
_shouldUpdate = true;
}
protected override bool UpdateTransform()
{
if (hasAuthority)
{
return base.UpdateTransform();
}
if (!_shouldUpdate)
{
return false;
}
_shouldUpdate = false;
return base.UpdateTransform();
}
protected override void OnRenderObject()
{
if (!QSBCore.ShowLinesInDebug

View File

@ -13,7 +13,7 @@ namespace QSB.JellyfishSync.TransformSync
{
protected override bool IsReady => WorldObjectManager.AllObjectsAdded;
protected override bool UseInterpolation => false;
protected override bool IsPlayerObject => false;
protected override bool OnlyApplyOnDeserialize => true;
private QSBJellyfish _qsbJellyfish;
private static readonly List<JellyfishTransformSync> _instances = new();
@ -69,76 +69,26 @@ namespace QSB.JellyfishSync.TransformSync
{
base.Serialize(writer);
if (!WorldObjectManager.AllObjectsReady)
{
writer.Write(false);
return;
}
_qsbJellyfish.Align = true;
writer.Write(_qsbJellyfish.IsRising);
}
private bool _shouldUpdate;
protected override void Deserialize(NetworkReader reader)
{
base.Deserialize(reader);
if (!WorldObjectManager.AllObjectsReady)
{
reader.ReadBool();
return;
}
_qsbJellyfish.Align = false;
_qsbJellyfish.IsRising = reader.ReadBool();
_shouldUpdate = true;
}
/// replacement using SetPosition/Rotation instead of Move
protected override bool UpdateTransform()
protected override void ApplyToAttached()
{
if (hasAuthority)
{
SetValuesToSync();
return true;
}
if (!_shouldUpdate)
{
return false;
}
_shouldUpdate = false;
var hasMoved = CustomHasMoved(
transform.position,
_localPrevPosition,
transform.rotation,
_localPrevRotation,
_relativeVelocity,
_localPrevVelocity,
_relativeAngularVelocity,
_localPrevAngularVelocity);
_localPrevPosition = transform.position;
_localPrevRotation = transform.rotation;
_localPrevVelocity = _relativeVelocity;
_localPrevAngularVelocity = _relativeAngularVelocity;
if (!hasMoved)
{
return true;
}
var pos = ReferenceTransform.FromRelPos(transform.position);
AttachedRigidbody.SetPosition(pos);
AttachedRigidbody.SetRotation(ReferenceTransform.FromRelRot(transform.rotation));
AttachedRigidbody.SetVelocity(ReferenceTransform.GetAttachedOWRigidbody().FromRelVel(_relativeVelocity, pos));
AttachedRigidbody.SetAngularVelocity(ReferenceTransform.GetAttachedOWRigidbody().FromRelAngVel(_relativeAngularVelocity));
return true;
}
protected override void OnRenderObject()

View File

@ -13,7 +13,6 @@ namespace QSB.OrbSync.TransformSync
{
protected override bool IsReady => WorldObjectManager.AllObjectsAdded;
protected override bool UseInterpolation => true;
protected override bool IsPlayerObject => false;
protected override float DistanceLeeway => 1f;
protected override Transform InitLocalTransform() => _qsbOrb.AttachedObject.transform;

View File

@ -97,7 +97,7 @@ namespace QSB.Player.TransformSync
protected override Transform InitLocalTransform()
{
QSBCore.UnityEvents.RunWhen(() => WorldObjectManager.AllObjectsReady, () => SectorSync.Init(Locator.GetPlayerSectorDetector(), TargetType.Player));
SectorSync.Init(Locator.GetPlayerSectorDetector(), TargetType.Player);
// player body
var player = Locator.GetPlayerTransform();
@ -235,37 +235,40 @@ namespace QSB.Player.TransformSync
return REMOTE_Player_Body.transform;
}
protected override bool UpdateTransform()
protected override void GetFromAttached()
{
if (!base.UpdateTransform())
{
return false;
}
base.GetFromAttached();
UpdateSpecificTransform(_visibleStickPivot, _networkStickPivot, ref _pivotPositionVelocity, ref _pivotRotationVelocity);
UpdateSpecificTransform(_visibleStickTip, _networkStickTip, ref _tipPositionVelocity, ref _tipRotationVelocity);
UpdateSpecificTransform(_visibleCameraRoot, _networkCameraRoot, ref _cameraPositionVelocity, ref _cameraRotationVelocity);
UpdateSpecificTransform(_visibleRoastingSystem, _networkRoastingSystem, ref _roastingPositionVelocity, ref _roastingRotationVelocity);
return true;
GetFromChild(_visibleStickPivot, _networkStickPivot);
GetFromChild(_visibleStickTip, _networkStickTip);
GetFromChild(_visibleCameraRoot, _networkCameraRoot);
GetFromChild(_visibleRoastingSystem, _networkRoastingSystem);
}
private void UpdateSpecificTransform(Transform visible, Transform network, ref Vector3 positionVelocity, ref Quaternion rotationVelocity)
protected override void ApplyToAttached()
{
if (hasAuthority)
{
network.localPosition = visible.localPosition;
network.localRotation = visible.localRotation;
return;
}
base.ApplyToAttached();
ApplyToChild(_visibleStickPivot, _networkStickPivot, ref _pivotPositionVelocity, ref _pivotRotationVelocity);
ApplyToChild(_visibleStickTip, _networkStickTip, ref _tipPositionVelocity, ref _tipRotationVelocity);
ApplyToChild(_visibleCameraRoot, _networkCameraRoot, ref _cameraPositionVelocity, ref _cameraRotationVelocity);
ApplyToChild(_visibleRoastingSystem, _networkRoastingSystem, ref _roastingPositionVelocity, ref _roastingRotationVelocity);
}
private static void GetFromChild(Transform visible, Transform network)
{
network.localPosition = visible.localPosition;
network.localRotation = visible.localRotation;
}
private static void ApplyToChild(Transform visible, Transform network, ref Vector3 positionVelocity, ref Quaternion rotationVelocity)
{
visible.localPosition = Vector3.SmoothDamp(visible.localPosition, network.localPosition, ref positionVelocity, SmoothTime);
visible.localRotation = QuaternionHelper.SmoothDamp(visible.localRotation, network.localRotation, ref rotationVelocity, SmoothTime);
}
protected override void OnRenderObject()
{
base.OnRenderObject();
if (!QSBCore.ShowLinesInDebug
|| !IsInitialized
|| ReferenceTransform == null)
@ -273,7 +276,8 @@ namespace QSB.Player.TransformSync
return;
}
Popcron.Gizmos.Cube(ReferenceTransform.TransformPoint(_networkRoastingSystem.position), ReferenceTransform.TransformRotation(_networkRoastingSystem.rotation), Vector3.one / 4, Color.red);
base.OnRenderObject();
Popcron.Gizmos.Cube(ReferenceTransform.TransformPoint(_networkRoastingSystem.position), ReferenceTransform.TransformRotation(_networkRoastingSystem.rotation), Vector3.one / 4, Color.red);
Popcron.Gizmos.Cube(ReferenceTransform.TransformPoint(_networkStickPivot.position), ReferenceTransform.TransformRotation(_networkStickPivot.rotation), Vector3.one / 4, Color.red);
Popcron.Gizmos.Cube(ReferenceTransform.TransformPoint(_networkStickTip.position), ReferenceTransform.TransformRotation(_networkStickTip.rotation), Vector3.one / 4, Color.red);

View File

@ -1,7 +1,6 @@
using QSB.SectorSync;
using QSB.Syncs.Sectored.Rigidbodies;
using QSB.Utility;
using QSB.WorldSync;
using UnityEngine;
namespace QSB.ShipSync.TransformSync
@ -10,8 +9,6 @@ namespace QSB.ShipSync.TransformSync
{
public static ShipTransformSync LocalInstance { get; private set; }
protected override bool IsPlayerObject => false;
private const int ForcePositionAfterUpdates = 50;
private int _updateCount;
@ -26,11 +23,11 @@ namespace QSB.ShipSync.TransformSync
protected override OWRigidbody InitAttachedRigidbody()
{
QSBCore.UnityEvents.RunWhen(() => WorldObjectManager.AllObjectsReady, () => SectorSync.Init(Locator.GetShipDetector().GetComponent<SectorDetector>(), TargetType.Ship));
SectorSync.Init(Locator.GetShipDetector().GetComponent<SectorDetector>(), TargetType.Ship);
return Locator.GetShipBody();
}
private void ForcePosition()
protected override void ApplyToAttached()
{
if (ReferenceTransform == null || transform.position == Vector3.zero)
{
@ -38,48 +35,27 @@ namespace QSB.ShipSync.TransformSync
}
var targetPos = ReferenceTransform.FromRelPos(transform.position);
var targetRot = ReferenceTransform.FromRelRot(transform.rotation);
AttachedRigidbody.SetPosition(targetPos);
AttachedRigidbody.SetRotation(targetRot);
}
protected override bool UpdateTransform()
{
// Dont do base... this is a replacement!
if (hasAuthority)
{
SetValuesToSync();
return true;
}
_updateCount++;
if (_updateCount >= ForcePositionAfterUpdates)
{
_updateCount = 0;
ForcePosition();
}
if (ReferenceTransform == null || transform.position == Vector3.zero)
{
return false;
}
var targetRot = ReferenceTransform.FromRelRot(transform.rotation);
var targetPos = ReferenceTransform.FromRelPos(transform.position);
AttachedRigidbody.SetPosition(targetPos);
AttachedRigidbody.SetRotation(targetRot);
}
var targetVelocity = ReferenceTransform.GetAttachedOWRigidbody().FromRelVel(_relativeVelocity, targetPos);
var targetAngularVelocity = ReferenceTransform.GetAttachedOWRigidbody().FromRelAngVel(_relativeAngularVelocity);
SetVelocity(AttachedRigidbody, targetVelocity);
AttachedRigidbody.SetAngularVelocity(targetAngularVelocity);
return true;
}
/// use OWRigidbody version instead of ShipBody override
private void SetVelocity(OWRigidbody rigidbody, Vector3 newVelocity)
private static void SetVelocity(OWRigidbody rigidbody, Vector3 newVelocity)
{
if (rigidbody.RunningKinematicSimulation())
{

View File

@ -9,15 +9,15 @@ namespace QSB.Syncs.Sectored.Rigidbodies
{
protected override bool DestroyAttachedObject => false;
public const float PositionMovedThreshold = 0.05f;
public const float AngleRotatedThreshold = 0.05f;
public const float VelocityChangeThreshold = 0.05f;
public const float AngVelocityChangeThreshold = 0.05f;
private const float PositionMovedThreshold = 0.05f;
private const float AngleRotatedThreshold = 0.05f;
private const float VelocityChangeThreshold = 0.05f;
private const float AngVelocityChangeThreshold = 0.05f;
protected Vector3 _relativeVelocity;
protected Vector3 _relativeAngularVelocity;
protected Vector3 _prevVelocity;
protected Vector3 _prevAngularVelocity;
private Vector3 _prevVelocity;
private Vector3 _prevAngularVelocity;
public OWRigidbody AttachedRigidbody { get; private set; }
@ -55,7 +55,7 @@ namespace QSB.Syncs.Sectored.Rigidbodies
}
}
protected void SetValuesToSync()
protected override void GetFromAttached()
{
if (ReferenceTransform != null)
{
@ -73,17 +73,11 @@ namespace QSB.Syncs.Sectored.Rigidbodies
}
}
protected override bool UpdateTransform()
protected override void ApplyToAttached()
{
if (hasAuthority)
{
SetValuesToSync();
return true;
}
if (ReferenceTransform == null || transform.position == Vector3.zero)
{
return false;
return;
}
var targetPos = ReferenceTransform.FromRelPos(transform.position);
@ -106,52 +100,26 @@ namespace QSB.Syncs.Sectored.Rigidbodies
AttachedRigidbody.SetVelocity(targetVelocity);
AttachedRigidbody.SetAngularVelocity(targetAngularVelocity);
return true;
}
protected override bool HasChanged()
=> CustomHasMoved(
transform.position,
_prevPosition,
transform.rotation,
_prevRotation,
_relativeVelocity,
_prevVelocity,
_relativeAngularVelocity,
_prevAngularVelocity);
// OPTIMIZE : optimize by using sqrMagnitude
private bool CustomHasMoved(
Vector3 newPosition,
Vector3 prevPosition,
Quaternion newRotation,
Quaternion prevRotation,
Vector3 newVelocity,
Vector3 prevVelocity,
Vector3 newAngVelocity,
Vector3 prevAngVelocity)
{
var displacementMagnitude = (newPosition - prevPosition).magnitude;
if (displacementMagnitude > PositionMovedThreshold)
if (Vector3.Distance(transform.position, _prevPosition) > PositionMovedThreshold)
{
return true;
}
if (Quaternion.Angle(newRotation, prevRotation) > AngleRotatedThreshold)
if (Quaternion.Angle(transform.rotation, _prevRotation) > AngleRotatedThreshold)
{
return true;
}
var velocityChangeMagnitude = (newVelocity - prevVelocity).magnitude;
var angularVelocityChangeMagnitude = (newAngVelocity - prevAngVelocity).magnitude;
if (velocityChangeMagnitude > VelocityChangeThreshold)
if (Vector3.Distance(_relativeVelocity, _prevVelocity) > VelocityChangeThreshold)
{
return true;
}
if (angularVelocityChangeMagnitude > AngVelocityChangeThreshold)
if (Vector3.Distance(_relativeAngularVelocity, _prevAngularVelocity) > AngVelocityChangeThreshold)
{
return true;
}

View File

@ -1,7 +1,6 @@
using Mirror;
using OWML.Common;
using QSB.Utility;
using QSB.WorldSync;
using UnityEngine;
namespace QSB.Syncs.Sectored.Transforms
@ -26,27 +25,25 @@ namespace QSB.Syncs.Sectored.Transforms
}
}
protected override bool UpdateTransform()
protected override void GetFromAttached()
{
if (hasAuthority)
if (ReferenceTransform != null)
{
if (ReferenceTransform != null)
{
transform.position = ReferenceTransform.ToRelPos(AttachedTransform.position);
transform.rotation = ReferenceTransform.ToRelRot(AttachedTransform.rotation);
}
else
{
transform.position = Vector3.zero;
transform.rotation = Quaternion.identity;
}
return true;
transform.position = ReferenceTransform.ToRelPos(AttachedTransform.position);
transform.rotation = ReferenceTransform.ToRelRot(AttachedTransform.rotation);
}
else
{
transform.position = Vector3.zero;
transform.rotation = Quaternion.identity;
}
}
protected override void ApplyToAttached()
{
if (ReferenceTransform == null || transform.position == Vector3.zero)
{
return false;
return;
}
if (UseInterpolation)
@ -59,8 +56,6 @@ namespace QSB.Syncs.Sectored.Transforms
AttachedTransform.position = ReferenceTransform.FromRelPos(transform.position);
AttachedTransform.rotation = ReferenceTransform.FromRelRot(transform.rotation);
}
return true;
}
}
}

View File

@ -1,4 +1,5 @@
using OWML.Common;
using Mirror;
using OWML.Common;
using QSB.Player;
using QSB.Utility;
using QSB.WorldSync;
@ -51,8 +52,8 @@ namespace QSB.Syncs
protected abstract bool AllowDisabledAttachedObject { get; }
protected abstract bool AllowNullReferenceTransform { get; }
protected abstract bool DestroyAttachedObject { get; }
protected abstract bool IsPlayerObject { get; }
protected virtual bool OnlyUpdateOnDeserialize => false;
protected virtual bool IsPlayerObject => false;
protected virtual bool OnlyApplyOnDeserialize => false;
public Transform AttachedTransform { get; private set; }
public Transform ReferenceTransform { get; private set; }
@ -70,9 +71,8 @@ namespace QSB.Syncs
protected Quaternion SmoothRotation;
protected abstract Transform InitAttachedTransform();
// protected abstract bool GetFromAttached();
// protected abstract bool ApplyToAttached();
protected abstract bool UpdateTransform();
protected abstract void GetFromAttached();
protected abstract void ApplyToAttached();
public virtual void Start()
{
@ -117,6 +117,16 @@ namespace QSB.Syncs
protected virtual void OnSceneLoaded(OWScene oldScene, OWScene newScene, bool isInUniverse) => IsInitialized = false;
private bool _shouldApply;
protected override void Deserialize(NetworkReader reader)
{
base.Deserialize(reader);
if (OnlyApplyOnDeserialize)
{
_shouldApply = true;
}
}
protected sealed override void Update()
{
if (!IsInitialized && IsReady && _baseIsReady)
@ -176,7 +186,23 @@ namespace QSB.Syncs
SmoothRotation = SmartSmoothDamp(SmoothRotation, transform.rotation);
}
UpdateTransform();
if (hasAuthority)
{
GetFromAttached();
}
else
{
if (OnlyApplyOnDeserialize && _shouldApply)
{
_shouldApply = false;
ApplyToAttached();
}
else
{
ApplyToAttached();
}
}
base.Update();
}

View File

@ -1,36 +1,22 @@
using Mirror;
using QSB.Utility;
using QSB.WorldSync;
using UnityEngine;
namespace QSB.Syncs.Unsectored.Rigidbodies
{
public abstract class UnsectoredRigidbodySync : BaseUnsectoredSync
{
public const float PositionMovedThreshold = 0.05f;
public const float AngleRotatedThreshold = 0.05f;
public const float VelocityChangeThreshold = 0.05f;
public const float AngVelocityChangeThreshold = 0.05f;
private const float PositionMovedThreshold = 0.05f;
private const float AngleRotatedThreshold = 0.05f;
private const float VelocityChangeThreshold = 0.05f;
private const float AngVelocityChangeThreshold = 0.05f;
protected Vector3 _relativeVelocity;
protected Vector3 _relativeAngularVelocity;
protected Vector3 _prevVelocity;
protected Vector3 _prevAngularVelocity;
private Vector3 _prevVelocity;
private Vector3 _prevAngularVelocity;
/// <summary>
/// The previous position of the VISIBLE object, as if parented to the reference.
/// </summary>
protected Vector3 _localPrevPosition;
/// <summary>
/// The previous rotation of the VISIBLE object, as if parented to the reference.
/// </summary>
protected Quaternion _localPrevRotation;
protected Vector3 _localPrevVelocity;
protected Vector3 _localPrevAngularVelocity;
public OWRigidbody AttachedRigidbody { get; set; }
protected OWRigidbody AttachedRigidbody { get; private set; }
protected abstract OWRigidbody InitAttachedRigidbody();
@ -61,7 +47,7 @@ namespace QSB.Syncs.Unsectored.Rigidbodies
_relativeAngularVelocity = reader.ReadVector3();
}
protected void SetValuesToSync()
protected override void GetFromAttached()
{
transform.position = ReferenceTransform.ToRelPos(AttachedRigidbody.GetPosition());
transform.rotation = ReferenceTransform.ToRelRot(AttachedRigidbody.GetRotation());
@ -69,14 +55,8 @@ namespace QSB.Syncs.Unsectored.Rigidbodies
_relativeAngularVelocity = ReferenceTransform.GetAttachedOWRigidbody().ToRelAngVel(AttachedRigidbody.GetAngularVelocity());
}
protected override bool UpdateTransform()
protected override void ApplyToAttached()
{
if (hasAuthority)
{
SetValuesToSync();
return true;
}
var targetPos = ReferenceTransform.FromRelPos(transform.position);
var targetRot = ReferenceTransform.FromRelRot(transform.rotation);
@ -89,26 +69,6 @@ namespace QSB.Syncs.Unsectored.Rigidbodies
rotationToSet = ReferenceTransform.FromRelRot(SmoothRotation);
}
var hasMoved = CustomHasMoved(
transform.position,
_localPrevPosition,
transform.rotation,
_localPrevRotation,
_relativeVelocity,
_localPrevVelocity,
_relativeAngularVelocity,
_localPrevAngularVelocity);
_localPrevPosition = transform.position;
_localPrevRotation = transform.rotation;
_localPrevVelocity = _relativeVelocity;
_localPrevAngularVelocity = _relativeAngularVelocity;
if (!hasMoved)
{
return true;
}
AttachedRigidbody.MoveToPosition(positionToSet);
AttachedRigidbody.MoveToRotation(rotationToSet);
@ -117,52 +77,26 @@ namespace QSB.Syncs.Unsectored.Rigidbodies
AttachedRigidbody.SetVelocity(targetVelocity);
AttachedRigidbody.SetAngularVelocity(targetAngularVelocity);
return true;
}
protected override bool HasChanged()
=> CustomHasMoved(
transform.position,
_prevPosition,
transform.rotation,
_prevRotation,
_relativeVelocity,
_prevVelocity,
_relativeAngularVelocity,
_prevAngularVelocity);
// OPTIMIZE : optimize by using sqrMagnitude
internal bool CustomHasMoved(
Vector3 newPosition,
Vector3 prevPosition,
Quaternion newRotation,
Quaternion prevRotation,
Vector3 newVelocity,
Vector3 prevVelocity,
Vector3 newAngVelocity,
Vector3 prevAngVelocity)
{
var displacementMagnitude = (newPosition - prevPosition).magnitude;
if (displacementMagnitude > PositionMovedThreshold)
if (Vector3.Distance(transform.position, _prevPosition) > PositionMovedThreshold)
{
return true;
}
if (Quaternion.Angle(newRotation, prevRotation) > AngleRotatedThreshold)
if (Quaternion.Angle(transform.rotation, _prevRotation) > AngleRotatedThreshold)
{
return true;
}
var velocityChangeMagnitude = (newVelocity - prevVelocity).magnitude;
var angularVelocityChangeMagnitude = (newAngVelocity - prevAngVelocity).magnitude;
if (velocityChangeMagnitude > VelocityChangeThreshold)
if (Vector3.Distance(_relativeVelocity, _prevVelocity) > VelocityChangeThreshold)
{
return true;
}
if (angularVelocityChangeMagnitude > AngVelocityChangeThreshold)
if (Vector3.Distance(_relativeAngularVelocity, _prevAngularVelocity) > AngVelocityChangeThreshold)
{
return true;
}

View File

@ -26,15 +26,14 @@ namespace QSB.Syncs.Unsectored.Transforms
transform.rotation = reader.ReadQuaternion();
}
protected override bool UpdateTransform()
protected override void GetFromAttached()
{
if (hasAuthority)
{
transform.position = ReferenceTransform.ToRelPos(AttachedTransform.position);
transform.rotation = ReferenceTransform.ToRelRot(AttachedTransform.rotation);
return true;
}
transform.position = ReferenceTransform.ToRelPos(AttachedTransform.position);
transform.rotation = ReferenceTransform.ToRelRot(AttachedTransform.rotation);
}
protected override void ApplyToAttached()
{
if (UseInterpolation)
{
AttachedTransform.position = ReferenceTransform.FromRelPos(SmoothPosition);
@ -45,8 +44,6 @@ namespace QSB.Syncs.Unsectored.Transforms
AttachedTransform.position = ReferenceTransform.FromRelPos(transform.position);
AttachedTransform.rotation = ReferenceTransform.FromRelRot(transform.rotation);
}
return true;
}
}
}

View File

@ -3,7 +3,6 @@ using QSB.SectorSync;
using QSB.Syncs.Sectored.Transforms;
using QSB.Tools.ProbeLauncherTool;
using QSB.Utility;
using QSB.WorldSync;
using UnityEngine;
namespace QSB.Tools.ProbeTool.TransformSync
@ -21,7 +20,7 @@ namespace QSB.Tools.ProbeTool.TransformSync
protected override Transform InitLocalTransform()
{
QSBCore.UnityEvents.RunWhen(() => WorldObjectManager.AllObjectsReady, () => SectorSync.Init(Locator.GetProbe().GetSectorDetector(), TargetType.Probe));
SectorSync.Init(Locator.GetProbe().GetSectorDetector(), TargetType.Probe);
var body = Locator.GetProbe().transform;
Player.ProbeBody = body.gameObject;
@ -64,51 +63,35 @@ namespace QSB.Tools.ProbeTool.TransformSync
return body;
}
protected override bool UpdateTransform()
protected override void GetFromAttached()
{
if (!base.UpdateTransform())
if (AttachedTransform.gameObject.activeInHierarchy)
{
return false;
base.GetFromAttached();
return;
}
if (hasAuthority)
var probeOWRigidbody = Locator.GetProbe().GetOWRigidbody();
if (probeOWRigidbody == null)
{
if (!AttachedTransform.gameObject.activeInHierarchy)
{
var probeOWRigidbody = Locator.GetProbe().GetComponent<SurveyorProbe>().GetOWRigidbody();
if (probeOWRigidbody == null)
{
DebugLog.ToConsole($"Warning - Could not find OWRigidbody of local probe.", MessageType.Warning);
}
var probeLauncher = Player.LocalProbeLauncher;
// TODO : make this sync to the *active* probe launcher's _launcherTransform
var launcherTransform = probeLauncher._launcherTransform;
probeOWRigidbody.SetPosition(launcherTransform.position);
probeOWRigidbody.SetRotation(launcherTransform.rotation);
if (ReferenceTransform != null)
{
transform.position = ReferenceTransform.ToRelPos(AttachedTransform.position);
transform.rotation = ReferenceTransform.ToRelRot(AttachedTransform.rotation);
}
else
{
transform.position = Vector3.zero;
transform.rotation = Quaternion.identity;
}
var currentReferenceSector = ReferenceSector;
var playerReferenceSector = Player.TransformSync.ReferenceSector;
if (currentReferenceSector != playerReferenceSector)
{
SetReferenceSector(playerReferenceSector);
}
}
DebugLog.ToConsole($"Warning - Could not find OWRigidbody of local probe.", MessageType.Warning);
}
return true;
var probeLauncher = Player.LocalProbeLauncher;
// TODO : make this sync to the *active* probe launcher's _launcherTransform
var launcherTransform = probeLauncher._launcherTransform;
probeOWRigidbody.SetPosition(launcherTransform.position);
probeOWRigidbody.SetRotation(launcherTransform.rotation);
base.GetFromAttached();
var currentReferenceSector = ReferenceSector;
var playerReferenceSector = Player.TransformSync.ReferenceSector;
if (currentReferenceSector != playerReferenceSector)
{
SetReferenceSector(playerReferenceSector);
}
}
protected override bool IsReady => AttachedTransform != null || Locator.GetProbe() != null;

View File

@ -17,7 +17,7 @@ namespace QSB.TornadoSync.TransformSync
&& CenterOfTheUniverse.s_rigidbodies.IsInRange(_bodyIndex)
&& CenterOfTheUniverse.s_rigidbodies.IsInRange(_refBodyIndex);
protected override bool UseInterpolation => false;
protected override bool IsPlayerObject => false;
protected override bool OnlyApplyOnDeserialize => true;
protected override OWRigidbody InitAttachedRigidbody() => CenterOfTheUniverse.s_rigidbodies[_bodyIndex];
@ -59,55 +59,8 @@ namespace QSB.TornadoSync.TransformSync
_refBodyIndex = reader.ReadInt();
}
private bool _shouldUpdate;
protected override void Deserialize(NetworkReader reader)
protected override void ApplyToAttached()
{
base.Deserialize(reader);
if (!WorldObjectManager.AllObjectsReady)
{
return;
}
_shouldUpdate = true;
}
protected override bool UpdateTransform()
{
if (hasAuthority)
{
SetValuesToSync();
return true;
}
if (!_shouldUpdate)
{
return false;
}
_shouldUpdate = false;
var hasMoved = CustomHasMoved(
transform.position,
_localPrevPosition,
transform.rotation,
_localPrevRotation,
_relativeVelocity,
_localPrevVelocity,
_relativeAngularVelocity,
_localPrevAngularVelocity);
_localPrevPosition = transform.position;
_localPrevRotation = transform.rotation;
_localPrevVelocity = _relativeVelocity;
_localPrevAngularVelocity = _relativeAngularVelocity;
if (!hasMoved)
{
return true;
}
if (_sectors.Contains(PlayerTransformSync.LocalInstance?.ReferenceSector?.AttachedObject))
{
QueueMove(Locator._playerBody);
@ -135,8 +88,6 @@ namespace QSB.TornadoSync.TransformSync
AttachedRigidbody.SetAngularVelocity(ReferenceTransform.GetAttachedOWRigidbody().FromRelAngVel(_relativeAngularVelocity));
Move();
return true;
}
private readonly List<MoveData> _toMove = new();

View File

@ -13,7 +13,7 @@ namespace QSB.Utility
protected virtual void SerializeInitial(NetworkWriter writer) { }
protected virtual void DeserializeInitial(NetworkReader reader) { }
public override bool OnSerialize(NetworkWriter writer, bool initialState)
public sealed override bool OnSerialize(NetworkWriter writer, bool initialState)
{
var changed = base.OnSerialize(writer, initialState);
if (initialState && isServer)
@ -24,7 +24,7 @@ namespace QSB.Utility
return changed;
}
public override void OnDeserialize(NetworkReader reader, bool initialState)
public sealed override void OnDeserialize(NetworkReader reader, bool initialState)
{
base.OnDeserialize(reader, initialState);
if (initialState && !isServer)