CarPool.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Assertions.Must;
  6. using UnityEngine.Serialization;
  7. using Random = UnityEngine.Random;
  8. namespace TrafficSimulation{
  9. public class CarPool : MonoBehaviour
  10. {
  11. public TrafficSystem trafficSystem;
  12. public static CarPool SharedInstance;
  13. public List<GameObject> carModels;
  14. public List<GameObject> pooledCars;
  15. public int poolSize;
  16. public float standardRayLength = 5;
  17. public bool randomizeRayLength = false;
  18. public float rayRandomizeRange = 4;
  19. public Camera bikeCam;
  20. public float spawnPositionCorrectionY;
  21. private void Awake() {
  22. SharedInstance = this;
  23. pooledCars = new List<GameObject>();
  24. GameObject tmpInstance;
  25. for(int i = 0; i < poolSize; i++){
  26. tmpInstance = Instantiate(carModels[Random.Range (0, carModels.Count)]);
  27. VehicleAI vAI = tmpInstance.GetComponent<VehicleAI>();
  28. if(vAI == null){
  29. Debug.Log("Carmodel has no AI. Destorying Instance!");
  30. Destroy(tmpInstance);
  31. continue;
  32. }
  33. vAI.trafficSystem = this.trafficSystem;
  34. vAI.raycastLength = standardRayLength;
  35. CarSitter carSitter = tmpInstance.GetComponent<CarSitter>();
  36. if(carSitter != null){
  37. carSitter.carPool = this;
  38. }
  39. tmpInstance.SetActive(false);
  40. pooledCars.Add(tmpInstance);
  41. }
  42. }
  43. public GameObject GetCar(){
  44. foreach(GameObject car in pooledCars){
  45. if(!car.activeInHierarchy){
  46. return car;
  47. }
  48. }
  49. Debug.Log("no car free to use. returning null!");
  50. return null;
  51. }
  52. public GameObject SpawnCar(Transform spawnTranform){
  53. Vector3 carPosition = spawnTranform.position;
  54. if (spawnPositionCorrectionY != 0f)
  55. {
  56. carPosition = new Vector3(carPosition.x, carPosition.y + spawnPositionCorrectionY, carPosition.z);
  57. }
  58. return SpawnCar(carPosition, spawnTranform.rotation, null);
  59. }
  60. public GameObject SpawnCar(Vector3 spawnPos, Quaternion spawnRot, Transform lookAt)
  61. {
  62. GameObject car = this.GetCar();
  63. if (car == null)
  64. {
  65. Debug.Log("No car was available. Spawn aborted");
  66. return null;
  67. }
  68. PrepareCarToSpawn(car, spawnPos, spawnRot, lookAt);
  69. return car;
  70. }
  71. public void DespawnCar(GameObject car)
  72. {
  73. Plane[] planes = GeometryUtility.CalculateFrustumPlanes(bikeCam);
  74. Collider carCollider = car.GetComponent<Collider>();
  75. if (GeometryUtility.TestPlanesAABB(planes, carCollider.bounds))
  76. {
  77. Debug.Log("Car in Cam, prospone the despawn!");
  78. StartCoroutine(ProsponeDespawn(car, 1.0f));
  79. return;
  80. }
  81. Debug.Log("Despawn Car!");
  82. car.SetActive(false);
  83. }
  84. IEnumerator ProsponeDespawn(GameObject car,float time)
  85. {
  86. yield return new WaitForSeconds(time);
  87. DespawnCar(car);
  88. }
  89. private void PrepareCarToSpawn(GameObject car,Vector3 position, Quaternion rotation,Transform lookAt)
  90. {
  91. //Position + Rotation + Lookat
  92. car.transform.position = position;
  93. car.transform.rotation = rotation;
  94. if(lookAt != null)
  95. {
  96. car.transform.LookAt(lookAt);
  97. }
  98. //Status correction. Necessary when the car was despawned while in intersection
  99. VehicleAI ai = car.GetComponent<VehicleAI>();
  100. ai.vehicleStatus = Status.GO;
  101. //If given, randomize RaycastLength
  102. if (this.randomizeRayLength)
  103. {
  104. ai.raycastLength = Random.Range(this.standardRayLength - this.rayRandomizeRange,
  105. this.standardRayLength + rayRandomizeRange);
  106. }
  107. car.SetActive(true);
  108. }
  109. }
  110. }