2021-05-07 23:10:30 +00:00
|
|
|
|
using OWML.Common;
|
|
|
|
|
using QSB.Utility;
|
2021-04-27 21:09:57 +00:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
2021-05-15 10:25:47 +00:00
|
|
|
|
namespace QSB.Syncs
|
2021-04-27 21:09:57 +00:00
|
|
|
|
{
|
2021-04-27 23:22:08 +00:00
|
|
|
|
public class IntermediaryTransform
|
2021-04-27 21:09:57 +00:00
|
|
|
|
{
|
|
|
|
|
private Transform _attachedTransform;
|
2021-04-27 23:22:08 +00:00
|
|
|
|
private Transform _referenceTransform;
|
2021-04-27 21:09:57 +00:00
|
|
|
|
|
2021-06-19 10:26:05 +00:00
|
|
|
|
public IntermediaryTransform(Transform transform)
|
2021-04-28 09:02:16 +00:00
|
|
|
|
=> _attachedTransform = transform;
|
|
|
|
|
|
2021-04-27 21:09:57 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get the world position of this INVISIBLE transform.
|
|
|
|
|
/// </summary>
|
2021-06-19 10:26:05 +00:00
|
|
|
|
public Vector3 GetPosition()
|
2021-04-27 21:09:57 +00:00
|
|
|
|
=> _attachedTransform.position;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Set the world position of this INVISIBLE transform.
|
|
|
|
|
/// </summary>
|
2021-06-19 10:26:05 +00:00
|
|
|
|
public void SetPosition(Vector3 worldPos)
|
2021-04-27 21:09:57 +00:00
|
|
|
|
=> _attachedTransform.position = worldPos;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get the world rotation of this INVISIBLE transform.
|
|
|
|
|
/// </summary>
|
2021-06-19 10:26:05 +00:00
|
|
|
|
public Quaternion GetRotation()
|
2021-04-27 21:09:57 +00:00
|
|
|
|
=> _attachedTransform.rotation;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Set the world rotation of this INVISIBLE transform.
|
|
|
|
|
/// </summary>
|
2021-06-19 10:26:05 +00:00
|
|
|
|
public void SetRotation(Quaternion worldRot)
|
2021-04-27 21:09:57 +00:00
|
|
|
|
=> _attachedTransform.rotation = worldRot;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-04-27 23:22:08 +00:00
|
|
|
|
/// Sets the reference transform - what transform this transform is syncing to.
|
2021-04-27 21:09:57 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sector">The new reference sector.</param>
|
2021-06-19 10:26:05 +00:00
|
|
|
|
public void SetReferenceTransform(Transform transform)
|
2021-04-27 23:22:08 +00:00
|
|
|
|
=> _referenceTransform = transform;
|
2021-04-27 21:09:57 +00:00
|
|
|
|
|
2021-07-11 15:18:47 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the reference transform - what transform this transform is syncing to.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Transform GetReferenceTransform()
|
|
|
|
|
=> _referenceTransform;
|
|
|
|
|
|
2021-04-27 21:09:57 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets the position of the INVISIBLE transform to be correct, according to the reference sector and the position of the VISIBLE transform.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="worldPosition">The world position of the VISIBLE transform.</param>
|
2021-05-02 07:54:00 +00:00
|
|
|
|
public void EncodePosition(Vector3 worldPosition)
|
|
|
|
|
{
|
|
|
|
|
if (_referenceTransform == null)
|
|
|
|
|
{
|
2021-10-24 09:47:25 +00:00
|
|
|
|
DebugLog.ToConsole($"Error - _referenceTransform has not been set for {_attachedTransform.name}", MessageType.Error);
|
2021-05-02 07:54:00 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2021-06-19 10:26:05 +00:00
|
|
|
|
|
2021-05-02 07:54:00 +00:00
|
|
|
|
SetPosition(_referenceTransform.InverseTransformPoint(worldPosition));
|
|
|
|
|
}
|
2021-04-27 21:09:57 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets the rotation of the INVISIBLE transform to be correct, according to the reference sector and the rotation of the VISIBLE transform.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="worldPosition">The world rotation of the VISIBLE transform.</param>
|
|
|
|
|
public void EncodeRotation(Quaternion worldRotation)
|
2021-05-02 07:54:00 +00:00
|
|
|
|
{
|
|
|
|
|
if (_referenceTransform == null)
|
|
|
|
|
{
|
2021-10-24 09:47:25 +00:00
|
|
|
|
DebugLog.ToConsole($"Error - _referenceTransform has not been set for {_attachedTransform.name}", MessageType.Error);
|
2021-05-02 07:54:00 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2021-06-18 21:38:32 +00:00
|
|
|
|
|
2021-05-02 07:54:00 +00:00
|
|
|
|
SetRotation(_referenceTransform.InverseTransformRotation(worldRotation));
|
|
|
|
|
}
|
2021-04-27 21:09:57 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-04-28 09:02:16 +00:00
|
|
|
|
/// Returns the local position the VISIBLE transform should be set to, from the INVISIBLE transform.
|
2021-04-27 21:09:57 +00:00
|
|
|
|
/// </summary>
|
2021-04-28 09:02:16 +00:00
|
|
|
|
public Vector3 GetTargetPosition_ParentedToReference()
|
2021-04-27 21:09:57 +00:00
|
|
|
|
=> GetPosition();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-04-28 09:02:16 +00:00
|
|
|
|
/// Returns the local rotation the VISIBLE transform should be set to, from the INVISIBLE transform.
|
2021-04-27 21:09:57 +00:00
|
|
|
|
/// </summary>
|
2021-04-28 09:02:16 +00:00
|
|
|
|
public Quaternion GetTargetRotation_ParentedToReference()
|
2021-04-27 21:09:57 +00:00
|
|
|
|
=> GetRotation();
|
2021-04-28 09:02:16 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the world position the VISIBLE transform should be set to, from the INVISIBLE transform.
|
|
|
|
|
/// </summary>
|
2021-05-07 23:10:30 +00:00
|
|
|
|
public Vector3 GetTargetPosition_Unparented()
|
|
|
|
|
{
|
|
|
|
|
if (_referenceTransform == null)
|
|
|
|
|
{
|
2021-10-24 09:47:25 +00:00
|
|
|
|
DebugLog.ToConsole($"Error - _referenceTransform has not been set for {_attachedTransform.name}", MessageType.Error);
|
2021-05-07 23:10:30 +00:00
|
|
|
|
return Vector3.zero;
|
|
|
|
|
}
|
2021-06-18 21:38:32 +00:00
|
|
|
|
|
2021-05-07 23:10:30 +00:00
|
|
|
|
return _referenceTransform.TransformPoint(GetPosition());
|
|
|
|
|
}
|
2021-04-28 09:02:16 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the world rotation the VISIBLE transform should be set to, from the INVISIBLE transform.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Quaternion GetTargetRotation_Unparented()
|
2021-05-07 23:10:30 +00:00
|
|
|
|
{
|
|
|
|
|
if (_referenceTransform == null)
|
|
|
|
|
{
|
2021-10-24 09:47:25 +00:00
|
|
|
|
DebugLog.ToConsole($"Error - _referenceTransform has not been set for {_attachedTransform.name}", MessageType.Error);
|
2021-05-07 23:10:30 +00:00
|
|
|
|
return Quaternion.identity;
|
|
|
|
|
}
|
2021-06-18 21:38:32 +00:00
|
|
|
|
|
2021-05-07 23:10:30 +00:00
|
|
|
|
return _referenceTransform.TransformRotation(GetRotation());
|
|
|
|
|
}
|
2021-04-27 21:09:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|