Advertisement
Guest User

StarSystemSpawner - Ver 2

a guest
Mar 3rd, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.30 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 int sectorVariable;
  13.     public Vector2 amountOfSectorsPerAxis;
  14.     public Vector2 systemSector;
  15.  
  16.     public GameObject[] starObjects;
  17.     public Transform starContainer;
  18.     public GameObject planetPrefab;
  19.     public Transform planetContainer;
  20.  
  21.     int noForPlanetSpawn;
  22.     public int maxStarsPerSector;
  23.     public int chanceOfPlanetSpawn;
  24.  
  25.     private void Start()
  26.     {
  27.         SetStarSystemSize();
  28.         SectorSize();
  29.         noForPlanetSpawn = UnityEngine.Random.Range(1, chanceOfPlanetSpawn);
  30.         StartCoroutine(SetUpSystemSectors());
  31.     }
  32.  
  33.     void SetStarSystemSize()
  34.     {
  35.         //Create a system between 100x100 and 200x200
  36.         horizonalSize = UnityEngine.Random.Range(MIN_Hori, MAX_Hori);
  37.         verticalSize = UnityEngine.Random.Range(MIN_Vert, MAX_Vert);
  38.  
  39.         MAX_POSITION = new Vector2((float)horizonalSize / 2, (float)verticalSize / 2);
  40.         MIN_POSITION = new Vector2(-((float)horizonalSize / 2), -((float)verticalSize / 2));
  41.     }
  42.  
  43.     void SectorSize()
  44.     {
  45.         amountOfSectorsPerAxis.x = (int)(horizonalSize / sectorVariable);
  46.         amountOfSectorsPerAxis.y = (int)(verticalSize / sectorVariable);
  47.  
  48.         systemSector = new Vector2(horizonalSize / amountOfSectorsPerAxis.x,
  49.             verticalSize / amountOfSectorsPerAxis.y);
  50.         //Debug.Log(systemQuadrant.ToString());
  51.     }
  52.  
  53.     IEnumerator SetUpSystemSectors()
  54.     {
  55.         for (int i = 0; i < amountOfSectorsPerAxis.x; i++)
  56.         {
  57.             for (int j = 0; j < amountOfSectorsPerAxis.y; j++)
  58.             {
  59.                 //Min position.x + (sysQuad.x * i) , (Min position.x + (sysQuad.x * i)) + sysQuad.x
  60.                 //Min position.y + (sysQuad.y * j) , (Min position.y + (sysQuad.y * j)) + sysQuad.y
  61.                 SetStarsInSector(new Vector2(MIN_POSITION.x + (systemSector.x * i),
  62.                     MIN_POSITION.y + (systemSector.y * j)),
  63.                     new Vector2((MIN_POSITION.x + (systemSector.x * i))+systemSector.x,
  64.                     (MIN_POSITION.y + (systemSector.y * j)) + systemSector.y));
  65.  
  66.                 SetPlanetsInSector(new Vector2(MIN_POSITION.x + (systemSector.x * i),
  67.                     MIN_POSITION.y + (systemSector.y * j)),
  68.                     new Vector2((MIN_POSITION.x + (systemSector.x * i)) + systemSector.x,
  69.                     (MIN_POSITION.y + (systemSector.y * j)) + systemSector.y));
  70.             }
  71.         }
  72.         yield return null;
  73.     }
  74.  
  75.     void SetStarsInSector(Vector2 inSectorBoundsMIN, Vector2 inSectorBoundsMAX)
  76.     {
  77.         //Roll for amount
  78.         //Plot randomly within sector bounds
  79.         int amountOfStars = UnityEngine.Random.Range(1, maxStarsPerSector);
  80.  
  81.         for (int i = 0; i < amountOfStars; i++)
  82.         {
  83.             SpawnStar(new Vector2(UnityEngine.Random.Range(inSectorBoundsMIN.x, inSectorBoundsMAX.x),
  84.                 UnityEngine.Random.Range(inSectorBoundsMIN.y, inSectorBoundsMAX.y)));
  85.         }
  86.     }
  87.     void SetPlanetsInSector(Vector2 inSectorBoundsMIN, Vector2 inSectorBoundsMAX)
  88.     {
  89.         //1 in 100 roll on planets
  90.         int spawnRoll = UnityEngine.Random.Range(1, chanceOfPlanetSpawn);
  91.  
  92.         if (spawnRoll == noForPlanetSpawn)
  93.         {
  94.             SpawnPlanet(new Vector2(UnityEngine.Random.Range(inSectorBoundsMIN.x, inSectorBoundsMAX.x),
  95.                 UnityEngine.Random.Range(inSectorBoundsMIN.y, inSectorBoundsMAX.y)));
  96.         }
  97.     }
  98.  
  99.     void SpawnStar(Vector2 inCoord)
  100.     {
  101.         int starType = UnityEngine.Random.Range(0, starObjects.Length);
  102.         GameObject.Instantiate(starObjects[starType], new Vector3(inCoord.x, inCoord.y, 1), new Quaternion(),starContainer);
  103.     }
  104.     void SpawnPlanet(Vector2 inCoord)
  105.     {
  106.         GameObject.Instantiate(planetPrefab, new Vector3(inCoord.x, inCoord.y, 1), new Quaternion(), planetContainer);
  107.     }
  108. }
  109.  
  110. //TODO
  111. /*
  112.  * Mark out total area of the star system, set boundries.
  113.  * Mark out sectors within the area
  114.  * For each area, roll whether or not there's a planet there and an n-amount of stars.
  115.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement