using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; using UnityEngine.UI; using DG.Tweening; using TMPro; public class GameController : UnitySingleton { private float cameraTransDistance = 0; private float cameraStartX; public struct Death{ public enum DeadReason { Camera , PersonFall } public DeadReason deadReason; } private Horse horse; private CameraManager cameraManager; private Person person; public bool hasGameOver = false; void Start(){ FindSth(); } void Update(){ // //当同时按下左右扳机,触发重开,仅调试用 // if(Gamepad.current != null && Gamepad.current.leftTrigger.isPressed && Gamepad.current.rightTrigger.isPressed){ // UnityEngine.SceneManagement.SceneManager.LoadScene(UnityEngine.SceneManagement.SceneManager.GetActiveScene().name); // } //如果已经游戏结束,并且按下手柄Y键,则重新开始游戏 if(hasGameOver && Gamepad.current != null && Gamepad.current.buttonNorth.isPressed){ StartCoroutine(RestartCoroutine()); //UnityEngine.SceneManagement.SceneManager.LoadScene(UnityEngine.SceneManagement.SceneManager.GetActiveScene().name); } } private IEnumerator RestartCoroutine(){ //仅防止多次触发 hasGameOver = false; //首先呼出黑幕 BlackController.Instance.Trans(); //等到屏幕全黑 yield return new WaitUntil(() => {return BlackController.Instance.AllBlack;}); //切换场景 UnityEngine.SceneManagement.SceneManager.LoadScene(UnityEngine.SceneManagement.SceneManager.GetActiveScene().name); } void FindSth(){ horse = FindObjectOfType(); cameraManager = FindObjectOfType(); person = FindObjectOfType(); cameraStartX = Camera.main.transform.position.x; } public void GameOver(Death deathInfo){ hasGameOver = true; //触发马的死亡功能 horse.Death(deathInfo); //触发人的死亡功能 person.Death(deathInfo); //告诉相机马死了 cameraManager.Death(deathInfo); StartCoroutine(UICoroutine()); } public RectTransform restartUI; public TextMeshProUGUI meter; private IEnumerator UICoroutine(){ //计算相机移动距离 cameraTransDistance = Camera.main.transform.position.x - cameraStartX; cameraTransDistance /= 5; //设定UI分数显示为cameraTransDistance //restartUI = GameObject.Find("重开").GetComponent(); meter.text = ((int)cameraTransDistance).ToString(); //等待2秒钟 yield return new WaitForSeconds(2); //UI进入页面 restartUI.DOLocalMoveX(0,1f); } }