137 lines
4.5 KiB
C#
137 lines
4.5 KiB
C#
|
|
using Core;
|
||
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
namespace Interactions
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// Triggers an animation when an interactable interaction completes successfully.
|
||
|
|
/// Auto-discovers Interactable and Animator components on the same GameObject or children.
|
||
|
|
/// </summary>
|
||
|
|
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);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Auto-discover Interactable and Animator components if not manually assigned
|
||
|
|
/// </summary>
|
||
|
|
private void DiscoverComponents()
|
||
|
|
{
|
||
|
|
// Use assigned Interactable or try to find it
|
||
|
|
_interactable = interactable;
|
||
|
|
if (_interactable == null)
|
||
|
|
{
|
||
|
|
_interactable = GetComponent<InteractableBase>();
|
||
|
|
}
|
||
|
|
if (_interactable == null)
|
||
|
|
{
|
||
|
|
_interactable = GetComponentInChildren<InteractableBase>();
|
||
|
|
}
|
||
|
|
|
||
|
|
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<Animator>();
|
||
|
|
}
|
||
|
|
if (_animator == null)
|
||
|
|
{
|
||
|
|
_animator = GetComponentInChildren<Animator>();
|
||
|
|
}
|
||
|
|
|
||
|
|
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})");
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Called when interaction completes
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="success">Whether the interaction was successful</param>
|
||
|
|
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
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|