107 lines
3.1 KiB
C#
107 lines
3.1 KiB
C#
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using System.Collections;
|
|
|
|
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;
|
|
|
|
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<PlayerInventory>();
|
|
if (player == null) player = other.GetComponentInParent<PlayerInventory>();
|
|
|
|
if (player != null)
|
|
{
|
|
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));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
Time.timeScale = 0f;
|
|
Cursor.lockState = CursorLockMode.None;
|
|
Cursor.visible = true;
|
|
}
|
|
|
|
IEnumerator ShowTempUI(GameObject ui)
|
|
{
|
|
if (ui == null) yield break;
|
|
ui.SetActive(true);
|
|
yield return new WaitForSeconds(3f);
|
|
ui.SetActive(false);
|
|
}
|
|
|
|
public void RestartGame()
|
|
{
|
|
Time.timeScale = 1f;
|
|
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
|
|
}
|
|
|
|
public void QuitGame()
|
|
{
|
|
Application.Quit();
|
|
#if UNITY_EDITOR
|
|
UnityEditor.EditorApplication.isPlaying = false;
|
|
#endif
|
|
}
|
|
}
|