1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- 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();
- }
- }
- /// <summary>
- /// Starts preset it not already running
- /// </summary>
- private void HandleInTrigger(){
- Debug.Log("In Trigger");
- if(this.presetRunning){
- Debug.Log("Preset already running");
- return;
- }
- this.presetRunning = true;
- SpawnCars();
- }
- /// <summary>
- /// Ends the preset if it was running
- /// </summary>
- private void handleOutTrigger(){
- Debug.Log("Out Trigger");
- if(!this.presetRunning){
- Debug.Log("Preset wasn´t running, so couldn´t be ended.");
- return;
- }
- this.presetRunning = false;
- DespawnCars();
- }
- /// <summary>
- /// If Cars are set, they will be spawned on the given positions. If a carSpawner is set, it will be activated
- /// </summary>
- private 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();
- }
- }
- /// <summary>
- /// Each car, used from this preset will be despawned, if set the carspawner will be stopped
- /// </summary>
- private void DespawnCars(){
- Debug.Log("despawnCars");
- this.activeCars.ForEach(car => this.carPool.DespawnCar(car));
- this.activeCars.Clear();
- if(this.carSpawner != null){
- this.carSpawner.StopSpawner();
- }
- }
- }
- }
|