Advertisement
shadowplaycoding

P005_Galaxy

Apr 29th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.67 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. public class Galaxy : MonoBehaviour {
  7.  
  8.     // TODO: Have these values import from user settings
  9.     public int numberOfStars = 300;
  10.     public int minimumRadius = 0;
  11.     public int maximumRadius = 100;
  12.     public int seedNumber = 100;
  13.  
  14.     public float minDistBetweenStars;
  15.  
  16.     public string[] availablePlanetTypes = { "Barren", "Terran", "Gas Giant" };
  17.  
  18.     public Dictionary<Star, GameObject> starToObjectMap {get; protected set;}
  19.  
  20.     public static Galaxy GalaxyInstance;
  21.  
  22.     void OnEnable()
  23.     {
  24.         GalaxyInstance = this;
  25.     }
  26.  
  27.     // Use this for initialization
  28.     void Start () {
  29.  
  30.         SanityChecks();
  31.  
  32.         starToObjectMap = new Dictionary<Star, GameObject>();
  33.  
  34.         Random.InitState(seedNumber);
  35.  
  36.         int failCount = 0;
  37.  
  38.         for (int i = 0; i < numberOfStars; i++)
  39.         {
  40.  
  41.             Star starData = new Star("Star" + i, Random.Range(1, 10));
  42.             //Debug.Log("Created " + starData.starName + " with " + starData.numberOfPlanets + " planets");
  43.             CreatePlanetData(starData);
  44.  
  45.             Vector3 cartPosition = RandomPosition();
  46.  
  47.             Collider[] positionCollider = Physics.OverlapSphere(cartPosition, minDistBetweenStars);
  48.  
  49.             if (positionCollider.Length == 0)
  50.             {
  51.                 GameObject starGO = CreateSphereObject(starData, cartPosition);
  52.                 starToObjectMap.Add(starData, starGO);
  53.                 failCount = 0;
  54.             }
  55.             else
  56.             {
  57.                 i--;
  58.                 failCount++;
  59.             }
  60.  
  61.             if (failCount > numberOfStars)
  62.             {
  63.                 Debug.LogError("Could not fit all the stars in the galaxy. Distance between stars too big!");
  64.                 break;
  65.             }
  66.         }
  67.    
  68.     }
  69.  
  70.     // This method checks game logic to make sure things are correct
  71.     // before the galaxy is created
  72.     void SanityChecks()
  73.     {
  74.         if (minimumRadius > maximumRadius)
  75.         {
  76.             int tempValue = maximumRadius;
  77.             maximumRadius = minimumRadius;
  78.             minimumRadius = tempValue;
  79.         }
  80.     }
  81.  
  82.     // This method creates a random polar coordinate then converts and returns it as a Cartesian coordinate
  83.     Vector3 RandomPosition() {
  84.  
  85.         float distance = Random.Range(minimumRadius, maximumRadius);
  86.         float angle = Random.Range(0, 2 * Mathf.PI);
  87.  
  88.         Vector3 cartPosition = new Vector3(distance * Mathf.Cos(angle), 0, distance * Mathf.Sin(angle));
  89.  
  90.         return cartPosition;
  91.     }
  92.  
  93.     // This method creates a sphere object using the built in sphere model in unity
  94.     GameObject CreateSphereObject(Star starData, Vector3 cartPosition)
  95.     {
  96.         GameObject starGO = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  97.         starGO.name = starData.starName;
  98.         starGO.transform.position = cartPosition;
  99.         starGO.transform.SetParent(this.transform);
  100.  
  101.         return starGO;
  102.     }
  103.  
  104.     // This method creates all the planet data for a star
  105.     void CreatePlanetData(Star star)
  106.     {
  107.         for (int i = 0; i < star.numberOfPlanets; i++)
  108.         {
  109.             string name = star.starName + (star.planetList.Count + 1).ToString();
  110.  
  111.             int random = Random.Range(1, 100);
  112.             string type = "";
  113.            
  114.             if (random < 40)
  115.             {
  116.                 type = availablePlanetTypes[0];
  117.             }
  118.             else if (40 <= random && random < 50)
  119.             {
  120.                 type = availablePlanetTypes[1];
  121.             }
  122.             else
  123.             {
  124.                 type = availablePlanetTypes[2];
  125.             }
  126.  
  127.             Planet planetData = new Planet(name, type);
  128.             //Debug.Log(planetData.planetName + " " + planetData.planetType );
  129.  
  130.             star.planetList.Add(planetData);
  131.  
  132.         }
  133.     }
  134.  
  135.     public Star ReturnStarFromGameObject(GameObject go)
  136.     {
  137.         if (starToObjectMap.ContainsValue(go))
  138.         {
  139.             int index = starToObjectMap.Values.ToList().IndexOf(go);
  140.             Star star = starToObjectMap.Keys.ToList()[index];
  141.  
  142.             return star;
  143.         }
  144.         else
  145.         {
  146.             return null;
  147.         }
  148.     }
  149.  
  150.     public void DestroyGalaxy()
  151.     {
  152.         while (transform.childCount > 0)
  153.         {
  154.             Transform go = transform.GetChild(0);
  155.             go.SetParent(null);
  156.             Destroy(go.gameObject);
  157.         }
  158.  
  159.     }
  160.  
  161.     /*
  162.    Copyright Shadowplay Coding 2017 - see www.shadowplaycoding.com for licensing details
  163.    Removing this comment forfits any rights given to the user under licensing.
  164.    */
  165.  
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement