Advertisement
shadowplaycoding

P030_Galaxy

Jul 20th, 2018
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.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.     public Material starOwnedMaterial;
  34.  
  35.     float percent;
  36.     float starsInCentre;
  37.     int starsInCentreRounded;
  38.  
  39.     float starsPerArm;
  40.     int starsPerArmRounded;
  41.     int difference;
  42.  
  43.     int starCount = 0;
  44.     int starOwned;
  45.  
  46.     List<string> availableStarNames;
  47.  
  48.     void OnEnable()
  49.     {
  50.         GalaxyInstance = this;
  51.     }
  52.  
  53.     // Use this for initialization
  54.     void Start () {
  55.  
  56.         SanityChecks();
  57.         CreateSelectionIcon();
  58.         //CreateGalaxy();
  59.         CreateSpiralGalaxy();        
  60.    
  61.     }
  62.  
  63.     // This method checks game logic to make sure things are correct
  64.     // before the galaxy is created
  65.     void SanityChecks()
  66.     {
  67.         if (minimumRadius > maximumRadius)
  68.         {
  69.             int tempValue = maximumRadius;
  70.             maximumRadius = minimumRadius;
  71.             minimumRadius = tempValue;
  72.         }
  73.     }
  74.  
  75.     // This method creates all the planet data for a star
  76.     void CreatePlanetData(Star star)
  77.     {
  78.         for (int i = 0; i < star.numberOfPlanets; i++)
  79.         {
  80.             string name = star.starName + " " + RomanNumerals.RomanNumeralGenerator(star.planetList.Count + 1);
  81.  
  82.             int random = Random.Range(1, 100);
  83.             string type = "";
  84.            
  85.             if (random < 40)
  86.             {
  87.                 type = availablePlanetTypes[0];
  88.             }
  89.             else if (40 <= random && random < 50)
  90.             {
  91.                 type = availablePlanetTypes[1];
  92.             }
  93.             else
  94.             {
  95.                 type = availablePlanetTypes[2];
  96.             }
  97.  
  98.             Planet planetData = new Planet(name, type);
  99.             //Debug.Log(planetData.planetName + " " + planetData.planetType );
  100.  
  101.             star.planetList.Add(planetData);
  102.  
  103.         }
  104.     }
  105.  
  106.     public Star ReturnStarFromGameObject(GameObject go)
  107.     {
  108.         if (starToObjectMap.ContainsValue(go))
  109.         {
  110.             int index = starToObjectMap.Values.ToList().IndexOf(go);
  111.             Star star = starToObjectMap.Keys.ToList()[index];
  112.  
  113.             return star;
  114.         }
  115.         else
  116.         {
  117.             return null;
  118.         }
  119.     }
  120.  
  121.     // This method creates a galaxy of stars and planet information.
  122.     public void CreateGalaxy()
  123.     {
  124.         InitializeGalaxy();
  125.  
  126.         int failCount = 0;
  127.  
  128.         for (int i = 0; i < numberOfStars; i++)
  129.         {
  130.  
  131.             Star starData = new Star("Star" + i, Random.Range(1, 10));
  132.             //Debug.Log("Created " + starData.starName + " with " + starData.numberOfPlanets + " planets");
  133.             CreatePlanetData(starData);
  134.  
  135.             Vector3 cartPosition = PositionMath.RandomPosition(minimumRadius, maximumRadius);
  136.  
  137.             bool collision = PositionMath.CheckCollisions(minDistBetweenStars, cartPosition);
  138.  
  139.             if (!collision == true)
  140.             {
  141.                 GameObject starGO = SpaceObjects.CreateSphereObject(starData.starName, cartPosition, this.transform);
  142.                 starToObjectMap.Add(starData, starGO);
  143.                 failCount = 0;
  144.             }
  145.             else
  146.             {
  147.                 i--;
  148.                 failCount++;
  149.             }
  150.  
  151.             if (failCount > numberOfStars)
  152.             {
  153.                 Debug.LogError("Could not fit all the stars in the galaxy. Distance between stars too big!");
  154.                 break;
  155.             }
  156.         }
  157.     }
  158.  
  159.     // This method creates a spiral galaxy
  160.     public void CreateSpiralGalaxy()
  161.     {
  162.         InitializeGalaxy();
  163.         GalaxyMaths();
  164.         CreateArms();
  165.         CreateCentre();
  166.  
  167.     }
  168.  
  169.     // This method destroys all children of the Galaxy Manager object
  170.     public void DestroyGalaxy()
  171.     {
  172.         while (transform.childCount > 0)
  173.         {
  174.             Transform go = transform.GetChild(0);
  175.             go.SetParent(null);
  176.             Destroy(go.gameObject);
  177.         }
  178.  
  179.         GUIManagementScript.GUIManagerInstance.namePlates.Clear();
  180.  
  181.     }
  182.  
  183.     // This method creates the selection icon
  184.     void CreateSelectionIcon()
  185.     {
  186.         selectionIcon = GameObject.Instantiate(selectionIcon);
  187.         selectionIcon.transform.localScale = selectionIcon.transform.localScale * 2.5f;
  188.         selectionIcon.SetActive(false);
  189.     }
  190.  
  191.     // This method moves the selection icon when mouse moves over a new object
  192.     public void MoveSelectionIcon(RaycastHit hit)
  193.     {
  194.         selectionIcon.SetActive(true);
  195.         selectionIcon.transform.position = hit.transform.position;
  196.         selectionIcon.transform.rotation = CameraController.currentAngle;
  197.     }
  198.  
  199.     // This method initializes the galaxy by setting seed number, creating a new dictionary, setting galaxy view to true and setting star count to 0
  200.     void InitializeGalaxy()
  201.     {
  202.         starToObjectMap = new Dictionary<Star, GameObject>();
  203.         Random.InitState(seedNumber);
  204.         galaxyView = true;
  205.         starCount = 0;
  206.         availableStarNames = TextAssetManager.TextToList(starNames);
  207.     }
  208.  
  209.     // This method works out number of stars in arms and centre
  210.     void GalaxyMaths()
  211.     {
  212.         percent = percentageStarsCentre / 100f;
  213.         starsInCentre = percent * numberOfStars;
  214.         starsInCentreRounded = Mathf.RoundToInt(starsInCentre);
  215.         //Debug.Log(starsInCentreRounded);
  216.  
  217.         starsPerArm = (numberOfStars - starsInCentreRounded) / numberOfArms;
  218.         starsPerArmRounded = Mathf.RoundToInt(starsPerArm);
  219.         //Debug.Log(starsPerArmRounded);
  220.         difference = numberOfStars - (starsPerArmRounded * numberOfArms) - starsInCentreRounded;
  221.         //Debug.Log(difference);
  222.  
  223.         maximumRadius = Mathf.RoundToInt((6 * numberOfArms) * Mathf.Sqrt(numberOfStars / numberOfArms) + minimumRadius);
  224.  
  225.         starOwned = Random.Range(0, numberOfStars - 1);
  226.     }
  227.  
  228.     Star CreateStarData(int starCount)
  229.     {
  230.         string name;
  231.         int randomIndex;
  232.  
  233.         if (availableStarNames.Count > 0)
  234.         {
  235.             randomIndex = Random.Range(0, availableStarNames.Count - 1);
  236.             name = availableStarNames[randomIndex];
  237.             availableStarNames.RemoveAt(randomIndex);
  238.         }
  239.         else
  240.         {
  241.             name = "Star " + starCount;
  242.         }    
  243.        
  244.         Star starData = new Star(name, Random.Range(1, 10));
  245.         CreatePlanetData(starData);
  246.                
  247.         return starData;
  248.     }
  249.  
  250.     void CreateStarObject(Star starData, Vector3 cartPosition)
  251.     {
  252.         GameObject starGO = SpaceObjects.CreateSphereObject(starData.starName, cartPosition, this.transform);
  253.  
  254.         SetStarMaterial(starGO, starData);
  255.  
  256.         starToObjectMap.Add(starData, starGO);
  257.  
  258.     }
  259.  
  260.     // This method creats the galaxy arms
  261.     void CreateArms()
  262.     {
  263.         // Spawn Arms
  264.         for (int i = 0; i < numberOfArms; i++)
  265.         {
  266.             for (int j = 0; j < starsPerArmRounded; j++)
  267.             {
  268.                 float armAngle = (((Mathf.PI * 2f) / numberOfArms) * i);
  269.                 float starAngle = (((Mathf.PI * 2f) / starsPerArmRounded) * j);
  270.  
  271.                 float angle = PositionMath.SpiralAngle(armAngle, starAngle) + Random.Range(-Mathf.PI / (2 * numberOfArms), Mathf.PI / (2 * numberOfArms));
  272.                 float distance = (6 * numberOfArms) * Mathf.Sqrt(j + 1) + minimumRadius;
  273.  
  274.                 Vector3 cartPosition = PositionMath.PolarToCart(distance, angle);
  275.  
  276.                 int failCount = 0;
  277.  
  278.                 bool collision = PositionMath.CheckCollisions(minDistBetweenStars, cartPosition);
  279.  
  280.                 if (collision != true)
  281.                 {
  282.                     // Data for Star
  283.                     Star starData = CreateStarData(starCount);
  284.                     starData.starPosition = cartPosition;
  285.                     starCount++;
  286.                     SetStarOwned(starData);
  287.  
  288.                     // Object for Star
  289.                     CreateStarObject(starData, cartPosition);
  290.                     failCount = 0;
  291.  
  292.  
  293.                 }
  294.                 else
  295.                 {
  296.                     j--;
  297.                     failCount++;
  298.                 }
  299.                 if (failCount > numberOfStars)
  300.                 {
  301.                     break;
  302.                 }
  303.  
  304.             }
  305.  
  306.         }
  307.     }
  308.  
  309.     // This method creates the galaxy centre
  310.     void CreateCentre()
  311.     {
  312.         // Spawn Centre
  313.         for (int k = 0; k < (starsInCentreRounded + difference); k++)
  314.         {
  315.             Vector3 cartPosition = PositionMath.RandomPosition(minimumRadius, minimumRadius + (numberOfArms * 20));
  316.  
  317.             bool collision = PositionMath.CheckCollisions(minDistBetweenStars, cartPosition);
  318.  
  319.             int failCount = 0;
  320.  
  321.             if (collision != true)
  322.             {
  323.                 // Data for Star
  324.                 Star starData = CreateStarData(starCount);
  325.                 starData.starPosition = cartPosition;
  326.                 starCount++;
  327.                 SetStarOwned(starData);
  328.  
  329.                 // Object for Star
  330.                 CreateStarObject(starData, cartPosition);
  331.                 failCount = 0;
  332.             }
  333.             else
  334.             {
  335.                 k--;
  336.                 failCount++;
  337.             }
  338.             if (failCount > numberOfStars)
  339.             {
  340.                 break;
  341.             }
  342.  
  343.         }
  344.     }
  345.  
  346.     public void ResetGalaxy()
  347.     {
  348.         List<Star> starList = starToObjectMap.Keys.ToList<Star>();
  349.         starToObjectMap = new Dictionary<Star, GameObject>();
  350.  
  351.         for (int i = 0; i < starList.Count; i++)
  352.         {
  353.             Star star = starList[i];
  354.             GameObject starGO = SpaceObjects.CreateSphereObject(star.starName, star.starPosition, this.transform);
  355.             SetStarMaterial(starGO, star);
  356.             starToObjectMap.Add(star, starGO);
  357.         }
  358.  
  359.         GalaxyInstance.galaxyView = true;
  360.        
  361.     }
  362.  
  363.     void SetStarOwned(Star starData)
  364.     {
  365.         if (starCount == starOwned)
  366.         {
  367.             starData.starOwned = true;
  368.             starData.planetList[0].planetColonised = true;
  369.             starData.planetList[0].starBase = new StarBase();
  370.             PlayerManager.PlayerManagerInstance.ownedPlanets.Add(starData.planetList[0]);
  371.         }
  372.     }
  373.  
  374.     void SetStarMaterial(GameObject starGO, Star starData)
  375.     {
  376.         if(starData.starOwned == true)
  377.         {
  378.             starGO.GetComponent<MeshRenderer>().material = starOwnedMaterial;
  379.         }
  380.     }
  381.  
  382.  
  383.    /*
  384.    Copyright Shadowplay Coding 2017 - see www.shadowplaycoding.com for licensing details
  385.    Removing this comment forfits any rights given to the user under licensing.
  386.    */
  387.  
  388. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement