using Core; using UnityEngine; namespace Interactions { /// /// Triggers an animation when an interactable interaction completes successfully. /// Auto-discovers Interactable and Animator components on the same GameObject or children. /// public class TriggerAnimationOnInteraction : MonoBehaviour { [Header("Component References (Optional)")] [Tooltip("Interactable to listen to. Leave empty to auto-discover on this GameObject or children.")] [SerializeField] private InteractableBase interactable; [Tooltip("Animator to trigger. Leave empty to auto-discover on this GameObject or children.")] [SerializeField] private Animator animator; [Header("Animation Settings")] [Tooltip("Name of the animation trigger to fire when interaction completes")] [SerializeField] private string triggerName; private InteractableBase _interactable; private Animator _animator; private bool _isInitialized; private void Awake() { DiscoverComponents(); } private void OnEnable() { if (_isInitialized && _interactable != null) { _interactable.interactionComplete.AddListener(OnInteractionComplete); } } private void OnDisable() { if (_interactable != null) { _interactable.interactionComplete.RemoveListener(OnInteractionComplete); } } /// /// Auto-discover Interactable and Animator components if not manually assigned /// private void DiscoverComponents() { // Use assigned Interactable or try to find it _interactable = interactable; if (_interactable == null) { _interactable = GetComponent(); } if (_interactable == null) { _interactable = GetComponentInChildren(); } if (_interactable == null) { Debug.LogError($"[TriggerAnimationOnInteraction] No InteractableBase found on {gameObject.name} or its children!"); return; } // Use assigned Animator or try to find it _animator = animator; if (_animator == null) { _animator = GetComponent(); } if (_animator == null) { _animator = GetComponentInChildren(); } if (_animator == null) { Debug.LogError($"[TriggerAnimationOnInteraction] No Animator found on {gameObject.name} or its children!"); return; } if (string.IsNullOrEmpty(triggerName)) { Debug.LogWarning($"[TriggerAnimationOnInteraction] Trigger name is empty on {gameObject.name}!"); } _isInitialized = true; string interactableSource = interactable != null ? "assigned" : "auto-discovered"; string animatorSource = animator != null ? "assigned" : "auto-discovered"; Logging.Debug($"[TriggerAnimationOnInteraction] Initialized on {gameObject.name} - Interactable: {_interactable.gameObject.name} ({interactableSource}), Animator: {_animator.gameObject.name} ({animatorSource})"); } /// /// Called when interaction completes /// /// Whether the interaction was successful private void OnInteractionComplete(bool success) { if (!success) { Logging.Debug($"[TriggerAnimationOnInteraction] Interaction failed, not triggering animation"); return; } if (_animator == null || string.IsNullOrEmpty(triggerName)) { return; } _animator.SetTrigger(triggerName); Logging.Debug($"[TriggerAnimationOnInteraction] Triggered animation: {triggerName}"); } #if UNITY_EDITOR private void OnValidate() { // Validate in editor if (string.IsNullOrEmpty(triggerName)) { name = "TriggerAnimationOnInteraction (NO TRIGGER)"; } else { name = $"TriggerAnimationOnInteraction ({triggerName})"; } } #endif } }