Advertisement
shadowplaycoding

P006_Galaxy

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