OverallLogics.cs 7.4 KB

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