shadowplaycoding

P002_GalaxyManager

Sep 13th, 2019 (edited)
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.27 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class GalaxyManager : MonoBehaviour
  6. {
  7.  
  8.     public Galaxy Galaxy;
  9.  
  10.     // Start is called before the first frame update
  11.     void Start()
  12.     {
  13.         ValidateGalaxySettings();
  14.         CreateStarData();
  15.         PopulateRoundGalaxy();
  16.     }
  17.  
  18.     // Update is called once per frame
  19.     void Update()
  20.     {
  21.        
  22.     }
  23.  
  24.     void PopulateRoundGalaxy(){
  25.  
  26.         int failCount = 0;
  27.  
  28.         for(int i = 0; i < Galaxy.NumberOfStars; i++){
  29.            
  30.             Vector3 position = PositionMaths.RandomPolarPosition(Galaxy.MinimumRadius, Galaxy.MaximumRadius);
  31.             float rad = Galaxy.MinDistanceBetweenStars;
  32.  
  33.            
  34.             Physics.SyncTransforms();
  35.             bool collision  = Physics.CheckSphere(position, rad);
  36.            
  37.             if(!collision){
  38.                 string name = Galaxy.Stars[i].Name;
  39.                 GameObject starObject = SpaceObjects.CreateSphereObject(name, position, this.transform);
  40.                 failCount = 0;
  41.             }
  42.             else{
  43.                 failCount++;
  44.                
  45.                 if(failCount > Galaxy.NumberOfStars){
  46.                     Debug.LogError("Could not fit all the stars in the Galaxy! Try changing the settings!");
  47.                     break;
  48.                 }
  49.  
  50.                 i--;
  51.             }
  52.  
  53.  
  54.         }
  55.     }
  56.  
  57.     public void CreateStarData(){
  58.         Galaxy.Stars.Clear();
  59.  
  60.         for(int i = 0; i <  Galaxy.NumberOfStars; i++){
  61.             string name = "Star " + (i+1);
  62.             int numberOfPlanets = Random.Range(1,10);
  63.             Star star = new Star(name, numberOfPlanets);
  64.             Galaxy.Stars.Add(star);
  65.             Debug.Log("Created Star " + star.Name + " with " + star.NumberOfPlanets + " planets");
  66.         }
  67.     }
  68.  
  69.     void ValidateGalaxySettings(){
  70.         if (Galaxy.MinimumRadius > Galaxy.MaximumRadius){
  71.             int tempValue = Galaxy.MaximumRadius;
  72.             Galaxy.MaximumRadius = Galaxy.MinimumRadius;
  73.             Galaxy.MinimumRadius = tempValue;
  74.         }
  75.     }
  76. }
  77.  
  78. /*
  79. Copyright Shadowplay Coding 2021 - see www.shadowplaycoding.com for licensing details
  80. Removing this comment forfits any rights given to the user under licensing.
  81. */
Add Comment
Please, Sign In to add comment