Advertisement
shadowplaycoding

P013_Galaxy

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