using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.UI; public class OverallLogics : MonoBehaviour { [SerializeField] private Button _startGameBtn; [SerializeField] private GameObject GameOverPanel; [SerializeField] private GameObject Stains; [SerializeField] private TextMeshProUGUI Scores; [SerializeField] private Image Logo; [SerializeField] private Image MenuBackground; [SerializeField] private Button ShutdownBtn; [SerializeField] private Button ResetAimBtn; private bool bPlayingGameOverAnim; private float TimeGameOverAnimPlayed = 0f; private readonly float[] StainEmergeTimes = new float[] { 0.2f, 0.5f, 1f }; private const float GameOverPanelEmergeTime = 1.5f; private const float GameOverFinishAnimTime = 2f; private void Awake() { Debug.Assert(_startGameBtn); Debug.Assert(GameOverPanel); Debug.Assert(Stains); Debug.Assert(Scores); Debug.Assert(Logo); Debug.Assert(MenuBackground); } // Start is called before the first frame update void Start() { ShutdownBtn.onClick.AddListener(Shutdown); ResetAimBtn.onClick.AddListener(ResetAim); ResetAimBtn.gameObject.AddComponent().onLongPress += ResetAimLongPress; GetComponent().OnConnectionLost += OnSmartBowConnectionLost; _startGameBtn.onClick.AddListener(StartGame); GetComponent().OnGameEnd += OnGameEnd; Screen.sleepTimeout = SleepTimeout.NeverSleep; _startGameBtn.gameObject.SetActive(true); Logo.gameObject.SetActive(true); GameOverPanel.SetActive(false); Stains.gameObject.SetActive(false); float screen_ratio = (float)Screen.width / (float)Screen.height; float background_ratio = 2300f / 1080f; float background_scale; if (screen_ratio > background_ratio) { background_scale = Screen.width / 2300f; } else { background_scale = Screen.height / 1080f; } MenuBackground.rectTransform.sizeDelta = new Vector2(background_scale * 2300f, background_scale * 1080f); } // Update is called once per frame void Update() { if (bPlayingGameOverAnim) { if (TimeGameOverAnimPlayed >= GameOverFinishAnimTime) { GameOverPanel.transform.localPosition = new Vector3(0f, 0f, 0f); TimeGameOverAnimPlayed = 0f; bPlayingGameOverAnim = false; _startGameBtn.gameObject.SetActive(true); return; } TimeGameOverAnimPlayed += Time.deltaTime; if (TimeGameOverAnimPlayed < GameOverPanelEmergeTime) { GameObject stain_0 = Stains.transform.GetChild(0).gameObject; GameObject stain_1 = Stains.transform.GetChild(1).gameObject; GameObject stain_2 = Stains.transform.GetChild(2).gameObject; if (TimeGameOverAnimPlayed < StainEmergeTimes[0]) { // do nothing return; } else if (TimeGameOverAnimPlayed < StainEmergeTimes[1]) { if (stain_0.activeSelf == false) { stain_0.SetActive(true); GetComponent().GetAudioManager().InstantiateTemprorarySound(SoundCategory.FruitSmash, Vector3.zero, 2f); } } else if (TimeGameOverAnimPlayed < StainEmergeTimes[2]) { if (stain_1.activeSelf == false) { stain_1.SetActive(true); GetComponent().GetAudioManager().InstantiateTemprorarySound(SoundCategory.FruitSmash, Vector3.zero, 2f); } } else if (TimeGameOverAnimPlayed < GameOverPanelEmergeTime) { if (stain_2.activeSelf == false) { stain_2.SetActive(true); GetComponent().GetAudioManager().InstantiateTemprorarySound(SoundCategory.FruitSmash, Vector3.zero, 2f); } } } else { if (TimeGameOverAnimPlayed < GameOverFinishAnimTime) { float game_over_panel_y = Mathf.Lerp(1000f, 0f, (TimeGameOverAnimPlayed - GameOverPanelEmergeTime) / (GameOverFinishAnimTime - GameOverPanelEmergeTime)); GameOverPanel.transform.localPosition = new Vector3(0f, game_over_panel_y, 0f); } } } } public void StartGame() { GetComponent().StartGame(); _startGameBtn.gameObject.SetActive(false); Logo.gameObject.SetActive(false); MenuBackground.gameObject.SetActive(false); // Reset GameOver Effect Stains.gameObject.SetActive(false); GameOverPanel.SetActive(false); } private void OnGameEnd(EndGameReason reason, int scores) { Scores.text = "Scores " + scores; if (reason == EndGameReason.GameOver) { bPlayingGameOverAnim = true; Stains.gameObject.SetActive(true); Stains.transform.GetChild(0).gameObject.SetActive(false); Stains.transform.GetChild(1).gameObject.SetActive(false); Stains.transform.GetChild(2).gameObject.SetActive(false); GameOverPanel.SetActive(true); GameOverPanel.transform.localPosition = new Vector3(0f, 1000f, 0f); } else { GameOverPanel.SetActive(true); GameOverPanel.transform.localPosition = new Vector3(0f, 0f, 0f); _startGameBtn.gameObject.SetActive(true); Logo.gameObject.SetActive(false); Stains.gameObject.SetActive(false); } } private void OnSmartBowConnectionLost() { gameObject.GetComponent().PauseGame(); } private void Shutdown() { // Implementation here Debug.Log("Shutdown"); UnityEngine.SceneManagement.SceneManager.LoadScene("Home", UnityEngine.SceneManagement.LoadSceneMode.Single); } private void ResetAim() { Debug.Log("Reset Aim"); if (ResetAimBtn.GetComponent().isLongPress) return; GetComponent().ResetAim(); } private void ResetAimLongPress() { if (SB_EventSystem.ins) SB_EventSystem.ins.AwakenSimulateMouse(); } }