quantum-space-buddies/QSB/TransformSync/NomaiOrbTransformSync.cs

75 lines
1.6 KiB
C#
Raw Normal View History

2020-12-05 14:00:56 +00:00
using QSB.WorldSync;
2020-12-04 22:15:41 +00:00
using QuantumUNET;
using UnityEngine;
namespace QSB.TransformSync
{
2020-12-23 12:58:45 +00:00
public class NomaiOrbTransformSync : QNetworkBehaviour
2020-12-02 21:29:53 +00:00
{
public NomaiInterfaceOrb AttachedOrb { get; private set; }
public Transform OrbTransform { get; private set; }
2020-12-11 13:14:58 +00:00
private int Index => QSBWorldSync.OrbSyncList.IndexOf(this);
2020-12-02 21:29:53 +00:00
private bool _isInitialized;
private bool _isReady;
private Transform _orbParent;
public override void OnStartClient()
{
DontDestroyOnLoad(this);
2020-12-11 13:14:58 +00:00
QSBWorldSync.OrbSyncList.Add(this);
2020-12-02 21:29:53 +00:00
2020-12-14 16:24:52 +00:00
QSBCore.Helper.Events.Unity.RunWhen(() => QSBCore.HasWokenUp, () => QSBCore.Helper.Events.Unity.FireOnNextUpdate(OnReady));
2020-12-02 21:29:53 +00:00
}
private void OnReady()
{
2020-12-11 13:14:58 +00:00
AttachedOrb = QSBWorldSync.OldOrbList[Index];
2020-12-02 21:29:53 +00:00
_isReady = true;
}
2020-12-18 20:28:22 +00:00
public void OnDestroy() => QSBWorldSync.OrbSyncList.Remove(this);
2020-12-02 21:29:53 +00:00
protected void Init()
{
OrbTransform = AttachedOrb.transform;
_orbParent = AttachedOrb.GetAttachedOWRigidbody().GetOrigParent();
_isInitialized = true;
}
2020-12-14 21:41:56 +01:00
public void Update()
2020-12-02 21:29:53 +00:00
{
if (!_isInitialized && _isReady)
{
Init();
}
else if (_isInitialized && !_isReady)
{
_isInitialized = false;
}
if (OrbTransform == null || !_isInitialized)
{
return;
}
UpdateTransform();
}
private void UpdateTransform()
2020-12-02 21:29:53 +00:00
{
if (HasAuthority)
{
transform.position = _orbParent.InverseTransformPoint(OrbTransform.position);
2020-12-20 10:56:15 +00:00
transform.rotation = OrbTransform.rotation;
2020-12-02 21:29:53 +00:00
return;
}
2020-12-06 17:00:23 +00:00
if (transform.position != Vector3.zero)
{
2020-12-20 17:44:45 +00:00
OrbTransform.position = _orbParent.TransformPoint(transform.position);
2020-12-20 10:56:15 +00:00
OrbTransform.rotation = transform.rotation;
2020-12-06 17:00:23 +00:00
}
2020-12-02 21:29:53 +00:00
}
}
2020-10-23 23:08:48 +01:00
}