using Core.SaveLoad; using UnityEngine; using Pixelplacement; using Pixelplacement.TweenSystem; using System.Collections; public class WorkerBeltRoamingBehaviour : AppleState { public Spline RoamingSpline; public Transform workerObjectTransform; public float roamDuration; public float roamDelay; public bool walkingRight; public bool pantsLess; public Coroutine pantsRoutine; //References to the Worker Gameobject public GameObject workerBeltObject; public Animator workerAnimator; public Vector3 midRoamPosition; // Reference to the active spline tween so we can pause/stop it private TweenBase roamingTween; void OnEnable() { workerAnimator.SetBool("isLifting?", false); Debug.Log("Entered Worker Belt Roaming State"); if (RoamingSpline == null || workerObjectTransform == null) { Debug.LogWarning("WorkerBeltRoamingBehaviour: RoamingSpline or workerObjectTransform is not assigned.", this); return; } // Store the returned TweenBase so we can control it later roamingTween = Tween.Spline(RoamingSpline, workerObjectTransform, 0, 1, false, roamDuration, roamDelay, Tween.EaseLinear, Tween.LoopType.Loop); if (pantsLess) { pantsRoutine = StartCoroutine(RandomFallChance()); } } // Also stop the tween if the GameObject is disabled for any reason (covers StateMachine deactivation) void OnDisable() { midRoamPosition = workerObjectTransform.position; Debug.Log("WorkerBeltRoamingBehaviour: OnExitState - stopping roaming tween" + midRoamPosition); roamingTween?.Stop(); } public void Update() { if (RoamingSpline.GetDirection(roamingTween.Percentage).x > 0.1) workerAnimator.SetBool("walkingRight?", true); else workerAnimator.SetBool("walkingRight?", false); } IEnumerator RandomFallChance() { while (pantsLess) yield return new WaitForSeconds(2); CheckForFall(); } public void CheckForFall() { int _randNumber; _randNumber = Random.Range(0, 1); if (_randNumber > 5) { workerAnimator.SetTrigger("shouldFall?"); } } public void OnDestroy() { if (pantsRoutine != null) { StopCoroutine(pantsRoutine); } } }