OverallLogics.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using TMPro;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class OverallLogics : MonoBehaviour
  7. {
  8. [SerializeField] private Button _startGameBtn;
  9. [SerializeField] private GameObject GameOverPanel;
  10. [SerializeField] private GameObject Stains;
  11. [SerializeField] private TextMeshProUGUI Scores;
  12. [SerializeField] private Image Logo;
  13. [SerializeField] private Image MenuBackground;
  14. [SerializeField] private Button ShutdownBtn;
  15. [SerializeField] private Button ResetAimBtn;
  16. [SerializeField] private Button CrossHairBtn;
  17. [SerializeField] private Button CalibrationOffsetBtn;
  18. [SerializeField] Material outlight;
  19. private bool bPlayingGameOverAnim;
  20. private float TimeGameOverAnimPlayed = 0f;
  21. private readonly float[] StainEmergeTimes = new float[] { 0.2f, 0.5f, 1f };
  22. private const float GameOverPanelEmergeTime = 1.5f;
  23. private const float GameOverFinishAnimTime = 2f;
  24. private void Awake()
  25. {
  26. Debug.Assert(_startGameBtn);
  27. Debug.Assert(GameOverPanel);
  28. Debug.Assert(Stains);
  29. Debug.Assert(Scores);
  30. Debug.Assert(Logo);
  31. Debug.Assert(MenuBackground);
  32. }
  33. // Start is called before the first frame update
  34. void Start()
  35. {
  36. // ShutdownBtn.onClick.AddListener(Shutdown);
  37. if (GlobalData.MyDeviceMode == DeviceMode.Gun || BluetoothAim.ins && BluetoothAim.ins.isMainConnectToInfraredDevice())
  38. {
  39. ResetAimBtn.gameObject.SetActive(false);
  40. //获取设置值和一个存储值
  41. bool onInitOpen = InfraredDemo._ins? InfraredDemo._ins.bInitCrosshairShow():true;
  42. Debug.Log("onInitOpen:"+ onInitOpen);
  43. Image crossHairImage = CrossHairBtn.transform.Find("Image").GetComponent<Image>();
  44. crossHairImage.material = onInitOpen ? outlight : null;
  45. //StartGame 控制 AimingCross_img
  46. CrossHairBtn.onClick.AddListener(delegate ()
  47. {
  48. bool onlyShow = !JCFruitMaster.ins.GetOnlyShow();
  49. JCFruitMaster.ins.SetOnlyShow(onlyShow);
  50. //记录准心值
  51. if (InfraredDemo._ins) InfraredDemo._ins.setCrosshairValue(onlyShow);
  52. if (onlyShow)
  53. {
  54. crossHairImage.material = outlight;
  55. }
  56. else
  57. {
  58. crossHairImage.material = null;
  59. }
  60. });
  61. //校准
  62. CalibrationOffsetBtn.onClick.AddListener(delegate () {
  63. AudioMgr.ins.PlayBtn();
  64. GetComponent<SmartBowManager>().ResetAim();
  65. //ResetAim();
  66. });
  67. }
  68. else
  69. {
  70. ResetAimBtn.onClick.AddListener(ResetAim);
  71. ResetAimBtn.gameObject.AddComponent<LongPressMonitor>().onLongPress += ResetAimLongPress;
  72. }
  73. //GetComponent<SmartBowManager>().OnConnectionLost += OnSmartBowConnectionLost;
  74. _startGameBtn.onClick.AddListener(StartGame);
  75. GetComponent<GamingManager>().OnGameEnd += OnGameEnd;
  76. Screen.sleepTimeout = SleepTimeout.NeverSleep;
  77. _startGameBtn.gameObject.SetActive(true);
  78. Logo.gameObject.SetActive(true);
  79. GameOverPanel.SetActive(false);
  80. Stains.gameObject.SetActive(false);
  81. float screen_ratio = (float)Screen.width / (float)Screen.height;
  82. float background_ratio = 2300f / 1080f;
  83. float background_scale;
  84. if (screen_ratio > background_ratio)
  85. {
  86. background_scale = Screen.width / 2300f;
  87. }
  88. else
  89. {
  90. background_scale = Screen.height / 1080f;
  91. }
  92. MenuBackground.rectTransform.sizeDelta = new Vector2(background_scale * 2300f, background_scale * 1080f);
  93. }
  94. /// <summary>
  95. /// 根据GetOnlyShow 设置 outlight
  96. /// </summary>
  97. public void SetCrosshairOutLight() {
  98. if (AimHandler.ins&&AimHandler.ins.bRuning9Axis()) return;
  99. //显示准心控制
  100. CrossHairBtn.gameObject.SetActive(true);
  101. CalibrationOffsetBtn.gameObject.SetActive(true);
  102. //关闭9轴按钮
  103. ResetAimBtn.gameObject.SetActive(false);
  104. Image crossHairImage = CrossHairBtn.transform.Find("Image").GetComponent<Image>();
  105. bool onlyShow = JCFruitMaster.ins.GetOnlyShow();
  106. if (onlyShow)
  107. {
  108. crossHairImage.material = outlight;
  109. }
  110. else
  111. {
  112. crossHairImage.material = null;
  113. }
  114. }
  115. // Update is called once per frame
  116. void Update()
  117. {
  118. if (bPlayingGameOverAnim)
  119. {
  120. if (TimeGameOverAnimPlayed >= GameOverFinishAnimTime)
  121. {
  122. GameOverPanel.transform.localPosition = new Vector3(0f, 0f, 0f);
  123. TimeGameOverAnimPlayed = 0f;
  124. bPlayingGameOverAnim = false;
  125. _startGameBtn.gameObject.SetActive(true);
  126. return;
  127. }
  128. TimeGameOverAnimPlayed += Time.deltaTime;
  129. if (TimeGameOverAnimPlayed < GameOverPanelEmergeTime)
  130. {
  131. GameObject stain_0 = Stains.transform.GetChild(0).gameObject;
  132. GameObject stain_1 = Stains.transform.GetChild(1).gameObject;
  133. GameObject stain_2 = Stains.transform.GetChild(2).gameObject;
  134. if (TimeGameOverAnimPlayed < StainEmergeTimes[0])
  135. {
  136. // do nothing
  137. return;
  138. }
  139. else if (TimeGameOverAnimPlayed < StainEmergeTimes[1])
  140. {
  141. if (stain_0.activeSelf == false)
  142. {
  143. stain_0.SetActive(true);
  144. GetComponent<GamingManager>().GetAudioManager().InstantiateTemprorarySound(SoundCategory.FruitSmash, Vector3.zero, 2f);
  145. }
  146. }
  147. else if (TimeGameOverAnimPlayed < StainEmergeTimes[2])
  148. {
  149. if (stain_1.activeSelf == false)
  150. {
  151. stain_1.SetActive(true);
  152. GetComponent<GamingManager>().GetAudioManager().InstantiateTemprorarySound(SoundCategory.FruitSmash, Vector3.zero, 2f);
  153. }
  154. }
  155. else if (TimeGameOverAnimPlayed < GameOverPanelEmergeTime)
  156. {
  157. if (stain_2.activeSelf == false)
  158. {
  159. stain_2.SetActive(true);
  160. GetComponent<GamingManager>().GetAudioManager().InstantiateTemprorarySound(SoundCategory.FruitSmash, Vector3.zero, 2f);
  161. }
  162. }
  163. }
  164. else
  165. {
  166. if (TimeGameOverAnimPlayed < GameOverFinishAnimTime)
  167. {
  168. float game_over_panel_y = Mathf.Lerp(1000f, 0f, (TimeGameOverAnimPlayed - GameOverPanelEmergeTime) / (GameOverFinishAnimTime - GameOverPanelEmergeTime));
  169. GameOverPanel.transform.localPosition = new Vector3(0f, game_over_panel_y, 0f);
  170. }
  171. }
  172. }
  173. }
  174. public void StartGame()
  175. {
  176. GetComponent<GamingManager>().StartGame();
  177. _startGameBtn.gameObject.SetActive(false);
  178. Logo.gameObject.SetActive(false);
  179. MenuBackground.gameObject.SetActive(false);
  180. // Reset GameOver Effect
  181. Stains.gameObject.SetActive(false);
  182. GameOverPanel.SetActive(false);
  183. }
  184. private void OnGameEnd(EndGameReason reason, int scores)
  185. {
  186. Scores.text = "Scores " + scores;
  187. if (reason == EndGameReason.GameOver)
  188. {
  189. bPlayingGameOverAnim = true;
  190. Stains.gameObject.SetActive(true);
  191. Stains.transform.GetChild(0).gameObject.SetActive(false);
  192. Stains.transform.GetChild(1).gameObject.SetActive(false);
  193. Stains.transform.GetChild(2).gameObject.SetActive(false);
  194. GameOverPanel.SetActive(true);
  195. GameOverPanel.transform.localPosition = new Vector3(0f, 1000f, 0f);
  196. }
  197. else
  198. {
  199. GameOverPanel.SetActive(true);
  200. GameOverPanel.transform.localPosition = new Vector3(0f, 0f, 0f);
  201. _startGameBtn.gameObject.SetActive(true);
  202. Logo.gameObject.SetActive(false);
  203. Stains.gameObject.SetActive(false);
  204. }
  205. GameOverInterface.OnGameOver(GameMgr.gameType);
  206. }
  207. private void OnSmartBowConnectionLost()
  208. {
  209. gameObject.GetComponent<GamingManager>().PauseGame();
  210. }
  211. private void Shutdown()
  212. {
  213. // Implementation here
  214. Debug.Log("Shutdown");
  215. UnityEngine.SceneManagement.SceneManager.LoadScene("Home", UnityEngine.SceneManagement.LoadSceneMode.Single);
  216. }
  217. private void ResetAim()
  218. {
  219. Debug.Log("Reset Aim");
  220. if (ResetAimBtn.GetComponent<LongPressMonitor>().isLongPress) return;
  221. GetComponent<SmartBowManager>().ResetAim();
  222. }
  223. private void ResetAimLongPress()
  224. {
  225. if (SB_EventSystem.ins) SB_EventSystem.ins.AwakenSimulateMouse();
  226. }
  227. }