123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- 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<Coroutine> activeRoutines;
- // Start is called before the first frame update
- void Start()
- {
- this.leftIndicatorsRunning = false;
- this.rightIndicatorsRunning = false;
- this.activeRoutines = new List<Coroutine>();
- }
- 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);
- }
- }
- }
- }
|