Advertisement
shadowplaycoding

P008_Galaxy

Apr 29th, 2017
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.07 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.     public bool galaxyView { get; set; }
  23.  
  24.     void OnEnable()
  25.     {
  26.         GalaxyInstance = this;
  27.     }
  28.  
  29.     // Use this for initialization
  30.     void Start () {
  31.  
  32.         SanityChecks();
  33.         CreateGalaxy();        
  34.    
  35.     }
  36.  
  37.     // This method checks game logic to make sure things are correct
  38.     // before the galaxy is created
  39.     void SanityChecks()
  40.     {
  41.         if (minimumRadius > maximumRadius)
  42.         {
  43.             int tempValue = maximumRadius;
  44.             maximumRadius = minimumRadius;
  45.             minimumRadius = tempValue;
  46.         }
  47.     }
  48.  
  49.     // This method creates all the planet data for a star
  50.     void CreatePlanetData(Star star)
  51.     {
  52.         for (int i = 0; i < star.numberOfPlanets; i++)
  53.         {
  54.             string name = star.starName + (star.planetList.Count + 1).ToString();
  55.  
  56.             int random = Random.Range(1, 100);
  57.             string type = "";
  58.            
  59.             if (random < 40)
  60.             {
  61.                 type = availablePlanetTypes[0];
  62.             }
  63.             else if (40 <= random && random < 50)
  64.             {
  65.                 type = availablePlanetTypes[1];
  66.             }
  67.             else
  68.             {
  69.                 type = availablePlanetTypes[2];
  70.             }
  71.  
  72.             Planet planetData = new Planet(name, type);
  73.             //Debug.Log(planetData.planetName + " " + planetData.planetType );
  74.  
  75.             star.planetList.Add(planetData);
  76.  
  77.         }
  78.     }
  79.  
  80.     public Star ReturnStarFromGameObject(GameObject go)
  81.     {
  82.         if (starToObjectMap.ContainsValue(go))
  83.         {
  84.             int index = starToObjectMap.Values.ToList().IndexOf(go);
  85.             Star star = starToObjectMap.Keys.ToList()[index];
  86.  
  87.             return star;
  88.         }
  89.         else
  90.         {
  91.             return null;
  92.         }
  93.     }
  94.  
  95.     public void CreateGalaxy()
  96.     {
  97.         starToObjectMap = new Dictionary<Star, GameObject>();
  98.  
  99.         Random.InitState(seedNumber);
  100.  
  101.         galaxyView = true;
  102.  
  103.         int failCount = 0;
  104.  
  105.         for (int i = 0; i < numberOfStars; i++)
  106.         {
  107.  
  108.             Star starData = new Star("Star" + i, Random.Range(1, 10));
  109.             //Debug.Log("Created " + starData.starName + " with " + starData.numberOfPlanets + " planets");
  110.             CreatePlanetData(starData);
  111.  
  112.             Vector3 cartPosition = PositionMath.RandomPosition(minimumRadius, maximumRadius);
  113.  
  114.             Collider[] positionCollider = Physics.OverlapSphere(cartPosition, minDistBetweenStars);
  115.  
  116.             if (positionCollider.Length == 0)
  117.             {
  118.                 GameObject starGO = SpaceObjects.CreateSphereObject(starData.starName, cartPosition, this.transform);
  119.                 starToObjectMap.Add(starData, starGO);
  120.                 failCount = 0;
  121.             }
  122.             else
  123.             {
  124.                 i--;
  125.                 failCount++;
  126.             }
  127.  
  128.             if (failCount > numberOfStars)
  129.             {
  130.                 Debug.LogError("Could not fit all the stars in the galaxy. Distance between stars too big!");
  131.                 break;
  132.             }
  133.         }
  134.     }
  135.  
  136.     public void DestroyGalaxy()
  137.     {
  138.         while (transform.childCount > 0)
  139.         {
  140.             Transform go = transform.GetChild(0);
  141.             go.SetParent(null);
  142.             Destroy(go.gameObject);
  143.         }
  144.  
  145.     }
  146.  
  147.    /*
  148.    Copyright Shadowplay Coding 2017 - see www.shadowplaycoding.com for licensing details
  149.    Removing this comment forfits any rights given to the user under licensing.
  150.    */
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement