TrafficLightHandler.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TrafficSimulation;
  5. public class TrafficLightHandler : MonoBehaviour
  6. {
  7. public Intersection intersection;
  8. public int lightPhase;
  9. public GameObject RedLight;
  10. public GameObject YellowLight;
  11. public GameObject GreenLight;
  12. public Color RedColor;
  13. public Color YellowColor;
  14. public Color GreenColor;
  15. public Color BlackColor;
  16. private int currentLight;
  17. // Start is called before the first frame update
  18. void Start()
  19. {
  20. }
  21. // Update is called once per frame
  22. void Update()
  23. {
  24. ManageLightChanges();
  25. }
  26. private void ManageLightChanges()
  27. {
  28. if(intersection.currentRedLightsGroup == lightPhase)
  29. {
  30. ChangeLight(3);
  31. currentLight = 3;
  32. }else{
  33. ChangeLight(1);
  34. currentLight = 1;
  35. }
  36. }
  37. private void ChangeLight(int lightToSet)
  38. {
  39. //turn off all lights
  40. RedLight.GetComponent<Renderer>().material.color = BlackColor;
  41. YellowLight.GetComponent<Renderer>().material.color = BlackColor;
  42. GreenLight.GetComponent<Renderer>().material.color = BlackColor;
  43. switch (lightToSet)
  44. {
  45. case (1): //green
  46. GreenLight.GetComponent<Renderer>().material.color = GreenColor;
  47. break;
  48. case (2): //yellow
  49. YellowLight.GetComponent<Renderer>().material.color = YellowColor;
  50. break;
  51. case (3): //red
  52. RedLight.GetComponent<Renderer>().material.color = RedColor;
  53. break;
  54. }
  55. }
  56. }