Advertisement
maus234

FarmerController

Apr 14th, 2024
13,846
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.91 KB | Gaming | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3. using System;
  4. using Random = UnityEngine.Random;
  5.  
  6. namespace Farmer
  7. {
  8.     public class FarmerController : MonoBehaviour
  9.     {
  10.         private GameObject campFireZone;
  11.         private GameObject field;
  12.         public Transform pointA;
  13.         public Transform pointB;
  14.         private Transform currentTarget;
  15.         private bool isMovingToB = true;
  16.         private float speed = 5f;
  17.         private bool insideZone = false;
  18.         private float nextPauseTime = 0;
  19.         private float timeBetweenPauses = 2.5f;
  20.         private Vector3 lastTurnPosition;
  21.         private float minimumMoveDistance = Random.Range(1f, 3f);
  22.         private bool isCoroutineRunning = false;
  23.         private Coroutine movingBetweenPointsCoroutine = null;
  24.         public void SetCampFireZone(GameObject zone)
  25.         {
  26.             campFireZone = zone;
  27.             pointA = campFireZone.transform.GetChild(0);
  28.             pointB = campFireZone.transform.GetChild(1);
  29.             currentTarget = pointA;
  30.             lastTurnPosition = transform.position;
  31.         }
  32.  
  33.         public void SetField(GameObject field)
  34.         {
  35.             this.field = field;
  36.         }
  37.  
  38.         private void Update()
  39.         {
  40.             if (IsNightTime())
  41.             {
  42.                 if (!insideZone)
  43.                 {
  44.                     MoveTowards(campFireZone.transform.position); // Move towards the campfire zone at night if outside
  45.                 }
  46.                 else if (!isCoroutineRunning)
  47.                 {
  48.                     StartCoroutine(MoveBetweenPoints()); // Start moving between points at night if inside zone
  49.                 }
  50.             }
  51.             else // Daytime logic
  52.             {
  53.                 if (field != null) // If there is a field
  54.                 {
  55.                     StopCoroutineIfNeeded();
  56.                     MoveTowards(field.transform.position); // Move towards the field
  57.                 }
  58.                 else if (!isCoroutineRunning && insideZone) // No field, and coroutine is not running, ensure it's day and inside zone
  59.                 {
  60.                     StartCoroutine(MoveBetweenPoints());
  61.                 }
  62.             }
  63.         }
  64.  
  65.         private bool IsNightTime()
  66.         {
  67.             TimeSpan currentTime = WorldTimeSystem.WorldTime.Instance.GetCurrentTime();
  68.             return currentTime.Hours < 6 || currentTime.Hours >= 18;
  69.         }
  70.  
  71.         private void OnTriggerEnter2D(Collider2D collision)
  72.         {
  73.             if (collision.gameObject == campFireZone)
  74.             {
  75.                 insideZone = true;
  76.                 if (IsNightTime())
  77.                 {
  78.                     movingBetweenPointsCoroutine = StartCoroutine(MoveBetweenPoints());
  79.                 }
  80.             }
  81.         }
  82.         private void OnTriggerExit2D(Collider2D collision)
  83.         {
  84.             if (collision.gameObject == campFireZone)
  85.             {
  86.                 insideZone = false;
  87.                 StopCoroutine(MoveBetweenPoints());
  88.                 isCoroutineRunning = false;
  89.             }
  90.         }
  91.  
  92.         private IEnumerator MoveBetweenPoints()
  93.         {
  94.             isCoroutineRunning = true;
  95.             while (insideZone)
  96.             {
  97.                 while (Vector3.Distance(transform.position, currentTarget.position) > 2f)
  98.                 {
  99.                     MoveTowards(currentTarget.position);
  100.                     if (Time.time >= nextPauseTime && Vector3.Distance(transform.position, lastTurnPosition) > minimumMoveDistance)
  101.                     {
  102.                         yield return PauseAndPotentiallyTurn();
  103.                     }
  104.                     yield return null;
  105.                 }
  106.                 yield return PauseAtTarget();
  107.             }
  108.             isCoroutineRunning = false;
  109.         }
  110.  
  111.         private void StopCoroutineIfNeeded()
  112.         {
  113.             if (isCoroutineRunning)
  114.             {
  115.                 StopAllCoroutines();
  116.                 isCoroutineRunning = false; // Reset the coroutine flag
  117.             }
  118.         }
  119.  
  120.         private IEnumerator PauseAndPotentiallyTurn()
  121.         {
  122.             if (Random.Range(0, 100) < 20)
  123.             {
  124.                 float pauseTime = Random.Range(2f, 6f);
  125.                 yield return new WaitForSeconds(pauseTime);
  126.                 if (Random.Range(0, 2) > 0)
  127.                 {
  128.                     isMovingToB = !isMovingToB;
  129.                     currentTarget = isMovingToB ? pointB : pointA;
  130.                     FlipDirection();
  131.                     lastTurnPosition = transform.position;
  132.                 }
  133.                 nextPauseTime = Time.time + timeBetweenPauses;
  134.             }
  135.         }
  136.  
  137.         private IEnumerator PauseAtTarget()
  138.         {
  139.             float stopTime = Random.Range(2f, 4f);
  140.             yield return new WaitForSeconds(stopTime);
  141.             if (Random.Range(0, 2) > 0)
  142.             {
  143.                 isMovingToB = !isMovingToB;
  144.                 currentTarget = isMovingToB ? pointB : pointA;
  145.                 FlipDirection();
  146.                 lastTurnPosition = transform.position;
  147.             }
  148.         }
  149.  
  150.         private void MoveTowards(Vector3 targetPosition)
  151.         {
  152.             Vector3 newPosition = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
  153.             if (GetComponent<Rigidbody2D>() != null)
  154.             {
  155.                 GetComponent<Rigidbody2D>().MovePosition(newPosition);
  156.             }
  157.             else
  158.             {
  159.                 transform.position = newPosition;
  160.             }
  161.             FlipDirection();
  162.         }
  163.  
  164.         private void FlipDirection()
  165.         {
  166.             Vector3 localScale = transform.localScale;
  167.             if ((currentTarget == pointB && localScale.x < 0) || (currentTarget == pointA && localScale.x > 0))
  168.             {
  169.                 localScale.x *= -1;
  170.                 transform.localScale = localScale;
  171.             }
  172.         }
  173.     }
  174. }
  175.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement