Yay, it flaps!
This commit is contained in:
72
Assets/Scripts/Minigames/BirdPooper/BirdFlapAnimator.cs
Normal file
72
Assets/Scripts/Minigames/BirdPooper/BirdFlapAnimator.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9ba8e12a2b8b4d468ccfa995ed56980c
|
||||
timeCreated: 1763600531
|
||||
@@ -11,6 +11,9 @@ namespace Minigames.BirdPooper
|
||||
/// </summary>
|
||||
public class BirdPlayerController : ManagedBehaviour, ITouchInputConsumer
|
||||
{
|
||||
[Header("Events")]
|
||||
public UnityEngine.Events.UnityEvent OnFlap;
|
||||
|
||||
private Rigidbody2D rb;
|
||||
private IBirdPooperSettings settings;
|
||||
private float verticalVelocity = 0f;
|
||||
@@ -21,6 +24,10 @@ namespace Minigames.BirdPooper
|
||||
{
|
||||
base.OnManagedAwake();
|
||||
|
||||
// Initialize event
|
||||
if (OnFlap == null)
|
||||
OnFlap = new UnityEngine.Events.UnityEvent();
|
||||
|
||||
// Load settings
|
||||
settings = GameManager.GetSettingsObject<IBirdPooperSettings>();
|
||||
if (settings == null)
|
||||
@@ -103,6 +110,9 @@ namespace Minigames.BirdPooper
|
||||
{
|
||||
verticalVelocity = settings.FlapForce;
|
||||
Debug.Log($"[BirdPlayerController] Flap! velocity = {verticalVelocity}");
|
||||
|
||||
// Emit flap event
|
||||
OnFlap?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user