Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class StarSystemSpawner : MonoBehaviour
- {
- public int MIN_Hori, MAX_Hori, MIN_Vert, MAX_Vert;
- public int horizonalSize, verticalSize;
- public Vector2 MAX_POSITION;
- public Vector2 MIN_POSITION;
- public int sectorVariable;
- public Vector2 amountOfSectorsPerAxis;
- public Vector2 systemSector;
- public GameObject[] starObjects;
- public Transform starContainer;
- public GameObject planetPrefab;
- public Transform planetContainer;
- int noForPlanetSpawn;
- public int maxStarsPerSector;
- public int chanceOfPlanetSpawn;
- private void Start()
- {
- SetStarSystemSize();
- SectorSize();
- noForPlanetSpawn = UnityEngine.Random.Range(1, chanceOfPlanetSpawn);
- StartCoroutine(SetUpSystemSectors());
- }
- void SetStarSystemSize()
- {
- //Create a system between 100x100 and 200x200
- horizonalSize = UnityEngine.Random.Range(MIN_Hori, MAX_Hori);
- verticalSize = UnityEngine.Random.Range(MIN_Vert, MAX_Vert);
- MAX_POSITION = new Vector2((float)horizonalSize / 2, (float)verticalSize / 2);
- MIN_POSITION = new Vector2(-((float)horizonalSize / 2), -((float)verticalSize / 2));
- }
- void SectorSize()
- {
- amountOfSectorsPerAxis.x = (int)(horizonalSize / sectorVariable);
- amountOfSectorsPerAxis.y = (int)(verticalSize / sectorVariable);
- systemSector = new Vector2(horizonalSize / amountOfSectorsPerAxis.x,
- verticalSize / amountOfSectorsPerAxis.y);
- //Debug.Log(systemQuadrant.ToString());
- }
- IEnumerator SetUpSystemSectors()
- {
- for (int i = 0; i < amountOfSectorsPerAxis.x; i++)
- {
- for (int j = 0; j < amountOfSectorsPerAxis.y; j++)
- {
- //Min position.x + (sysQuad.x * i) , (Min position.x + (sysQuad.x * i)) + sysQuad.x
- //Min position.y + (sysQuad.y * j) , (Min position.y + (sysQuad.y * j)) + sysQuad.y
- SetStarsInSector(new Vector2(MIN_POSITION.x + (systemSector.x * i),
- MIN_POSITION.y + (systemSector.y * j)),
- new Vector2((MIN_POSITION.x + (systemSector.x * i))+systemSector.x,
- (MIN_POSITION.y + (systemSector.y * j)) + systemSector.y));
- SetPlanetsInSector(new Vector2(MIN_POSITION.x + (systemSector.x * i),
- MIN_POSITION.y + (systemSector.y * j)),
- new Vector2((MIN_POSITION.x + (systemSector.x * i)) + systemSector.x,
- (MIN_POSITION.y + (systemSector.y * j)) + systemSector.y));
- }
- }
- yield return null;
- }
- void SetStarsInSector(Vector2 inSectorBoundsMIN, Vector2 inSectorBoundsMAX)
- {
- //Roll for amount
- //Plot randomly within sector bounds
- int amountOfStars = UnityEngine.Random.Range(1, maxStarsPerSector);
- for (int i = 0; i < amountOfStars; i++)
- {
- SpawnStar(new Vector2(UnityEngine.Random.Range(inSectorBoundsMIN.x, inSectorBoundsMAX.x),
- UnityEngine.Random.Range(inSectorBoundsMIN.y, inSectorBoundsMAX.y)));
- }
- }
- void SetPlanetsInSector(Vector2 inSectorBoundsMIN, Vector2 inSectorBoundsMAX)
- {
- //1 in 100 roll on planets
- int spawnRoll = UnityEngine.Random.Range(1, chanceOfPlanetSpawn);
- if (spawnRoll == noForPlanetSpawn)
- {
- SpawnPlanet(new Vector2(UnityEngine.Random.Range(inSectorBoundsMIN.x, inSectorBoundsMAX.x),
- UnityEngine.Random.Range(inSectorBoundsMIN.y, inSectorBoundsMAX.y)));
- }
- }
- void SpawnStar(Vector2 inCoord)
- {
- int starType = UnityEngine.Random.Range(0, starObjects.Length);
- GameObject.Instantiate(starObjects[starType], new Vector3(inCoord.x, inCoord.y, 1), new Quaternion(),starContainer);
- }
- void SpawnPlanet(Vector2 inCoord)
- {
- GameObject.Instantiate(planetPrefab, new Vector3(inCoord.x, inCoord.y, 1), new Quaternion(), planetContainer);
- }
- }
- //TODO
- /*
- * Mark out total area of the star system, set boundries.
- * Mark out sectors within the area
- * For each area, roll whether or not there's a planet there and an n-amount of stars.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement