Advertisement
shadowplaycoding

P030_SolarSytem

Jul 21st, 2018
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.57 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.                     Debug.Log("Credits: " + planet.planetResources.credits + " Minerals: " + planet.planetResources.minerals + " Food: " + planet.planetResources.food);
  77.                     GUIManagementScript.GUIManagerInstance.planetPanel.SetActive(true);
  78.                     currentPlanet = planet;
  79.  
  80.                     if (planet.starBase != null)
  81.                     {
  82.                         GUIManagementScript.GUIManagerInstance.starBasePanel.SetActive(true);
  83.                     }
  84.                 }
  85.             }
  86.         }
  87.         else
  88.         {
  89.             Galaxy.GalaxyInstance.selectionIcon.SetActive(false);
  90.         }
  91.  
  92.     }
  93.  
  94.     // This method creates the solar system view after a star is clicked on in the galaxy view
  95.     public void CreateSolarSystem(Star star)
  96.     {
  97.         planetToGameObjectMap = new Dictionary<Planet, GameObject>();
  98.  
  99.         CameraController.cameraController.ResetCamera();
  100.  
  101.         Galaxy.GalaxyInstance.selectionIcon.SetActive(false);
  102.  
  103.         Random.InitState(Galaxy.GalaxyInstance.seedNumber);
  104.  
  105.         Galaxy.GalaxyInstance.galaxyView = false;
  106.  
  107.         GameObject starGO = SpaceObjects.CreateSphereObject(star.starName, Vector3.zero, this.transform);
  108.  
  109.         if (star.starOwned == true)
  110.         {
  111.             starGO.GetComponent<MeshRenderer>().material = starOwnedMaterial;
  112.         }
  113.  
  114.         for (int i = 0; i < star.planetList.Count; i++)
  115.         {
  116.             Planet planet = star.planetList[i];
  117.  
  118.             Vector3 planetPos = PositionMath.PlanetPosition(i);
  119.  
  120.             GameObject planetGO = SpaceObjects.CreateSphereObject(planet.planetName, planetPos, this.transform);
  121.  
  122.             if (planet.planetColonised == true)
  123.             {
  124.                 planetGO.GetComponent<MeshRenderer>().material = planetColonisedMaterial;
  125.             }
  126.  
  127.             GameObject orbit = SpaceObjects.CreateOrbitPath(OrbitSpritePrefab, planet.planetName + " Orbit", i + 1, this.transform);
  128.  
  129.             if(planet.planetColonised == true && planet.starBase == null)
  130.             {
  131.                 BuildStarBase(planet, planetGO);
  132.             }
  133.             else if(planet.planetColonised == true && planet.starBase != null)
  134.             {
  135.                 CreateStarBase(planet, planetGO);
  136.             }
  137.  
  138.             planetToGameObjectMap.Add(planet, planetGO);
  139.         }
  140.  
  141.         galaxyViewButton.interactable = true;
  142.         buildShipButton.interactable = true;
  143.  
  144.         storeMaxZoom = CameraController.cameraController.maxZoom;
  145.         CameraController.cameraController.maxZoom = solarSystemZoom;
  146.  
  147.         fleetManager.EnableFleets();
  148.  
  149.     }
  150.  
  151.     public void DestroySolarSystem()
  152.     {
  153.         while (transform.childCount > 0)
  154.         {
  155.             Transform go = transform.GetChild(0);
  156.             go.SetParent(null);
  157.             Destroy(go.gameObject);
  158.         }
  159.  
  160.         CameraController.cameraController.MoveTo(starPosition);
  161.         galaxyViewButton.interactable = false;
  162.         buildShipButton.interactable = false;
  163.  
  164.         CameraController.cameraController.maxZoom = storeMaxZoom;
  165.  
  166.         GUIManagementScript.GUIManagerInstance.namePlates.Clear();
  167.         GUIManagementScript.GUIManagerInstance.planetPanel.SetActive(false);
  168.         GUIManagementScript.GUIManagerInstance.starBasePanel.SetActive(false);
  169.  
  170.     }
  171.  
  172.     public void BuildStarBase(Planet planetData, GameObject planetGO)
  173.     {
  174.         planetData.starBase = new StarBase();
  175.  
  176.         CreateStarBase(planetData, planetGO);
  177.     }
  178.  
  179.     public void CreateStarBase(Planet planetData, GameObject planetGO)
  180.     {
  181.         GameObject starBaseGO = GameObject.CreatePrimitive(PrimitiveType.Capsule);
  182.         starBaseGO.name = planetData.planetName + " Starbase";
  183.         starBaseGO.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
  184.         starBaseGO.transform.SetParent(planetGO.transform);
  185.         starBaseGO.transform.localPosition = new Vector3(0.6f, 0.6f, 0.6f);
  186.     }
  187.  
  188.     public Planet ReturnPlanetFromGameObject(GameObject go)
  189.     {
  190.         if (planetToGameObjectMap.ContainsValue(go))
  191.         {
  192.             int index = planetToGameObjectMap.Values.ToList().IndexOf(go);
  193.             Planet planet = planetToGameObjectMap.Keys.ToList()[index];
  194.  
  195.             return planet;
  196.         }
  197.         else
  198.         {
  199.             return null;
  200.         }
  201.     }
  202.  
  203.     /*
  204.    Copyright Shadowplay Coding 2017 - see www.shadowplaycoding.com for licensing details
  205.    Removing this comment forfits any rights given to the user under licensing.
  206.    */
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement