quantum-space-buddies/QSB/Syncs/SyncBase.cs

341 lines
7.8 KiB
C#
Raw Normal View History

2022-01-16 14:53:45 +00:00
using Mirror;
using OWML.Common;
using QSB.Player;
2022-02-25 11:59:10 +00:00
using QSB.Tools.ProbeTool.TransformSync;
using QSB.Utility;
2021-07-17 08:51:47 +00:00
using QSB.WorldSync;
2021-08-14 13:45:52 +00:00
using System;
using System.Linq;
using UnityEngine;
2022-02-25 09:36:15 +00:00
using Gizmos = Popcron.Gizmos;
namespace QSB.Syncs;
/*
* Rewrite number : 11
* God has cursed me for my hubris, and my work is never finished.
*/
2022-01-15 06:39:41 +00:00
public abstract class SyncBase : QSBNetworkTransform
{
/// <summary>
/// valid if IsPlayerObject, otherwise null
/// </summary>
public PlayerInfo Player
2022-01-15 04:59:42 +00:00
{
get
2022-01-16 16:15:50 +00:00
{
if (_player == null)
2022-01-16 16:15:50 +00:00
{
DebugLog.ToConsole($"Error - trying to get SyncBase.Player for {netId} before Start has been called! "
+ "this really should not be happening!\n"
+ Environment.StackTrace,
MessageType.Error);
2022-01-16 16:15:50 +00:00
}
return _player;
}
}
private PlayerInfo _player;
private bool IsInitialized;
protected virtual bool CheckReady()
{
if (netId is uint.MaxValue or 0)
{
return false;
2022-01-16 16:15:50 +00:00
}
2022-01-15 04:59:42 +00:00
if (!QSBWorldSync.AllObjectsAdded)
{
return false;
}
if (IsPlayerObject)
2022-01-15 04:59:42 +00:00
{
if (_player == null)
2022-01-15 04:59:42 +00:00
{
return false;
}
if (!isLocalPlayer && !_player.IsReady)
2022-01-15 04:59:42 +00:00
{
return false;
}
}
2022-01-15 04:59:42 +00:00
return true;
}
2022-01-15 04:59:42 +00:00
/// <summary>
/// can be true with null reference transform. <br/>
/// can be true with inactive attached object.
/// </summary>
public bool IsValid { get; private set; }
protected virtual bool CheckValid()
{
if (!IsInitialized)
{
return false;
2022-01-15 04:59:42 +00:00
}
if (!AttachedTransform)
{
DebugLog.ToConsole($"Error - AttachedObject {this} is null!", MessageType.Error);
return false;
}
if (!AllowInactiveAttachedObject && !AttachedTransform.gameObject.activeInHierarchy)
{
return false;
}
if (!AllowNullReferenceTransform && !ReferenceTransform)
{
DebugLog.ToConsole($"Warning - {this}'s ReferenceTransform is null.", MessageType.Warning);
return false;
}
2022-01-15 04:59:42 +00:00
return true;
}
protected abstract bool UseInterpolation { get; }
protected virtual bool AllowInactiveAttachedObject => false;
protected abstract bool AllowNullReferenceTransform { get; }
protected virtual bool IsPlayerObject => false;
protected virtual bool OnlyApplyOnDeserialize => false;
public Transform AttachedTransform { get; private set; }
public Transform ReferenceTransform { get; private set; }
2022-01-15 04:59:42 +00:00
public string Name => AttachedTransform ? AttachedTransform.name : "<NullObject!>";
2022-01-15 04:59:42 +00:00
public override string ToString() => (IsPlayerObject ? $"{Player.PlayerId}." : string.Empty)
+ $"{netId}:{GetType().Name} ({Name})";
2022-01-15 04:59:42 +00:00
protected virtual float DistanceChangeThreshold => 5f;
protected const float SmoothTime = 0.1f;
private Vector3 _positionSmoothVelocity;
private Quaternion _rotationSmoothVelocity;
protected Vector3 SmoothPosition { get; private set; }
protected Quaternion SmoothRotation { get; private set; }
2022-01-23 08:29:59 +00:00
protected abstract Transform InitAttachedTransform();
protected abstract void GetFromAttached();
protected abstract void ApplyToAttached();
2022-01-22 02:00:02 +00:00
public override void OnStartClient()
{
if (IsPlayerObject)
{
// get player objects spawned before this object (or is this one)
// and use the closest one
_player = QSBPlayerManager.PlayerList
.Where(x => x.PlayerId <= netId)
.MaxBy(x => x.PlayerId);
}
2022-01-15 04:59:42 +00:00
DontDestroyOnLoad(gameObject);
QSBSceneManager.OnSceneLoaded += OnSceneLoaded;
}
2022-01-15 04:59:42 +00:00
public override void OnStopClient()
{
QSBSceneManager.OnSceneLoaded -= OnSceneLoaded;
if (IsInitialized)
2022-01-15 04:59:42 +00:00
{
SafeUninit();
}
}
2022-01-15 04:59:42 +00:00
private void OnSceneLoaded(OWScene oldScene, OWScene newScene, bool isInUniverse)
{
if (IsInitialized)
{
SafeUninit();
2022-01-15 04:59:42 +00:00
}
}
private const float _pauseTimerDelay = 1;
private float _pauseTimer;
2022-01-15 04:59:42 +00:00
private void SafeInit()
{
this.Try("initializing", () =>
2022-01-15 04:59:42 +00:00
{
Init();
IsInitialized = true;
});
if (!IsInitialized)
{
_pauseTimer = _pauseTimerDelay;
2022-01-15 04:59:42 +00:00
}
}
2022-01-15 04:59:42 +00:00
private void SafeUninit()
{
this.Try("uninitializing", () =>
2022-01-15 04:59:42 +00:00
{
Uninit();
IsInitialized = false;
IsValid = false;
});
if (IsInitialized)
{
_pauseTimer = _pauseTimerDelay;
2022-01-22 00:26:21 +00:00
}
}
2022-01-15 04:59:42 +00:00
protected virtual void Init() =>
AttachedTransform = InitAttachedTransform();
protected virtual void Uninit()
{
if (IsPlayerObject && !hasAuthority && AttachedTransform)
{
Destroy(AttachedTransform.gameObject);
}
}
private bool _shouldApply;
2022-01-15 04:59:42 +00:00
protected override void Deserialize(NetworkReader reader)
{
base.Deserialize(reader);
if (OnlyApplyOnDeserialize)
{
_shouldApply = true;
}
}
protected sealed override void Update()
{
if (_pauseTimer > 0)
2022-01-22 00:26:21 +00:00
{
_pauseTimer = Mathf.Max(0, _pauseTimer - Time.unscaledDeltaTime);
return;
2022-01-15 04:59:42 +00:00
}
if (!IsInitialized && CheckReady())
{
SafeInit();
}
else if (IsInitialized && !CheckReady())
{
SafeUninit();
}
2022-01-16 16:15:50 +00:00
IsValid = CheckValid();
if (!IsValid)
2022-01-16 14:53:45 +00:00
{
return;
2022-01-16 14:53:45 +00:00
}
if (hasAuthority)
{
GetFromAttached();
2022-02-25 11:59:10 +00:00
base.Update();
}
else if (!OnlyApplyOnDeserialize || _shouldApply)
{
_shouldApply = false;
2022-02-25 11:59:10 +00:00
if (UseInterpolation)
{
Interpolate();
}
ApplyToAttached();
}
}
2022-01-15 04:59:42 +00:00
2022-02-25 09:36:15 +00:00
private Vector3 _prevSmoothPosition;
2022-02-25 11:59:10 +00:00
private float _prevSmoothSpeed;
2022-02-25 09:36:15 +00:00
private void Interpolate()
{
2022-02-25 11:59:10 +00:00
var smoothPosition = SmoothPosition;
var smoothSpeed = Vector3.Distance(smoothPosition, _prevSmoothPosition);
var smoothAccel = smoothSpeed - _prevSmoothSpeed;
2022-02-25 09:36:15 +00:00
2022-02-25 11:59:10 +00:00
if (QSBCore.IsHost && this is PlayerProbeSync && !hasAuthority && smoothSpeed > 0.0100)
{
2022-02-25 11:59:10 +00:00
// DebugLog.DebugWrite($"speed {smoothSpeed:F4} accel {smoothAccel:F4}");
}
2022-02-25 11:59:10 +00:00
_prevSmoothPosition = smoothPosition;
_prevSmoothSpeed = smoothSpeed;
if (smoothAccel > DistanceChangeThreshold)
{
2022-02-25 11:59:10 +00:00
if (QSBCore.IsHost && this is PlayerProbeSync && !hasAuthority)
{
// DebugLog.DebugWrite($"teleport", MessageType.Success);
}
// SmoothPosition = transform.position;
// SmoothRotation = transform.rotation;
}
2022-02-25 11:59:10 +00:00
SmoothPosition = Vector3.SmoothDamp(SmoothPosition, transform.position, ref _positionSmoothVelocity, SmoothTime);
SmoothRotation = QuaternionHelper.SmoothDamp(SmoothRotation, transform.rotation, ref _rotationSmoothVelocity, SmoothTime);
}
public virtual void SetReferenceTransform(Transform referenceTransform)
{
if (ReferenceTransform == referenceTransform)
{
return;
2022-01-15 04:59:42 +00:00
}
ReferenceTransform = referenceTransform;
2022-02-25 11:59:10 +00:00
if (IsPlayerObject && !hasAuthority && AttachedTransform)
{
AttachedTransform.parent = ReferenceTransform;
AttachedTransform.localScale = Vector3.one;
}
2022-02-25 11:59:10 +00:00
if (UseInterpolation && !hasAuthority && AttachedTransform)
2022-01-16 16:04:15 +00:00
{
SmoothPosition = ReferenceTransform.ToRelPos(AttachedTransform.position);
SmoothRotation = ReferenceTransform.ToRelRot(AttachedTransform.rotation);
2022-01-16 16:04:15 +00:00
}
}
2022-01-16 16:04:15 +00:00
protected virtual void OnRenderObject()
{
if (!QSBCore.DebugSettings.DrawLines
|| !IsValid
|| !ReferenceTransform)
2022-01-15 04:59:42 +00:00
{
return;
2022-01-15 04:59:42 +00:00
}
/* Red Cube = Where visible object should be
* Green cube = Where visible object is
* Magenta cube = Reference transform
* Red Line = Connection between Red Cube and Green Cube
* Cyan Line = Connection between Green cube and reference transform
*/
2022-02-25 09:36:15 +00:00
Gizmos.Cube(ReferenceTransform.FromRelPos(transform.position), ReferenceTransform.FromRelRot(transform.rotation), Vector3.one / 8, Color.red);
Gizmos.Line(ReferenceTransform.FromRelPos(transform.position), AttachedTransform.transform.position, Color.red);
Gizmos.Cube(AttachedTransform.transform.position, AttachedTransform.transform.rotation, Vector3.one / 6, Color.green);
Gizmos.Cube(ReferenceTransform.position, ReferenceTransform.rotation, Vector3.one / 8, Color.magenta);
Gizmos.Line(AttachedTransform.transform.position, ReferenceTransform.position, Color.cyan);
}
2022-01-15 04:59:42 +00:00
private void OnGUI()
{
if (!QSBCore.DebugSettings.DrawLabels
|| Event.current.type != EventType.Repaint
|| !IsValid
|| !ReferenceTransform)
2022-01-15 04:59:42 +00:00
{
return;
2022-01-15 04:59:42 +00:00
}
DebugGUI.DrawLabel(AttachedTransform.transform, ToString());
2022-01-15 04:59:42 +00:00
}
2022-02-25 09:36:15 +00:00
}