| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class GameMgr : MonoBehaviour
- {
- public static GameMgr instance;
- public GameObject[] levels = new GameObject[3];
- /// <summary>
- /// 城堡位置
- /// </summary>
- public GameObject playerTF;
- /// <summary>
- /// 目标数量
- /// </summary>
- private int int_targetCount = 0;
- /// <summary>
- /// 游戏倒计时
- /// </summary>
- public int int_gameTime = 120;
- /// <summary>
- /// 子弹数量
- /// </summary>
- public int int_bulletCount = 10;
- /// <summary>
- /// 气球飞行速度
- /// </summary>
- //public int int_balloonFlySpeed = 2;
- /// <summary>
- /// 准备时间
- /// </summary>
- int readyTime = 3;
- public Dictionary<int, BalloonCtrl> dic_shootTarget = new Dictionary<int, BalloonCtrl>();
- 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();
- }
- /// <summary>
- /// 初始化游戏
- /// </summary>
- 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<BalloonCtrl>();
- gos[i].SetActive(true);
- }
- //int_targetCount = dic_shootTarget.Count;
- //foreach (var item in dic_shootTarget)
- //{
- // item.Value.gameObject.SetActive(true);
- //}
- StartCoroutine(ReadyGame());
- }
- /// <summary>
- /// 准备游戏
- /// </summary>
- /// <returns></returns>
- 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();
- }
- /// <summary>
- /// 开始游戏
- /// </summary>
- 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 });
- }
- /// <summary>
- /// 增加子弹
- /// </summary>
- public void AddBulletCount()
- {
- LauncherCtrl.instance.SetArrowCount(int_bulletCount);
- PinBallUIManager.instance.BroadcastUI(new object[] { "GameView", true, 2, int_bulletCount });
- }
- /// <summary>
- /// 受击目标
- /// </summary>
- /// <param name="targetIdx"></param>
- 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;
- /// <summary>
- /// 游戏结束
- /// </summary>
- 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;
- /// <summary>旋转方向 </summary>
- 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);
- }
- }
- }
|