Advertisement
shadowplaycoding

P028_SolarSystem

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