Advertisement
Guest User

Untitled

a guest
Feb 1st, 2015
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4.  
  5. /*
  6. *
  7. * Programmed by William Rouse(Kuliu) for Nomad
  8. * Sunday, February 01, 2015
  9. *
  10. *
  11. */
  12.  
  13.  
  14.  
  15.  
  16. public class CarSpawnSystem : MonoBehaviour {
  17.  
  18. public bool showDebug = false;
  19.  
  20.  
  21. //How many cars are allowed on the map?
  22. public int carsAllowedOnMap = 6;
  23.  
  24. //How close can a car be to the spawnspot for it to not spawn?
  25. public int spawnDistance = 5;
  26.  
  27. //How many seconds will it take before it will try and spawn a car
  28. public float trySpawnTime = 20;
  29.  
  30.  
  31. //a 0 - chance, chance rating. If it is 1 it is a success.
  32. //(The higher the name the more rare of a chance a car will spawn)
  33. public int chance = 4;
  34.  
  35. //The list of spawnable cars
  36. public GameObject[] cars;
  37.  
  38. //All the locations a car is allowed to spawn in.
  39. private Transform[] spawnSpots;
  40.  
  41. //All the cars that are alive on the map.
  42. private GameObject[] aliveCars;
  43.  
  44. //Timer used for trySpawnTime. See line 77
  45. private float timer = 0;
  46.  
  47. //Ignore these
  48. private void addAliveCar(GameObject car) {
  49. for (int i = 0; i < aliveCars.Length; i++) {
  50. if (aliveCars[i] == null) {
  51. aliveCars[i] = car;
  52. return;
  53. }
  54. }
  55. }
  56.  
  57. //Ignore these
  58. private void removeAliveCar(int index) {
  59. aliveCars[index] = null;
  60. }
  61.  
  62. //Ignore these
  63. public int getAliveCars() {
  64. int alive = 0;
  65. for (int i = 0; i < aliveCars.Length; i++) {
  66. if (aliveCars[i] != null) {
  67. if (aliveCars[i].gameObject != null) {
  68. alive++;
  69. }
  70. }
  71. }
  72. return alive;
  73. }
  74.  
  75. void Start () {
  76. //Initalization
  77. aliveCars = new GameObject[carsAllowedOnMap];
  78. timer = trySpawnTime;
  79.  
  80. spawnSpots = new Transform[transform.childCount];
  81.  
  82. for (int i = 0; i < spawnSpots.Length; i++) {
  83. children[i] = transform.GetChild(i);
  84. }
  85.  
  86. }
  87.  
  88. void Update () {
  89. //If there is slot allowed for a car to spawn?
  90. if (getAliveCars () < carsAllowedOnMap) {
  91.  
  92. timer -= Time.deltaTime;
  93. if (showDebug) {
  94. Debug.Log("Time before trySpawnCar: " + Mathf.Round(timer));
  95. }
  96. if (timer <= 0) {
  97. //Once the timer runs out of time.
  98.  
  99. int spawnChance = Random.Range(0, chance);
  100.  
  101.  
  102. //if the number is 1 then we will spawn a car.
  103. if (spawnChance == 1) {
  104.  
  105. SpawnCar:
  106. //Picking the location to spawn the car
  107. Transform transToSpawn = spawnSpots[Random.Range(0, spawnSpots.Length)];
  108.  
  109. //Testing if there is a car already at this spot.
  110. for (int i = 0; i < aliveCars.Length; i++) {
  111. if (aliveCars[i] != null) {
  112. if (aliveCars[i].gameObject != null) {
  113.  
  114. if (Vector3.Distance(aliveCars[i].transform.position, transToSpawn.position) < spawnDistance) {
  115. //There is a car within 'spawnDistance' time to restart the process to find a new empty spot!
  116. goto SpawnCar;
  117. }
  118. }
  119. }
  120. }
  121. spawnCar(transToSpawn);
  122. timer = trySpawnTime;
  123.  
  124. } else {
  125. if (showDebug) {
  126. Debug.Log("TRIED TO SPAWN CAR BUT THE CHANCES WOULDNT LET IT.");
  127. }
  128. goto CarDidntSpawn;
  129. }
  130. }
  131.  
  132. goto NotReady;
  133.  
  134. CarDidntSpawn:
  135. timer = trySpawnTime;
  136.  
  137.  
  138. NotReady:
  139. timer = timer;
  140.  
  141. }
  142. }
  143.  
  144. private void spawnCar(Transform transform) {
  145. //Picking a car to spawn.
  146. GameObject carToSpawn = cars [Random.Range (0, cars.Length)];
  147.  
  148.  
  149. //CHANGE THIS TO SPAWN IT ON THE NETWORK.
  150. carToSpawn = (GameObject)Instantiate (carToSpawn, transform.position, transform.rotation);
  151. addAliveCar (carToSpawn);
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement