using OWML.Common; using QSB.Utility; using System.Reflection; using UnityEngine; namespace QSB.Syncs { public class IntermediaryTransform { private Transform _attachedTransform; private Transform _referenceTransform; public IntermediaryTransform(Transform transform) => _attachedTransform = transform; /// /// Get the world position of this INVISIBLE transform. /// public Vector3 GetPosition() => _attachedTransform.position; /// /// Set the world position of this INVISIBLE transform. /// public void SetPosition(Vector3 worldPos) => _attachedTransform.position = worldPos; /// /// Get the world rotation of this INVISIBLE transform. /// public Quaternion GetRotation() => _attachedTransform.rotation; /// /// Set the world rotation of this INVISIBLE transform. /// public void SetRotation(Quaternion worldRot) => _attachedTransform.rotation = worldRot; /// /// Sets the reference transform - what transform this transform is syncing to. /// /// The new reference sector. public void SetReferenceTransform(Transform transform) => _referenceTransform = transform; /// /// Returns the reference transform - what transform this transform is syncing to. /// public Transform GetReferenceTransform() => _referenceTransform; /// /// Sets the position of the INVISIBLE transform to be correct, according to the reference sector and the position of the VISIBLE transform. /// /// The world position of the VISIBLE transform. public void EncodePosition(Vector3 worldPosition) { if (_referenceTransform == null) { DebugLog.ToConsole($"Error - _referenceTransform has not been set for {_attachedTransform.name} ({MethodBase.GetCurrentMethod().Name})", MessageType.Error); return; } SetPosition(_referenceTransform.InverseTransformPoint(worldPosition)); } /// /// Sets the rotation of the INVISIBLE transform to be correct, according to the reference sector and the rotation of the VISIBLE transform. /// /// The world rotation of the VISIBLE transform. public void EncodeRotation(Quaternion worldRotation) { if (_referenceTransform == null) { DebugLog.ToConsole($"Error - _referenceTransform has not been set for {_attachedTransform.name} ({MethodBase.GetCurrentMethod().Name})", MessageType.Error); return; } SetRotation(_referenceTransform.InverseTransformRotation(worldRotation)); } /// /// Returns the local position the VISIBLE transform should be set to, from the INVISIBLE transform. /// public Vector3 GetTargetPosition_ParentedToReference() => GetPosition(); /// /// Returns the local rotation the VISIBLE transform should be set to, from the INVISIBLE transform. /// public Quaternion GetTargetRotation_ParentedToReference() => GetRotation(); /// /// Returns the world position the VISIBLE transform should be set to, from the INVISIBLE transform. /// public Vector3 GetTargetPosition_Unparented() { if (_referenceTransform == null) { DebugLog.ToConsole($"Error - _referenceTransform has not been set for {_attachedTransform.name} ({MethodBase.GetCurrentMethod().Name})", MessageType.Error); return Vector3.zero; } return _referenceTransform.TransformPoint(GetPosition()); } /// /// Returns the world rotation the VISIBLE transform should be set to, from the INVISIBLE transform. /// public Quaternion GetTargetRotation_Unparented() { if (_referenceTransform == null) { DebugLog.ToConsole($"Error - _referenceTransform has not been set for {_attachedTransform.name} ({MethodBase.GetCurrentMethod().Name})", MessageType.Error); return Quaternion.identity; } return _referenceTransform.TransformRotation(GetRotation()); } } }