OverallLogics.cs 9.5 KB

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