Advertisement
wasdswag

foodSpawner

Oct 23rd, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.99 KB | None | 0 0
  1. public class FoodSpawner : MonoBehaviour
  2. {
  3.  
  4.     public List<GameObject> items;
  5.     List<GameObject> tempItems = new List<GameObject>();
  6.     GameObject item;
  7.  
  8.     Vector3[] initialSpawnPos;
  9.     public List<Vector3> spawned = new List<Vector3>();
  10.     List<int> SpawnIndex = new List<int> { 0, 1, 2 };
  11.     List<int> tempSpawnIndex = new List<int>();
  12.  
  13.     void Start()
  14.     {
  15.  
  16.         tempSpawnIndex = new List<int>(SpawnIndex);
  17.  
  18.         initialSpawnPos = new Vector3[3];
  19.         initialSpawnPos[0] = new Vector3(256f, 41.48f, 242.21f);
  20.         initialSpawnPos[1] = new Vector3(254.44f, 41.29f, 239.68f);
  21.         initialSpawnPos[1] = new Vector3(256.81f, 41.19f, 239.79f);
  22.         tempItems = new List<GameObject>(items);
  23.  
  24.  
  25.         for (int i = 0; i < 3; i++)
  26.         {
  27.             GameObject generated = Instantiate(uniqalObject(tempItems.Count), initialSpawnPos[i], Quaternion.identity);
  28.             spawned.Add(generated.transform.position);
  29.  
  30.         }
  31.  
  32.     }
  33.  
  34.  
  35.     public void Spawner()
  36.     {
  37.         for (int i = 0; i < initialSpawnPos.Length; i++)
  38.         {
  39.             if (spawned.Contains(initialSpawnPos[i]))
  40.             {
  41.                 tempSpawnIndex.Remove(i);
  42.             }
  43.         }
  44.  
  45.         GameObject generated = Instantiate(uniqalObject(tempItems.Count), initialSpawnPos[freeslot(tempSpawnIndex.Count)], Quaternion.identity);
  46.         spawned.Add(generated.transform.position);
  47.     }
  48.  
  49.  
  50.  
  51.  
  52.     GameObject uniqalObject(int a)
  53.     {
  54.         if (a == 0)
  55.         {
  56.             tempItems = new List<GameObject>(items);
  57.         }
  58.         int index = Random.Range(0, a);
  59.         item = tempItems[index];
  60.         tempItems.Remove(item);
  61.         return item;
  62.     }
  63.  
  64.  
  65.     int freeslot(int a)
  66.     {
  67.         int res = 0;
  68.         if (a == 0)
  69.         {
  70.             tempSpawnIndex = new List<int>(SpawnIndex);
  71.         }
  72.         int index = Random.Range(0, a);
  73.         res = tempSpawnIndex[index];
  74.         tempSpawnIndex.Remove(res);
  75.         return res;
  76.     }
  77.  
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement