OverallLogics.cs 11 KB

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