Last active
May 8, 2019 01:34
-
-
Save SenpaiRar/0bdce9177f01d51af6575d9bca2e32fa to your computer and use it in GitHub Desktop.
This populator places the zombies in the game world.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class Zombie_Populator : MonoBehaviour { | |
//This determines how many packs of zombies will be spawned IE; how many clusters around the map, not how many zombie gameobjects | |
public int PackNumber; | |
//This determines how many zombie gameobjects are in each pack of zombies | |
public int PackSize; | |
//The number of zombie gameobjects in each pack can be given a random variance to give variety | |
public int PackSizeVariance; | |
//When the random positions for the packs are generated, this number determines how far a zombie could be spawned from the pack center | |
public float PackSizeDistance; | |
//This is the lower bound of the spawn area for zombies. If the spawn area is a square, this can be thought as the position of the bottom left corner | |
public Vector3 StartVector; | |
//This is the lower bound of the spawn area for zombies. If the spawn area is a square, this can be thought as the position of the upper right corner | |
public Vector3 End; | |
//Because there's only one zombie gameobject, we don't need to make a list of gameobjects. We just need to make a gameobject for us to drag in our zombie prefab. | |
public GameObject ZombieGameObject; | |
//This is the list of the objects we have spawned already | |
private List<GameObject> SpawnedGameObjects = new List<GameObject>(); | |
//We define a function that gives us a list of random positions in the spawn area defined by the upper/lower bounds. Every position represents the center of a zombie pack. | |
public List<Vector3> GetSpawnVectors(){ | |
List<Vector3> returnList = new List<Vector3>(); | |
//We create a loop with the number of packs so we can generate that number of pack centers | |
for (int i = 0; i < PackNumber; i++) | |
{ | |
//We generate a random number between the x and z components of the upper/lower bounds, effectively giving us the x and z components of a random position in those bounds | |
float x = Random.Range(StartVector.x, End.x); | |
float z = Random.Range(StartVector.z, End.z); | |
//We then use those numbers to create a new Vector3 and add it to our list which we return below | |
returnList.Add(new Vector3(x, 0, z)); | |
} | |
return (returnList); | |
} | |
//We define a function that gives us the positions of the individual zombie gameobjects in the packs. This is the function we'll be using | |
//to actually spawn our gameobjects | |
//It takes in a Vector3 position and gives a list of randomly generated positions around that position | |
public List<Vector3> GeneratePack(Vector3 Location){ | |
List<Vector3> PackSpawns = new List<Vector3>(); | |
//We create a loop with the number of zombies in a pack + an additional random amount between 0 and PackSizeVariance | |
for (int i = 0; i < PackSize + Random.Range(0, PackSizeVariance); i++) | |
{ | |
//We create a new Vector3 that has the x and z components of the inputed Vector3 Location modified by adding on a | |
//random number between -PacksizeDistance and Packsize distance. This let the zombies spawn around the location in a circle | |
//We then add it to the list we return below | |
PackSpawns.Add(new Vector3(Location.x + Random.Range(-PackSizeDistance, PackSizeDistance), 0, Location.z + Random.Range(-PackSizeDistance, PackSizeDistance))); | |
} | |
return (PackSpawns); | |
} | |
//This function actually places the zombie gameobjects in the gameworld. The function takes in a list of positions to generate packs around. | |
public void PopulateWorld(List<Vector3> SpawnVectors){ | |
//We go through the list the function takes in, calling the entry we're on x | |
foreach (Vector3 x in SpawnVectors) | |
{ | |
//We then go through the list generated by GeneratePack() when we pass in Vector3 x. | |
//We are essentially going through the list above, generating the random positions around each entry, | |
//And then spawning the game objects | |
foreach (Vector3 y in GeneratePack(x)) | |
{ | |
GameObject z = Instantiate(ZombieGameObject, y, Quaternion.identity) as GameObject; | |
SpawnedGameObjects.Add(z); | |
} | |
} | |
} | |
public void DestroyAllSpawnedObjects(){ | |
foreach (GameObject x in SpawnedGameObjects){ | |
DestroyImmediate(x); | |
}} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment