2020-09-04 20:54:34 +01:00
|
|
|
|
using QSB.WorldSync;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.Networking;
|
|
|
|
|
|
|
|
|
|
namespace QSB.TransformSync
|
|
|
|
|
{
|
|
|
|
|
public class NomaiOrbTransformSync : NetworkBehaviour
|
|
|
|
|
{
|
2020-09-05 13:24:51 +01:00
|
|
|
|
public NomaiInterfaceOrb AttachedOrb { get; private set; }
|
2020-09-07 20:27:56 +02:00
|
|
|
|
public Transform OrbTransform { get; private set; }
|
|
|
|
|
|
2020-09-16 15:53:40 +01:00
|
|
|
|
private int Index => WorldRegistry.OrbSyncList.IndexOf(this);
|
2020-09-04 20:54:34 +01:00
|
|
|
|
|
2020-09-07 19:05:17 +01:00
|
|
|
|
private const int MaxUpdatesBeforeDisable = 5;
|
|
|
|
|
|
2020-09-04 20:54:34 +01:00
|
|
|
|
private bool _isInitialized;
|
|
|
|
|
private bool _isReady;
|
2020-09-07 20:27:56 +02:00
|
|
|
|
private Transform _orbParent;
|
2020-09-06 09:07:31 +01:00
|
|
|
|
private int _updateCount;
|
2020-09-04 20:54:34 +01:00
|
|
|
|
|
|
|
|
|
public override void OnStartClient()
|
|
|
|
|
{
|
2020-09-06 09:07:31 +01:00
|
|
|
|
WorldRegistry.OrbSyncList.Add(this);
|
2020-09-04 20:54:34 +01:00
|
|
|
|
|
2020-09-29 21:34:46 +01:00
|
|
|
|
QSB.Helper.Events.Unity.RunWhen(() => QSB.HasWokenUp, () => QSB.Helper.Events.Unity.FireOnNextUpdate(OnReady));
|
2020-09-04 20:54:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnReady()
|
|
|
|
|
{
|
2020-09-05 13:24:51 +01:00
|
|
|
|
AttachedOrb = WorldRegistry.OldOrbList[Index];
|
2020-09-04 20:54:34 +01:00
|
|
|
|
_isReady = true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 20:27:56 +02:00
|
|
|
|
private void Awake()
|
2020-09-04 20:54:34 +01:00
|
|
|
|
{
|
|
|
|
|
DontDestroyOnLoad(this);
|
|
|
|
|
QSBSceneManager.OnSceneLoaded += OnSceneLoaded;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-22 21:21:41 +01:00
|
|
|
|
private void OnDestroy()
|
|
|
|
|
{
|
|
|
|
|
WorldRegistry.OrbSyncList.Remove(this);
|
|
|
|
|
QSBSceneManager.OnSceneLoaded -= OnSceneLoaded;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-05 15:46:20 +01:00
|
|
|
|
private void OnSceneLoaded(OWScene scene, bool isInUniverse) => _isInitialized = false;
|
2020-09-04 20:54:34 +01:00
|
|
|
|
|
|
|
|
|
protected void Init()
|
|
|
|
|
{
|
2020-09-05 13:24:51 +01:00
|
|
|
|
OrbTransform = AttachedOrb.transform;
|
2020-09-07 20:27:56 +02:00
|
|
|
|
_orbParent = AttachedOrb.GetAttachedOWRigidbody().GetOrigParent();
|
2020-09-04 20:54:34 +01:00
|
|
|
|
_isInitialized = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Update()
|
|
|
|
|
{
|
|
|
|
|
if (!_isInitialized && _isReady)
|
|
|
|
|
{
|
|
|
|
|
Init();
|
|
|
|
|
}
|
|
|
|
|
else if (_isInitialized && !_isReady)
|
|
|
|
|
{
|
|
|
|
|
_isInitialized = false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-05 13:24:51 +01:00
|
|
|
|
if (OrbTransform == null || !_isInitialized)
|
2020-09-04 20:54:34 +01:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UpdateTransform();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void UpdateTransform()
|
|
|
|
|
{
|
2020-09-05 13:24:51 +01:00
|
|
|
|
if (hasAuthority)
|
2020-09-04 20:54:34 +01:00
|
|
|
|
{
|
2020-09-07 20:27:56 +02:00
|
|
|
|
transform.position = _orbParent.InverseTransformPoint(OrbTransform.position);
|
|
|
|
|
transform.rotation = _orbParent.InverseTransformRotation(OrbTransform.rotation);
|
2020-09-04 20:54:34 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2020-09-07 20:27:56 +02:00
|
|
|
|
OrbTransform.position = _orbParent.TransformPoint(transform.position);
|
|
|
|
|
OrbTransform.rotation = _orbParent.InverseTransformRotation(OrbTransform.rotation);
|
2020-09-16 15:53:40 +01:00
|
|
|
|
|
2020-09-06 09:07:31 +01:00
|
|
|
|
if (transform.localPosition == Vector3.zero)
|
|
|
|
|
{
|
|
|
|
|
_updateCount++;
|
|
|
|
|
}
|
2020-09-07 19:05:17 +01:00
|
|
|
|
if (_updateCount >= MaxUpdatesBeforeDisable)
|
2020-09-06 09:07:31 +01:00
|
|
|
|
{
|
|
|
|
|
enabled = false;
|
|
|
|
|
_updateCount = 0;
|
|
|
|
|
}
|
2020-09-04 20:54:34 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-23 23:08:48 +01:00
|
|
|
|
}
|