GameController.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using UnityEngine.UI;
  6. /**
  7. * 主要游戏控制
  8. */
  9. public enum PlayerType {
  10. FirstPlayer,
  11. SecondPlayer,
  12. }
  13. public class GameController : MonoBehaviour
  14. {
  15. public bool debugInEditor = false;
  16. [Header("1P相关参数")]
  17. [SerializeField]
  18. BowCameraDoublePlayer bowCameraDoublePlayer1P;
  19. [SerializeField]
  20. ArmBowDoublePlayer armBowDoublePlayer1P;
  21. [Header("2P相关参数")]
  22. [SerializeField]
  23. BowCameraDoublePlayer bowCameraDoublePlayer2P;
  24. [SerializeField]
  25. ArmBowDoublePlayer armBowDoublePlayer2P;
  26. [Header("双人玩家准心")]
  27. public AimCrossHair[] aimCrossHairs;
  28. [NonSerialized] public float[] scores;
  29. [NonSerialized] public float time;
  30. [NonSerialized] public bool gameStart = false;
  31. [NonSerialized] public bool gameOver = false;
  32. [NonSerialized] public float targetDistance;
  33. public GameObject arrowPrefab;
  34. public GameObject arrowSecondPrefab;
  35. public static GameController ins;
  36. DoublePKGameView doublePKGameViewScript;
  37. void Awake() {
  38. ins = this;
  39. if (Application.platform == RuntimePlatform.WindowsEditor)
  40. {
  41. //debugInEditor = true;
  42. }
  43. //BowCameraDoublePlayer playertype
  44. bowCameraDoublePlayer1P.playerType = PlayerType.FirstPlayer;
  45. bowCameraDoublePlayer2P.playerType = PlayerType.SecondPlayer;
  46. aimCrossHairs[0].playerType = PlayerType.FirstPlayer;
  47. aimCrossHairs[1].playerType = PlayerType.SecondPlayer;
  48. }
  49. void Start()
  50. {
  51. SimulateMouseController.ins?.RemoveOpenLocker("NotGame");
  52. AudioMgr.Init();
  53. InitGame();
  54. InitDistanceSelectView();
  55. SetDisplayDistanceSelectView(true);
  56. //添加游戏界面
  57. GameObject view = Resources.Load<GameObject>("DoublePKGameView");
  58. doublePKGameViewScript = Instantiate(view).GetComponent<DoublePKGameView>();
  59. }
  60. void Update()
  61. {
  62. RefreshRender();
  63. if (gameStart && !gameOver)
  64. {
  65. if (time > 0) time -= Time.deltaTime;
  66. else
  67. {
  68. time = 0;
  69. gameOver = true;
  70. //添加结算界面
  71. transform.Find("PKGameSettleView").gameObject.SetActive(true);
  72. }
  73. }
  74. }
  75. public void HitTarget(float score, int playerIndex = -1)
  76. {
  77. if (!gameStart || gameOver) return;
  78. if (playerIndex >= 0)
  79. {
  80. scores[playerIndex] += score;
  81. HitTargetNumberNew.Create(score, playerIndex);
  82. }
  83. }
  84. private void InitDistanceSelectView()
  85. {
  86. DistanceSelectView distanceSelectView = transform.Find("DistanceSelectView").GetComponent<DistanceSelectView>();
  87. distanceSelectView.OnClickSelectDistance = HandleSelectDistance;
  88. }
  89. public void SetDisplayDistanceSelectView(bool active)
  90. {
  91. transform.Find("DistanceSelectView").gameObject.SetActive(active);
  92. }
  93. public void HandleSelectDistance(float distance)
  94. {
  95. SetDisplayDistanceSelectView(false);
  96. targetDistance = distance;
  97. foreach (var item in FindObjectsOfType<TargetBodyNew>()) item.SetDistance(distance);
  98. StartGame();
  99. }
  100. public void InitGame()
  101. {
  102. scores = new float[] { 0, 0 };
  103. time = 60;
  104. gameStart = false;
  105. gameOver = false;
  106. foreach (var item in FindObjectsOfType<ArrowNew2>())
  107. {
  108. if (item && item.gameObject) Destroy(item.gameObject);
  109. }
  110. foreach (var item in FindObjectsOfType<PointSignLastHitNew>())
  111. {
  112. if (item) item.Hide();
  113. }
  114. //重新调整射箭状态
  115. armBowDoublePlayer1P.readyShoot();
  116. armBowDoublePlayer2P.readyShoot();
  117. }
  118. public void StartGame()
  119. {
  120. gameStart = true;
  121. }
  122. //渲染时间和分数
  123. void RefreshRender()
  124. {
  125. doublePKGameViewScript.setTime(GetTimeStr(time));
  126. doublePKGameViewScript.setScore1P((float)System.Math.Round(scores[0], CommonConfig.ringsPrecision));
  127. doublePKGameViewScript.setScore2P((float)System.Math.Round(scores[1], CommonConfig.ringsPrecision));
  128. if (DeviceBatteryView.ins) {
  129. //1P当前电量
  130. doublePKGameViewScript.setBattery1P(DeviceBatteryView.ins.batteryValue);
  131. }
  132. if (BluetoothAim.ins&&BluetoothAim.ins.get2PBattery() > 0) {
  133. //更新2P显示电量
  134. //int battery = BluetoothAim.ins.getSmartBowHelper2P().GetBattery();
  135. doublePKGameViewScript.setBattery2P(BluetoothAim.ins.get2PBattery());
  136. }
  137. }
  138. string GetTimeStr(float time)
  139. {
  140. int seconds = (int)Mathf.Ceil(time);
  141. string str = "";
  142. int m = seconds / 60;
  143. if (m < 10)
  144. {
  145. str += 0;
  146. }
  147. str += m;
  148. str += " : ";
  149. int s = seconds % 60;
  150. if (s < 10)
  151. {
  152. str += 0;
  153. }
  154. str += s;
  155. return str;
  156. }
  157. public BowCameraDoublePlayer GetBowCameraDoublePlayer(PlayerType playerType)
  158. {
  159. Debug.Log("GetBowCameraDoublePlayer:"+ playerType);
  160. if (playerType == PlayerType.FirstPlayer)
  161. {
  162. return bowCameraDoublePlayer1P;
  163. }
  164. else
  165. {
  166. return bowCameraDoublePlayer2P;
  167. }
  168. }
  169. //获取不同用户的AimCrossHair
  170. public ArmBowDoublePlayer GetArmBowDoublePlayer(PlayerType playerType) {
  171. if (playerType == PlayerType.FirstPlayer)
  172. {
  173. return armBowDoublePlayer1P;
  174. }
  175. else {
  176. return armBowDoublePlayer2P;
  177. }
  178. }
  179. public AimCrossHair GetAimCrossHair(PlayerType playerType)
  180. {
  181. if (playerType == PlayerType.FirstPlayer)
  182. {
  183. return aimCrossHairs[0];
  184. }
  185. else
  186. {
  187. return aimCrossHairs[1];
  188. }
  189. }
  190. }