Advertisement
shadowplaycoding

P004_Galaxy

Apr 29th, 2017
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.38 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.  
  100.         return starGO;
  101.     }
  102.  
  103.     // This method creates all the planet data for a star
  104.     void CreatePlanetData(Star star)
  105.     {
  106.         for (int i = 0; i < star.numberOfPlanets; i++)
  107.         {
  108.             string name = star.starName + (star.planetList.Count + 1).ToString();
  109.  
  110.             int random = Random.Range(1, 100);
  111.             string type = "";
  112.            
  113.             if (random < 40)
  114.             {
  115.                 type = availablePlanetTypes[0];
  116.             }
  117.             else if (40 <= random && random < 50)
  118.             {
  119.                 type = availablePlanetTypes[1];
  120.             }
  121.             else
  122.             {
  123.                 type = availablePlanetTypes[2];
  124.             }
  125.  
  126.             Planet planetData = new Planet(name, type);
  127.             //Debug.Log(planetData.planetName + " " + planetData.planetType );
  128.  
  129.             star.planetList.Add(planetData);
  130.  
  131.         }
  132.     }
  133.  
  134.     public Star ReturnStarFromGameObject(GameObject go)
  135.     {
  136.         if (starToObjectMap.ContainsValue(go))
  137.         {
  138.             int index = starToObjectMap.Values.ToList().IndexOf(go);
  139.             Star star = starToObjectMap.Keys.ToList()[index];
  140.  
  141.             return star;
  142.         }
  143.         else
  144.         {
  145.             return null;
  146.         }
  147.     }
  148.  
  149.     /*
  150.    Copyright Shadowplay Coding 2017 - see www.shadowplaycoding.com for licensing details
  151.    Removing this comment forfits any rights given to the user under licensing.
  152.    */
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement