using System.Collections; using System.Collections.Generic; using UnityEngine; public class GameMgr : MonoBehaviour { public static GameMgr instance; public GameObject[] levels = new GameObject[3]; /// /// 城堡位置 /// public GameObject playerTF; /// /// 目标数量 /// private int int_targetCount = 0; /// /// 游戏倒计时 /// public int int_gameTime = 120; /// /// 子弹数量 /// public int int_bulletCount = 10; /// /// 气球飞行速度 /// //public int int_balloonFlySpeed = 2; /// /// 准备时间 /// int readyTime = 3; public Dictionary dic_shootTarget = new Dictionary(); private void Awake() { instance = this; if (playerTF == null) { playerTF = GameObject.Find("Player"); } } // Start is called before the first frame update void Start() { Instantiate(levels[GameInstantiateData.Instance.int_levelIdx],transform); InitGame(); } /// /// 初始化游戏 /// public void InitGame() { //获取气球 GameObject[] gos = GameObject.FindGameObjectsWithTag("balloon"); int_targetCount = gos.Length; for (int i = 0; i < int_targetCount; i++) { dic_shootTarget[gos[i].GetInstanceID()] = gos[i].GetComponent(); gos[i].SetActive(true); } //int_targetCount = dic_shootTarget.Count; //foreach (var item in dic_shootTarget) //{ // item.Value.gameObject.SetActive(true); //} StartCoroutine(ReadyGame()); } /// /// 准备游戏 /// /// IEnumerator ReadyGame() { for (int i = readyTime; i > 0; i--) { PinBallUIManager.instance.ShowUI(new object[] { "ReadyView", i }); yield return new WaitForSeconds(1); } PinBallUIManager.instance.CloseUI(new object[] { "ReadyView"}); StartGame(); } /// /// 开始游戏 /// private void StartGame() { LauncherCtrl.instance.ReadyShoot(int_bulletCount); foreach (var item in dic_shootTarget) { item.Value.StartFly(); } isGameOver = false; //打开游戏UI,传值 - 开始倒计时,游戏倒计时时间,子弹数量,目标数量 PinBallUIManager.instance.ShowUI(new object[] { "GameView", true , int_gameTime , int_bulletCount, int_targetCount }); } /// /// 增加子弹 /// public void AddBulletCount() { LauncherCtrl.instance.SetArrowCount(int_bulletCount); PinBallUIManager.instance.BroadcastUI(new object[] { "GameView", true, 2, int_bulletCount }); } /// /// 受击目标 /// /// public void HitTarget(int targetIdx) { if (dic_shootTarget.TryGetValue(targetIdx, out BalloonCtrl go)) { if (go.gameObject.activeSelf) { go.gameObject.SetActive(false); go.StopFly(); //目标数量 int_targetCount--; PinBallUIManager.instance.BroadcastUI(new object[] { "GameView", true, 1, int_targetCount }); if (int_targetCount <= 0) { print("游戏结束"); GameOver(); } } } } private bool isGameOver; /// /// 游戏结束 /// public void GameOver() { if (isGameOver) { return; } isGameOver = true; PinBallUIManager.instance.BroadcastUI(new object[] { "GameView", false}); LauncherCtrl.instance.EndShoot(); bool result = int_targetCount <= 0; PinBallUIManager.instance.ShowUI(new object[] { "GameOverView", result }); } public void RotateCastle(int value) { isRotate = true; orientation = value; if (value > 0) { if (rotateNum == 360) { rotateNum = 0; } rotateNum = (rotateNum + 90) % 360; } else { if (rotateNum == 0) { rotateNum = 360; } rotateNum = (rotateNum - 90) % 360; } } bool isRotate; float startNum; float rotateNum; /// 旋转方向 public int orientation = 0; // Update is called once per frame void Update() { if (Input.GetKeyUp(KeyCode.A)) { LauncherCtrl.instance.ReadyShoot(); } if (Input.GetKeyUp(KeyCode.Q)) { RotateCastle(1); } if (Input.GetKeyUp(KeyCode.E)) { RotateCastle(-1); } } private void FixedUpdate() { if (isRotate) { CastleRotate(); //startNum 0~359 //startNum = castleTF.transform.localEulerAngles.y; //if (Mathf.Abs(startNum - rotateNum) < 1) //{ // // Debug.LogError("结束旋转:" + startNum); // isRotate = false; //} //else //{ // castleTF.transform.RotateAround(transform.position, Vector3.up, 100 * 0.02f * orientation); //} } } void CastleRotate() { //startNum 0~359 startNum = playerTF.transform.localEulerAngles.y; if (Mathf.Abs( startNum - rotateNum) < 1) { // print("结束旋转:" + startNum); isRotate = false; } else { playerTF.transform.RotateAround(transform.position, Vector3.up, 100 * Time.deltaTime * orientation); } } }