shadowplaycoding

P015_Galaxy

Apr 29th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.33 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6.  
  7. public class Galaxy : MonoBehaviour {
  8.  
  9.     // TODO: Have these values import from user settings
  10.     public int numberOfStars = 300;
  11.     public int minimumRadius = 0;
  12.     public int maximumRadius = 100;
  13.     public int seedNumber = 100;
  14.     public int numberOfArms = 2;
  15.  
  16.     [Range(0,100)]
  17.     public int percentageStarsCentre = 25;
  18.  
  19.     public float minDistBetweenStars;
  20.  
  21.     public string[] availablePlanetTypes = { "Barren", "Terran", "Gas Giant" };
  22.  
  23.     public Dictionary<Star, GameObject> starToObjectMap {get; protected set;}
  24.  
  25.     public static Galaxy GalaxyInstance;
  26.  
  27.     public bool galaxyView { get; set; }
  28.  
  29.     public GameObject selectionIcon;
  30.  
  31.     public TextAsset starNames;
  32.  
  33.     float percent;
  34.     float starsInCentre;
  35.     int starsInCentreRounded;
  36.  
  37.     float starsPerArm;
  38.     int starsPerArmRounded;
  39.     int difference;
  40.  
  41.     int starCount = 0;
  42.  
  43.     List<string> availableStarNames;
  44.  
  45.     void OnEnable()
  46.     {
  47.         GalaxyInstance = this;
  48.     }
  49.  
  50.     // Use this for initialization
  51.     void Start () {
  52.  
  53.         SanityChecks();
  54.         CreateSelectionIcon();
  55.         //CreateGalaxy();
  56.         CreateSpiralGalaxy();        
  57.    
  58.     }
  59.  
  60.     // This method checks game logic to make sure things are correct
  61.     // before the galaxy is created
  62.     void SanityChecks()
  63.     {
  64.         if (minimumRadius > maximumRadius)
  65.         {
  66.             int tempValue = maximumRadius;
  67.             maximumRadius = minimumRadius;
  68.             minimumRadius = tempValue;
  69.         }
  70.     }
  71.  
  72.     // This method creates all the planet data for a star
  73.     void CreatePlanetData(Star star)
  74.     {
  75.         for (int i = 0; i < star.numberOfPlanets; i++)
  76.         {
  77.             string name = star.starName + " " + (star.planetList.Count + 1).ToString();
  78.  
  79.             int random = Random.Range(1, 100);
  80.             string type = "";
  81.            
  82.             if (random < 40)
  83.             {
  84.                 type = availablePlanetTypes[0];
  85.             }
  86.             else if (40 <= random && random < 50)
  87.             {
  88.                 type = availablePlanetTypes[1];
  89.             }
  90.             else
  91.             {
  92.                 type = availablePlanetTypes[2];
  93.             }
  94.  
  95.             Planet planetData = new Planet(name, type);
  96.             //Debug.Log(planetData.planetName + " " + planetData.planetType );
  97.  
  98.             star.planetList.Add(planetData);
  99.  
  100.         }
  101.     }
  102.  
  103.     public Star ReturnStarFromGameObject(GameObject go)
  104.     {
  105.         if (starToObjectMap.ContainsValue(go))
  106.         {
  107.             int index = starToObjectMap.Values.ToList().IndexOf(go);
  108.             Star star = starToObjectMap.Keys.ToList()[index];
  109.  
  110.             return star;
  111.         }
  112.         else
  113.         {
  114.             return null;
  115.         }
  116.     }
  117.  
  118.     // This method creates a galaxy of stars and planet information.
  119.     public void CreateGalaxy()
  120.     {
  121.         InitializeGalaxy();
  122.  
  123.         int failCount = 0;
  124.  
  125.         for (int i = 0; i < numberOfStars; i++)
  126.         {
  127.  
  128.             Star starData = new Star("Star" + i, Random.Range(1, 10));
  129.             //Debug.Log("Created " + starData.starName + " with " + starData.numberOfPlanets + " planets");
  130.             CreatePlanetData(starData);
  131.  
  132.             Vector3 cartPosition = PositionMath.RandomPosition(minimumRadius, maximumRadius);
  133.  
  134.             bool collision = PositionMath.CheckCollisions(minDistBetweenStars, cartPosition);
  135.  
  136.             if (!collision == true)
  137.             {
  138.                 GameObject starGO = SpaceObjects.CreateSphereObject(starData.starName, cartPosition, this.transform);
  139.                 starToObjectMap.Add(starData, starGO);
  140.                 failCount = 0;
  141.             }
  142.             else
  143.             {
  144.                 i--;
  145.                 failCount++;
  146.             }
  147.  
  148.             if (failCount > numberOfStars)
  149.             {
  150.                 Debug.LogError("Could not fit all the stars in the galaxy. Distance between stars too big!");
  151.                 break;
  152.             }
  153.         }
  154.     }
  155.  
  156.     // This method creates a spiral galaxy
  157.     public void CreateSpiralGalaxy()
  158.     {
  159.         InitializeGalaxy();
  160.         GalaxyMaths();
  161.         CreateArms();
  162.         CreateCentre();
  163.  
  164.     }
  165.  
  166.     // This method destroys all children of the Galaxy Manager object
  167.     public void DestroyGalaxy()
  168.     {
  169.         while (transform.childCount > 0)
  170.         {
  171.             Transform go = transform.GetChild(0);
  172.             go.SetParent(null);
  173.             Destroy(go.gameObject);
  174.         }
  175.  
  176.     }
  177.  
  178.     // This method creates the selection icon
  179.     void CreateSelectionIcon()
  180.     {
  181.         selectionIcon = GameObject.Instantiate(selectionIcon);
  182.         selectionIcon.transform.localScale = selectionIcon.transform.localScale * 2.5f;
  183.         selectionIcon.SetActive(false);
  184.     }
  185.  
  186.     // This method moves the selection icon when mouse moves over a new object
  187.     public void MoveSelectionIcon(RaycastHit hit)
  188.     {
  189.         selectionIcon.SetActive(true);
  190.         selectionIcon.transform.position = hit.transform.position;
  191.         selectionIcon.transform.rotation = CameraController.currentAngle;
  192.     }
  193.  
  194.     // This method initializes the galaxy by setting seed number, creating a new dictionary, setting galaxy view to true and setting star count to 0
  195.     void InitializeGalaxy()
  196.     {
  197.         starToObjectMap = new Dictionary<Star, GameObject>();
  198.         Random.InitState(seedNumber);
  199.         galaxyView = true;
  200.         starCount = 0;
  201.         availableStarNames = TextAssetManager.TextToList(starNames);
  202.     }
  203.  
  204.     // This method works out number of stars in arms and centre
  205.     void GalaxyMaths()
  206.     {
  207.         percent = percentageStarsCentre / 100f;
  208.         starsInCentre = percent * numberOfStars;
  209.         starsInCentreRounded = Mathf.RoundToInt(starsInCentre);
  210.         Debug.Log(starsInCentreRounded);
  211.  
  212.         starsPerArm = (numberOfStars - starsInCentreRounded) / numberOfArms;
  213.         starsPerArmRounded = Mathf.RoundToInt(starsPerArm);
  214.         Debug.Log(starsPerArmRounded);
  215.         difference = numberOfStars - (starsPerArmRounded * numberOfArms) - starsInCentreRounded;
  216.         Debug.Log(difference);
  217.  
  218.         maximumRadius = Mathf.RoundToInt((6 * numberOfArms) * Mathf.Sqrt(numberOfStars / numberOfArms) + minimumRadius);
  219.     }
  220.  
  221.     Star CreateStarData(int starCount)
  222.     {
  223.         int randomIndex = Random.Range(0, availableStarNames.Count - 1);
  224.         Star starData = new Star(availableStarNames[randomIndex], Random.Range(1, 10));
  225.         CreatePlanetData(starData);
  226.         availableStarNames.RemoveAt(randomIndex);
  227.        
  228.         return starData;
  229.     }
  230.  
  231.     void CreateStarObject(Star starData, Vector3 cartPosition)
  232.     {
  233.         GameObject starGO = SpaceObjects.CreateSphereObject(starData.starName, cartPosition, this.transform);
  234.         starToObjectMap.Add(starData, starGO);
  235.  
  236.     }
  237.  
  238.     // This method creats the galaxy arms
  239.     void CreateArms()
  240.     {
  241.         // Spawn Arms
  242.         for (int i = 0; i < numberOfArms; i++)
  243.         {
  244.             for (int j = 0; j < starsPerArmRounded; j++)
  245.             {
  246.                 float armAngle = (((Mathf.PI * 2f) / numberOfArms) * i);
  247.                 float starAngle = (((Mathf.PI * 2f) / starsPerArmRounded) * j);
  248.  
  249.                 float angle = PositionMath.SpiralAngle(armAngle, starAngle) + Random.Range(-Mathf.PI / (2 * numberOfArms), Mathf.PI / (2 * numberOfArms));
  250.                 float distance = (6 * numberOfArms) * Mathf.Sqrt(j + 1) + minimumRadius;
  251.  
  252.                 Vector3 cartPosition = PositionMath.PolarToCart(distance, angle);
  253.  
  254.                 int failCount = 0;
  255.  
  256.                 bool collision = PositionMath.CheckCollisions(minDistBetweenStars, cartPosition);
  257.  
  258.                 if (collision != true)
  259.                 {
  260.                     Star starData = CreateStarData(starCount);
  261.                     starCount++;
  262.                     CreateStarObject(starData, cartPosition);
  263.                     failCount = 0;
  264.                 }
  265.                 else
  266.                 {
  267.                     j--;
  268.                     failCount++;
  269.                 }
  270.                 if (failCount > numberOfStars)
  271.                 {
  272.                     break;
  273.                 }
  274.  
  275.             }
  276.  
  277.         }
  278.     }
  279.  
  280.     // This method creates the galaxy centre
  281.     void CreateCentre()
  282.     {
  283.         // Spawn Centre
  284.         for (int k = 0; k < (starsInCentreRounded + difference); k++)
  285.         {
  286.             Vector3 cartPosition = PositionMath.RandomPosition(minimumRadius, minimumRadius + (numberOfArms * 20));
  287.  
  288.             bool collision = PositionMath.CheckCollisions(minDistBetweenStars, cartPosition);
  289.  
  290.             int failCount = 0;
  291.  
  292.             if (collision != true)
  293.             {
  294.                 Star starData = CreateStarData(starCount);
  295.                 starCount++;
  296.                 CreateStarObject(starData, cartPosition);
  297.                 failCount = 0;
  298.             }
  299.             else
  300.             {
  301.                 k--;
  302.                 failCount++;
  303.             }
  304.             if (failCount > numberOfStars)
  305.             {
  306.                 break;
  307.             }
  308.  
  309.         }
  310.     }
  311.    /*
  312.    Copyright Shadowplay Coding 2017 - see www.shadowplaycoding.com for licensing details
  313.    Removing this comment forfits any rights given to the user under licensing.
  314.    */
  315.  
  316. }
Add Comment
Please, Sign In to add comment