Skip to content

Instantly share code, notes, and snippets.

@SenpaiRar
Last active May 6, 2019 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SenpaiRar/e950a5f37883c72ad52b6b93afbaf565 to your computer and use it in GitHub Desktop.
Save SenpaiRar/e950a5f37883c72ad52b6b93afbaf565 to your computer and use it in GitHub Desktop.
Environment_Populator handles the placing of grass among other enviornmental objects.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Environment_Populator : MonoBehaviour {
//This list of gameobjects holds the prefabs we'll be spawning using our populator
public List<GameObject> EnvironmentSpawnables = new List<GameObject>();
//Texture2D is a component we can make in our script to import pictures. It gives us different methods to check color, picture coords, etc.
public Texture2D SpawnMap;
//We can't spawn objects at every pixel because that would be too many objects. So, only spawn objects every spread number pixels
public int Spread;
//Because we're using the x/y pixels of the picture for the position of the gameobjects, we need a way to control how those coordinates are scaled into the x/z of the game world.
public float Scale;
//We keep track of the objects we HAVE spawned, so we can manipulate them later if needed
public List<GameObject> GameObjectsSpawned = new List<GameObject>();
//Here we define a function that will give us the list of positions to spawn gameobjects at.
public List<Vector3> SpawnVectors()
{
//We create a int variable. We use this to spread out the gameobjects
int z = 0;
List<Vector3> returnList = new List<Vector3>(); //We make a list of Vector3 to add the spawn positions and return at the end.
for (int x = 0; x < SpawnMap.width; x++) //We make a loop that will iterate as many times as the width of our Noisemap
{
for (int y = 0; y < SpawnMap.height; y++) //We then put another loop that iterates as many times
{
//We add one to our z variable to to keep track of spread.
z++;
//We check if the pixel we're at using the iteriors in our for loops (x,y) and see if the grayscale value is between 0.6 and 1 if it is...
if (SpawnMap.GetPixel(x, y).grayscale >= 0.6 && SpawnMap.GetPixel(x,y).grayscale <= 1)
{
//we check if z is greater than the spread
if(z >= Spread)
{
//if it is, we add a Vector3 using the x and y pixels dividing them by Scale to get a corresponding position in the game world
returnList.Add(new Vector3(x / Scale, 0, y / Scale));
z = 0;
}
}
//We add in another check of the pixel if it doesn't satisfy the one above
//if the pixel is grey but not between .6 and 1, we give it a 50% chance to spawn a game object
//This let's us have a gradual transition of grass instead of a hard cutoff
else if (SpawnMap.GetPixel(x, y).grayscale > 0.3 && SpawnMap.GetPixel(x, y).grayscale < 0.6)
{
if(Random.Range(0,1) == 1)
{
if(z >= Spread)
{
returnList.Add(new Vector3(x / Scale, 0, y / Scale));
z = 0;
}
}
}
}
}
return (returnList); //we return the vector3 list after we're done adding the spawn positions.
}
//We define a function that takes in a list of Vectors3 to spawn gameobjects at
public void Populate_World(List<Vector3> SpawnVectors)
{
foreach (Vector3 x in SpawnVectors) //We go through every Vector3 in the list we pass into the function
{
//Because we know which prefabs we put into the list, we can give each object a certain chance to spawn
//In this case, if a randomly generated number between 0 and 1 is greater than 0.1 (which is effectively
//giving it a 90% chance), we'll spawn EnvironmentSpawnables[0], which
//Is one of our prefabs in our list.
if(Random.Range(0,1f) > .01)
{
//We make a gameobject that is set to the copied gameobject using Instantiate at position x, which is the current
//Vector3 while we're going through the list
GameObject z = Instantiate(EnvironmentSpawnables[0], x, Quaternion.Euler(0, Random.Range(0, 360.0f), 0)) as GameObject;
//We then add the Gameobject z to our list of gameobjects we have created
GameObjectsSpawned.Add(z);
}
else
{
//In a 10% chance case, we'll spawn a ranodm gameobject in the prefab list excluding the first prefab, using the same Instnatiate command and adding it to our list.
GameObject z = Instantiate(EnvironmentSpawnables[Random.Range(1, EnvironmentSpawnables.Count)], x, Quaternion.Euler(0, Random.Range(0, 360.0f), 0)) as GameObject;
GameObjectsSpawned.Add(z);
}
}
}
public void DestroyGameObjectsSpawned(){
foreach (GameObject x in GameObjectsSpawned){
DestroyImmediate(x);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment