Advertisement
shadowplaycoding

P002_Galaxy

Apr 29th, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.01 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Galaxy : MonoBehaviour {
  5.  
  6.     // TODO: Have these values import from user settings
  7.     public int numberOfStars = 300;
  8.     public int minimumRadius = 0;
  9.     public int maximumRadius = 100;
  10.  
  11.     public float minDistBetweenStars;
  12.  
  13.     // Use this for initialization
  14.     void Start () {
  15.  
  16.         if (minimumRadius > maximumRadius)
  17.         {
  18.             int tempValue = maximumRadius;
  19.             maximumRadius = minimumRadius;
  20.             minimumRadius = tempValue;
  21.         }
  22.  
  23.         int failCount = 0;
  24.  
  25.         for (int i = 0; i < numberOfStars; i++)
  26.         {
  27.  
  28.             Star starData = new Star("Star" + i, Random.Range(1, 10));
  29.             Debug.Log("Created " + starData.starName + " with " + starData.numberOfPlanets + " planets");
  30.  
  31.             float distance = Random.Range(minimumRadius, maximumRadius);
  32.             float angle = Random.Range(0, 2 * Mathf.PI);
  33.  
  34.             Vector3 cartPosition = new Vector3(distance * Mathf.Cos(angle), 0, distance * Mathf.Sin(angle));
  35.  
  36.             Collider[] positionCollider = Physics.OverlapSphere(cartPosition, minDistBetweenStars);
  37.  
  38.             if (positionCollider.Length == 0)
  39.             {
  40.                 GameObject starGO = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  41.                 starGO.name = starData.starName;
  42.                 starGO.transform.position = cartPosition;
  43.                 failCount = 0;
  44.             }
  45.             else
  46.             {
  47.                 i--;
  48.                 failCount++;
  49.             }
  50.  
  51.             if (failCount > numberOfStars)
  52.             {
  53.                 Debug.LogError("Could not fit all the stars in the galaxy. Distance between stars too big!");
  54.                 break;
  55.             }
  56.         }
  57.    
  58.     }
  59.    
  60.     // Update is called once per frame
  61.     void Update () {
  62.    
  63.     }
  64.  
  65.     /*
  66.     Copyright Shadowplay Coding 2016 - see www.shadowplaycoding.com for licensing details
  67.     Removing this comment forfits any rights given to the user under licensing.
  68.     */
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement