Advertisement
shadowplaycoding

Galaxy_BeforePart6

Mar 7th, 2017
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.92 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 = PositionMath.RandomPosition(minimumRadius, maximumRadius);
  46.  
  47.             Collider[] positionCollider = Physics.OverlapSphere(cartPosition, minDistBetweenStars);
  48.  
  49.             if (positionCollider.Length == 0)
  50.             {
  51.                 GameObject starGO = SpaceObjects.CreateSphereObject(starData.starName, cartPosition, this.transform);
  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 all the planet data for a star
  83.     void CreatePlanetData(Star star)
  84.     {
  85.         for (int i = 0; i < star.numberOfPlanets; i++)
  86.         {
  87.             string name = star.starName + (star.planetList.Count + 1).ToString();
  88.  
  89.             int random = Random.Range(1, 100);
  90.             string type = "";
  91.            
  92.             if (random < 40)
  93.             {
  94.                 type = availablePlanetTypes[0];
  95.             }
  96.             else if (40 <= random && random < 50)
  97.             {
  98.                 type = availablePlanetTypes[1];
  99.             }
  100.             else
  101.             {
  102.                 type = availablePlanetTypes[2];
  103.             }
  104.  
  105.             Planet planetData = new Planet(name, type);
  106.             //Debug.Log(planetData.planetName + " " + planetData.planetType );
  107.  
  108.             star.planetList.Add(planetData);
  109.  
  110.         }
  111.     }
  112.  
  113.     public Star ReturnStarFromGameObject(GameObject go)
  114.     {
  115.         if (starToObjectMap.ContainsValue(go))
  116.         {
  117.             int index = starToObjectMap.Values.ToList().IndexOf(go);
  118.             Star star = starToObjectMap.Keys.ToList()[index];
  119.  
  120.             return star;
  121.         }
  122.         else
  123.         {
  124.             return null;
  125.         }
  126.     }
  127.  
  128.     public void DestroyGalaxy()
  129.     {
  130.         while (transform.childCount > 0)
  131.         {
  132.             Transform go = transform.GetChild(0);
  133.             go.SetParent(null);
  134.             Destroy(go.gameObject);
  135.         }
  136.  
  137.     }
  138.  
  139.     /*
  140.    Copyright Shadowplay Coding 2017 - see www.shadowplaycoding.com for licensing details
  141.    Removing this comment forfits any rights given to the user under licensing.
  142.    */
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement