update UI
This commit is contained in:
@@ -1,20 +1,20 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using System.Collections;
|
||||
using Hallucinate.Audio; // Import namespace for AudioManager
|
||||
using Hallucinate.Audio; // Import namespace hệ thống âm thanh của bạn
|
||||
|
||||
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!"
|
||||
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)")]
|
||||
[Header("Cài đặt Sao trên HUD (Giao diện chính lúc đang chạy)")]
|
||||
public GameObject hudStar1;
|
||||
public GameObject hudStar2;
|
||||
public GameObject hudStar3;
|
||||
|
||||
[Header("Cài đặt Sao trên Bảng Win (Kết thúc)")]
|
||||
[Header("Cài đặt Sao trên Bảng Win (Kết thúc game)")]
|
||||
public GameObject winStar1;
|
||||
public GameObject winStar2;
|
||||
public GameObject winStar3;
|
||||
@@ -24,35 +24,45 @@ public class FinishGate : MonoBehaviour
|
||||
public string warningSound = "UI_Warning";
|
||||
public string clickSound = "UI_Click";
|
||||
|
||||
[Header("Cấu hình Tag Hệ thống")]
|
||||
[Tooltip("Tag của Người chơi/NPC dùng để kích hoạt Gate")]
|
||||
public string playerTag = "Player";
|
||||
|
||||
private void Start()
|
||||
{
|
||||
// Đảm bảo thời gian chạy bình thường khi load màn
|
||||
Time.timeScale = 1f;
|
||||
|
||||
// Ẩn các bảng UI khi vừa vào game
|
||||
if (winPanel != null) winPanel.SetActive(false);
|
||||
if (warningUI != null) warningUI.SetActive(false);
|
||||
|
||||
// Ẩn tất cả sao lúc bắt đầu
|
||||
// Reset trạng thái sao về 0 lúc bắt đầu
|
||||
UpdateStarsUI(0);
|
||||
UpdateWinStarsUI(0);
|
||||
}
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
if (other.CompareTag("Check"))
|
||||
// KIỂM TRA 1: Phải đúng Tag quy định (Nên để là "Player")
|
||||
if (other.CompareTag(playerTag))
|
||||
{
|
||||
// KIỂM TRA 2: Tìm linh hoạt component PlayerInventory ở cả con và cha
|
||||
PlayerInventory player = other.GetComponentInChildren<PlayerInventory>();
|
||||
if (player == null) player = other.GetComponentInParent<PlayerInventory>();
|
||||
|
||||
if (player != null)
|
||||
{
|
||||
// TRƯỜNG HỢP 1: Đã đi đến cuối bản đồ nhặt được ít nhất 1 cổ vật -> CHIẾN THẮNG
|
||||
if (player.treasuresCollected > 0)
|
||||
{
|
||||
Debug.Log($"<color=green>[Gate]</color> VỀ ĐÍCH! Kết thúc màn chơi với {player.treasuresCollected} sao.");
|
||||
Debug.Log($"<color=green>[Gate]</color> VỀ ĐÍCH HỢP LỆ! Hoàn thành với {player.treasuresCollected} cổ vật.");
|
||||
WinGame(player.treasuresCollected);
|
||||
}
|
||||
// TRƯỜNG HỢP 2: Đi qua cổng lúc bắt đầu game (chưa nhặt được gì) -> HIỆN CẢNH BÁO
|
||||
else
|
||||
{
|
||||
Debug.Log("<color=yellow>[Gate]</color> Bạn chưa nhặt rương nào, hãy đi tìm rương trước khi về.");
|
||||
Debug.Log("<color=yellow>[Gate]</color> NPC/Player chưa nhặt cổ vật! Hãy chạy đến cuối map.");
|
||||
StopAllCoroutines();
|
||||
StartCoroutine(ShowTempUI(warningUI));
|
||||
}
|
||||
@@ -60,60 +70,70 @@ public class FinishGate : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
// Hàm public để TreasureItem có thể gọi cập nhật HUD ngay khi nhặt
|
||||
// Hàm public được gọi từ TreasureItem để cập nhật HUD ngay lập tức khi vừa 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);
|
||||
if (hudStar1 != null) hudStar1.SetActive(count >= 1);
|
||||
if (hudStar2 != null) hudStar2.SetActive(count >= 2);
|
||||
if (hudStar3 != null) hudStar3.SetActive(count >= 3);
|
||||
}
|
||||
|
||||
void UpdateWinStarsUI(int count)
|
||||
// Hàm nội bộ cập nhật số sao trên bảng kết quả tổng kết
|
||||
private void UpdateWinStarsUI(int count)
|
||||
{
|
||||
if (winStar1) winStar1.SetActive(count >= 1);
|
||||
if (winStar2) winStar2.SetActive(count >= 2);
|
||||
if (winStar3) winStar3.SetActive(count >= 3);
|
||||
if (winStar1 != null) winStar1.SetActive(count >= 1);
|
||||
if (winStar2 != null) winStar2.SetActive(count >= 2);
|
||||
if (winStar3 != null) winStar3.SetActive(count >= 3);
|
||||
}
|
||||
|
||||
void WinGame(int count)
|
||||
private 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
|
||||
UpdateWinStarsUI(count);
|
||||
}
|
||||
|
||||
AudioManager.PlayGlobal(winSound); // Chạy âm thanh thắng cuộc
|
||||
// Phát âm thanh thắng cuộc toàn cục
|
||||
AudioManager.PlayGlobal(winSound);
|
||||
|
||||
// Dừng thời gian game và mở khóa chuột
|
||||
Time.timeScale = 0f;
|
||||
Cursor.lockState = CursorLockMode.None;
|
||||
Cursor.visible = true;
|
||||
}
|
||||
|
||||
IEnumerator ShowTempUI(GameObject ui)
|
||||
private 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
|
||||
// Chỉ phát âm thanh cảnh báo nếu đây là UI cảnh báo thiếu đồ
|
||||
if (ui == warningUI)
|
||||
{
|
||||
AudioManager.PlayGlobal(warningSound);
|
||||
}
|
||||
|
||||
yield return new WaitForSeconds(3f);
|
||||
ui.SetActive(false);
|
||||
}
|
||||
|
||||
// --- CÁC HÀM ĐIỀU HƯỚNG UI BUTTONS ---
|
||||
public void RestartGame()
|
||||
{
|
||||
AudioManager.PlayGlobal(clickSound); // Âm thanh click nút
|
||||
AudioManager.PlayGlobal(clickSound);
|
||||
Time.timeScale = 1f;
|
||||
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
|
||||
}
|
||||
|
||||
public void QuitGame()
|
||||
{
|
||||
AudioManager.PlayGlobal(clickSound); // Âm thanh click nút
|
||||
AudioManager.PlayGlobal(clickSound);
|
||||
Application.Quit();
|
||||
|
||||
#if UNITY_EDITOR
|
||||
UnityEditor.EditorApplication.isPlaying = false;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user