IntersectionPreset.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Routes;
  4. using UnityEngine;
  5. using UnityEditor;
  6. namespace TrafficSimulation
  7. {
  8. public class IntersectionPreset : MonoBehaviour
  9. {
  10. public TrafficSystem trafficSystem;
  11. public Intersection intersection;
  12. public GameObject cars;
  13. public CarSituationSpawner carSpawner;
  14. public PresetTrigger triggerIn;
  15. public PresetTrigger triggerOut;
  16. public CarPool carPool;
  17. public int number;
  18. public string condition = "X";
  19. public RoadDirection inTriggerPosition;
  20. public RoadDirection outTriggerPosition;
  21. private bool presetRunning;
  22. private List<GameObject> activeCars;
  23. // Start is called before the first frame update
  24. void Start()
  25. {
  26. this.presetRunning = false;
  27. this.activeCars = new List<GameObject>();
  28. }
  29. // Update is called once per frame
  30. void Update()
  31. {
  32. if (triggerIn.checkLastTrigger())
  33. {
  34. HandleInTrigger();
  35. }
  36. if (triggerOut.checkLastTrigger())
  37. {
  38. HandleOutTrigger();
  39. }
  40. }
  41. /// <summary>
  42. /// Starts preset it not already running
  43. /// </summary>
  44. private void HandleInTrigger()
  45. {
  46. Debug.Log("In Trigger - " + this.gameObject.name);
  47. if (this.presetRunning)
  48. {
  49. Debug.Log("Preset already running");
  50. return;
  51. }
  52. this.presetRunning = true;
  53. SpawnCars();
  54. }
  55. /// <summary>
  56. /// Ends the preset if it was running
  57. /// </summary>
  58. private void HandleOutTrigger()
  59. {
  60. Debug.Log("Out Trigger - " + this.gameObject.name);
  61. if (!this.presetRunning)
  62. {
  63. Debug.Log("Preset wasn´t running, so couldn´t be ended.");
  64. return;
  65. }
  66. this.presetRunning = false;
  67. DespawnCars();
  68. }
  69. /// <summary>
  70. /// If Cars are set, they will be spawned on the given positions. If a carSpawner is set, it will be activated
  71. /// </summary>
  72. private void SpawnCars()
  73. {
  74. if (this.cars == null)
  75. {
  76. Debug.Log("Found no Car Object. Can´t init preset");
  77. return;
  78. }
  79. else
  80. {
  81. // Spawn all Cars which are set manually
  82. foreach (Transform carSpawn in this.cars.transform)
  83. {
  84. this.activeCars.Add(this.carPool.SpawnCar(carSpawn));
  85. }
  86. }
  87. //start spawners
  88. if (this.carSpawner != null)
  89. {
  90. this.carSpawner.StartSpawner();
  91. }
  92. }
  93. /// <summary>
  94. /// Each car, used from this preset will be despawned, if set the carspawner will be stopped
  95. /// </summary>
  96. private void DespawnCars()
  97. {
  98. Debug.Log("despawnCars");
  99. this.activeCars.ForEach(car => this.carPool.DespawnCar(car));
  100. this.activeCars.Clear();
  101. if (this.carSpawner != null)
  102. {
  103. this.carSpawner.StopSpawner();
  104. }
  105. }
  106. }
  107. }