IndicatorHandler.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Roads;
  4. using UnityEngine;
  5. namespace TrafficSimulation
  6. {
  7. public class IndicatorHandler : MonoBehaviour
  8. {
  9. public GameObject[] rightIndicators;
  10. public GameObject[] leftIndicators;
  11. public float blinkPause = 0.5f;
  12. private bool leftIndicatorsRunning;
  13. private bool rightIndicatorsRunning;
  14. private List<Coroutine> activeRoutines;
  15. // Start is called before the first frame update
  16. void Start()
  17. {
  18. this.leftIndicatorsRunning = false;
  19. this.rightIndicatorsRunning = false;
  20. this.activeRoutines = new List<Coroutine>();
  21. }
  22. public void ActivateLeftIndicators()
  23. {
  24. if (leftIndicatorsRunning)
  25. {
  26. //Debug.Log("Left indicator already running. Abort!");
  27. return;
  28. }
  29. this.leftIndicatorsRunning = true;
  30. ActivateIndicators(this.leftIndicators);
  31. }
  32. public void ActivateRightIndicators()
  33. {
  34. if (rightIndicatorsRunning)
  35. {
  36. //Debug.Log("Right indicator already running. Abort!");
  37. return;
  38. }
  39. this.rightIndicatorsRunning = true;
  40. ActivateIndicators(this.rightIndicators);
  41. }
  42. public void DeactivateAllIndicators()
  43. {
  44. foreach (Coroutine coroutine in activeRoutines)
  45. {
  46. StopCoroutine(coroutine);
  47. }
  48. if (this.leftIndicatorsRunning)
  49. {
  50. foreach (GameObject indicator in this.leftIndicators)
  51. {
  52. indicator.SetActive(false);
  53. }
  54. this.leftIndicatorsRunning = false;
  55. }
  56. if (this.rightIndicatorsRunning)
  57. {
  58. foreach (GameObject indicator in this.rightIndicators)
  59. {
  60. indicator.SetActive(false);
  61. }
  62. this.rightIndicatorsRunning = false;
  63. }
  64. }
  65. private void ActivateIndicators(GameObject[] indicators)
  66. {
  67. this.activeRoutines.Add(StartCoroutine(RunIndicator(indicators, this.blinkPause)));
  68. }
  69. IEnumerator RunIndicator(GameObject[] indicators,float pause)
  70. {
  71. while (true)
  72. {
  73. foreach (GameObject indicator in indicators)
  74. {
  75. indicator.SetActive(!indicator.activeSelf);
  76. }
  77. yield return new WaitForSeconds(blinkPause);
  78. }
  79. }
  80. }
  81. }