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() { if (workerAnimator != null) 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() { if (workerObjectTransform != null) midRoamPosition = workerObjectTransform.position; Debug.Log("WorkerBeltRoamingBehaviour: Stopping roaming tween " + midRoamPosition); roamingTween?.Stop(); roamingTween = null; if (pantsRoutine != null) { StopCoroutine(pantsRoutine); pantsRoutine = null; } } public void Update() { if (RoamingSpline == null || roamingTween == null) return; float pct = Mathf.Clamp01(roamingTween.Percentage); Vector3 dir = RoamingSpline.GetDirection(pct); if (dir.x > 0.1f) workerAnimator?.SetBool("walkingRight?", true); else workerAnimator?.SetBool("walkingRight?", false); } IEnumerator RandomFallChance() { while (pantsLess) { yield return new WaitForSeconds(2); CheckForFall(); } } public void CheckForFall() { // 5% chance to fall each check if (Random.value < 0.05f) { workerAnimator?.SetTrigger("shouldFall?"); } } public void OnDestroy() { if (pantsRoutine != null) { StopCoroutine(pantsRoutine); pantsRoutine = null; } } }