GameMgr.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class GameMgr : MonoBehaviour
  5. {
  6. public static GameMgr instance;
  7. public GameObject[] levels = new GameObject[3];
  8. /// <summary>
  9. /// 城堡位置
  10. /// </summary>
  11. public GameObject playerTF;
  12. /// <summary>
  13. /// 目标数量
  14. /// </summary>
  15. private int int_targetCount = 0;
  16. /// <summary>
  17. /// 游戏倒计时
  18. /// </summary>
  19. public int int_gameTime = 120;
  20. /// <summary>
  21. /// 子弹数量
  22. /// </summary>
  23. public int int_bulletCount = 10;
  24. /// <summary>
  25. /// 气球飞行速度
  26. /// </summary>
  27. //public int int_balloonFlySpeed = 2;
  28. /// <summary>
  29. /// 准备时间
  30. /// </summary>
  31. int readyTime = 3;
  32. public Dictionary<int, BalloonCtrl> dic_shootTarget = new Dictionary<int, BalloonCtrl>();
  33. private void Awake()
  34. {
  35. instance = this;
  36. if (playerTF == null)
  37. {
  38. playerTF = GameObject.Find("Player");
  39. }
  40. }
  41. // Start is called before the first frame update
  42. void Start()
  43. {
  44. Instantiate(levels[GameInstantiateData.Instance.int_levelIdx],transform);
  45. InitGame();
  46. }
  47. /// <summary>
  48. /// 初始化游戏
  49. /// </summary>
  50. public void InitGame()
  51. {
  52. //获取气球
  53. GameObject[] gos = GameObject.FindGameObjectsWithTag("balloon");
  54. int_targetCount = gos.Length;
  55. for (int i = 0; i < int_targetCount; i++)
  56. {
  57. dic_shootTarget[gos[i].GetInstanceID()] = gos[i].GetComponent<BalloonCtrl>();
  58. gos[i].SetActive(true);
  59. }
  60. //int_targetCount = dic_shootTarget.Count;
  61. //foreach (var item in dic_shootTarget)
  62. //{
  63. // item.Value.gameObject.SetActive(true);
  64. //}
  65. StartCoroutine(ReadyGame());
  66. }
  67. /// <summary>
  68. /// 准备游戏
  69. /// </summary>
  70. /// <returns></returns>
  71. IEnumerator ReadyGame()
  72. {
  73. for (int i = readyTime; i > 0; i--)
  74. {
  75. PinBallUIManager.instance.ShowUI(new object[] { "ReadyView", i });
  76. yield return new WaitForSeconds(1);
  77. }
  78. PinBallUIManager.instance.CloseUI(new object[] { "ReadyView"});
  79. StartGame();
  80. }
  81. /// <summary>
  82. /// 开始游戏
  83. /// </summary>
  84. private void StartGame()
  85. {
  86. LauncherCtrl.instance.ReadyShoot(int_bulletCount);
  87. foreach (var item in dic_shootTarget)
  88. {
  89. item.Value.StartFly();
  90. }
  91. isGameOver = false;
  92. //打开游戏UI,传值 - 开始倒计时,游戏倒计时时间,子弹数量,目标数量
  93. PinBallUIManager.instance.ShowUI(new object[] { "GameView", true , int_gameTime , int_bulletCount, int_targetCount });
  94. }
  95. /// <summary>
  96. /// 增加子弹
  97. /// </summary>
  98. public void AddBulletCount()
  99. {
  100. LauncherCtrl.instance.SetArrowCount(int_bulletCount);
  101. PinBallUIManager.instance.BroadcastUI(new object[] { "GameView", true, 2, int_bulletCount });
  102. }
  103. /// <summary>
  104. /// 受击目标
  105. /// </summary>
  106. /// <param name="targetIdx"></param>
  107. public void HitTarget(int targetIdx)
  108. {
  109. if (dic_shootTarget.TryGetValue(targetIdx, out BalloonCtrl go))
  110. {
  111. if (go.gameObject.activeSelf)
  112. {
  113. go.gameObject.SetActive(false);
  114. go.StopFly();
  115. //目标数量
  116. int_targetCount--;
  117. PinBallUIManager.instance.BroadcastUI(new object[] { "GameView", true, 1, int_targetCount });
  118. if (int_targetCount <= 0)
  119. {
  120. print("游戏结束");
  121. GameOver();
  122. }
  123. }
  124. }
  125. }
  126. private bool isGameOver;
  127. /// <summary>
  128. /// 游戏结束
  129. /// </summary>
  130. public void GameOver()
  131. {
  132. if (isGameOver)
  133. {
  134. return;
  135. }
  136. isGameOver = true;
  137. PinBallUIManager.instance.BroadcastUI(new object[] { "GameView", false});
  138. LauncherCtrl.instance.EndShoot();
  139. bool result = int_targetCount <= 0;
  140. PinBallUIManager.instance.ShowUI(new object[] { "GameOverView", result });
  141. }
  142. public void RotateCastle(int value)
  143. {
  144. isRotate = true;
  145. orientation = value;
  146. if (value > 0)
  147. {
  148. if (rotateNum == 360)
  149. {
  150. rotateNum = 0;
  151. }
  152. rotateNum = (rotateNum + 90) % 360;
  153. }
  154. else
  155. {
  156. if (rotateNum == 0)
  157. {
  158. rotateNum = 360;
  159. }
  160. rotateNum = (rotateNum - 90) % 360;
  161. }
  162. }
  163. bool isRotate;
  164. float startNum;
  165. float rotateNum;
  166. /// <summary>旋转方向 </summary>
  167. public int orientation = 0;
  168. // Update is called once per frame
  169. void Update()
  170. {
  171. if (Input.GetKeyUp(KeyCode.A))
  172. {
  173. LauncherCtrl.instance.ReadyShoot();
  174. }
  175. if (Input.GetKeyUp(KeyCode.Q))
  176. {
  177. RotateCastle(1);
  178. }
  179. if (Input.GetKeyUp(KeyCode.E))
  180. {
  181. RotateCastle(-1);
  182. }
  183. }
  184. private void FixedUpdate()
  185. {
  186. if (isRotate)
  187. {
  188. CastleRotate();
  189. //startNum 0~359
  190. //startNum = castleTF.transform.localEulerAngles.y;
  191. //if (Mathf.Abs(startNum - rotateNum) < 1)
  192. //{
  193. // // Debug.LogError("结束旋转:" + startNum);
  194. // isRotate = false;
  195. //}
  196. //else
  197. //{
  198. // castleTF.transform.RotateAround(transform.position, Vector3.up, 100 * 0.02f * orientation);
  199. //}
  200. }
  201. }
  202. void CastleRotate()
  203. {
  204. //startNum 0~359
  205. startNum = playerTF.transform.localEulerAngles.y;
  206. if (Mathf.Abs( startNum - rotateNum) < 1)
  207. {
  208. // print("结束旋转:" + startNum);
  209. isRotate = false;
  210. }
  211. else
  212. {
  213. playerTF.transform.RotateAround(transform.position, Vector3.up, 100 * Time.deltaTime * orientation);
  214. }
  215. }
  216. }