90 lines
2.5 KiB
C#
90 lines
2.5 KiB
C#
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using System.Collections;
|
|
|
|
public class FinishGate : MonoBehaviour
|
|
{
|
|
[Header("Cài đặt UI Chính")]
|
|
public GameObject winPanel; // Bảng You Win
|
|
public GameObject warningUI; // Chữ "Chưa đủ rương"
|
|
|
|
[Header("Cài đặt Sao (Stars)")]
|
|
public GameObject star1;
|
|
public GameObject star2;
|
|
public GameObject star3;
|
|
|
|
private void Start()
|
|
{
|
|
// Reset thời gian và ẩn UI lúc bắt đầu
|
|
Time.timeScale = 1f;
|
|
|
|
if (winPanel != null) winPanel.SetActive(false);
|
|
if (warningUI != null) warningUI.SetActive(false);
|
|
|
|
if (star1) star1.SetActive(false);
|
|
if (star2) star2.SetActive(false);
|
|
if (star3) star3.SetActive(false);
|
|
}
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
PlayerInventory player = other.GetComponentInParent<PlayerInventory>();
|
|
|
|
if (player != null)
|
|
{
|
|
if (player.treasuresCollected >= player.totalTreasuresNeeded)
|
|
{
|
|
WinGame(player.treasuresCollected);
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("Chưa đủ rương! Bạn cần " + player.totalTreasuresNeeded);
|
|
if (warningUI != null) StartCoroutine(ShowWarning());
|
|
}
|
|
}
|
|
}
|
|
|
|
IEnumerator ShowWarning()
|
|
{
|
|
warningUI.SetActive(true);
|
|
yield return new WaitForSeconds(2f);
|
|
warningUI.SetActive(false);
|
|
}
|
|
|
|
void WinGame(int count)
|
|
{
|
|
if (winPanel != null) winPanel.SetActive(true);
|
|
|
|
// Hiện sao dựa trên số lượng nhặt được
|
|
if (count >= 1 && star1) star1.SetActive(true);
|
|
if (count >= 2 && star2) star2.SetActive(true);
|
|
if (count >= 3 && star3) star3.SetActive(true);
|
|
|
|
// Dừng game
|
|
Time.timeScale = 0f;
|
|
|
|
// Hiện chuột để bấm nút
|
|
Cursor.lockState = CursorLockMode.None;
|
|
Cursor.visible = true;
|
|
}
|
|
|
|
// --- HÀM CHO CÁC NÚT BẤM ---
|
|
public void RestartGame()
|
|
{
|
|
Debug.Log("Đang tải lại màn chơi...");
|
|
Time.timeScale = 1f;
|
|
|
|
// Sử dụng buildIndex để tránh lỗi tên scene
|
|
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
|
|
}
|
|
|
|
public void QuitGame()
|
|
{
|
|
Debug.Log("Đang thoát game...");
|
|
Application.Quit();
|
|
|
|
#if UNITY_EDITOR
|
|
UnityEditor.EditorApplication.isPlaying = false;
|
|
#endif
|
|
}
|
|
} |