Files
AppleHillsProduction/Assets/Scripts/DamianExperiments/SoundBirdPuzzleSection/soundBird_LandingBehaviour1.cs
2025-11-05 20:37:17 +01:00

84 lines
2.2 KiB
C#

using Core.SaveLoad;
using Pixelplacement;
using Pixelplacement.TweenSystem;
using UnityEngine;
public class soundBird_LandingBehaviour1 : MonoBehaviour
{
public Spline FlightSpline;
public Transform SoundBirdObject;
public float flightDuration;
public float flightDelay;
public soundBird_FlyingBehaviour flyingBehaviour;
private AppleMachine stateMachine;
private Animator animator;
private TweenBase objectTween;
private float lastX;
private bool facingRight = true;
void Awake()
{
stateMachine = GetComponentInParent<AppleMachine>();
animator = GetComponentInParent<Animator>();
}
private void OnEnable()
{
Transform anchorB = transform.Find("AnchorB");
if (anchorB != null)
{
anchorB.position = flyingBehaviour.midFlightPosition;
}
objectTween = Tween.Spline(FlightSpline, SoundBirdObject, 0, 1, false, flightDuration, flightDelay, Tween.EaseOut, Tween.LoopType.None, HandleTweenStarted, HandleTweenFinished);
// Initialize lastX for flipping logic
if (SoundBirdObject != null)
{
lastX = SoundBirdObject.position.x;
}
}
void HandleTweenStarted()
{
}
void HandleTweenFinished()
{
if (SoundBirdObject != null)
{
objectTween.Cancel(); // Stop the spline tween for this object
}
//Logging.Debug("Tween finished!");
if (stateMachine != null)
{
animator.SetBool("isScared", false);
stateMachine.ChangeState(0); // Change to the desired state name
}
}
void Update()
{
if (SoundBirdObject == null) return;
float currentX = SoundBirdObject.position.x;
if (currentX > lastX && !facingRight)
{
Flip(true);
}
else if (currentX < lastX && facingRight)
{
Flip(false);
}
lastX = currentX;
}
private void Flip(bool faceRight)
{
Vector3 scale = SoundBirdObject.localScale;
scale.x = Mathf.Abs(scale.x) * (faceRight ? 1 : -1);
SoundBirdObject.localScale = scale;
facingRight = faceRight;
}
}