The worker respects the splines now

This commit is contained in:
2025-12-05 15:49:06 +01:00
parent 82b1a1e78c
commit c96be39e64
4 changed files with 314 additions and 59 deletions

View File

@@ -19,11 +19,11 @@ public class WorkerBeltApproachingBehaviour : AppleState
private void OnEnable()
{
Transform anchorB = transform.Find("AnchorA");
if (anchorB != null)
// ensure roaming ref exists before accessing
Transform anchorA = transform.Find("AnchorA");
if (anchorA != null && workerBeltRoamingRef != null)
{
anchorB.position = workerBeltRoamingRef.midRoamPosition;
anchorA.position = workerBeltRoamingRef.midRoamPosition;
}
Debug.Log("Entered Worker Belt Approaching State");
@@ -50,6 +50,9 @@ public class WorkerBeltApproachingBehaviour : AppleState
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
@@ -63,15 +66,24 @@ public class WorkerBeltApproachingBehaviour : AppleState
Debug.Log("Approach tween finished");
// cleanup
aproachTween?.Stop();
workerAnimator.SetBool("isLifting?", true);
workerBeltStateMAchineRef.ChangeState(2);
aproachTween = null;
if (workerAnimator != null)
workerAnimator.SetBool("isLifting?", true);
if (workerBeltStateMAchineRef != null)
workerBeltStateMAchineRef.ChangeState(2);
}
public void Update()
{
if (ApproachingSpline.GetDirection(aproachTween.Percentage).x > 0.1)
workerAnimator.SetBool("walkingRight?", true);
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);
workerAnimator?.SetBool("walkingRight?", false);
}
}

View File

@@ -25,7 +25,9 @@ public class WorkerBeltRoamingBehaviour : AppleState
void OnEnable()
{
workerAnimator.SetBool("isLifting?", false);
if (workerAnimator != null)
workerAnimator.SetBool("isLifting?", false);
Debug.Log("Entered Worker Belt Roaming State");
if (RoamingSpline == null || workerObjectTransform == null)
@@ -46,35 +48,47 @@ public class WorkerBeltRoamingBehaviour : AppleState
// 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);
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.GetDirection(roamingTween.Percentage).x > 0.1)
workerAnimator.SetBool("walkingRight?", true);
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);
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)
// 5% chance to fall each check
if (Random.value < 0.05f)
{
workerAnimator.SetTrigger("shouldFall?");
workerAnimator?.SetTrigger("shouldFall?");
}
}
@@ -83,6 +97,7 @@ public class WorkerBeltRoamingBehaviour : AppleState
if (pantsRoutine != null)
{
StopCoroutine(pantsRoutine);
pantsRoutine = null;
}
}

View File

@@ -20,26 +20,29 @@ public class WorkerBeltReturningBehaviour : AppleState
private void OnEnable()
{
roamingWorker.SetActive(true);
if (roamingWorker != null)
roamingWorker.SetActive(true);
Transform anchorA = transform.Find("AnchorA");
if (anchorA != null)
if (anchorA != null && StartingAnchor != null)
{
anchorA.position = StartingAnchor.position;
}
Transform anchorB = transform.Find("AnchorB");
if (anchorB != null)
if (anchorB != null && EndingAnchor != null)
{
anchorB.position = EndingAnchor.position;
}
workerAnimator.SetBool("isLifting?", false);
if (workerAnimator != null)
workerAnimator.SetBool("isLifting?", false);
Debug.Log("Entered Worker Belt Returning State");
if (ReturningSpline == null || workerObjectTransform == null)
{
Debug.LogWarning("WorkerBeltApproachingBehaviour: ApproachingSpline or workerObjectTransform is not assigned.", this);
Debug.LogWarning("WorkerBeltReturningBehaviour: ReturningSpline or workerObjectTransform is not assigned.", this);
return;
}
// Start the tween to move the worker along the approaching spline
// Start the tween to move the worker along the returning spline
returnTween = Tween.Spline(
ReturningSpline,
workerObjectTransform,
@@ -49,29 +52,33 @@ public class WorkerBeltReturningBehaviour : AppleState
returnDelay,
Tween.EaseLinear,
Tween.LoopType.None,
HandleApproachStarted, // optional
HandleApproachFinished // called when spline completes
HandleReturnStarted, // optional
HandleReturnFinished // called when spline completes
);
}
private void OnDisable()
{
Debug.Log("Exited Worker Belt Returning State");
returnTween?.Stop();
returnTween = null;
}
// callback implementations
void HandleApproachStarted()
void HandleReturnStarted()
{
// optional: play audio/anim etc.
}
void HandleApproachFinished()
void HandleReturnFinished()
{
Debug.Log("Return tween finished");
// cleanup
returnTween?.Stop();
workerBeltStateMAchineRef.ChangeState(0);
returnTween = null;
if (workerBeltStateMAchineRef != null)
workerBeltStateMAchineRef.ChangeState(0);
}
}