Advertisement
shadowplaycoding

P012_Galaxy

Apr 29th, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.87 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.     public float minDistBetweenStars;
  16.  
  17.     public string[] availablePlanetTypes = { "Barren", "Terran", "Gas Giant" };
  18.  
  19.     public Dictionary<Star, GameObject> starToObjectMap {get; protected set;}
  20.  
  21.     public static Galaxy GalaxyInstance;
  22.  
  23.     public bool galaxyView { get; set; }
  24.  
  25.     public GameObject selectionIcon;
  26.  
  27.     void OnEnable()
  28.     {
  29.         GalaxyInstance = this;
  30.     }
  31.  
  32.     // Use this for initialization
  33.     void Start () {
  34.  
  35.         SanityChecks();
  36.         CreateSelectionIcon();
  37.         //CreateGalaxy();
  38.         CreateSpiralGalaxy();        
  39.    
  40.     }
  41.  
  42.     // This method checks game logic to make sure things are correct
  43.     // before the galaxy is created
  44.     void SanityChecks()
  45.     {
  46.         if (minimumRadius > maximumRadius)
  47.         {
  48.             int tempValue = maximumRadius;
  49.             maximumRadius = minimumRadius;
  50.             minimumRadius = tempValue;
  51.         }
  52.     }
  53.  
  54.     // This method creates all the planet data for a star
  55.     void CreatePlanetData(Star star)
  56.     {
  57.         for (int i = 0; i < star.numberOfPlanets; i++)
  58.         {
  59.             string name = star.starName + " " + (star.planetList.Count + 1).ToString();
  60.  
  61.             int random = Random.Range(1, 100);
  62.             string type = "";
  63.            
  64.             if (random < 40)
  65.             {
  66.                 type = availablePlanetTypes[0];
  67.             }
  68.             else if (40 <= random && random < 50)
  69.             {
  70.                 type = availablePlanetTypes[1];
  71.             }
  72.             else
  73.             {
  74.                 type = availablePlanetTypes[2];
  75.             }
  76.  
  77.             Planet planetData = new Planet(name, type);
  78.             //Debug.Log(planetData.planetName + " " + planetData.planetType );
  79.  
  80.             star.planetList.Add(planetData);
  81.  
  82.         }
  83.     }
  84.  
  85.     public Star ReturnStarFromGameObject(GameObject go)
  86.     {
  87.         if (starToObjectMap.ContainsValue(go))
  88.         {
  89.             int index = starToObjectMap.Values.ToList().IndexOf(go);
  90.             Star star = starToObjectMap.Keys.ToList()[index];
  91.  
  92.             return star;
  93.         }
  94.         else
  95.         {
  96.             return null;
  97.         }
  98.     }
  99.  
  100.     // This method creates a galaxy of stars and planet information.
  101.     public void CreateGalaxy()
  102.     {
  103.         starToObjectMap = new Dictionary<Star, GameObject>();
  104.  
  105.         Random.InitState(seedNumber);
  106.  
  107.         galaxyView = true;
  108.  
  109.         int failCount = 0;
  110.  
  111.         for (int i = 0; i < numberOfStars; i++)
  112.         {
  113.  
  114.             Star starData = new Star("Star" + i, Random.Range(1, 10));
  115.             //Debug.Log("Created " + starData.starName + " with " + starData.numberOfPlanets + " planets");
  116.             CreatePlanetData(starData);
  117.  
  118.             Vector3 cartPosition = PositionMath.RandomPosition(minimumRadius, maximumRadius);
  119.  
  120.             Collider[] positionCollider = Physics.OverlapSphere(cartPosition, minDistBetweenStars);
  121.  
  122.             if (positionCollider.Length == 0)
  123.             {
  124.                 GameObject starGO = SpaceObjects.CreateSphereObject(starData.starName, cartPosition, this.transform);
  125.                 starToObjectMap.Add(starData, starGO);
  126.                 failCount = 0;
  127.             }
  128.             else
  129.             {
  130.                 i--;
  131.                 failCount++;
  132.             }
  133.  
  134.             if (failCount > numberOfStars)
  135.             {
  136.                 Debug.LogError("Could not fit all the stars in the galaxy. Distance between stars too big!");
  137.                 break;
  138.             }
  139.         }
  140.     }
  141.  
  142.     public void CreateSpiralGalaxy()
  143.     {
  144.         Random.InitState(seedNumber);
  145.  
  146.         float starsPerArm = numberOfStars / numberOfArms;
  147.         int starsPerArmRounded = Mathf.RoundToInt(starsPerArm);
  148.         int difference = numberOfStars - (starsPerArmRounded * numberOfArms);
  149.  
  150.         for (int i = 0; i < numberOfArms; i++)
  151.         {
  152.             for (int j = 0; j < starsPerArmRounded; j++)
  153.             {
  154.                 Star starData = new Star("Star " + (j + (starsPerArm * i)), Random.Range(1, 10));
  155.                 float armAngle = (((Mathf.PI * 2f) / numberOfArms) * i);
  156.                 float starAngle = (((Mathf.PI * 2f) / starsPerArmRounded) * j);
  157.  
  158.                 float angle = PositionMath.SpiralAngle(armAngle, starAngle) + Random.Range(-Mathf.PI / (2 * numberOfArms), Mathf.PI / (2 * numberOfArms));
  159.                 float distance = (4 * numberOfArms) * Mathf.Sqrt(j + 1) + minimumRadius;
  160.  
  161.                 Vector3 cartPos = PositionMath.PolarToCart(distance, angle);
  162.                 SpaceObjects.CreateSphereObject(starData.starName, cartPos, this.transform);
  163.  
  164.             }
  165.         }
  166.     }
  167.  
  168.     public void DestroyGalaxy()
  169.     {
  170.         while (transform.childCount > 0)
  171.         {
  172.             Transform go = transform.GetChild(0);
  173.             go.SetParent(null);
  174.             Destroy(go.gameObject);
  175.         }
  176.  
  177.     }
  178.  
  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.     public void MoveSelectionIcon(RaycastHit hit)
  187.     {
  188.         selectionIcon.SetActive(true);
  189.         selectionIcon.transform.position = hit.transform.position;
  190.         selectionIcon.transform.rotation = CameraController.currentAngle;
  191.     }
  192.  
  193.    /*
  194.    Copyright Shadowplay Coding 2017 - see www.shadowplaycoding.com for licensing details
  195.    Removing this comment forfits any rights given to the user under licensing.
  196.    */
  197.  
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement