Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- using UnityEngine.EventSystems;
- public class GameController : MonoBehaviour {
- public Player playerPrefab;
- Player player;
- public Image blackFade;
- float fadeSmoothing;
- public Slider playerHealthSlider;
- public Slider playerAmmoSlider;
- public Camera mainCamera;
- public GameObject pauseButtons;
- public GameObject menuButtons;
- public GameObject optionButtons;
- public GameObject stageSelectButtons;
- StagesInfo[] stageInfo;
- GameObject currentStage;
- public Canvas canvas;
- public GameObject[] dropItems;
- public GameObject[] stages;
- public static GameController instance;
- Vector2 directionalInput;
- public static bool menuOn = true;
- public static bool gameIsPaused = false;
- // bool gameIsOn;
- void Awake ()
- {
- instance = this;
- mainCamera = Camera.main;
- stageInfo = new StagesInfo[stages.Length];
- }
- // Use this for initialization
- void Start () {
- EventSystem.current.SetSelectedGameObject(GameObject.Find("Menu Buttons/Start Game Button"));
- }
- // Update is called once per frame
- void Update () {
- if (!menuOn)
- {
- HandleSlideBars();
- }
- print(stageInfo[1].heartStatus);
- }
- void HandleSlideBars()
- {
- playerHealthSlider.value = player.currentHealth;
- if(player.currentWeapon != 0)
- {
- playerAmmoSlider.gameObject.SetActive(true);
- playerAmmoSlider.value = player.bulletAmmo[player.currentWeapon];
- } else
- {
- playerAmmoSlider.gameObject.SetActive(false);
- }
- }
- void InitializeGUI()
- {
- playerHealthSlider.maxValue = player.startingHealth;
- playerAmmoSlider.maxValue = 100;
- playerAmmoSlider.gameObject.SetActive(false);
- }
- public void OnStartGameButtonPress()
- {
- StartCoroutine(Fade(true, 1));
- foreach(StagesInfo element in stageInfo)
- {
- print("We in here nigga");
- element.Initialize();
- }
- StartCoroutine(StageSetup(0,2));
- }
- public void OnOptionsButtonPress()
- {
- if (!optionButtons.activeSelf)
- {
- menuButtons.SetActive(false);
- optionButtons.SetActive(true);
- EventSystem.current.SetSelectedGameObject(GameObject.Find("Option Buttons/Back Button"));
- } else
- {
- optionButtons.SetActive(false);
- menuButtons.SetActive(true);
- EventSystem.current.SetSelectedGameObject(GameObject.Find("Menu Buttons/Options Button"));
- }
- }
- public void OnGamePauseBackButtonPress()
- {
- gameIsPaused = (gameIsPaused) ? false : true;
- GameController.menuOn = gameIsPaused;
- Time.timeScale = (gameIsPaused) ? 0 : 1;
- instance.pauseButtons.SetActive(false);
- }
- public static void OnGamePause()
- {
- gameIsPaused = (gameIsPaused) ? false : true;
- GameController.menuOn = gameIsPaused;
- Time.timeScale = (gameIsPaused) ? 0 : 1;
- if (gameIsPaused)
- {
- instance.pauseButtons.SetActive(true);
- EventSystem.current.SetSelectedGameObject(GameObject.Find("Pause Buttons/Back Button"));
- } else
- {
- instance.pauseButtons.SetActive(false);
- }
- }
- public void OnExitButtonPress()
- {
- Application.Quit();
- }
- public void StageEndWrapper()
- {
- instance.StartCoroutine(instance.OnStageEnd(false));
- }
- public static void StageEndWrapper(bool completed)
- {
- instance.StartCoroutine(instance.OnStageEnd(completed));
- }
- public IEnumerator OnStageEnd(bool completed)
- {
- StageInfo endingStageInfo = currentStage.GetComponent<StageInfo>();
- menuOn = true;
- if (endingStageInfo.stageID != 0)
- {
- print("Stage ending, updating stageInfo");
- stageInfo[endingStageInfo.stageID].Update(endingStageInfo.heartStatus, endingStageInfo.armorStatus, (stageInfo[endingStageInfo.stageID].completionStatus == true)?true:completed);
- }
- yield return new WaitForSeconds(5);
- StartCoroutine(Fade(true, 1));
- yield return new WaitForSeconds(4);
- if (player != null)
- {
- Destroy(player.gameObject);
- }
- Destroy(currentStage);
- playerHealthSlider.gameObject.SetActive(false);
- OnStageSelectButtonPress();
- }
- public void StageSetupWrapper(int stageID)
- {
- StartCoroutine(Fade(true, 1));
- StartCoroutine(StageSetup(stageID, 0));
- }
- public void OnStageSelectButtonPress()
- {
- StartCoroutine(Fade(false, 1));
- menuButtons.SetActive(false);
- stageSelectButtons.SetActive(true);
- EventSystem.current.SetSelectedGameObject(GameObject.Find("Stage Selection Buttons/Stage 1 Button"));
- }
- public IEnumerator StageSetup(int stageID, int checkpoint)
- {
- yield return new WaitForSeconds(3);
- menuButtons.SetActive(false);
- menuOn = false;
- Instantiate(stages[stageID], new Vector3(0,0,-0.69f), Quaternion.identity);
- Instantiate(playerPrefab, stages[stageID].GetComponent<StageInfo>().checkpoints[checkpoint], Quaternion.identity);
- player = GameObject.FindWithTag("Player").GetComponent<Player>();
- currentStage = GameObject.FindWithTag("Stage");
- StageInfo currentStageInfo = currentStage.GetComponent<StageInfo>();
- if (currentStageInfo.stageID != 0)
- {
- print(stageInfo[currentStageInfo.stageID].heartStatus);
- currentStageInfo.Initialize(stageInfo[currentStageInfo.stageID].heartStatus, stageInfo[currentStageInfo.stageID].armorStatus, stageInfo[currentStageInfo.stageID].completionStatus);
- }
- playerHealthSlider.gameObject.SetActive(true);
- InitializeGUI();
- mainCamera.GetComponent<CameraFollow>().GetTarget(player.GetComponent<Controller2D>());
- StartCoroutine(Fade(false, 1));
- }
- IEnumerator Fade(bool fadeIn, float fadeSpeed)
- {
- float currentAlpha = blackFade.color.a;
- while(blackFade.color.a <= 1 && blackFade.color.a >= 0)
- {
- if (fadeIn)
- {
- currentAlpha += fadeSpeed * Time.deltaTime;
- } else
- {
- currentAlpha -= fadeSpeed * Time.deltaTime;
- }
- blackFade.color = new Color (0,0,0,currentAlpha);
- yield return new WaitForFixedUpdate();
- }
- blackFade.color = new Color (0,0,0,(blackFade.color.a > 1)?1:0);
- }
- struct StagesInfo
- {
- public bool heartStatus;
- public bool armorStatus;
- public bool subItemStatus;
- public bool completionStatus;
- public void Initialize()
- {
- print("It initialized");
- heartStatus = true;
- print(heartStatus);
- armorStatus = true;
- completionStatus = false;
- print(heartStatus);
- }
- public void Update(bool heartUpdate, bool armorUpdate, bool completionUpdate)
- {
- heartStatus = heartUpdate;
- armorStatus = armorUpdate;
- completionStatus = completionUpdate;
- }
- }
- public static GameObject GetDropItem()
- {
- int randomNumber = Random.Range(0,9);
- switch (randomNumber)
- {
- case 0:
- return instance.dropItems[0];
- case 1:
- return instance.dropItems[1];
- case 2:
- return instance.dropItems[2];
- case 3:
- return instance.dropItems[3];
- default:
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement