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

184 lines
5.9 KiB
C#
Raw Normal View History

using OWML.Common;
using QSB.Player;
using QSB.Utility;
using QuantumUNET.Components;
using UnityEngine;
namespace QSB.Syncs
{
2021-07-07 22:54:09 +00:00
public abstract class SyncBase : QNetworkTransform
{
public uint AttachedNetId
{
get
{
if (NetIdentity == null)
{
DebugLog.ToConsole($"Error - Trying to get AttachedNetId with null NetIdentity! Type:{GetType().Name} GrandType:{GetType().GetType().Name}", MessageType.Error);
return uint.MaxValue;
}
return NetIdentity.NetId.Value;
}
}
public uint PlayerId
{
get
{
if (NetIdentity == null)
{
DebugLog.ToConsole($"Error - Trying to get PlayerId with null NetIdentity! Type:{GetType().Name} GrandType:{GetType().GetType().Name}", MessageType.Error);
return uint.MaxValue;
}
return NetIdentity.RootIdentity != null
? NetIdentity.RootIdentity.NetId.Value
: AttachedNetId;
}
}
public PlayerInfo Player => QSBPlayerManager.GetPlayer(PlayerId);
public abstract bool IsReady { get; }
public abstract bool UseInterpolation { get; }
2021-07-11 15:18:47 +00:00
public abstract bool IgnoreDisabledAttachedObject { get; }
public abstract bool IgnoreNullReferenceTransform { get; }
2021-07-07 22:54:09 +00:00
public Component AttachedObject { get; set; }
public Transform ReferenceTransform { get; set; }
protected string _logName => $"{PlayerId}.{GetType().Name}";
protected virtual float DistanceLeeway { get; } = 5f;
private float _previousDistance;
protected const float SmoothTime = 0.1f;
protected Vector3 _positionSmoothVelocity;
protected Quaternion _rotationSmoothVelocity;
protected IntermediaryTransform _intermediaryTransform;
protected bool _isInitialized;
2021-07-07 22:54:09 +00:00
protected abstract Component InitLocalTransform();
protected abstract Component InitRemoteTransform();
2021-07-11 15:18:47 +00:00
protected abstract bool UpdateTransform();
2021-07-07 22:04:00 +00:00
protected abstract void Init();
protected Vector3 SmartSmoothDamp(Vector3 currentPosition, Vector3 targetPosition)
{
var distance = Vector3.Distance(currentPosition, targetPosition);
if (distance > _previousDistance + DistanceLeeway)
{
_previousDistance = distance;
return targetPosition;
}
_previousDistance = distance;
return Vector3.SmoothDamp(currentPosition, targetPosition, ref _positionSmoothVelocity, SmoothTime);
}
2021-07-07 22:04:00 +00:00
public override void Update()
{
if (!_isInitialized && IsReady)
{
Init();
}
else if (_isInitialized && !IsReady)
{
_isInitialized = false;
2021-07-11 15:18:47 +00:00
base.Update();
2021-07-07 22:04:00 +00:00
return;
}
if (!_isInitialized)
{
2021-07-11 15:18:47 +00:00
base.Update();
2021-07-07 22:04:00 +00:00
return;
}
if (AttachedObject == null)
{
DebugLog.ToConsole($"Warning - AttachedObject {_logName} is null.", MessageType.Warning);
_isInitialized = false;
2021-07-11 15:18:47 +00:00
base.Update();
2021-07-07 22:04:00 +00:00
return;
}
if (ReferenceTransform != null && ReferenceTransform.position == Vector3.zero)
{
DebugLog.ToConsole($"Warning - {_logName}'s ReferenceTransform is at (0,0,0). ReferenceTransform:{ReferenceTransform.name}, AttachedObject:{AttachedObject.name}", MessageType.Warning);
}
2021-07-11 15:18:47 +00:00
if (!AttachedObject.gameObject.activeInHierarchy && !IgnoreDisabledAttachedObject)
2021-07-07 22:04:00 +00:00
{
2021-07-11 15:18:47 +00:00
base.Update();
2021-07-07 22:04:00 +00:00
return;
}
2021-07-11 15:18:47 +00:00
if (ReferenceTransform == null && !IgnoreNullReferenceTransform)
2021-07-07 22:04:00 +00:00
{
DebugLog.ToConsole($"Warning - {_logName}'s ReferenceTransform is null. AttachedObject:{AttachedObject.name}", MessageType.Warning);
2021-07-11 15:18:47 +00:00
base.Update();
2021-07-07 22:04:00 +00:00
return;
}
2021-07-11 15:18:47 +00:00
if (ReferenceTransform != _intermediaryTransform.GetReferenceTransform())
{
DebugLog.ToConsole($"Warning - {_logName}'s ReferenceTransform does not match the reference transform set for the intermediary. ReferenceTransform null : {ReferenceTransform == null}, Intermediary reference null : {_intermediaryTransform.GetReferenceTransform() == null}");
base.Update();
return;
}
var state = UpdateTransform();
if (!state)
{
DebugLog.ToConsole($"{_logName} UpdateTransform() fail.", MessageType.Error);
base.Update();
return;
}
2021-07-07 22:04:00 +00:00
2021-07-12 21:02:50 +00:00
/*
var expectedPosition = _intermediaryTransform.GetTargetPosition_Unparented();
var actualPosition = AttachedObject.transform.position;
var distance = Vector3.Distance(expectedPosition, actualPosition);
if (distance > 20)
{
2021-07-11 15:18:47 +00:00
var intermediaryReference = _intermediaryTransform.GetReferenceTransform();
DebugLog.ToConsole($"Warning - {_logName}'s AttachedObject ({AttachedObject?.name}) is far away from it's expected position! Info:" +
$"\r\n AttachedObject's parent : {AttachedObject?.transform.parent?.name}" +
$"\r\n Distance : {distance}" +
2021-07-11 15:18:47 +00:00
$"\r\n ReferenceTransform : {(ReferenceTransform == null ? "NULL" : ReferenceTransform.name)}" +
$"\r\n Intermediary's ReferenceTransform : {(intermediaryReference == null ? "NULL" : intermediaryReference.name)}", MessageType.Warning);
}
2021-07-12 21:02:50 +00:00
*/
2021-07-07 22:04:00 +00:00
base.Update();
}
protected virtual void OnRenderObject()
{
if (!QSBCore.WorldObjectsReady
|| !QSBCore.DebugMode
|| !QSBCore.ShowLinesInDebug
|| !IsReady
2021-07-11 15:18:47 +00:00
|| ReferenceTransform == null
|| _intermediaryTransform.GetReferenceTransform() == null)
{
return;
}
/* Red Cube = Where visible object should be
* Green/Yellow Cube = Where visible object is
* Magenta cube = Reference transform
* Red Line = Connection between Red Cube and Green/Yellow Cube
* Cyan Line = Connection between Green/Yellow cube and reference transform
*/
Popcron.Gizmos.Cube(_intermediaryTransform.GetTargetPosition_Unparented(), _intermediaryTransform.GetTargetRotation_Unparented(), Vector3.one / 2, Color.red);
Popcron.Gizmos.Line(_intermediaryTransform.GetTargetPosition_Unparented(), AttachedObject.transform.position, Color.red);
var color = HasMoved() ? Color.green : Color.yellow;
Popcron.Gizmos.Cube(AttachedObject.transform.position, AttachedObject.transform.rotation, Vector3.one / 2, color);
Popcron.Gizmos.Cube(ReferenceTransform.position, ReferenceTransform.rotation, Vector3.one / 2, Color.magenta);
Popcron.Gizmos.Line(AttachedObject.transform.position, ReferenceTransform.position, Color.cyan);
}
}
}