OverallLogics.cs 11 KB

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