12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- 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 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.isTriggered){
- HandleInTrigger();
- }
- if(triggerOut.isTriggered){
- handleOutTrigger();
- }
- }
- void HandleInTrigger(){
- triggerIn.isTriggered = false;
- Debug.Log("In Trigger");
- if(this.presetRunning){
- Debug.Log("Preset already running");
- return;
- }
- SpawnCars();
- this.presetRunning = true;
- }
- void handleOutTrigger(){
- triggerOut.isTriggered = false;
- 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;
- }
- foreach (Transform carSpawn in this.cars.transform)
- {
- this.activeCars.Add(this.carPool.SpawnCar(carSpawn));
- }
- }
- void DespawnCars(){
- Debug.Log("despawnCars");
- foreach (GameObject car in this.activeCars)
- {
- this.carPool.despawnCar(car);
- }
- }
- }
- }
|