Advertisement
shadowplaycoding

P023_SolarSystem

Jul 15th, 2017
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.88 KB | None | 0 0
  1. ?using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4.  
  5. public class SolarSystem : MonoBehaviour {
  6.  
  7.     public static SolarSystem SolarSystemInstance;
  8.  
  9.     public Button galaxyViewButton;
  10.     public Button buildShipButton;
  11.  
  12.     public Vector3 starPosition { get; set; }
  13.  
  14.     public GameObject OrbitSpritePrefab;
  15.  
  16.     FleetManager fleetManager;
  17.  
  18.     public float solarSystemZoom = 100;
  19.     float storeMaxZoom = 0;
  20.  
  21.     public Material starOwnedMaterial;
  22.     public Material planetColonisedMaterial;
  23.  
  24.     void OnEnable()
  25.     {
  26.         SolarSystemInstance = this;
  27.         galaxyViewButton.interactable = false;
  28.         buildShipButton.interactable = false;
  29.  
  30.         fleetManager = GameObject.Find("Fleets").GetComponent<FleetManager>();
  31.     }
  32.  
  33.     // Use this for initialization
  34.     void Start () {
  35.    
  36.     }
  37.    
  38.     // Update is called once per frame
  39.     void Update () {
  40.  
  41.         Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
  42.         RaycastHit hit = new RaycastHit();
  43.  
  44.         if (Physics.Raycast(mouseRay, out hit))
  45.         {
  46.             Galaxy.GalaxyInstance.MoveSelectionIcon(hit);
  47.  
  48.             if (Input.GetMouseButtonDown(0) && Galaxy.GalaxyInstance.galaxyView == true)
  49.             {
  50.                 Star star = Galaxy.GalaxyInstance.ReturnStarFromGameObject(hit.transform.gameObject);
  51.                 starPosition = hit.transform.position;
  52.                 Debug.Log("This Star is Called: " + star.starName + "\n" + "It Has " + star.numberOfPlanets + " Planets");
  53.  
  54.                 Galaxy.GalaxyInstance.DestroyGalaxy();
  55.                 CreateSolarSystem(star);
  56.             }
  57.         }
  58.         else
  59.         {
  60.             Galaxy.GalaxyInstance.selectionIcon.SetActive(false);
  61.         }
  62.  
  63.     }
  64.  
  65.     // This method creates the solar system view after a star is clicked on in the galaxy view
  66.     public void CreateSolarSystem(Star star)
  67.     {
  68.         CameraController.cameraController.ResetCamera();
  69.  
  70.         Galaxy.GalaxyInstance.selectionIcon.SetActive(false);
  71.  
  72.         Random.InitState(Galaxy.GalaxyInstance.seedNumber);
  73.  
  74.         Galaxy.GalaxyInstance.galaxyView = false;
  75.  
  76.         GameObject starGO = SpaceObjects.CreateSphereObject(star.starName, Vector3.zero, this.transform);
  77.  
  78.         if (star.starOwned == true)
  79.         {
  80.             starGO.GetComponent<MeshRenderer>().material = starOwnedMaterial;
  81.         }
  82.  
  83.         for (int i = 0; i < star.planetList.Count; i++)
  84.         {
  85.             Planet planet = star.planetList[i];
  86.  
  87.             Vector3 planetPos = PositionMath.PlanetPosition(i);
  88.  
  89.             GameObject planetGO = SpaceObjects.CreateSphereObject(planet.planetName, planetPos, this.transform);
  90.  
  91.             if (planet.planetColonised == true)
  92.             {
  93.                 planetGO.GetComponent<MeshRenderer>().material = planetColonisedMaterial;
  94.             }
  95.  
  96.             GameObject orbit = SpaceObjects.CreateOrbitPath(OrbitSpritePrefab, planet.planetName + " Orbit", i + 1, this.transform);
  97.  
  98.             if(planet.planetColonised == true && planet.starBase == null)
  99.             {
  100.                 BuildStarBase(planet, planetGO);
  101.             }
  102.             else if(planet.planetColonised == true && planet.starBase != null)
  103.             {
  104.                 CreateStarBase(planet, planetGO);
  105.             }
  106.         }
  107.  
  108.         galaxyViewButton.interactable = true;
  109.         buildShipButton.interactable = true;
  110.  
  111.         storeMaxZoom = CameraController.cameraController.maxZoom;
  112.         CameraController.cameraController.maxZoom = solarSystemZoom;
  113.  
  114.         fleetManager.EnableFleets();
  115.  
  116.     }
  117.  
  118.     public void DestroySolarSystem()
  119.     {
  120.         while (transform.childCount > 0)
  121.         {
  122.             Transform go = transform.GetChild(0);
  123.             go.SetParent(null);
  124.             Destroy(go.gameObject);
  125.         }
  126.  
  127.         CameraController.cameraController.MoveTo(starPosition);
  128.         galaxyViewButton.interactable = false;
  129.         buildShipButton.interactable = false;
  130.  
  131.         CameraController.cameraController.maxZoom = storeMaxZoom;
  132.  
  133.         GUIManagementScript.GUIManagerInstance.namePlates.Clear();
  134.  
  135.     }
  136.  
  137.     public void BuildStarBase(Planet planetData, GameObject planetGO)
  138.     {
  139.         planetData.starBase = new StarBase();
  140.  
  141.         CreateStarBase(planetData, planetGO);
  142.     }
  143.  
  144.     public void CreateStarBase(Planet planetData, GameObject planetGO)
  145.     {
  146.         GameObject starBaseGO = GameObject.CreatePrimitive(PrimitiveType.Capsule);
  147.         starBaseGO.name = planetData.planetName + " Starbase";
  148.         starBaseGO.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
  149.         starBaseGO.transform.SetParent(planetGO.transform);
  150.         starBaseGO.transform.localPosition = new Vector3(0.6f, 0.6f, 0.6f);
  151.     }
  152.  
  153.     /*
  154.    Copyright Shadowplay Coding 2017 - see www.shadowplaycoding.com for licensing details
  155.    Removing this comment forfits any rights given to the user under licensing.
  156.    */
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement