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