OverallLogics.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. //关闭9轴按钮
  107. ResetAimBtn.gameObject.SetActive(false);
  108. if (!CommonConfig.StandaloneModeOrPlatformB)
  109. {
  110. //显示准心控制
  111. CrossHairBtn.gameObject.SetActive(true);
  112. CalibrationOffsetBtn.gameObject.SetActive(true);
  113. Image crossHairImage = CrossHairBtn.transform.Find("Image").GetComponent<Image>();
  114. bool onlyShow = JCFruitMaster.ins.GetOnlyShow();
  115. if (onlyShow)
  116. {
  117. crossHairImage.material = outlight;
  118. }
  119. else
  120. {
  121. crossHairImage.material = null;
  122. }
  123. }
  124. }
  125. // Update is called once per frame
  126. void Update()
  127. {
  128. if (bPlayingGameOverAnim)
  129. {
  130. if (TimeGameOverAnimPlayed >= GameOverFinishAnimTime)
  131. {
  132. GameOverPanel.transform.localPosition = new Vector3(0f, 0f, 0f);
  133. TimeGameOverAnimPlayed = 0f;
  134. bPlayingGameOverAnim = false;
  135. _startGameBtn.gameObject.SetActive(true);
  136. return;
  137. }
  138. TimeGameOverAnimPlayed += Time.deltaTime;
  139. if (TimeGameOverAnimPlayed < GameOverPanelEmergeTime)
  140. {
  141. GameObject stain_0 = Stains.transform.GetChild(0).gameObject;
  142. GameObject stain_1 = Stains.transform.GetChild(1).gameObject;
  143. GameObject stain_2 = Stains.transform.GetChild(2).gameObject;
  144. if (TimeGameOverAnimPlayed < StainEmergeTimes[0])
  145. {
  146. // do nothing
  147. return;
  148. }
  149. else if (TimeGameOverAnimPlayed < StainEmergeTimes[1])
  150. {
  151. if (stain_0.activeSelf == false)
  152. {
  153. stain_0.SetActive(true);
  154. GetComponent<GamingManager>().GetAudioManager().InstantiateTemprorarySound(SoundCategory.FruitSmash, Vector3.zero, 2f);
  155. }
  156. }
  157. else if (TimeGameOverAnimPlayed < StainEmergeTimes[2])
  158. {
  159. if (stain_1.activeSelf == false)
  160. {
  161. stain_1.SetActive(true);
  162. GetComponent<GamingManager>().GetAudioManager().InstantiateTemprorarySound(SoundCategory.FruitSmash, Vector3.zero, 2f);
  163. }
  164. }
  165. else if (TimeGameOverAnimPlayed < GameOverPanelEmergeTime)
  166. {
  167. if (stain_2.activeSelf == false)
  168. {
  169. stain_2.SetActive(true);
  170. GetComponent<GamingManager>().GetAudioManager().InstantiateTemprorarySound(SoundCategory.FruitSmash, Vector3.zero, 2f);
  171. }
  172. }
  173. }
  174. else
  175. {
  176. if (TimeGameOverAnimPlayed < GameOverFinishAnimTime)
  177. {
  178. float game_over_panel_y = Mathf.Lerp(1000f, 0f, (TimeGameOverAnimPlayed - GameOverPanelEmergeTime) / (GameOverFinishAnimTime - GameOverPanelEmergeTime));
  179. GameOverPanel.transform.localPosition = new Vector3(0f, game_over_panel_y, 0f);
  180. }
  181. }
  182. }
  183. }
  184. public void StartGame()
  185. {
  186. GetComponent<GamingManager>().StartGame();
  187. _startGameBtn.gameObject.SetActive(false);
  188. Logo.gameObject.SetActive(false);
  189. MenuBackground.gameObject.SetActive(false);
  190. // Reset GameOver Effect
  191. Stains.gameObject.SetActive(false);
  192. GameOverPanel.SetActive(false);
  193. }
  194. private void OnGameEnd(EndGameReason reason, int scores)
  195. {
  196. Scores.text = "Scores " + scores;
  197. if (reason == EndGameReason.GameOver)
  198. {
  199. bPlayingGameOverAnim = true;
  200. Stains.gameObject.SetActive(true);
  201. Stains.transform.GetChild(0).gameObject.SetActive(false);
  202. Stains.transform.GetChild(1).gameObject.SetActive(false);
  203. Stains.transform.GetChild(2).gameObject.SetActive(false);
  204. GameOverPanel.SetActive(true);
  205. GameOverPanel.transform.localPosition = new Vector3(0f, 1000f, 0f);
  206. }
  207. else
  208. {
  209. GameOverPanel.SetActive(true);
  210. GameOverPanel.transform.localPosition = new Vector3(0f, 0f, 0f);
  211. _startGameBtn.gameObject.SetActive(true);
  212. Logo.gameObject.SetActive(false);
  213. Stains.gameObject.SetActive(false);
  214. }
  215. GameOverInterface.OnGameOver(GameMgr.gameType);
  216. }
  217. private void OnSmartBowConnectionLost()
  218. {
  219. gameObject.GetComponent<GamingManager>().PauseGame();
  220. }
  221. private void Shutdown()
  222. {
  223. // Implementation here
  224. Debug.Log("Shutdown");
  225. UnityEngine.SceneManagement.SceneManager.LoadScene("Home", UnityEngine.SceneManagement.LoadSceneMode.Single);
  226. }
  227. private void ResetAim()
  228. {
  229. Debug.Log("Reset Aim");
  230. if (ResetAimBtn.GetComponent<LongPressMonitor>().isLongPress) return;
  231. GetComponent<SmartBowManager>().ResetAim();
  232. }
  233. private void ResetAimLongPress()
  234. {
  235. if (SB_EventSystem.ins) SB_EventSystem.ins.AwakenSimulateMouse();
  236. }
  237. }