using UnityEngine; using UnityEngine.SceneManagement; using System.Collections; using Hallucinate.Audio; // Import namespace for AudioManager public class FinishGate : MonoBehaviour { [Header("Cài đặt UI Chính")] public GameObject winPanel; public GameObject warningUI; // Thông báo "Bạn chưa nhặt rương nào!" [Header("Cài đặt Sao trên HUD (Giao diện chính)")] public GameObject hudStar1; public GameObject hudStar2; public GameObject hudStar3; [Header("Cài đặt Sao trên Bảng Win (Kết thúc)")] public GameObject winStar1; public GameObject winStar2; public GameObject winStar3; [Header("Cài đặt Âm thanh")] public string winSound = "UI_Win"; public string warningSound = "UI_Warning"; public string clickSound = "UI_Click"; private void Start() { Time.timeScale = 1f; if (winPanel != null) winPanel.SetActive(false); if (warningUI != null) warningUI.SetActive(false); // Ẩn tất cả sao lúc bắt đầu UpdateStarsUI(0); UpdateWinStarsUI(0); } private void OnTriggerEnter(Collider other) { if (other.CompareTag("Check")) { PlayerInventory player = other.GetComponentInChildren(); if (player == null) player = other.GetComponentInParent(); if (player != null) { if (player.treasuresCollected > 0) { Debug.Log($"[Gate] VỀ ĐÍCH! Kết thúc màn chơi với {player.treasuresCollected} sao."); WinGame(player.treasuresCollected); } else { Debug.Log("[Gate] Bạn chưa nhặt rương nào, hãy đi tìm rương trước khi về."); StopAllCoroutines(); StartCoroutine(ShowTempUI(warningUI)); } } } } // Hàm public để TreasureItem có thể gọi cập nhật HUD ngay khi nhặt public void UpdateStarsUI(int count) { if (hudStar1) hudStar1.SetActive(count >= 1); if (hudStar2) hudStar2.SetActive(count >= 2); if (hudStar3) hudStar3.SetActive(count >= 3); } void UpdateWinStarsUI(int count) { if (winStar1) winStar1.SetActive(count >= 1); if (winStar2) winStar2.SetActive(count >= 2); if (winStar3) winStar3.SetActive(count >= 3); } void WinGame(int count) { if (winPanel != null) { winPanel.SetActive(true); UpdateWinStarsUI(count); // Hiện số sao tương ứng trên bảng kết thúc } AudioManager.PlayGlobal(winSound); // Chạy âm thanh thắng cuộc Time.timeScale = 0f; Cursor.lockState = CursorLockMode.None; Cursor.visible = true; } IEnumerator ShowTempUI(GameObject ui) { if (ui == null) yield break; ui.SetActive(true); if (ui == warningUI) AudioManager.PlayGlobal(warningSound); // Chạy âm thanh cảnh báo yield return new WaitForSeconds(3f); ui.SetActive(false); } public void RestartGame() { AudioManager.PlayGlobal(clickSound); // Âm thanh click nút Time.timeScale = 1f; SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); } public void QuitGame() { AudioManager.PlayGlobal(clickSound); // Âm thanh click nút Application.Quit(); #if UNITY_EDITOR UnityEditor.EditorApplication.isPlaying = false; #endif } }