Files
AppleHillsProduction/Assets/Scripts/DamianExperiments/WorkerBeltApproachingBehaviour.cs

90 lines
2.7 KiB
C#

using Core.SaveLoad;
using UnityEngine;
using Pixelplacement;
using Pixelplacement.TweenSystem;
public class WorkerBeltApproachingBehaviour : AppleState
{
public Spline ApproachingSpline;
public Transform workerObjectTransform;
public float approachduration;
public float approachDelay;
//References to the Worker Gameobject
public GameObject workerBeltObject;
public Animator workerAnimator;
public WorkerBeltRoamingBehaviour workerBeltRoamingRef;
private TweenBase aproachTween;
public AppleMachine workerBeltStateMAchineRef;
private void OnEnable()
{
// ensure roaming ref exists before accessing
Transform anchorA = transform.Find("AnchorA");
if (anchorA != null && workerBeltRoamingRef != null)
{
anchorA.position = workerBeltRoamingRef.midRoamPosition;
}
Debug.Log("Entered Worker Belt Approaching State");
if (ApproachingSpline == null || workerObjectTransform == null)
{
Debug.LogWarning("WorkerBeltApproachingBehaviour: ApproachingSpline or workerObjectTransform is not assigned.", this);
return;
}
// Start the tween to move the worker along the approaching spline
aproachTween = Tween.Spline(
ApproachingSpline,
workerObjectTransform,
0, 1,
false,
approachduration,
approachDelay,
Tween.EaseLinear,
Tween.LoopType.None,
HandleApproachStarted, // optional
HandleApproachFinished // called when spline completes
);
}
private void OnDisable()
{
Debug.Log("Exited Worker Belt Approaching State");
// stop active tween to avoid it running while disabled
aproachTween?.Stop();
aproachTween = null;
}
// callback implementations
void HandleApproachStarted()
{
// optional: play audio/anim etc.
}
void HandleApproachFinished()
{
Debug.Log("Approach tween finished");
// cleanup
aproachTween?.Stop();
aproachTween = null;
if (workerAnimator != null)
workerAnimator.SetBool("isLifting?", true);
if (workerBeltStateMAchineRef != null)
workerBeltStateMAchineRef.ChangeState(2);
}
public void Update()
{
if (ApproachingSpline == null || aproachTween == null)
return;
// defensively clamp percentage
float pct = Mathf.Clamp01(aproachTween.Percentage);
Vector3 dir = ApproachingSpline.GetDirection(pct);
if (dir.x > 0.1f)
workerAnimator?.SetBool("walkingRight?", true);
else
workerAnimator?.SetBool("walkingRight?", false);
}
}