This commit is contained in:
JohnCorby 2022-01-14 21:29:57 -08:00
parent 4dc77e0472
commit 1a818e5fd3
2 changed files with 303 additions and 2 deletions

View File

@ -1,4 +1,5 @@
using QSB.Anglerfish.WorldObjects;
using Mirror;
using QSB.Anglerfish.WorldObjects;
using QSB.AuthoritySync;
using QSB.Syncs.Unsectored.Rigidbodies;
using QSB.WorldSync;
@ -8,6 +9,122 @@ using UnityEngine;
namespace QSB.Anglerfish.TransformSync
{
public class AnglerTransformSync2 : UnsectoredRigidbodySync2
{
public override bool IsReady => WorldObjectManager.AllObjectsAdded;
public override bool UseInterpolation => false;
public override bool IsPlayerObject => false;
private QSBAngler _qsbAngler;
private static readonly List<AnglerTransformSync2> _instances = new();
protected override OWRigidbody GetRigidbody()
=> _qsbAngler.AttachedObject._anglerBody;
public override void Start()
{
_instances.Add(this);
base.Start();
}
protected override void OnDestroy()
{
_instances.Remove(this);
base.OnDestroy();
if (QSBCore.IsHost)
{
netIdentity.UnregisterAuthQueue();
}
AttachedRigidbody.OnUnsuspendOWRigidbody -= OnUnsuspend;
AttachedRigidbody.OnSuspendOWRigidbody -= OnSuspend;
}
protected override float SendInterval => 1;
protected override bool UseReliableRpc => true;
protected override void Init()
{
_qsbAngler = AnglerManager.Anglers[_instances.IndexOf(this)].GetWorldObject<QSBAngler>();
// todo _qsbAngler.TransformSync = this;
base.Init();
SetReferenceTransform(_qsbAngler.AttachedObject._brambleBody.transform);
if (QSBCore.IsHost)
{
netIdentity.RegisterAuthQueue();
}
AttachedRigidbody.OnUnsuspendOWRigidbody += OnUnsuspend;
AttachedRigidbody.OnSuspendOWRigidbody += OnSuspend;
netIdentity.SendAuthQueueMessage(AttachedRigidbody.IsSuspended() ? AuthQueueAction.Remove : AuthQueueAction.Add);
}
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, bool initialState)
{
base.Deserialize(reader, initialState);
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
|| !WorldObjectManager.AllObjectsReady
|| !IsReady
|| AttachedRigidbody == null
|| AttachedRigidbody.IsSuspended())
{
return;
}
base.OnRenderObject();
Popcron.Gizmos.Sphere(AttachedRigidbody.GetPosition(), _qsbAngler.AttachedObject._arrivalDistance, Color.blue);
Popcron.Gizmos.Sphere(AttachedRigidbody.GetPosition(), _qsbAngler.AttachedObject._pursueDistance, Color.red);
Popcron.Gizmos.Sphere(AttachedRigidbody.GetPosition(), _qsbAngler.AttachedObject._escapeDistance, Color.yellow);
Popcron.Gizmos.Sphere(AttachedRigidbody.GetPosition()
+ AttachedRigidbody.transform.TransformDirection(_qsbAngler.AttachedObject._mouthOffset), 3, Color.grey);
if (_qsbAngler.TargetTransform != null)
{
Popcron.Gizmos.Line(_qsbAngler.TargetTransform.position, AttachedRigidbody.GetPosition(), Color.gray);
Popcron.Gizmos.Line(_qsbAngler.TargetTransform.position, _qsbAngler.TargetTransform.position + _qsbAngler.TargetVelocity, Color.green);
Popcron.Gizmos.Line(AttachedRigidbody.GetPosition(), _qsbAngler.AttachedObject._targetPos, Color.red);
Popcron.Gizmos.Sphere(_qsbAngler.AttachedObject._targetPos, 5, Color.red);
}
// Popcron.Gizmos.Line(AttachedObject.GetPosition(), _qsbAngler.AttachedObject.GetTargetPosition(), Color.white);
}
}
public class AnglerTransformSync : UnsectoredRigidbodySync
{
public override bool IsReady => WorldObjectManager.AllObjectsAdded;

View File

@ -1,4 +1,5 @@
using QSB.Player.TransformSync;
using Mirror;
using QSB.Player.TransformSync;
using QSB.ShipSync.TransformSync;
using QSB.Syncs.Unsectored.Rigidbodies;
using QSB.Tools.ProbeTool.TransformSync;
@ -11,6 +12,189 @@ using UnityEngine;
namespace QSB.TornadoSync.TransformSync
{
public class OccasionalTransformSync2 : UnsectoredRigidbodySync2
{
public override bool IsReady => WorldObjectManager.AllObjectsReady
&& CenterOfTheUniverse.s_rigidbodies.IsInRange(_bodyIndex)
&& CenterOfTheUniverse.s_rigidbodies.IsInRange(_refBodyIndex);
public override bool UseInterpolation => false;
public override bool IsPlayerObject => false;
protected override OWRigidbody GetRigidbody() => CenterOfTheUniverse.s_rigidbodies[_bodyIndex];
private int _bodyIndex = -1;
private int _refBodyIndex = -1;
private Sector[] _sectors;
private OWRigidbody[] _childBodies;
public void InitBodyIndexes(OWRigidbody body, OWRigidbody refBody)
{
_bodyIndex = CenterOfTheUniverse.s_rigidbodies.IndexOf(body);
_refBodyIndex = CenterOfTheUniverse.s_rigidbodies.IndexOf(refBody);
}
protected override float SendInterval => 20;
protected override bool UseReliableRpc => true;
protected override void Init()
{
base.Init();
SetReferenceTransform(CenterOfTheUniverse.s_rigidbodies[_refBodyIndex].transform);
_sectors = SectorManager.s_sectors
.Where(x => x._attachedOWRigidbody == AttachedRigidbody).ToArray();
_childBodies = CenterOfTheUniverse.s_rigidbodies
.Where(x => x._origParentBody == AttachedRigidbody)
.ToArray();
}
protected override void Serialize(NetworkWriter writer, bool initialState)
{
base.Serialize(writer, initialState);
if (initialState)
{
writer.Write(_bodyIndex);
writer.Write(_refBodyIndex);
}
}
private bool _shouldUpdate;
protected override void Deserialize(NetworkReader reader, bool initialState)
{
base.Deserialize(reader, initialState);
if (initialState)
{
_bodyIndex = reader.ReadInt();
_refBodyIndex = reader.ReadInt();
}
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);
}
if (_sectors.Contains(ShipTransformSync.LocalInstance?.ReferenceSector?.AttachedObject))
{
QueueMove(Locator._shipBody);
}
if (_sectors.Contains(PlayerProbeSync.LocalInstance?.ReferenceSector?.AttachedObject))
{
QueueMove(Locator._probe._owRigidbody);
}
foreach (var child in _childBodies)
{
QueueMove(child);
}
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));
Move();
return true;
}
private readonly List<MoveData> _toMove = new();
private struct MoveData
{
public OWRigidbody Child;
public Vector3 RelPos;
public Quaternion RelRot;
public Vector3 RelVel;
public Vector3 RelAngVel;
}
private void QueueMove(OWRigidbody child)
{
if (!child)
{
return; // wtf
}
if (child.transform.parent != null)
{
// it's parented to AttachedObject or one of its children
return;
}
var pos = child.GetPosition();
_toMove.Add(new MoveData
{
Child = child,
RelPos = AttachedRigidbody.transform.ToRelPos(pos),
RelRot = AttachedRigidbody.transform.ToRelRot(child.GetRotation()),
RelVel = AttachedRigidbody.ToRelVel(child.GetVelocity(), pos),
RelAngVel = AttachedRigidbody.ToRelAngVel(child.GetAngularVelocity())
});
}
private void Move()
{
foreach (var data in _toMove)
{
var pos = AttachedRigidbody.transform.FromRelPos(data.RelPos);
data.Child.SetPosition(pos);
data.Child.SetRotation(AttachedRigidbody.transform.FromRelRot(data.RelRot));
data.Child.SetVelocity(AttachedRigidbody.FromRelVel(data.RelVel, pos));
data.Child.SetAngularVelocity(AttachedRigidbody.FromRelAngVel(data.RelAngVel));
}
_toMove.Clear();
}
}
public class OccasionalTransformSync : UnsectoredRigidbodySync
{
public override bool IsReady => WorldObjectManager.AllObjectsReady