Advertisement
Guest User

Untitled

a guest
Jul 25th, 2020
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.01 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3.  
  4. [RequireComponent(typeof(LineRenderer))]
  5. public class BRS_ChangeCircle : MonoBehaviour {
  6.  
  7. [Range(0, 360)]
  8. public int Segments;
  9. [Range(0, 5000)]
  10. public float XRadius;
  11. [Range(0, 5000)]
  12. public float YRadius;  //THIS IS NOT USED - SHOULD BE ELIMINATED
  13.  
  14. [Range(10, 100)]
  15. public int ZoneRadiusFactor = 50; //default to 50%
  16.  
  17. [Header("Shrinking Zones")]
  18. public List<int> ZoneTimes;
  19.  
  20. #region Private Members
  21. private bool Shrinking;  // this can be set to PUBLIC in order to troubleshoot.  It will show a checkbox in the Inspector
  22. private int countdownPrecall = 10;  //this MIGHT be public, but it should not need to be changed
  23. private int timeToShrink = 30; //seconds
  24. private int count = 0;
  25. private bool newCenterObtained = false;
  26. private Vector3 centerPoint = new Vector3(0, -100, 0);
  27. private float distanceToMoveCenter;
  28. private WorldCircle circle;
  29. private LineRenderer renderer;
  30. private GameObject ZoneWall;
  31. private float[] radii = new float[2];
  32. private float shrinkRadius;
  33. private int zoneRadiusIndex = 0;
  34. private int zoneTimesIndex = 0;
  35. private float timePassed;
  36. #endregion
  37.  
  38. void Start() {
  39.     renderer = gameObject.GetComponent<LineRenderer>();
  40.     radii[0] = XRadius; radii[1] = YRadius;
  41.     circle = new WorldCircle(ref renderer, Segments, radii);
  42.     ZoneWall = GameObject.FindGameObjectWithTag("ZoneWall");
  43.  
  44.     timePassed = Time.deltaTime;
  45. }
  46.  
  47.  
  48. void Update() {
  49.     ZoneWall.transform.localScale = new Vector3((XRadius * 0.01f), 1, (XRadius * 0.01f));
  50.  
  51.     if (Shrinking) {
  52.         // we need a new center point (that is within the bounds of the current zone)
  53.         if (!newCenterObtained) {
  54.             centerPoint = NewCenterPoint(transform.position, XRadius, shrinkRadius);
  55.             distanceToMoveCenter = Vector3.Distance(transform.position, centerPoint); //this is used in the Lerp (below)
  56.             newCenterObtained = (centerPoint != new Vector3(0, -100, 0));
  57.         }
  58.  
  59.         Debug.Log("New Center Point is " + centerPoint);
  60.  
  61.         // move the center point, over time
  62.         transform.position = Vector3.Lerp(transform.position, centerPoint, (distanceToMoveCenter / timeToShrink) * Time.deltaTime);
  63.         // shrink the zone diameter, over time
  64.         XRadius = Mathf.MoveTowards(XRadius, shrinkRadius, (shrinkRadius / timeToShrink) * Time.deltaTime);
  65.         circle.Draw(Segments, XRadius, XRadius);
  66.  
  67.         // MoveTowards will continue infinitum, so we must test that we have gotten close enough to be DONE
  68.         if (1 > (XRadius - shrinkRadius)) {
  69.             timePassed = Time.deltaTime;
  70.             Shrinking = false;
  71.             newCenterObtained = false;
  72.         }
  73.     } else {
  74.         timePassed += Time.deltaTime; // increment clock time
  75.     }
  76.  
  77.     // have we passed the next threshold for time delay?
  78.     if (((int)timePassed) > ZoneTimes[zoneTimesIndex]) {
  79.         shrinkRadius = ShrinkCircle((float)(XRadius * (ZoneRadiusFactor * 0.01)))[1];  //use the ZoneRadiusFactor as a percentage
  80.         Shrinking = true;
  81.         timePassed = Time.deltaTime;  //reset the time so other operations are halted.
  82.         NextZoneTime();
  83.     }
  84.  
  85.     // COUNT DOWN
  86.     if (timePassed > (ZoneTimes[zoneTimesIndex] - countdownPrecall)) {  // we need to begin counting down
  87.         if (ZoneTimes[zoneTimesIndex] - (int)timePassed != count) {
  88.             count = Mathf.Clamp(ZoneTimes[zoneTimesIndex] - (int)timePassed, 1, 1000);  // this ensures our value never falls below zero
  89.  
  90.             //FILL IN APPROPRIATE UI CALLS HERE FOR THE COUNTDOWN
  91.             Debug.Log("Shrinking in " + count + " seconds.");
  92.         }
  93.     }
  94. }
  95.  
  96. // ***********************************
  97. // PRIVATE (helper) FUNCTIONS
  98. // ***********************************
  99. private Vector3 NewCenterPoint(Vector3 currentCenter, float currentRadius, float newRadius) {
  100.     Vector3 newPoint = Vector3.zero;
  101.  
  102.     var totalCountDown = 30000; //prevent endless loop which will kill Unity
  103.     var foundSuitable = false;
  104.     while (!foundSuitable) {
  105.         totalCountDown--;
  106.         Vector2 randPoint = Random.insideUnitCircle * (currentRadius * 2.0f);
  107.         newPoint = new Vector3(randPoint.x, 0, randPoint.y);
  108.         foundSuitable = (Vector3.Distance(currentCenter, newPoint) < currentRadius);
  109.         if (totalCountDown < 1)
  110.             return new Vector3(0, -100, 0);  //explicitly define an error has occured.  In this case we did not locate a reasonable point
  111.     }
  112.     return newPoint;
  113. }
  114.  
  115. private int NextZoneTime() {
  116.     //if we have exceeded the count, just start over
  117.     if (zoneTimesIndex >= ZoneTimes.Count - 1) // Lists are zero-indexed
  118.         zoneTimesIndex = -1;  // the fall-through (below) will increment this
  119.  
  120.     // next time to wait
  121.     return ZoneTimes[++zoneTimesIndex];
  122. }
  123.  
  124. // This is a general purpose method
  125. private float[] ShrinkCircle(float amount) {
  126.     float newXR = circle.radii[0] - amount;
  127.     float newYR = circle.radii[1] - amount;
  128.     float[] retVal = new float[2];
  129.     retVal[0] = newXR;
  130.     retVal[1] = newYR;
  131.     return retVal;
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement