GameController.cs 6.4 KB

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