Advertisement
Guest User

My spaghetti code

a guest
Nov 13th, 2016
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.66 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using UnityEngine.EventSystems;
  5.  
  6. public class GameController : MonoBehaviour {
  7.     public Player playerPrefab;
  8.     Player player;
  9.     public Image blackFade;
  10.     float fadeSmoothing;
  11.     public Slider playerHealthSlider;
  12.     public Slider playerAmmoSlider;
  13.     public Camera mainCamera;
  14.  
  15.     public GameObject pauseButtons;
  16.     public GameObject menuButtons;
  17.     public GameObject optionButtons;
  18.     public GameObject stageSelectButtons;
  19.  
  20.     StagesInfo[] stageInfo;
  21.     GameObject currentStage;
  22.  
  23.     public Canvas canvas;
  24.     public GameObject[] dropItems;
  25.     public GameObject[] stages;
  26.     public static GameController instance;
  27.     Vector2 directionalInput;
  28.     public static bool menuOn = true;
  29.     public static bool gameIsPaused = false;
  30. //  bool gameIsOn;
  31.  
  32.  
  33.  
  34.     void Awake ()
  35.     {
  36.         instance = this;
  37.         mainCamera = Camera.main;
  38.         stageInfo = new StagesInfo[stages.Length];
  39.     }
  40.  
  41.     // Use this for initialization
  42.     void Start () {
  43.         EventSystem.current.SetSelectedGameObject(GameObject.Find("Menu Buttons/Start Game Button"));
  44.     }
  45.    
  46.     // Update is called once per frame
  47.     void Update () {
  48.         if (!menuOn)
  49.         {
  50.             HandleSlideBars();
  51.         }
  52.         print(stageInfo[1].heartStatus);
  53.     }
  54.  
  55.     void HandleSlideBars()
  56.     {
  57.         playerHealthSlider.value = player.currentHealth;
  58.         if(player.currentWeapon != 0)
  59.         {
  60.             playerAmmoSlider.gameObject.SetActive(true);
  61.             playerAmmoSlider.value = player.bulletAmmo[player.currentWeapon];
  62.  
  63.         } else
  64.         {
  65.             playerAmmoSlider.gameObject.SetActive(false);
  66.         }
  67.     }
  68.  
  69.     void InitializeGUI()
  70.     {
  71.         playerHealthSlider.maxValue = player.startingHealth;
  72.         playerAmmoSlider.maxValue = 100;
  73.         playerAmmoSlider.gameObject.SetActive(false);
  74.     }
  75.  
  76.     public void OnStartGameButtonPress()
  77.     {
  78.         StartCoroutine(Fade(true, 1));
  79.         foreach(StagesInfo element in stageInfo)
  80.         {
  81.             print("We in here nigga");
  82.             element.Initialize();
  83.         }
  84.         StartCoroutine(StageSetup(0,2));
  85.  
  86.     }
  87.  
  88.     public void OnOptionsButtonPress()
  89.     {
  90.         if (!optionButtons.activeSelf)
  91.         {
  92.             menuButtons.SetActive(false);
  93.             optionButtons.SetActive(true);
  94.             EventSystem.current.SetSelectedGameObject(GameObject.Find("Option Buttons/Back Button"));
  95.         } else
  96.         {
  97.             optionButtons.SetActive(false);
  98.             menuButtons.SetActive(true);
  99.             EventSystem.current.SetSelectedGameObject(GameObject.Find("Menu Buttons/Options Button"));
  100.         }
  101.     }
  102.  
  103.     public void OnGamePauseBackButtonPress()
  104.     {
  105.         gameIsPaused = (gameIsPaused) ? false : true;
  106.         GameController.menuOn = gameIsPaused;
  107.         Time.timeScale = (gameIsPaused) ? 0 : 1;
  108.         instance.pauseButtons.SetActive(false);
  109.     }
  110.  
  111.     public static void OnGamePause()
  112.     {
  113.         gameIsPaused = (gameIsPaused) ? false : true;
  114.         GameController.menuOn = gameIsPaused;
  115.         Time.timeScale = (gameIsPaused) ? 0 : 1;
  116.         if (gameIsPaused)
  117.         {
  118.             instance.pauseButtons.SetActive(true);
  119.             EventSystem.current.SetSelectedGameObject(GameObject.Find("Pause Buttons/Back Button"));
  120.         } else
  121.         {
  122.             instance.pauseButtons.SetActive(false);
  123.         }
  124.     }
  125.  
  126.     public void OnExitButtonPress()
  127.     {
  128.         Application.Quit();
  129.     }
  130.  
  131.     public void StageEndWrapper()
  132.     {
  133.         instance.StartCoroutine(instance.OnStageEnd(false));
  134.     }
  135.  
  136.     public static void StageEndWrapper(bool completed)
  137.     {
  138.         instance.StartCoroutine(instance.OnStageEnd(completed));
  139.     }
  140.  
  141.     public IEnumerator OnStageEnd(bool completed)
  142.     {
  143.         StageInfo endingStageInfo = currentStage.GetComponent<StageInfo>();
  144.         menuOn = true;
  145.         if (endingStageInfo.stageID != 0)
  146.         {
  147.             print("Stage ending, updating stageInfo");
  148.             stageInfo[endingStageInfo.stageID].Update(endingStageInfo.heartStatus, endingStageInfo.armorStatus, (stageInfo[endingStageInfo.stageID].completionStatus == true)?true:completed);
  149.         }
  150.         yield return new WaitForSeconds(5);
  151.         StartCoroutine(Fade(true, 1));
  152.         yield return new WaitForSeconds(4);
  153.         if (player != null)
  154.         {
  155.             Destroy(player.gameObject);
  156.         }
  157.         Destroy(currentStage);
  158.         playerHealthSlider.gameObject.SetActive(false);
  159.         OnStageSelectButtonPress();
  160.     }
  161.        
  162.  
  163.     public void StageSetupWrapper(int stageID)
  164.     {
  165.         StartCoroutine(Fade(true, 1));
  166.         StartCoroutine(StageSetup(stageID, 0));
  167.     }
  168.  
  169.     public void OnStageSelectButtonPress()
  170.     {
  171.         StartCoroutine(Fade(false, 1));
  172.         menuButtons.SetActive(false);
  173.         stageSelectButtons.SetActive(true);
  174.         EventSystem.current.SetSelectedGameObject(GameObject.Find("Stage Selection Buttons/Stage 1 Button"));
  175.     }
  176.  
  177.     public IEnumerator StageSetup(int stageID, int checkpoint)
  178.     {
  179.         yield return new WaitForSeconds(3);
  180.  
  181.         menuButtons.SetActive(false);
  182.         menuOn = false;
  183.         Instantiate(stages[stageID], new Vector3(0,0,-0.69f), Quaternion.identity);
  184.         Instantiate(playerPrefab, stages[stageID].GetComponent<StageInfo>().checkpoints[checkpoint], Quaternion.identity);
  185.         player = GameObject.FindWithTag("Player").GetComponent<Player>();
  186.         currentStage = GameObject.FindWithTag("Stage");
  187.         StageInfo currentStageInfo = currentStage.GetComponent<StageInfo>();
  188.         if (currentStageInfo.stageID != 0)
  189.         {
  190.             print(stageInfo[currentStageInfo.stageID].heartStatus);
  191.             currentStageInfo.Initialize(stageInfo[currentStageInfo.stageID].heartStatus, stageInfo[currentStageInfo.stageID].armorStatus, stageInfo[currentStageInfo.stageID].completionStatus);
  192.         }
  193.         playerHealthSlider.gameObject.SetActive(true);
  194.         InitializeGUI();
  195.         mainCamera.GetComponent<CameraFollow>().GetTarget(player.GetComponent<Controller2D>());
  196.  
  197.         StartCoroutine(Fade(false, 1));
  198.     }
  199.        
  200.     IEnumerator Fade(bool fadeIn, float fadeSpeed)
  201.     {
  202.         float currentAlpha = blackFade.color.a;
  203.         while(blackFade.color.a <= 1 && blackFade.color.a >= 0)
  204.         {
  205.             if (fadeIn)
  206.             {
  207.                 currentAlpha += fadeSpeed * Time.deltaTime;
  208.             } else
  209.             {
  210.                 currentAlpha -= fadeSpeed * Time.deltaTime;
  211.             }
  212.             blackFade.color = new Color (0,0,0,currentAlpha);
  213.  
  214.             yield return new WaitForFixedUpdate();
  215.         }
  216.         blackFade.color = new Color (0,0,0,(blackFade.color.a > 1)?1:0);
  217.     }
  218.  
  219.     struct StagesInfo
  220.     {
  221.         public bool heartStatus;
  222.         public bool armorStatus;
  223.         public bool subItemStatus;
  224.         public bool completionStatus;
  225.  
  226.         public void Initialize()
  227.         {
  228.             print("It initialized");
  229.             heartStatus = true;
  230.             print(heartStatus);
  231.             armorStatus = true;
  232.             completionStatus = false;
  233.             print(heartStatus);
  234.         }
  235.  
  236.         public void Update(bool heartUpdate, bool armorUpdate, bool completionUpdate)
  237.         {
  238.             heartStatus = heartUpdate;
  239.             armorStatus = armorUpdate;
  240.             completionStatus = completionUpdate;
  241.         }
  242.     }
  243.  
  244.     public static GameObject GetDropItem()
  245.     {
  246.         int randomNumber = Random.Range(0,9);
  247.         switch (randomNumber)
  248.         {
  249.         case 0:
  250.             return instance.dropItems[0];
  251.         case 1:
  252.             return instance.dropItems[1];
  253.         case 2:
  254.             return instance.dropItems[2];
  255.         case 3:
  256.             return instance.dropItems[3];
  257.         default:
  258.             return null;
  259.         }
  260.     }
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement