12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- 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;
- 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();
- }
- }
- void HandleInTrigger(){
- Debug.Log("In Trigger");
- if(this.presetRunning){
- Debug.Log("Preset already running");
- return;
- }
- SpawnCars();
- this.presetRunning = true;
- }
- void handleOutTrigger(){
- Debug.Log("Out Trigger");
- if(!this.presetRunning){
- Debug.Log("Preset wasn´t running, so couldn´t be ended.");
- return;
- }
- DespawnCars();
- this.presetRunning = false;
- }
- 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();
- }
- }
- void DespawnCars(){
- Debug.Log("despawnCars");
- foreach (GameObject car in this.activeCars)
- {
- this.carPool.despawnCar(car);
- }
- if(this.carSpawner != null){
- this.carSpawner.StopSpawner();
- }
- }
- }
- }
|