123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using TrafficSimulation;
- public class TrafficLightHandler : MonoBehaviour
- {
- public Intersection intersection;
- public int lightPhase;
- public GameObject RedLight;
- public GameObject YellowLight;
- public GameObject GreenLight;
- public Color RedColor;
- public Color YellowColor;
- public Color GreenColor;
- public Color BlackColor;
- private int currentLight;
- // Start is called before the first frame update
- void Start()
- {
-
- }
- // Update is called once per frame
- void Update()
- {
- ManageLightChanges();
- }
- private void ManageLightChanges()
- {
- if(intersection.currentRedLightsGroup == lightPhase)
- {
- ChangeLight(3);
- currentLight = 3;
- }else{
- ChangeLight(1);
- currentLight = 1;
- }
- }
- private void ChangeLight(int lightToSet)
- {
- //turn off all lights
- RedLight.GetComponent<Renderer>().material.color = BlackColor;
- YellowLight.GetComponent<Renderer>().material.color = BlackColor;
- GreenLight.GetComponent<Renderer>().material.color = BlackColor;
- switch (lightToSet)
- {
- case (1): //green
- GreenLight.GetComponent<Renderer>().material.color = GreenColor;
- break;
- case (2): //yellow
- YellowLight.GetComponent<Renderer>().material.color = YellowColor;
- break;
- case (3): //red
- RedLight.GetComponent<Renderer>().material.color = RedColor;
- break;
- }
- }
- }
|