using System.Collections; using System.Collections.Generic; using Routes; using UnityEngine; using UnityEditor; 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; public int number; public string condition = "X"; public RoadDirection inTriggerPosition; public RoadDirection outTriggerPosition; 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(); } } /// /// Starts preset it not already running /// private void HandleInTrigger() { Debug.Log("In Trigger"); if (this.presetRunning) { Debug.Log("Preset already running"); return; } this.presetRunning = true; SpawnCars(); } /// /// Ends the preset if it was running /// private void handleOutTrigger() { Debug.Log("Out Trigger"); if (!this.presetRunning) { Debug.Log("Preset wasn´t running, so couldn´t be ended."); return; } this.presetRunning = false; DespawnCars(); } /// /// If Cars are set, they will be spawned on the given positions. If a carSpawner is set, it will be activated /// private 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(); } } /// /// Each car, used from this preset will be despawned, if set the carspawner will be stopped /// private void DespawnCars() { Debug.Log("despawnCars"); this.activeCars.ForEach(car => this.carPool.DespawnCar(car)); this.activeCars.Clear(); if (this.carSpawner != null) { this.carSpawner.StopSpawner(); } } } }