Files
AppleHillsProduction/Assets/Scripts/Minigames/BirdPooper/BirdFlapAnimator.cs
Michal Pikulski 28e6205bdd Yay, it flaps!
2025-11-20 02:47:12 +01:00

73 lines
2.4 KiB
C#

using UnityEngine;
namespace Minigames.BirdPooper
{
/// <summary>
/// Listens to BirdPlayerController flap events and triggers the flap animation.
/// Uses Animator with trigger parameter to transition from idle to flap animation.
/// </summary>
public class BirdFlapAnimator : MonoBehaviour
{
[Header("Animation Settings")]
[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 if not set
if (animator == null)
{
animator = GetComponent<Animator>();
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<BirdPlayerController>();
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);
}
}
/// <summary>
/// Called when the bird flaps. Triggers the flap animation via Animator parameter.
/// </summary>
private void OnBirdFlapped()
{
if (animator == null) return;
// Trigger the flap animation
animator.SetTrigger(flapTriggerName);
Debug.Log($"[BirdFlapAnimator] Flap trigger '{flapTriggerName}' activated");
}
}
}