Advertisement
maus234

ArcherController

Apr 14th, 2024
668
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.76 KB | Gaming | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Diagnostics;
  4. using UnityEngine;
  5. using UnityEngine.UIElements;
  6.  
  7. namespace Archer
  8. {
  9.     public class ArcherController : MonoBehaviour
  10.     {
  11.         [SerializeField] private Animator animator;
  12.         [SerializeField] private float patrolSpeed = 3f;
  13.         [SerializeField] private float runSpeed = 5;
  14.         archerShooting archerShooting;
  15.         private bool isPaused = false;
  16.         private GameObject searchZone;
  17.         private GameObject safeZone;
  18.         private bool isMovingRight = true;
  19.         WorldPoolManager poolManager;
  20.         private Vector3 bottomLeftCorner;
  21.         private Vector3 bottomRightCorner;
  22.         private Vector3 safeZoneLeftPoint;
  23.         private Vector3 safeZoneRightPoint;
  24.  
  25.  
  26.  
  27.         void Start()
  28.         {
  29.             animator = GetComponent<Animator>();
  30.             StartCoroutine(BehaviorController());
  31.             isMovingRight = true;
  32.         }
  33.         private void Awake()
  34.         {
  35.             archerShooting = GetComponent<archerShooting>();
  36.             OnDrawGizmos();
  37.         }
  38.  
  39.         private void Update()
  40.         {
  41.             if (archerShooting != null)
  42.             {
  43.                 archerShooting.CheckDetectionZone();
  44.             }
  45.             UpdatePatrolPoints();
  46.         }
  47.         private void UpdatePatrolPoints()
  48.         {
  49.             // Assuming 'BottomLeftCorner' and 'BottomRightCorner' are transform names of children of searchZone.
  50.             if (searchZone != null)
  51.             {
  52.                 bottomLeftCorner = searchZone.transform.Find("BottomLeftCorner").position;
  53.                 bottomRightCorner = searchZone.transform.Find("BottomRightCorner").position;
  54.             }
  55.         }
  56.         public void AssignPool(WorldPoolManager newPool)
  57.         {
  58.           poolManager = newPool;
  59.         }
  60.  
  61.         public void SetSafeZone(GameObject zone)
  62.         {
  63.             safeZone = zone;
  64.             SetSafeZonePoints();
  65.         }
  66.         private void SetSafeZonePoints()
  67.         {
  68.             if (safeZone != null)
  69.             {
  70.                 // Find "Point 1" which is a direct child of safeZone
  71.                 Transform point1Transform = safeZone.transform.Find("Point 1");
  72.                 if (point1Transform != null)
  73.                 {
  74.                     // Set the position of Point 1
  75.                     safeZoneLeftPoint = point1Transform.position;
  76.  
  77.                     // Now find "Point 2" which is a child of "Point 1"
  78.                     Transform point2Transform = point1Transform.Find("Point 2");
  79.                     if (point2Transform != null)
  80.                     {
  81.                         // Set the position of Point 2
  82.                         safeZoneRightPoint = point2Transform.position;
  83.                         UnityEngine.Debug.Log("SafeZone points initialized: Point 1 (" + safeZoneLeftPoint + "), Point 2 (" + safeZoneRightPoint + ")");
  84.                     }
  85.                     else
  86.                     {
  87.                         UnityEngine.Debug.LogError("Point 2 is not set as a child of Point 1 in the SafeZone.");
  88.                     }
  89.                 }
  90.                 else
  91.                 {
  92.                     UnityEngine.Debug.LogError("Point 1 is not set as a child in the SafeZone.");
  93.                 }
  94.             }
  95.             else
  96.             {
  97.                 UnityEngine.Debug.LogError("SafeZone GameObject is not assigned.");
  98.             }
  99.         }
  100.  
  101.  
  102.         private void SetPatrolPoints()
  103.         {
  104.             if (searchZone != null)
  105.             {
  106.                 Transform blcTransform = searchZone.transform.Find("BottomLeftCorner");
  107.                 Transform brcTransform = searchZone.transform.Find("BottomRightCorner");
  108.  
  109.                 if (blcTransform != null && brcTransform != null)
  110.                 {
  111.                     bottomLeftCorner = blcTransform.position;
  112.                     bottomRightCorner = brcTransform.position;
  113.                     UnityEngine.Debug.Log("Bottom Right Corner Position: " + bottomRightCorner + ", Bottom Left Corner Position: " + bottomLeftCorner);
  114.                 }
  115.                 else
  116.                 {
  117.                     UnityEngine.Debug.LogError("Patrol points not found. Check names and hierarchy.");
  118.                 }
  119.             }
  120.             else
  121.             {
  122.                 UnityEngine.Debug.LogError("Search zone not set.");
  123.             }
  124.         }
  125.  
  126.         void OnDrawGizmos()
  127.         {
  128.             if (searchZone != null)
  129.             {
  130.                 // Set the color of the Gizmos
  131.                 Gizmos.color = Color.red;
  132.  
  133.                 // Draw small spheres at the patrol points
  134.                 Gizmos.DrawSphere(bottomLeftCorner, 1f);
  135.                 Gizmos.DrawSphere(bottomRightCorner, 1f);
  136.  
  137.                 // Draw a line connecting the patrol points
  138.                 Gizmos.DrawLine(bottomLeftCorner, bottomRightCorner);
  139.             }
  140.         }
  141.  
  142.         public void SetSearchZone(GameObject zone)
  143.         {
  144.             searchZone = zone;
  145.             UnityEngine.Debug.Log("SetSearchZone: " + zone);
  146.             SetPatrolPoints();
  147.         }
  148.  
  149.         private IEnumerator BehaviorController()
  150.         {
  151.             while (true)
  152.             {
  153.                 if (IsNightTime())
  154.                 {
  155.                     yield return StartCoroutine(RunToSafeZone());
  156.                 }
  157.                 else
  158.                 {
  159.                     yield return StartCoroutine(Patrol());
  160.                 }
  161.             }
  162.         }
  163.         public void PauseMovement()
  164.         {
  165.             isPaused = true;
  166.             //animator.SetBool("isMove", false); // Assuming there is an 'isMove' animation parameter
  167.         }
  168.  
  169.         public void ResumeMovement()
  170.         {
  171.             isPaused = false;
  172.         }
  173.         private IEnumerator Patrol()
  174.         {
  175.             isMovingRight = true; // Assuming starting direction.
  176.             FlipDirection(isMovingRight); // Ensure the archer faces the right direction initially.
  177.  
  178.             while (true)
  179.             {
  180.                 if (!isPaused)
  181.                 {
  182.                     Vector3 target = isMovingRight ? searchZone.transform.Find("BottomRightCorner").position
  183.                                                    : searchZone.transform.Find("BottomLeftCorner").position;
  184.  
  185.                     float patrolTime = UnityEngine.Random.Range(5f, 8f);
  186.                     float timePassed = 0f;
  187.  
  188.                     // Patrol towards the target with checks to ensure responsiveness to pause.
  189.                     while (Vector3.Distance(transform.position, target) > 1.5f && !isPaused)
  190.                     {
  191.                         Move(target, patrolSpeed);
  192.                         timePassed += Time.deltaTime;
  193.  
  194.                         if (timePassed >= patrolTime)
  195.                         {
  196.                             float actionChance = UnityEngine.Random.value;
  197.                             HandlePatrolDecision(actionChance);
  198.  
  199.                             patrolTime = UnityEngine.Random.Range(5f, 8f);
  200.                             timePassed = 0f;  // Reset timer
  201.                         }
  202.  
  203.                         target = UpdateTarget(); // Always get the most current target
  204.                         yield return null;
  205.                     }
  206.                     if (!isPaused) FlipDirection(isMovingRight = !isMovingRight);
  207.                 }
  208.                 yield return null;
  209.             }
  210.         }
  211.  
  212.         private void HandlePatrolDecision(float chance)
  213.         {
  214.             if (chance < 0.5f)
  215.             {
  216.                 // 50% chance to pause. Implement wait directly here to ensure it handles immediately.
  217.                 StartCoroutine(PauseTemporarily(UnityEngine.Random.Range(5f, 8f)));
  218.             }
  219.             else if (chance < 0.9f)
  220.             {
  221.                 // 40% chance to turn around immediately
  222.                 FlipDirection(isMovingRight = !isMovingRight);
  223.             }
  224.         }
  225.  
  226.         private IEnumerator PauseTemporarily(float duration)
  227.         {
  228.             PauseMovement();
  229.             yield return new WaitForSeconds(duration);
  230.             ResumeMovement();
  231.         }
  232.  
  233.         private Vector3 UpdateTarget()
  234.         {
  235.             return isMovingRight ? searchZone.transform.Find("BottomRightCorner").position
  236.                                  : searchZone.transform.Find("BottomLeftCorner").position;
  237.         }
  238.  
  239.  
  240.         private void FlipDirection(bool facingRight)
  241.         {
  242.             Vector3 localScale = transform.localScale;
  243.             localScale.x = facingRight ? Mathf.Abs(localScale.x) : -Mathf.Abs(localScale.x);
  244.             transform.localScale = localScale;
  245.         }
  246.         private IEnumerator RunToSafeZone()
  247.         {
  248.  
  249.             // Ensure the SafeZone points have been set
  250.             if (safeZoneLeftPoint == Vector3.zero || safeZoneRightPoint == Vector3.zero)
  251.             {
  252.               //  Debug.LogError("SafeZone points have not been initialized.");
  253.                 yield break;
  254.             }
  255.  
  256.             // Calculate a random position between the SafeZone points
  257.             Vector3 randomPosition = Vector3.Lerp(safeZoneLeftPoint, safeZoneRightPoint, UnityEngine.Random.value);
  258.             isMovingRight = randomPosition.x > transform.position.x;
  259.             FlipDirection(isMovingRight);
  260.             UnityEngine.Debug.Log($"Moving to random position within SafeZone: {randomPosition}");
  261.  
  262.             // Move to the random position
  263.             while (Vector3.Distance(transform.position, randomPosition) > 1.7f)
  264.             {
  265.                 Move(randomPosition, runSpeed);
  266.                 yield return null;
  267.             }
  268.  
  269.             // Stop all movement once the random position is reached
  270.             PauseMovement();
  271.  
  272.             // Wait until it's day
  273.             while (IsNightTime())
  274.             {
  275.                 // Optionally, you can play  perform other night-time behaviors
  276.                 yield return null;
  277.             }
  278.  
  279.             // Resume patrolling once it's day
  280.             UnityEngine.Debug.Log("Daytime has arrived. Archer is resuming movement.");
  281.             ResumeMovement();
  282.            
  283.         }
  284.  
  285.  
  286.         private void Move(Vector3 target, float speed)
  287.         {
  288.            
  289.                 target.y = transform.position.y; // Keeps the movement in the horizontal plane
  290.                 float step = speed * Time.deltaTime;
  291.                 transform.position = Vector3.MoveTowards(transform.position, target, step);
  292.            
  293.  
  294.             //UnityEngine.Debug.Log("Moving to target: " + target + " Step: " + step);
  295.         }
  296.  
  297.  
  298.         private bool IsNightTime()
  299.         {
  300.             TimeSpan currentTime = WorldTimeSystem.WorldTime.Instance.GetCurrentTime();
  301.             return currentTime.Hours < 6 || currentTime.Hours >= 18;
  302.         }
  303.  
  304.  
  305.     }
  306. }
  307.  
  308.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement