Advertisement
shadowplaycoding

P014_Galaxy

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