Advertisement
Guest User

StarSystemSpawner

a guest
Feb 14th, 2020
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.93 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class StarSystemSpawner : MonoBehaviour
  7. {
  8.     public int MIN_Hori, MAX_Hori, MIN_Vert, MAX_Vert;
  9.     public int horizonalSize, verticalSize;
  10.     public Vector2 MAX_POSITION;
  11.     public Vector2 MIN_POSITION;
  12.     public Vector2 amountOfSectorsPerAxis;
  13.     public Vector2 systemSector;
  14.  
  15.     public GameObject[] starObjects;
  16.     public Transform starContainer;
  17.     public GameObject planetPrefab;
  18.     public Transform planetContainer;
  19.  
  20.     int noForPlanetSpawn;
  21.     public int maxStarsPerSector;
  22.     public int chanceOfPlanetSpawn;
  23.  
  24.     private void Start()
  25.     {
  26.         SetStarSystemSize();
  27.         SectorSize();
  28.         noForPlanetSpawn = UnityEngine.Random.Range(1, chanceOfPlanetSpawn);
  29.         StartCoroutine(SetUpSystemSectors());
  30.     }
  31.  
  32.     void SetStarSystemSize()
  33.     {
  34.         //Create a system between 100x100 and 200x200
  35.         horizonalSize = UnityEngine.Random.Range(MIN_Hori, MAX_Hori);
  36.         verticalSize = UnityEngine.Random.Range(MIN_Vert, MAX_Vert);
  37.  
  38.         MAX_POSITION = new Vector2((float)horizonalSize / 2, (float)verticalSize / 2);
  39.         MIN_POSITION = new Vector2(-((float)horizonalSize / 2), -((float)verticalSize / 2));
  40.     }
  41.  
  42.     void SectorSize()
  43.     {
  44.         systemSector = new Vector2(horizonalSize / amountOfSectorsPerAxis.x,
  45.             verticalSize / amountOfSectorsPerAxis.y);
  46.         //Debug.Log(systemQuadrant.ToString());
  47.     }
  48.  
  49.     IEnumerator SetUpSystemSectors()
  50.     {
  51.         for (int i = 0; i < amountOfSectorsPerAxis.x; i++)
  52.         {
  53.             for (int j = 0; j < amountOfSectorsPerAxis.y; j++)
  54.             {
  55.                 //Min position.x + (sysQuad.x * i) , (Min position.x + (sysQuad.x * i)) + sysQuad.x
  56.                 //Min position.y + (sysQuad.y * j) , (Min position.y + (sysQuad.y * j)) + sysQuad.y
  57.                 SetStarsInSector(new Vector2(MIN_POSITION.x + (systemSector.x * i),
  58.                     MIN_POSITION.y + (systemSector.y * j)),
  59.                     new Vector2((MIN_POSITION.x + (systemSector.x * i))+systemSector.x,
  60.                     (MIN_POSITION.y + (systemSector.y * j)) + systemSector.y));
  61.  
  62.                 SetPlanetsInSector(new Vector2(MIN_POSITION.x + (systemSector.x * i),
  63.                     MIN_POSITION.y + (systemSector.y * j)),
  64.                     new Vector2((MIN_POSITION.x + (systemSector.x * i)) + systemSector.x,
  65.                     (MIN_POSITION.y + (systemSector.y * j)) + systemSector.y));
  66.             }
  67.         }
  68.         yield return null;
  69.     }
  70.  
  71.     void SetStarsInSector(Vector2 inSectorBoundsMIN, Vector2 inSectorBoundsMAX)
  72.     {
  73.         //Roll for amount
  74.         //Plot randomly within sector bounds
  75.         int amountOfStars = UnityEngine.Random.Range(1, maxStarsPerSector);
  76.  
  77.         for (int i = 0; i < amountOfStars; i++)
  78.         {
  79.             SpawnStar(new Vector2(UnityEngine.Random.Range(inSectorBoundsMIN.x, inSectorBoundsMAX.x),
  80.                 UnityEngine.Random.Range(inSectorBoundsMIN.y, inSectorBoundsMAX.y)));
  81.         }
  82.     }
  83.     void SetPlanetsInSector(Vector2 inSectorBoundsMIN, Vector2 inSectorBoundsMAX)
  84.     {
  85.         //1 in 100 roll on planets
  86.         int spawnRoll = UnityEngine.Random.Range(1, chanceOfPlanetSpawn);
  87.  
  88.         if (spawnRoll == noForPlanetSpawn)
  89.         {
  90.             SpawnPlanet(new Vector2(UnityEngine.Random.Range(inSectorBoundsMIN.x, inSectorBoundsMAX.x),
  91.                 UnityEngine.Random.Range(inSectorBoundsMIN.y, inSectorBoundsMAX.y)));
  92.         }
  93.     }
  94.  
  95.     void SpawnStar(Vector2 inCoord)
  96.     {
  97.         int starType = UnityEngine.Random.Range(0, starObjects.Length);
  98.         GameObject.Instantiate(starObjects[starType], new Vector3(inCoord.x, inCoord.y, 1), new Quaternion(),starContainer);
  99.     }
  100.     void SpawnPlanet(Vector2 inCoord)
  101.     {
  102.         GameObject.Instantiate(planetPrefab, new Vector3(inCoord.x, inCoord.y, 1), new Quaternion(), planetContainer);
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement