GameController.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. //显示距离时候,设置是单点模式
  87. InfraredDemo._ins?.SetSinglePoint(true);
  88. DistanceSelectView distanceSelectView = transform.Find("DistanceSelectView").GetComponent<DistanceSelectView>();
  89. distanceSelectView.OnClickSelectDistance = HandleSelectDistance;
  90. }
  91. public void SetDisplayDistanceSelectView(bool active)
  92. {
  93. transform.Find("DistanceSelectView").gameObject.SetActive(active);
  94. }
  95. public void HandleSelectDistance(float distance)
  96. {
  97. //开始游戏设置成双点模式
  98. InfraredDemo._ins?.SetSinglePoint(false, ScreenPointTypeEnum.HalfScreen);
  99. SetDisplayDistanceSelectView(false);
  100. targetDistance = distance;
  101. foreach (var item in FindObjectsOfType<TargetBodyNew>()) item.SetDistance(distance);
  102. StartGame();
  103. }
  104. public void InitGame()
  105. {
  106. scores = new float[] { 0, 0 };
  107. time = 60;
  108. gameStart = false;
  109. gameOver = false;
  110. foreach (var item in FindObjectsOfType<ArrowNew2>())
  111. {
  112. if (item && item.gameObject) Destroy(item.gameObject);
  113. }
  114. foreach (var item in FindObjectsOfType<PointSignLastHitNew>())
  115. {
  116. if (item) item.Hide();
  117. }
  118. //重新调整射箭状态
  119. armBowDoublePlayer1P.readyShoot();
  120. armBowDoublePlayer2P.readyShoot();
  121. }
  122. public void StartGame()
  123. {
  124. gameStart = true;
  125. }
  126. //渲染时间和分数
  127. void RefreshRender()
  128. {
  129. doublePKGameViewScript.setTime(GetTimeStr(time));
  130. doublePKGameViewScript.setScore1P((float)System.Math.Round(scores[0], CommonConfig.ringsPrecision));
  131. doublePKGameViewScript.setScore2P((float)System.Math.Round(scores[1], CommonConfig.ringsPrecision));
  132. if (DeviceBatteryView.ins) {
  133. //1P当前电量
  134. doublePKGameViewScript.setBattery1P(DeviceBatteryView.ins.batteryValue);
  135. }
  136. if (BluetoothAim.ins&&BluetoothAim.ins.get2PBattery() > 0) {
  137. //更新2P显示电量
  138. //int battery = BluetoothAim.ins.getSmartBowHelper2P().GetBattery();
  139. doublePKGameViewScript.setBattery2P(BluetoothAim.ins.get2PBattery());
  140. }
  141. }
  142. string GetTimeStr(float time)
  143. {
  144. int seconds = (int)Mathf.Ceil(time);
  145. string str = "";
  146. int m = seconds / 60;
  147. if (m < 10)
  148. {
  149. str += 0;
  150. }
  151. str += m;
  152. str += " : ";
  153. int s = seconds % 60;
  154. if (s < 10)
  155. {
  156. str += 0;
  157. }
  158. str += s;
  159. return str;
  160. }
  161. public BowCameraDoublePlayer GetBowCameraDoublePlayer(PlayerType playerType)
  162. {
  163. Debug.Log("GetBowCameraDoublePlayer:"+ playerType);
  164. if (playerType == PlayerType.FirstPlayer)
  165. {
  166. return bowCameraDoublePlayer1P;
  167. }
  168. else
  169. {
  170. return bowCameraDoublePlayer2P;
  171. }
  172. }
  173. //获取不同用户的AimCrossHair
  174. public ArmBowDoublePlayer GetArmBowDoublePlayer(PlayerType playerType) {
  175. if (playerType == PlayerType.FirstPlayer)
  176. {
  177. return armBowDoublePlayer1P;
  178. }
  179. else {
  180. return armBowDoublePlayer2P;
  181. }
  182. }
  183. public AimCrossHair GetAimCrossHair(PlayerType playerType)
  184. {
  185. if (playerType == PlayerType.FirstPlayer)
  186. {
  187. return aimCrossHairs[0];
  188. }
  189. else
  190. {
  191. return aimCrossHairs[1];
  192. }
  193. }
  194. }