2021-12-04 19:48:41 -08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using QSB.Player;
|
2021-12-04 19:40:57 -08:00
|
|
|
|
using QSB.Syncs;
|
2021-12-04 06:21:11 -08:00
|
|
|
|
using QSB.Syncs.Unsectored.Rigidbodies;
|
|
|
|
|
using QSB.Utility;
|
|
|
|
|
using QSB.WorldSync;
|
|
|
|
|
using QuantumUNET.Transport;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace QSB.TornadoSync.TransformSync
|
|
|
|
|
{
|
|
|
|
|
public class OccasionalTransformSync : UnsectoredRigidbodySync
|
|
|
|
|
{
|
|
|
|
|
public override bool IsReady => WorldObjectManager.AllObjectsReady;
|
|
|
|
|
public override bool UseInterpolation => false;
|
|
|
|
|
|
|
|
|
|
protected override OWRigidbody GetRigidbody() => CenterOfTheUniverse.s_rigidbodies[_bodyIndex];
|
|
|
|
|
|
|
|
|
|
private int _bodyIndex = -1;
|
2021-12-04 19:48:41 -08:00
|
|
|
|
private string[] _sectors;
|
2021-12-04 06:21:11 -08:00
|
|
|
|
private int _refBodyIndex = -1;
|
|
|
|
|
|
|
|
|
|
public void InitBodyIndexes(OWRigidbody body, OWRigidbody refBody)
|
|
|
|
|
{
|
|
|
|
|
_bodyIndex = CenterOfTheUniverse.s_rigidbodies.IndexOf(body);
|
2021-12-04 18:33:43 -08:00
|
|
|
|
_refBodyIndex = CenterOfTheUniverse.s_rigidbodies.IndexOf(refBody);
|
2021-12-04 06:21:11 -08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-04 19:40:57 -08:00
|
|
|
|
public override float GetNetworkSendInterval() => 1;
|
2021-12-04 06:21:11 -08:00
|
|
|
|
|
|
|
|
|
protected override void Init()
|
|
|
|
|
{
|
|
|
|
|
base.Init();
|
|
|
|
|
SetReferenceTransform(CenterOfTheUniverse.s_rigidbodies[_refBodyIndex].transform);
|
2021-12-04 19:48:41 -08:00
|
|
|
|
_sectors = ((OWRigidbody)AttachedObject).GetComponentsInChildren<Sector>()
|
|
|
|
|
.Select(x => x.name).ToArray();
|
2021-12-04 06:21:11 -08:00
|
|
|
|
|
2021-12-04 18:33:43 -08:00
|
|
|
|
// to prevent change in rotation/angvel
|
|
|
|
|
if (!HasAuthority && AttachedObject.TryGetComponent<AlignWithDirection>(out var align))
|
2021-12-04 06:21:11 -08:00
|
|
|
|
{
|
|
|
|
|
align.enabled = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void SerializeTransform(QNetworkWriter writer, bool initialState)
|
|
|
|
|
{
|
|
|
|
|
base.SerializeTransform(writer, initialState);
|
|
|
|
|
|
|
|
|
|
if (initialState)
|
|
|
|
|
{
|
|
|
|
|
writer.Write(_bodyIndex);
|
|
|
|
|
writer.Write(_refBodyIndex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool _shouldUpdate;
|
|
|
|
|
|
|
|
|
|
public override void DeserializeTransform(QNetworkReader reader, bool initialState)
|
|
|
|
|
{
|
|
|
|
|
base.DeserializeTransform(reader, initialState);
|
|
|
|
|
|
|
|
|
|
if (initialState)
|
|
|
|
|
{
|
|
|
|
|
_bodyIndex = reader.ReadInt32();
|
|
|
|
|
_refBodyIndex = reader.ReadInt32();
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-04 18:33:43 -08:00
|
|
|
|
if (!WorldObjectManager.AllObjectsReady || HasAuthority)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-04 06:21:11 -08:00
|
|
|
|
_shouldUpdate = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// replacement that handles KinematicRigidbody
|
|
|
|
|
protected override bool UpdateTransform()
|
|
|
|
|
{
|
|
|
|
|
if (HasAuthority)
|
|
|
|
|
{
|
|
|
|
|
SetValuesToSync();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!_shouldUpdate)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_shouldUpdate = false;
|
|
|
|
|
|
|
|
|
|
var targetPos = ReferenceTransform.DecodePos(transform.position);
|
|
|
|
|
var targetRot = ReferenceTransform.DecodeRot(transform.rotation);
|
|
|
|
|
|
|
|
|
|
var positionToSet = targetPos;
|
|
|
|
|
var rotationToSet = targetRot;
|
|
|
|
|
|
|
|
|
|
if (UseInterpolation)
|
|
|
|
|
{
|
|
|
|
|
positionToSet = SmartSmoothDamp(AttachedObject.transform.position, targetPos);
|
|
|
|
|
rotationToSet = QuaternionHelper.SmoothDamp(AttachedObject.transform.rotation, targetRot, ref _rotationSmoothVelocity, SmoothTime);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-04 19:48:41 -08:00
|
|
|
|
// todo also do this with the ship/probe. oh and orbs. yeah this really sucks
|
|
|
|
|
string playerSector = null;
|
2021-12-04 19:40:57 -08:00
|
|
|
|
Vector3 playerPos = default;
|
|
|
|
|
Quaternion playerRot = default;
|
|
|
|
|
Vector3 playerVel = default;
|
|
|
|
|
Vector3 playerAngVel = default;
|
2021-12-04 19:48:41 -08:00
|
|
|
|
if (_sectors != null)
|
2021-12-04 19:40:57 -08:00
|
|
|
|
{
|
|
|
|
|
playerSector = QSBPlayerManager.LocalPlayer.TransformSync.ReferenceSector.Name;
|
2021-12-04 19:48:41 -08:00
|
|
|
|
if (_sectors.Contains(playerSector))
|
2021-12-04 19:40:57 -08:00
|
|
|
|
{
|
|
|
|
|
var pos = Locator.GetPlayerBody().GetPosition();
|
|
|
|
|
playerPos = ((OWRigidbody)AttachedObject).transform.EncodePos(pos);
|
|
|
|
|
playerRot = ((OWRigidbody)AttachedObject).transform.EncodeRot(Locator.GetPlayerBody().GetRotation());
|
|
|
|
|
playerVel = ((OWRigidbody)AttachedObject).EncodeVel(Locator.GetPlayerBody().GetVelocity(), pos);
|
|
|
|
|
playerAngVel = ((OWRigidbody)AttachedObject).EncodeAngVel(Locator.GetPlayerBody().GetAngularVelocity());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-04 06:21:11 -08:00
|
|
|
|
if (((OWRigidbody)AttachedObject).RunningKinematicSimulation())
|
|
|
|
|
{
|
|
|
|
|
((OWRigidbody)AttachedObject).SetPosition(positionToSet);
|
|
|
|
|
((OWRigidbody)AttachedObject).SetRotation(rotationToSet);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// does nothing for KinematicRigidbody
|
|
|
|
|
((OWRigidbody)AttachedObject).MoveToPosition(positionToSet);
|
|
|
|
|
((OWRigidbody)AttachedObject).MoveToRotation(rotationToSet);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var targetVelocity = ReferenceTransform.GetAttachedOWRigidbody().DecodeVel(_relativeVelocity, targetPos);
|
|
|
|
|
var targetAngularVelocity = ReferenceTransform.GetAttachedOWRigidbody().DecodeAngVel(_relativeAngularVelocity);
|
|
|
|
|
|
|
|
|
|
((OWRigidbody)AttachedObject).SetVelocity(targetVelocity);
|
|
|
|
|
((OWRigidbody)AttachedObject).SetAngularVelocity(targetAngularVelocity);
|
|
|
|
|
|
2021-12-04 19:48:41 -08:00
|
|
|
|
if (_sectors != null)
|
2021-12-04 19:40:57 -08:00
|
|
|
|
{
|
2021-12-04 19:48:41 -08:00
|
|
|
|
if (_sectors.Contains(playerSector))
|
2021-12-04 19:40:57 -08:00
|
|
|
|
{
|
|
|
|
|
var pos = ((OWRigidbody)AttachedObject).transform.DecodePos(playerPos);
|
|
|
|
|
Locator.GetPlayerBody().SetPosition(pos);
|
|
|
|
|
Locator.GetPlayerBody().SetRotation(((OWRigidbody)AttachedObject).transform.DecodeRot(playerRot));
|
|
|
|
|
Locator.GetPlayerBody().SetVelocity(((OWRigidbody)AttachedObject).DecodeVel(playerVel, pos));
|
|
|
|
|
Locator.GetPlayerBody().SetAngularVelocity(((OWRigidbody)AttachedObject).DecodeAngVel(playerAngVel));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-04 06:21:11 -08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|