2020-08-18 21:31:14 +02:00
|
|
|
|
using System;
|
|
|
|
|
using OWML.Common;
|
2020-07-28 00:13:43 +01:00
|
|
|
|
using QSB.Utility;
|
|
|
|
|
using UnityEngine;
|
2020-02-21 21:51:58 +01:00
|
|
|
|
using UnityEngine.Networking;
|
|
|
|
|
|
2020-02-21 23:36:07 +01:00
|
|
|
|
namespace QSB.TransformSync
|
2020-02-21 21:51:58 +01:00
|
|
|
|
{
|
|
|
|
|
public abstract class TransformSync : NetworkBehaviour
|
|
|
|
|
{
|
2020-08-18 21:31:14 +02:00
|
|
|
|
public abstract bool IsReady { get; }
|
|
|
|
|
protected abstract Transform InitLocalTransform();
|
|
|
|
|
protected abstract Transform InitRemoteTransform();
|
|
|
|
|
protected abstract uint PlayerIdOffset { get; }
|
2020-02-21 21:51:58 +01:00
|
|
|
|
|
2020-08-18 21:31:14 +02:00
|
|
|
|
public uint PlayerId => GetPlayerId();
|
|
|
|
|
public PlayerInfo Player => PlayerRegistry.GetPlayer(PlayerId);
|
2020-03-07 16:42:43 +01:00
|
|
|
|
public Transform SyncedTransform { get; private set; }
|
2020-08-16 17:15:36 +02:00
|
|
|
|
public QSBSector ReferenceSector { get; set; }
|
2020-03-07 16:42:43 +01:00
|
|
|
|
|
2020-08-18 21:31:14 +02:00
|
|
|
|
private const float SmoothTime = 0.1f;
|
|
|
|
|
private bool _isInitialized;
|
2020-02-21 21:51:58 +01:00
|
|
|
|
private Vector3 _positionSmoothVelocity;
|
|
|
|
|
private Quaternion _rotationSmoothVelocity;
|
2020-08-16 17:15:36 +02:00
|
|
|
|
private bool _isVisible;
|
|
|
|
|
|
2020-02-21 21:51:58 +01:00
|
|
|
|
protected virtual void Awake()
|
|
|
|
|
{
|
2020-08-05 21:45:48 +02:00
|
|
|
|
PlayerRegistry.TransformSyncs.Add(this);
|
2020-03-13 20:44:32 +01:00
|
|
|
|
DontDestroyOnLoad(gameObject);
|
2020-08-16 22:39:21 +02:00
|
|
|
|
QSBSceneManager.OnSceneLoaded += OnSceneLoaded;
|
2020-03-13 20:44:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-16 22:39:21 +02:00
|
|
|
|
private void OnSceneLoaded(OWScene scene, bool isInUniverse)
|
2020-03-13 20:44:32 +01:00
|
|
|
|
{
|
2020-08-10 20:14:19 +02:00
|
|
|
|
_isInitialized = false;
|
2020-02-21 21:51:58 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-13 20:44:32 +01:00
|
|
|
|
protected void Init()
|
2020-02-21 21:51:58 +01:00
|
|
|
|
{
|
2020-08-16 17:15:36 +02:00
|
|
|
|
ReferenceSector = QSBSectorManager.Instance.GetStartPlanetSector();
|
2020-03-07 16:42:43 +01:00
|
|
|
|
SyncedTransform = hasAuthority ? InitLocalTransform() : InitRemoteTransform();
|
2020-03-02 20:44:44 +01:00
|
|
|
|
if (!hasAuthority)
|
|
|
|
|
{
|
2020-08-16 17:15:36 +02:00
|
|
|
|
SyncedTransform.position = ReferenceSector.Position;
|
2020-03-02 20:44:44 +01:00
|
|
|
|
}
|
2020-08-10 20:14:19 +02:00
|
|
|
|
_isInitialized = true;
|
2020-08-16 17:15:36 +02:00
|
|
|
|
_isVisible = true;
|
2020-02-21 21:51:58 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Update()
|
|
|
|
|
{
|
2020-08-07 20:39:07 +01:00
|
|
|
|
if (!_isInitialized && IsReady)
|
2020-03-13 20:44:32 +01:00
|
|
|
|
{
|
|
|
|
|
Init();
|
|
|
|
|
}
|
2020-08-07 20:39:07 +01:00
|
|
|
|
else if (_isInitialized && !IsReady)
|
2020-03-13 20:44:32 +01:00
|
|
|
|
{
|
2020-08-10 20:14:19 +02:00
|
|
|
|
_isInitialized = false;
|
2020-03-13 20:44:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-10 20:14:19 +02:00
|
|
|
|
if (SyncedTransform == null || !_isInitialized)
|
2020-02-21 21:51:58 +01:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-28 00:13:43 +01:00
|
|
|
|
// Get which sector should be used as a reference point
|
2020-02-21 21:51:58 +01:00
|
|
|
|
|
2020-08-14 21:04:29 +02:00
|
|
|
|
if (ReferenceSector == null)
|
2020-07-29 22:04:50 +01:00
|
|
|
|
{
|
2020-08-05 21:45:48 +02:00
|
|
|
|
DebugLog.ToConsole($"Error - TransformSync with id {netId.Value} doesn't have a reference sector", MessageType.Error);
|
2020-07-29 22:04:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-07 20:39:07 +01:00
|
|
|
|
UpdateTransform();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void UpdateTransform()
|
|
|
|
|
{
|
2020-07-28 00:13:43 +01:00
|
|
|
|
if (hasAuthority) // If this script is attached to the client's own body on the client's side.
|
2020-02-21 21:51:58 +01:00
|
|
|
|
{
|
2020-08-19 21:28:04 +01:00
|
|
|
|
if (ReferenceSector.Sector == null)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.ToConsole($"Sector is null for referencesector for {GetType().Name}!", MessageType.Error);
|
|
|
|
|
}
|
2020-08-16 17:15:36 +02:00
|
|
|
|
transform.position = ReferenceSector.Transform.InverseTransformPoint(SyncedTransform.position);
|
|
|
|
|
transform.rotation = ReferenceSector.Transform.InverseTransformRotation(SyncedTransform.rotation);
|
2020-08-07 20:39:07 +01:00
|
|
|
|
return;
|
2020-02-21 21:51:58 +01:00
|
|
|
|
}
|
2020-08-07 20:39:07 +01:00
|
|
|
|
|
|
|
|
|
// If this script is attached to any other body, eg the representations of other players
|
|
|
|
|
if (SyncedTransform.position == Vector3.zero)
|
2020-02-21 21:51:58 +01:00
|
|
|
|
{
|
2020-08-16 17:15:36 +02:00
|
|
|
|
Hide();
|
2020-02-21 21:51:58 +01:00
|
|
|
|
}
|
2020-08-17 21:28:38 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Show();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-14 21:33:37 +01:00
|
|
|
|
SyncedTransform.localPosition = Vector3.SmoothDamp(SyncedTransform.localPosition, transform.position, ref _positionSmoothVelocity, SmoothTime);
|
2020-08-07 20:39:07 +01:00
|
|
|
|
SyncedTransform.localRotation = QuaternionHelper.SmoothDamp(SyncedTransform.localRotation, transform.rotation, ref _rotationSmoothVelocity, Time.deltaTime);
|
2020-02-21 21:51:58 +01:00
|
|
|
|
}
|
2020-08-05 21:45:48 +02:00
|
|
|
|
|
2020-08-16 17:15:36 +02:00
|
|
|
|
public void SetReferenceSector(QSBSector sector)
|
2020-08-14 19:27:27 +02:00
|
|
|
|
{
|
2020-08-16 17:15:36 +02:00
|
|
|
|
_positionSmoothVelocity = Vector3.zero;
|
2020-08-14 21:04:29 +02:00
|
|
|
|
ReferenceSector = sector;
|
2020-08-16 17:15:36 +02:00
|
|
|
|
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-14 19:27:27 +02:00
|
|
|
|
}
|
2020-08-16 17:15:36 +02:00
|
|
|
|
|
2020-08-18 21:31:14 +02:00
|
|
|
|
private uint GetPlayerId()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return netId.Value - PlayerIdOffset;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
DebugLog.ToConsole($"Error while getting netId of {GetType().Name}! " +
|
|
|
|
|
$"{Environment.NewLine} - Did you destroy the TransformSync without destroying the {GetType().Name}?" +
|
|
|
|
|
$"{Environment.NewLine} - Did a destroyed TransformSync/{GetType().Name} still have an active action/event listener?" +
|
|
|
|
|
$"{Environment.NewLine} If you are a user seeing this, please report this error.", MessageType.Error);
|
|
|
|
|
return uint.MaxValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-21 21:51:58 +01:00
|
|
|
|
}
|
|
|
|
|
}
|