Advertisement
shadowplaycoding

P024_SolarSystem

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