IntersectionPreset.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 CarSituationSpawner carSpawner;
  11. public PresetTrigger triggerIn;
  12. public PresetTrigger triggerOut;
  13. public CarPool carPool;
  14. private bool presetRunning;
  15. private List<GameObject> activeCars;
  16. // Start is called before the first frame update
  17. void Start()
  18. {
  19. this.presetRunning = false;
  20. this.activeCars = new List<GameObject>();
  21. }
  22. // Update is called once per frame
  23. void Update()
  24. {
  25. if(triggerIn.checkLastTrigger()){
  26. HandleInTrigger();
  27. }
  28. if(triggerOut.checkLastTrigger()){
  29. handleOutTrigger();
  30. }
  31. }
  32. void HandleInTrigger(){
  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. Debug.Log("Out Trigger");
  43. if(!this.presetRunning){
  44. Debug.Log("Preset wasn´t running, so couldn´t be ended.");
  45. return;
  46. }
  47. DespawnCars();
  48. this.presetRunning = false;
  49. }
  50. void SpawnCars(){
  51. if(this.cars == null){
  52. Debug.Log("Found no Car Object. Can´t init preset");
  53. return;
  54. }else{
  55. foreach (Transform carSpawn in this.cars.transform)
  56. {
  57. this.activeCars.Add(this.carPool.SpawnCar(carSpawn));
  58. }
  59. }
  60. if(this.carSpawner != null){
  61. this.carSpawner.StartSpawner();
  62. }
  63. }
  64. void DespawnCars(){
  65. Debug.Log("despawnCars");
  66. foreach (GameObject car in this.activeCars)
  67. {
  68. this.carPool.despawnCar(car);
  69. }
  70. if(this.carSpawner != null){
  71. this.carSpawner.StopSpawner();
  72. }
  73. }
  74. }
  75. }