using System.Collections; using System.Collections.Generic; using UnityEngine; namespace TrafficSimulation{ public class IntersectionPreset : MonoBehaviour { public TrafficSystem trafficSystem; public Intersection intersection; public GameObject cars; public CarSituationSpawner carSpawner; public PresetTrigger triggerIn; public PresetTrigger triggerOut; public CarPool carPool; private bool presetRunning; private List activeCars; // Start is called before the first frame update void Start() { this.presetRunning = false; this.activeCars = new List(); } // Update is called once per frame void Update() { if(triggerIn.checkLastTrigger()){ HandleInTrigger(); } if(triggerOut.checkLastTrigger()){ handleOutTrigger(); } } void HandleInTrigger(){ Debug.Log("In Trigger"); if(this.presetRunning){ Debug.Log("Preset already running"); return; } SpawnCars(); this.presetRunning = true; } void handleOutTrigger(){ Debug.Log("Out Trigger"); if(!this.presetRunning){ Debug.Log("Preset wasn´t running, so couldn´t be ended."); return; } DespawnCars(); this.presetRunning = false; } void SpawnCars(){ if(this.cars == null){ Debug.Log("Found no Car Object. Can´t init preset"); return; }else{ foreach (Transform carSpawn in this.cars.transform) { this.activeCars.Add(this.carPool.SpawnCar(carSpawn)); } } if(this.carSpawner != null){ this.carSpawner.StartSpawner(); } } void DespawnCars(){ Debug.Log("despawnCars"); foreach (GameObject car in this.activeCars) { this.carPool.despawnCar(car); } if(this.carSpawner != null){ this.carSpawner.StopSpawner(); } } } }