Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- /*
- *
- * Programmed by William Rouse(Kuliu) for Nomad
- * Sunday, February 01, 2015
- *
- *
- */
- public class CarSpawnSystem : MonoBehaviour {
- public bool showDebug = false;
- //How many cars are allowed on the map?
- public int carsAllowedOnMap = 6;
- //How close can a car be to the spawnspot for it to not spawn?
- public int spawnDistance = 5;
- //How many seconds will it take before it will try and spawn a car
- public float trySpawnTime = 20;
- //a 0 - chance, chance rating. If it is 1 it is a success.
- //(The higher the name the more rare of a chance a car will spawn)
- public int chance = 4;
- //The list of spawnable cars
- public GameObject[] cars;
- //All the locations a car is allowed to spawn in.
- private Transform[] spawnSpots;
- //All the cars that are alive on the map.
- private GameObject[] aliveCars;
- //Timer used for trySpawnTime. See line 77
- private float timer = 0;
- //Ignore these
- private void addAliveCar(GameObject car) {
- for (int i = 0; i < aliveCars.Length; i++) {
- if (aliveCars[i] == null) {
- aliveCars[i] = car;
- return;
- }
- }
- }
- //Ignore these
- private void removeAliveCar(int index) {
- aliveCars[index] = null;
- }
- //Ignore these
- public int getAliveCars() {
- int alive = 0;
- for (int i = 0; i < aliveCars.Length; i++) {
- if (aliveCars[i] != null) {
- if (aliveCars[i].gameObject != null) {
- alive++;
- }
- }
- }
- return alive;
- }
- void Start () {
- //Initalization
- aliveCars = new GameObject[carsAllowedOnMap];
- timer = trySpawnTime;
- spawnSpots = new Transform[transform.childCount];
- for (int i = 0; i < spawnSpots.Length; i++) {
- children[i] = transform.GetChild(i);
- }
- }
- void Update () {
- //If there is slot allowed for a car to spawn?
- if (getAliveCars () < carsAllowedOnMap) {
- timer -= Time.deltaTime;
- if (showDebug) {
- Debug.Log("Time before trySpawnCar: " + Mathf.Round(timer));
- }
- if (timer <= 0) {
- //Once the timer runs out of time.
- int spawnChance = Random.Range(0, chance);
- //if the number is 1 then we will spawn a car.
- if (spawnChance == 1) {
- SpawnCar:
- //Picking the location to spawn the car
- Transform transToSpawn = spawnSpots[Random.Range(0, spawnSpots.Length)];
- //Testing if there is a car already at this spot.
- for (int i = 0; i < aliveCars.Length; i++) {
- if (aliveCars[i] != null) {
- if (aliveCars[i].gameObject != null) {
- if (Vector3.Distance(aliveCars[i].transform.position, transToSpawn.position) < spawnDistance) {
- //There is a car within 'spawnDistance' time to restart the process to find a new empty spot!
- goto SpawnCar;
- }
- }
- }
- }
- spawnCar(transToSpawn);
- timer = trySpawnTime;
- } else {
- if (showDebug) {
- Debug.Log("TRIED TO SPAWN CAR BUT THE CHANCES WOULDNT LET IT.");
- }
- goto CarDidntSpawn;
- }
- }
- goto NotReady;
- CarDidntSpawn:
- timer = trySpawnTime;
- NotReady:
- timer = timer;
- }
- }
- private void spawnCar(Transform transform) {
- //Picking a car to spawn.
- GameObject carToSpawn = cars [Random.Range (0, cars.Length)];
- //CHANGE THIS TO SPAWN IT ON THE NETWORK.
- carToSpawn = (GameObject)Instantiate (carToSpawn, transform.position, transform.rotation);
- addAliveCar (carToSpawn);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement