namespace Tilia.Interactions.Interactables.Interactables { using UnityEngine; using Zinnia.Extension; /// /// Caches common properties for an to be restored at a later point in time. /// public class InteractablePropertyCache : MonoBehaviour { [Tooltip("The source to cache properties for.")] [SerializeField] private InteractableFacade source; /// /// The source to cache properties for. /// public InteractableFacade Source { get { return source; } set { source = value; } } /// /// The cached local position of the /// protected Vector3? cachedLocalPosition; /// /// The cached local rotation of the /// protected Quaternion cachedLocalRotation; /// /// The cached local scale of the /// protected Vector3 cachedLocalScale; /// /// The cached kinematic state of the the /// protected bool cachedRigidbodyKinematicState; /// /// Clears . /// public virtual void ClearSource() { if (!this.IsValidState()) { return; } Source = default; } /// /// Sets the property from a given . /// /// The source to set to. public virtual void SetSource(GameObject source) { if (!this.IsValidState() || source == null) { return; } Source = source.GetComponent(); } /// /// Caches the position. /// public virtual void CachePosition() { if (!this.IsValidState() || Source == null) { return; } cachedLocalPosition = Source.transform.localPosition; } /// /// Caches the rotation. /// public virtual void CacheRotation() { if (!this.IsValidState() || Source == null) { return; } cachedLocalRotation = Source.transform.localRotation; } /// /// Caches the scale. /// public virtual void CacheScale() { if (!this.IsValidState() || Source == null) { return; } cachedLocalScale = Source.transform.localScale; } /// /// Caches the rigidbody kinematic state.. /// public virtual void CacheRigidbodyKinematicState() { if (!this.IsValidState() || Source == null) { return; } cachedRigidbodyKinematicState = Source.Configuration.ConsumerRigidbody != null ? Source.Configuration.ConsumerRigidbody.isKinematic : false; } /// /// Caches all of the properties. /// public virtual void CacheAll() { if (!this.IsValidState()) { return; } CachePosition(); CacheRotation(); CacheScale(); CacheRigidbodyKinematicState(); } /// /// Restores the cached position. /// public virtual void RestorePosition() { if (!this.IsValidState() || Source == null) { return; } Source.transform.localPosition = (Vector3)cachedLocalPosition; } /// /// Restores the cached rotation. /// public virtual void RestoreRotation() { if (!this.IsValidState() || Source == null) { return; } Source.transform.localRotation = cachedLocalRotation; } /// /// Restores the cached scale. /// public virtual void RestoreScale() { if (!this.IsValidState() || Source == null) { return; } Source.transform.localScale = cachedLocalScale; } /// /// Restores the cached rigidbody kinematic state. /// public virtual void RestoreRigidbodyKinematicState() { if (!this.IsValidState() || Source == null || Source.Configuration.ConsumerRigidbody == null) { return; } Source.Configuration.ConsumerRigidbody.isKinematic = cachedRigidbodyKinematicState; } /// /// Restores the all of cached properties. /// public virtual void RestoreAll() { if (!this.IsValidState()) { return; } RestorePosition(); RestoreRotation(); RestoreScale(); RestoreRigidbodyKinematicState(); } } }