Advertisement
shadowplaycoding

P011_Galaxy

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