148 lines
3.8 KiB
C#
Raw Normal View History

2020-08-31 09:36:29 +01:00
using OWML.Common;
2020-11-03 21:33:48 +00:00
using QSB.Player;
2020-11-03 22:29:23 +00:00
using QSB.SectorSync;
using QSB.SectorSync.WorldObjects;
2020-08-31 09:36:29 +01:00
using QSB.Utility;
2020-12-14 19:23:24 +00:00
using System.Linq;
2020-08-31 09:36:29 +01:00
using UnityEngine;
namespace QSB.TransformSync
{
2020-12-02 21:29:53 +00:00
public abstract class TransformSync : PlayerSyncObject
{
public abstract bool IsReady { get; }
2020-12-03 08:28:05 +00:00
2020-12-02 21:29:53 +00:00
protected abstract Transform InitLocalTransform();
protected abstract Transform InitRemoteTransform();
public Transform SyncedTransform { get; private set; }
public QSBSector ReferenceSector { get; set; }
private const float SmoothTime = 0.1f;
private bool _isInitialized;
private Vector3 _positionSmoothVelocity;
private Quaternion _rotationSmoothVelocity;
private bool _isVisible;
2020-12-14 19:23:24 +00:00
protected override void Start()
2020-12-02 21:29:53 +00:00
{
2020-12-16 08:57:15 +00:00
DebugLog.DebugWrite("start of " + GetType().Name);
2020-12-14 19:23:24 +00:00
base.Start();
2020-12-19 14:49:15 +00:00
var lowestBound = QSBPlayerManager.GetSyncObjects<PlayerTransformSync>()
.Where(x => x.NetId.Value <= NetId.Value).OrderBy(x => x.NetId.Value).Last();
2020-12-14 19:23:24 +00:00
NetIdentity.SetRootIdentity(lowestBound.NetIdentity);
2020-12-02 21:29:53 +00:00
DontDestroyOnLoad(gameObject);
QSBSceneManager.OnSceneLoaded += OnSceneLoaded;
}
2020-12-14 16:04:16 +00:00
protected override void OnDestroy()
2020-12-02 21:29:53 +00:00
{
2020-12-14 16:04:16 +00:00
base.OnDestroy();
2020-12-12 11:27:28 +00:00
if (!HasAuthority && SyncedTransform != null)
{
Destroy(SyncedTransform.gameObject);
}
2020-12-02 21:29:53 +00:00
QSBSceneManager.OnSceneLoaded -= OnSceneLoaded;
}
2020-12-14 21:20:53 +00:00
private void OnSceneLoaded(OWScene scene, bool isInUniverse) =>
_isInitialized = false;
2020-12-02 21:29:53 +00:00
2020-12-14 21:20:53 +00:00
protected void Init()
2020-12-02 21:29:53 +00:00
{
SyncedTransform = HasAuthority ? InitLocalTransform() : InitRemoteTransform();
2020-12-12 11:27:28 +00:00
SetReferenceSector(QSBSectorManager.Instance.GetClosestSector(SyncedTransform));
2020-12-02 21:29:53 +00:00
_isInitialized = true;
_isVisible = 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;
return;
}
if (!_isInitialized)
{
return;
}
if (SyncedTransform == null)
{
DebugLog.ToConsole($"Warning - SyncedTransform {Player.PlayerId}.{GetType().Name} is null.", MessageType.Warning);
return;
}
UpdateTransform();
}
protected virtual void UpdateTransform()
{
2020-12-03 08:28:05 +00:00
if (HasAuthority) // If this script is attached to the client's own body on the client's side.
2020-12-02 21:29:53 +00:00
{
2020-12-23 22:43:05 +00:00
if (ReferenceSector == null || ReferenceSector.AttachedObject == null)
2020-12-02 21:29:53 +00:00
{
DebugLog.ToConsole($"Error - ReferenceSector has null value for {Player.PlayerId}.{GetType().Name}", MessageType.Error);
return;
}
transform.position = ReferenceSector.Transform.InverseTransformPoint(SyncedTransform.position);
transform.rotation = ReferenceSector.Transform.InverseTransformRotation(SyncedTransform.rotation);
return;
}
2020-12-03 08:28:05 +00:00
// If this script is attached to any other body, eg the representations of other players
2020-12-02 21:29:53 +00:00
if (SyncedTransform.position == Vector3.zero)
{
Hide();
}
else
{
Show();
}
SyncedTransform.localPosition = Vector3.SmoothDamp(SyncedTransform.localPosition, transform.position, ref _positionSmoothVelocity, SmoothTime);
SyncedTransform.localRotation = QuaternionHelper.SmoothDamp(SyncedTransform.localRotation, transform.rotation, ref _rotationSmoothVelocity, Time.deltaTime);
}
public void SetReferenceSector(QSBSector sector)
{
2021-01-26 14:07:17 +00:00
if (sector == ReferenceSector || sector == null)
2020-12-20 09:42:57 +00:00
{
return;
}
2020-12-02 21:29:53 +00:00
_positionSmoothVelocity = Vector3.zero;
ReferenceSector = sector;
if (!HasAuthority)
{
SyncedTransform.SetParent(sector.Transform, true);
transform.position = sector.Transform.InverseTransformPoint(SyncedTransform.position);
transform.rotation = sector.Transform.InverseTransformRotation(SyncedTransform.rotation);
}
}
private void Show()
{
if (!_isVisible)
{
SyncedTransform.gameObject.Show();
_isVisible = true;
}
}
private void Hide()
{
if (_isVisible)
{
SyncedTransform.gameObject.Hide();
_isVisible = false;
}
}
}
2020-08-31 09:36:29 +01:00
}