using System.Collections; using System.Collections.Generic; using Roads; using UnityEngine; namespace TrafficSimulation { public class IndicatorHandler : MonoBehaviour { public GameObject[] rightIndicators; public GameObject[] leftIndicators; public float blinkPause = 0.5f; private bool leftIndicatorsRunning; private bool rightIndicatorsRunning; private List activeRoutines; // Start is called before the first frame update void Start() { this.leftIndicatorsRunning = false; this.rightIndicatorsRunning = false; this.activeRoutines = new List(); } public void ActivateLeftIndicators() { if (leftIndicatorsRunning) { //Debug.Log("Left indicator already running. Abort!"); return; } this.leftIndicatorsRunning = true; ActivateIndicators(this.leftIndicators); } public void ActivateRightIndicators() { if (rightIndicatorsRunning) { //Debug.Log("Right indicator already running. Abort!"); return; } this.rightIndicatorsRunning = true; ActivateIndicators(this.rightIndicators); } public void DeactivateAllIndicators() { foreach (Coroutine coroutine in activeRoutines) { StopCoroutine(coroutine); } if (this.leftIndicatorsRunning) { foreach (GameObject indicator in this.leftIndicators) { indicator.SetActive(false); } this.leftIndicatorsRunning = false; } if (this.rightIndicatorsRunning) { foreach (GameObject indicator in this.rightIndicators) { indicator.SetActive(false); } this.rightIndicatorsRunning = false; } } private void ActivateIndicators(GameObject[] indicators) { this.activeRoutines.Add(StartCoroutine(RunIndicator(indicators, this.blinkPause))); } IEnumerator RunIndicator(GameObject[] indicators,float pause) { while (true) { foreach (GameObject indicator in indicators) { indicator.SetActive(!indicator.activeSelf); } yield return new WaitForSeconds(blinkPause); } } } }