IntersectionPreset.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace TrafficSimulation{
  5. public class IntersectionPreset : MonoBehaviour
  6. {
  7. public TrafficSystem trafficSystem;
  8. public Intersection intersection;
  9. public GameObject cars;
  10. public PresetTrigger triggerIn;
  11. public PresetTrigger triggerOut;
  12. public CarPool carPool;
  13. private bool presetRunning;
  14. private List<GameObject> activeCars;
  15. // Start is called before the first frame update
  16. void Start()
  17. {
  18. this.presetRunning = false;
  19. this.activeCars = new List<GameObject>();
  20. }
  21. // Update is called once per frame
  22. void Update()
  23. {
  24. if(triggerIn.isTriggered){
  25. HandleInTrigger();
  26. }
  27. if(triggerOut.isTriggered){
  28. handleOutTrigger();
  29. }
  30. }
  31. void HandleInTrigger(){
  32. triggerIn.isTriggered = false;
  33. Debug.Log("In Trigger");
  34. if(this.presetRunning){
  35. Debug.Log("Preset already running");
  36. return;
  37. }
  38. SpawnCars();
  39. this.presetRunning = true;
  40. }
  41. void handleOutTrigger(){
  42. triggerOut.isTriggered = false;
  43. Debug.Log("Out Trigger");
  44. if(!this.presetRunning){
  45. Debug.Log("Preset wasn´t running, so couldn´t be ended.");
  46. return;
  47. }
  48. DespawnCars();
  49. this.presetRunning = false;
  50. }
  51. void SpawnCars(){
  52. if(this.cars == null){
  53. Debug.Log("Found no Car Object. Can´t init preset");
  54. return;
  55. }
  56. foreach (Transform carSpawn in this.cars.transform)
  57. {
  58. this.activeCars.Add(this.carPool.SpawnCar(carSpawn));
  59. }
  60. }
  61. void DespawnCars(){
  62. Debug.Log("despawnCars");
  63. foreach (GameObject car in this.activeCars)
  64. {
  65. this.carPool.despawnCar(car);
  66. }
  67. }
  68. }
  69. }