浏览代码

Added Traffic light handler and car spawner

Marcel Zickler 3 年之前
父节点
当前提交
7d009f35fc

文件差异内容过多而无法显示
+ 813 - 57
Assets/Scenes/TrafficTestScene.unity


+ 25 - 6
Assets/Scripts/TrafficSimulation/CarSpawner.cs

@@ -7,21 +7,40 @@ public class CarSpawner : MonoBehaviour
     public List<GameObject> Spawners;
     public List<GameObject> Cars;
 
-    public int SpawnRate;
+    public float SpawnRate;
 
-    private List<Time> lastSpawn;
-    private Time 
+    private List<float> lastSpawn = new List<float>();
     // Start is called before the first frame update
     void Start()
     {
-        
+        Debug.Log("Start");
+        for (int i = 0; i <= Spawners.Count; i++)
+        {
+            lastSpawn.Add(Time.time);
+        }
     }
 
     // Update is called once per frame
     void Update()
     {
-        for(Spawn in spawners){
-            if(lastSpawn[index] )
+        int index = 0;
+        foreach(GameObject spawn in Spawners){
+            SpawnCar(spawn, index);
+            index++;
+        }
+    }
+
+    private void SpawnCar(GameObject spawn, int index)
+    {
+        if(lastSpawn[index] + SpawnRate > Time.time)
+        {
+            // No need for a new spawn
+            return;
         }
+        Debug.Log("Spawn!");
+        int carIndex = Random.Range(0, Cars.Count);
+        GameObject car = Cars[carIndex];
+        GameObject newCar = Object.Instantiate(car, spawn.transform.position,spawn.transform.rotation);
+        lastSpawn[index] = Time.time;
     }
 }

+ 57 - 0
Assets/Scripts/TrafficSimulation/TrafficLightHandler.cs

@@ -0,0 +1,57 @@
+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 && currentLight != 3)
+        {
+            ChangeLight(3);
+            currentLight = 3;
+        }
+    }
+    private void ChangeLight(int lightToSet)
+    {
+        //turn off all lights
+        RedLight.GetComponent<Renderer>().material.SetColor("Black", BlackColor);
+        YellowLight.GetComponent<Renderer>().material.SetColor("Black", BlackColor);
+        GreenLight.GetComponent<Renderer>().material.SetColor("Black", BlackColor);
+        switch (lightToSet)
+        {
+            case (1): //green
+                GreenLight.GetComponent<Renderer>().material.SetColor("Green", GreenColor);
+                break;
+            case (2): //yellow
+                YellowLight.GetComponent<Renderer>().material.SetColor("Yellow", YellowColor);
+                break;
+            case (3): //red
+                RedLight.GetComponent<Renderer>().material.SetColor("Red", RedColor);
+                break;
+        }
+    }
+}

部分文件因为文件数量过多而无法显示