Advertisement
shadowplaycoding

P003_Galaxy

Apr 29th, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.63 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class Galaxy : MonoBehaviour {
  6.  
  7.     // TODO: Have these values import from user settings
  8.     public int numberOfStars = 300;
  9.     public int minimumRadius = 0;
  10.     public int maximumRadius = 100;
  11.  
  12.     public float minDistBetweenStars;
  13.  
  14.     public string[] availablePlanetTypes = { "Barren", "Terran", "Gas Giant" };
  15.  
  16.     // Use this for initialization
  17.     void Start () {
  18.  
  19.         SanityChecks();
  20.  
  21.         int failCount = 0;
  22.  
  23.         for (int i = 0; i < numberOfStars; i++)
  24.         {
  25.  
  26.             Star starData = new Star("Star" + i, Random.Range(1, 10));
  27.             //Debug.Log("Created " + starData.starName + " with " + starData.numberOfPlanets + " planets");
  28.             CreatePlanetData(starData);
  29.  
  30.             Vector3 cartPosition = RandomPosition();
  31.  
  32.             Collider[] positionCollider = Physics.OverlapSphere(cartPosition, minDistBetweenStars);
  33.  
  34.             if (positionCollider.Length == 0)
  35.             {
  36.                 GameObject starGO = CreateSphereObject(starData, cartPosition);
  37.                 failCount = 0;
  38.             }
  39.             else
  40.             {
  41.                 i--;
  42.                 failCount++;
  43.             }
  44.  
  45.             if (failCount > numberOfStars)
  46.             {
  47.                 Debug.LogError("Could not fit all the stars in the galaxy. Distance between stars too big!");
  48.                 break;
  49.             }
  50.         }
  51.    
  52.     }
  53.  
  54.     // This method checks game logic to make sure things are correct
  55.     // before the galaxy is created
  56.     void SanityChecks()
  57.     {
  58.         if (minimumRadius > maximumRadius)
  59.         {
  60.             int tempValue = maximumRadius;
  61.             maximumRadius = minimumRadius;
  62.             minimumRadius = tempValue;
  63.         }
  64.     }
  65.  
  66.     // This method creates a random polar coordinate then converts and returns it as a Cartesian coordinate
  67.     Vector3 RandomPosition() {
  68.  
  69.         float distance = Random.Range(minimumRadius, maximumRadius);
  70.         float angle = Random.Range(0, 2 * Mathf.PI);
  71.  
  72.         Vector3 cartPosition = new Vector3(distance * Mathf.Cos(angle), 0, distance * Mathf.Sin(angle));
  73.  
  74.         return cartPosition;
  75.     }
  76.  
  77.     // This method creates a sphere object using the built in sphere model in unity
  78.     GameObject CreateSphereObject(Star starData, Vector3 cartPosition)
  79.     {
  80.         GameObject starGO = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  81.         starGO.name = starData.starName;
  82.         starGO.transform.position = cartPosition;
  83.  
  84.         return starGO;
  85.     }
  86.  
  87.     // This method creates all the planet data for a star
  88.     void CreatePlanetData(Star star)
  89.     {
  90.         for (int i = 0; i < star.numberOfPlanets; i++)
  91.         {
  92.             string name = star.starName + (star.planetList.Count + 1).ToString();
  93.  
  94.             int random = Random.Range(1, 100);
  95.             string type = "";
  96.            
  97.             if (random < 40)
  98.             {
  99.                 type = availablePlanetTypes[0];
  100.             }
  101.             else if (40 <= random && random < 50)
  102.             {
  103.                 type = availablePlanetTypes[1];
  104.             }
  105.             else
  106.             {
  107.                 type = availablePlanetTypes[2];
  108.             }
  109.  
  110.             Planet planetData = new Planet(name, type);
  111.             Debug.Log(planetData.planetName + " " + planetData.planetType );
  112.  
  113.             star.planetList.Add(planetData);
  114.  
  115.         }
  116.     }
  117.  
  118.     /*
  119.    Copyright Shadowplay Coding 2016 - see www.shadowplaycoding.com for licensing details
  120.    Removing this comment forfits any rights given to the user under licensing.
  121.    */
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement