Advertisement
shadowplaycoding

P020_Galaxy

Apr 29th, 2017
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.28 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 + " " + RomanNumerals.RomanNumeralGenerator(star.planetList.Count + 1);
  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.         GUIManagementScript.GUIManagerInstance.namePlates.Clear();
  177.  
  178.     }
  179.  
  180.     // This method creates the selection icon
  181.     void CreateSelectionIcon()
  182.     {
  183.         selectionIcon = GameObject.Instantiate(selectionIcon);
  184.         selectionIcon.transform.localScale = selectionIcon.transform.localScale * 2.5f;
  185.         selectionIcon.SetActive(false);
  186.     }
  187.  
  188.     // This method moves the selection icon when mouse moves over a new object
  189.     public void MoveSelectionIcon(RaycastHit hit)
  190.     {
  191.         selectionIcon.SetActive(true);
  192.         selectionIcon.transform.position = hit.transform.position;
  193.         selectionIcon.transform.rotation = CameraController.currentAngle;
  194.     }
  195.  
  196.     // This method initializes the galaxy by setting seed number, creating a new dictionary, setting galaxy view to true and setting star count to 0
  197.     void InitializeGalaxy()
  198.     {
  199.         starToObjectMap = new Dictionary<Star, GameObject>();
  200.         Random.InitState(seedNumber);
  201.         galaxyView = true;
  202.         starCount = 0;
  203.         availableStarNames = TextAssetManager.TextToList(starNames);
  204.     }
  205.  
  206.     // This method works out number of stars in arms and centre
  207.     void GalaxyMaths()
  208.     {
  209.         percent = percentageStarsCentre / 100f;
  210.         starsInCentre = percent * numberOfStars;
  211.         starsInCentreRounded = Mathf.RoundToInt(starsInCentre);
  212.         //Debug.Log(starsInCentreRounded);
  213.  
  214.         starsPerArm = (numberOfStars - starsInCentreRounded) / numberOfArms;
  215.         starsPerArmRounded = Mathf.RoundToInt(starsPerArm);
  216.         //Debug.Log(starsPerArmRounded);
  217.         difference = numberOfStars - (starsPerArmRounded * numberOfArms) - starsInCentreRounded;
  218.         //Debug.Log(difference);
  219.  
  220.         maximumRadius = Mathf.RoundToInt((6 * numberOfArms) * Mathf.Sqrt(numberOfStars / numberOfArms) + minimumRadius);
  221.     }
  222.  
  223.     Star CreateStarData(int starCount)
  224.     {
  225.         string name;
  226.         int randomIndex;
  227.  
  228.         if (availableStarNames.Count > 0)
  229.         {
  230.             randomIndex = Random.Range(0, availableStarNames.Count - 1);
  231.             name = availableStarNames[randomIndex];
  232.             availableStarNames.RemoveAt(randomIndex);
  233.         }
  234.         else
  235.         {
  236.             name = "Star " + starCount;
  237.         }    
  238.        
  239.         Star starData = new Star(name, Random.Range(1, 10));
  240.         CreatePlanetData(starData);
  241.                
  242.         return starData;
  243.     }
  244.  
  245.     void CreateStarObject(Star starData, Vector3 cartPosition)
  246.     {
  247.         GameObject starGO = SpaceObjects.CreateSphereObject(starData.starName, cartPosition, this.transform);
  248.         starToObjectMap.Add(starData, starGO);
  249.  
  250.     }
  251.  
  252.     // This method creats the galaxy arms
  253.     void CreateArms()
  254.     {
  255.         // Spawn Arms
  256.         for (int i = 0; i < numberOfArms; i++)
  257.         {
  258.             for (int j = 0; j < starsPerArmRounded; j++)
  259.             {
  260.                 float armAngle = (((Mathf.PI * 2f) / numberOfArms) * i);
  261.                 float starAngle = (((Mathf.PI * 2f) / starsPerArmRounded) * j);
  262.  
  263.                 float angle = PositionMath.SpiralAngle(armAngle, starAngle) + Random.Range(-Mathf.PI / (2 * numberOfArms), Mathf.PI / (2 * numberOfArms));
  264.                 float distance = (6 * numberOfArms) * Mathf.Sqrt(j + 1) + minimumRadius;
  265.  
  266.                 Vector3 cartPosition = PositionMath.PolarToCart(distance, angle);
  267.  
  268.                 int failCount = 0;
  269.  
  270.                 bool collision = PositionMath.CheckCollisions(minDistBetweenStars, cartPosition);
  271.  
  272.                 if (collision != true)
  273.                 {
  274.                     Star starData = CreateStarData(starCount);
  275.                     starData.starPosition = cartPosition;
  276.                     starCount++;
  277.                     CreateStarObject(starData, cartPosition);
  278.                     failCount = 0;
  279.                 }
  280.                 else
  281.                 {
  282.                     j--;
  283.                     failCount++;
  284.                 }
  285.                 if (failCount > numberOfStars)
  286.                 {
  287.                     break;
  288.                 }
  289.  
  290.             }
  291.  
  292.         }
  293.     }
  294.  
  295.     // This method creates the galaxy centre
  296.     void CreateCentre()
  297.     {
  298.         // Spawn Centre
  299.         for (int k = 0; k < (starsInCentreRounded + difference); k++)
  300.         {
  301.             Vector3 cartPosition = PositionMath.RandomPosition(minimumRadius, minimumRadius + (numberOfArms * 20));
  302.  
  303.             bool collision = PositionMath.CheckCollisions(minDistBetweenStars, cartPosition);
  304.  
  305.             int failCount = 0;
  306.  
  307.             if (collision != true)
  308.             {
  309.                 Star starData = CreateStarData(starCount);
  310.                 starData.starPosition = cartPosition;
  311.                 starCount++;
  312.                 CreateStarObject(starData, cartPosition);
  313.                 failCount = 0;
  314.             }
  315.             else
  316.             {
  317.                 k--;
  318.                 failCount++;
  319.             }
  320.             if (failCount > numberOfStars)
  321.             {
  322.                 break;
  323.             }
  324.  
  325.         }
  326.     }
  327.  
  328.     public void ResetGalaxy()
  329.     {
  330.         List<Star> starList = starToObjectMap.Keys.ToList<Star>();
  331.         starToObjectMap = new Dictionary<Star, GameObject>();
  332.  
  333.         for (int i = 0; i < starList.Count; i++)
  334.         {
  335.             Star star = starList[i];
  336.             GameObject starGO = SpaceObjects.CreateSphereObject(star.starName, star.starPosition, this.transform);
  337.             starToObjectMap.Add(star, starGO);
  338.         }
  339.  
  340.         GalaxyInstance.galaxyView = true;
  341.        
  342.     }
  343.  
  344.  
  345.    /*
  346.    Copyright Shadowplay Coding 2017 - see www.shadowplaycoding.com for licensing details
  347.    Removing this comment forfits any rights given to the user under licensing.
  348.    */
  349.  
  350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement