using UnityEngine;
namespace Minigames.BirdPooper
{
///
/// Listens to BirdPlayerController flap events and triggers wing flap animation.
/// Uses Animator with a trigger parameter to play flap animation from idle state.
///
public class BirdFlapAnimator : MonoBehaviour
{
[SerializeField] private Animator animator;
[Tooltip("Name of the trigger parameter in the Animator")]
[SerializeField] private string flapTriggerName = "Flap";
private BirdPlayerController birdController;
void Awake()
{
// Auto-assign animator component if not set
if (animator == null)
{
animator = GetComponent();
if (animator == null)
{
Debug.LogError("[BirdFlapAnimator] No Animator component found! Please add an Animator component or assign one in the Inspector.");
}
}
// Find parent BirdPlayerController
birdController = GetComponentInParent();
if (birdController == null)
{
Debug.LogError("[BirdFlapAnimator] No BirdPlayerController found in parent! This component must be a child of the bird GameObject.");
}
}
void OnEnable()
{
// Subscribe to flap event
if (birdController != null && birdController.OnFlap != null)
{
birdController.OnFlap.AddListener(OnBirdFlapped);
Debug.Log("[BirdFlapAnimator] Subscribed to OnFlap event");
}
}
void OnDisable()
{
// Unsubscribe from flap event
if (birdController != null && birdController.OnFlap != null)
{
birdController.OnFlap.RemoveListener(OnBirdFlapped);
}
}
///
/// Called when the bird flaps. Triggers the flap animation.
///
private void OnBirdFlapped()
{
if (animator == null) return;
// Trigger the flap animation
animator.SetTrigger(flapTriggerName);
Debug.Log($"[BirdFlapAnimator] Triggered animation: {flapTriggerName}");
}
}
}