123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- 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<GameObject> activeCars;
- // Start is called before the first frame update
- void Start()
- {
- this.presetRunning = false;
- this.activeCars = new List<GameObject>();
- }
- // Update is called once per frame
- void Update()
- {
- if (triggerIn.checkLastTrigger())
- {
- HandleInTrigger();
- }
- if (triggerOut.checkLastTrigger())
- {
- HandleOutTrigger();
- }
- }
- /// <summary>
- /// Starts preset it not already running
- /// </summary>
- private void HandleInTrigger()
- {
- Debug.Log("In Trigger - " + this.gameObject.name);
- if (this.presetRunning)
- {
- Debug.Log("Preset already running");
- return;
- }
- this.presetRunning = true;
- SpawnCars();
- }
- /// <summary>
- /// Ends the preset if it was running
- /// </summary>
- private void HandleOutTrigger()
- {
- Debug.Log("Out Trigger - " + this.gameObject.name);
- if (!this.presetRunning)
- {
- Debug.Log("Preset wasn´t running, so couldn´t be ended.");
- return;
- }
- this.presetRunning = false;
- DespawnCars();
- }
- /// <summary>
- /// If Cars are set, they will be spawned on the given positions. If a carSpawner is set, it will be activated
- /// </summary>
- private void SpawnCars()
- {
- if (this.cars == null)
- {
- Debug.Log("Found no Car Object. Can´t init preset");
- return;
- }
- else
- {
- // Spawn all Cars which are set manually
- foreach (Transform carSpawn in this.cars.transform)
- {
- this.activeCars.Add(this.carPool.SpawnCar(carSpawn));
- }
- }
- //start spawners
- if (this.carSpawner != null)
- {
- this.carSpawner.StartSpawner();
- }
- }
- /// <summary>
- /// Each car, used from this preset will be despawned, if set the carspawner will be stopped
- /// </summary>
- private void DespawnCars()
- {
- Debug.Log("despawnCars");
- this.activeCars.ForEach(car => this.carPool.DespawnCar(car));
- this.activeCars.Clear();
- if (this.carSpawner != null)
- {
- this.carSpawner.StopSpawner();
- }
- }
- }
- }
|