Files
BABA_YAGA/Assets/Scripts/Cho mon AI/FinishGate.cs

107 lines
3.1 KiB
C#
Raw Normal View History

2026-06-02 23:14:19 +07:00
using UnityEngine;
using UnityEngine.SceneManagement;
2026-06-03 04:32:58 +07:00
using System.Collections;
2026-06-02 23:14:19 +07:00
public class FinishGate : MonoBehaviour
{
2026-06-03 04:32:58 +07:00
[Header("Cài đặt UI Chính")]
2026-06-04 17:49:04 +07:00
public GameObject winPanel;
public GameObject warningUI; // Thông báo "Bạn chưa nhặt rương nào!"
2026-06-03 04:32:58 +07:00
2026-06-04 17:49:04 +07:00
[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;
2026-06-02 23:14:19 +07:00
private void Start()
{
2026-06-03 04:32:58 +07:00
Time.timeScale = 1f;
if (winPanel != null) winPanel.SetActive(false);
if (warningUI != null) warningUI.SetActive(false);
2026-06-04 17:49:04 +07:00
// Ẩn tất cả sao lúc bắt đầu
UpdateStarsUI(0);
UpdateWinStarsUI(0);
2026-06-02 23:14:19 +07:00
}
2026-06-03 04:32:58 +07:00
2026-06-02 23:14:19 +07:00
private void OnTriggerEnter(Collider other)
{
2026-06-04 17:49:04 +07:00
if (other.CompareTag("Check"))
2026-06-02 23:14:19 +07:00
{
2026-06-04 17:49:04 +07:00
PlayerInventory player = other.GetComponentInChildren<PlayerInventory>();
if (player == null) player = other.GetComponentInParent<PlayerInventory>();
if (player != null)
2026-06-03 04:32:58 +07:00
{
2026-06-04 17:49:04 +07:00
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.");
WinGame(player.treasuresCollected);
}
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ề.");
StopAllCoroutines();
StartCoroutine(ShowTempUI(warningUI));
}
2026-06-03 04:32:58 +07:00
}
2026-06-02 23:14:19 +07:00
}
}
2026-06-04 17:49:04 +07:00
// 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)
2026-06-03 04:32:58 +07:00
{
2026-06-04 17:49:04 +07:00
if (winStar1) winStar1.SetActive(count >= 1);
if (winStar2) winStar2.SetActive(count >= 2);
if (winStar3) winStar3.SetActive(count >= 3);
2026-06-03 04:32:58 +07:00
}
void WinGame(int count)
2026-06-02 23:14:19 +07:00
{
2026-06-04 17:49:04 +07:00
if (winPanel != null)
{
winPanel.SetActive(true);
UpdateWinStarsUI(count); // Hiện số sao tương ứng trên bảng kết thúc
}
2026-06-02 23:14:19 +07:00
Time.timeScale = 0f;
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
2026-06-04 17:49:04 +07:00
IEnumerator ShowTempUI(GameObject ui)
{
if (ui == null) yield break;
ui.SetActive(true);
yield return new WaitForSeconds(3f);
ui.SetActive(false);
}
2026-06-02 23:14:19 +07:00
public void RestartGame()
{
Time.timeScale = 1f;
2026-06-03 04:32:58 +07:00
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
2026-06-02 23:14:19 +07:00
}
public void QuitGame()
{
2026-06-03 04:32:58 +07:00
Application.Quit();
2026-06-02 23:14:19 +07:00
#if UNITY_EDITOR
2026-06-03 04:32:58 +07:00
UnityEditor.EditorApplication.isPlaying = false;
2026-06-02 23:14:19 +07:00
#endif
}
2026-06-04 17:49:04 +07:00
}