CarPool.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Assertions.Must;
  5. using UnityEngine.Serialization;
  6. namespace TrafficSimulation{
  7. public class CarPool : MonoBehaviour
  8. {
  9. public TrafficSystem trafficSystem;
  10. public static CarPool SharedInstance;
  11. public List<GameObject> carModels;
  12. public List<GameObject> pooledCars;
  13. public int poolSize;
  14. public bool randomizeRayLength = false;
  15. public int rayRandomizeRange = 4;
  16. void Awake() {
  17. SharedInstance = this;
  18. pooledCars = new List<GameObject>();
  19. GameObject tmpInstance;
  20. for(int i = 0; i < poolSize; i++){
  21. tmpInstance = Instantiate(carModels[Random.Range (0, carModels.Count)]);
  22. VehicleAI vAI = tmpInstance.GetComponent<VehicleAI>();
  23. if(vAI == null){
  24. Debug.Log("Carmodel has no AI. Destorying Instance!");
  25. Destroy(tmpInstance);
  26. continue;
  27. }
  28. vAI.trafficSystem = this.trafficSystem;
  29. CarSitter carSitter = tmpInstance.GetComponent<CarSitter>();
  30. if(carSitter != null){
  31. carSitter.carPool = this;
  32. }
  33. tmpInstance.SetActive(false);
  34. pooledCars.Add(tmpInstance);
  35. }
  36. }
  37. public GameObject GetCar(){
  38. foreach(GameObject car in pooledCars){
  39. if(!car.activeInHierarchy){
  40. return car;
  41. }
  42. }
  43. Debug.Log("no car free to use. returning null!");
  44. return null;
  45. }
  46. public GameObject SpawnCar(Transform spawnTranform){
  47. return SpawnCar(spawnTranform.position, spawnTranform.rotation, null);
  48. }
  49. public GameObject SpawnCar(Vector3 spawnPos, Quaternion spawnRot, Transform lookAt)
  50. {
  51. GameObject car = this.GetCar();
  52. if (car == null)
  53. {
  54. Debug.Log("No car was available. Spawn aborted");
  55. return null;
  56. }
  57. PrepareCarToSpawn(car, spawnPos, spawnRot, lookAt);
  58. return car;
  59. }
  60. public void DespawnCar(GameObject car){
  61. car.SetActive(false);
  62. }
  63. private void PrepareCarToSpawn(GameObject car,Vector3 position, Quaternion rotation,Transform lookAt)
  64. {
  65. //Position + Rotation + Lookat
  66. car.transform.position = position;
  67. car.transform.rotation = rotation;
  68. if(lookAt != null)
  69. {
  70. car.transform.LookAt(lookAt);
  71. }
  72. //Status correction. Necessary when the car was despawned while in intersection
  73. VehicleAI ai = car.GetComponent<VehicleAI>();
  74. ai.vehicleStatus = Status.GO;
  75. //If given, randomize RaycastLength
  76. if (this.randomizeRayLength)
  77. {
  78. float defaultLength = ai.raycastLength;
  79. ai.raycastLength = Random.Range(defaultLength - this.rayRandomizeRange,
  80. defaultLength + rayRandomizeRange);
  81. }
  82. car.SetActive(true);
  83. }
  84. }
  85. }