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