OverallLogics.cs 11 KB

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