OverallLogics.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. private bool bPlayingGameOverAnim;
  17. private float TimeGameOverAnimPlayed = 0f;
  18. private readonly float[] StainEmergeTimes = new float[] { 0.2f, 0.5f, 1f };
  19. private const float GameOverPanelEmergeTime = 1.5f;
  20. private const float GameOverFinishAnimTime = 2f;
  21. private void Awake()
  22. {
  23. Debug.Assert(_startGameBtn);
  24. Debug.Assert(GameOverPanel);
  25. Debug.Assert(Stains);
  26. Debug.Assert(Scores);
  27. Debug.Assert(Logo);
  28. Debug.Assert(MenuBackground);
  29. }
  30. // Start is called before the first frame update
  31. void Start()
  32. {
  33. ShutdownBtn.onClick.AddListener(Shutdown);
  34. ResetAimBtn.onClick.AddListener(ResetAim);
  35. ResetAimBtn.gameObject.AddComponent<LongPressMonitor>().onLongPress += ResetAimLongPress;
  36. GetComponent<SmartBowManager>().OnConnectionLost += OnSmartBowConnectionLost;
  37. _startGameBtn.onClick.AddListener(StartGame);
  38. GetComponent<GamingManager>().OnGameEnd += OnGameEnd;
  39. Screen.sleepTimeout = SleepTimeout.NeverSleep;
  40. _startGameBtn.gameObject.SetActive(true);
  41. Logo.gameObject.SetActive(true);
  42. GameOverPanel.SetActive(false);
  43. Stains.gameObject.SetActive(false);
  44. float screen_ratio = (float)Screen.width / (float)Screen.height;
  45. float background_ratio = 2300f / 1080f;
  46. float background_scale;
  47. if (screen_ratio > background_ratio)
  48. {
  49. background_scale = Screen.width / 2300f;
  50. }
  51. else
  52. {
  53. background_scale = Screen.height / 1080f;
  54. }
  55. MenuBackground.rectTransform.sizeDelta = new Vector2(background_scale * 2300f, background_scale * 1080f);
  56. }
  57. // Update is called once per frame
  58. void Update()
  59. {
  60. if (bPlayingGameOverAnim)
  61. {
  62. if (TimeGameOverAnimPlayed >= GameOverFinishAnimTime)
  63. {
  64. GameOverPanel.transform.localPosition = new Vector3(0f, 0f, 0f);
  65. TimeGameOverAnimPlayed = 0f;
  66. bPlayingGameOverAnim = false;
  67. _startGameBtn.gameObject.SetActive(true);
  68. return;
  69. }
  70. TimeGameOverAnimPlayed += Time.deltaTime;
  71. if (TimeGameOverAnimPlayed < GameOverPanelEmergeTime)
  72. {
  73. GameObject stain_0 = Stains.transform.GetChild(0).gameObject;
  74. GameObject stain_1 = Stains.transform.GetChild(1).gameObject;
  75. GameObject stain_2 = Stains.transform.GetChild(2).gameObject;
  76. if (TimeGameOverAnimPlayed < StainEmergeTimes[0])
  77. {
  78. // do nothing
  79. return;
  80. }
  81. else if (TimeGameOverAnimPlayed < StainEmergeTimes[1])
  82. {
  83. if (stain_0.activeSelf == false)
  84. {
  85. stain_0.SetActive(true);
  86. GetComponent<GamingManager>().GetAudioManager().InstantiateTemprorarySound(SoundCategory.FruitSmash, Vector3.zero, 2f);
  87. }
  88. }
  89. else if (TimeGameOverAnimPlayed < StainEmergeTimes[2])
  90. {
  91. if (stain_1.activeSelf == false)
  92. {
  93. stain_1.SetActive(true);
  94. GetComponent<GamingManager>().GetAudioManager().InstantiateTemprorarySound(SoundCategory.FruitSmash, Vector3.zero, 2f);
  95. }
  96. }
  97. else if (TimeGameOverAnimPlayed < GameOverPanelEmergeTime)
  98. {
  99. if (stain_2.activeSelf == false)
  100. {
  101. stain_2.SetActive(true);
  102. GetComponent<GamingManager>().GetAudioManager().InstantiateTemprorarySound(SoundCategory.FruitSmash, Vector3.zero, 2f);
  103. }
  104. }
  105. }
  106. else
  107. {
  108. if (TimeGameOverAnimPlayed < GameOverFinishAnimTime)
  109. {
  110. float game_over_panel_y = Mathf.Lerp(1000f, 0f, (TimeGameOverAnimPlayed - GameOverPanelEmergeTime) / (GameOverFinishAnimTime - GameOverPanelEmergeTime));
  111. GameOverPanel.transform.localPosition = new Vector3(0f, game_over_panel_y, 0f);
  112. }
  113. }
  114. }
  115. }
  116. public void StartGame()
  117. {
  118. GetComponent<GamingManager>().StartGame();
  119. _startGameBtn.gameObject.SetActive(false);
  120. Logo.gameObject.SetActive(false);
  121. MenuBackground.gameObject.SetActive(false);
  122. // Reset GameOver Effect
  123. Stains.gameObject.SetActive(false);
  124. GameOverPanel.SetActive(false);
  125. }
  126. private void OnGameEnd(EndGameReason reason, int scores)
  127. {
  128. Scores.text = "Scores " + scores;
  129. if (reason == EndGameReason.GameOver)
  130. {
  131. bPlayingGameOverAnim = true;
  132. Stains.gameObject.SetActive(true);
  133. Stains.transform.GetChild(0).gameObject.SetActive(false);
  134. Stains.transform.GetChild(1).gameObject.SetActive(false);
  135. Stains.transform.GetChild(2).gameObject.SetActive(false);
  136. GameOverPanel.SetActive(true);
  137. GameOverPanel.transform.localPosition = new Vector3(0f, 1000f, 0f);
  138. }
  139. else
  140. {
  141. GameOverPanel.SetActive(true);
  142. GameOverPanel.transform.localPosition = new Vector3(0f, 0f, 0f);
  143. _startGameBtn.gameObject.SetActive(true);
  144. Logo.gameObject.SetActive(false);
  145. Stains.gameObject.SetActive(false);
  146. }
  147. }
  148. private void OnSmartBowConnectionLost()
  149. {
  150. gameObject.GetComponent<GamingManager>().PauseGame();
  151. }
  152. private void Shutdown()
  153. {
  154. // Implementation here
  155. Debug.Log("Shutdown");
  156. UnityEngine.SceneManagement.SceneManager.LoadScene("Home", UnityEngine.SceneManagement.LoadSceneMode.Single);
  157. }
  158. private void ResetAim()
  159. {
  160. Debug.Log("Reset Aim");
  161. if (ResetAimBtn.GetComponent<LongPressMonitor>().isLongPress) return;
  162. GetComponent<SmartBowManager>().ResetAim();
  163. }
  164. private void ResetAimLongPress()
  165. {
  166. if (SB_EventSystem.ins) SB_EventSystem.ins.AwakenSimulateMouse();
  167. }
  168. }