OverallLogics.cs 8.7 KB

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