154 lines
4.5 KiB
C#
154 lines
4.5 KiB
C#
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using System.Collections;
|
|
using Hallucinate.Audio;
|
|
|
|
public class FinishGate : MonoBehaviour
|
|
{
|
|
[Header("Cài đặt UI Chính")]
|
|
public GameObject winPanel;
|
|
public GameObject losePanel; // Bảng Lose
|
|
public GameObject welcomeUI; // THÔNG BÁO 1: "Chào mừng!"
|
|
public GameObject warningUI; // THÔNG BÁO 2: "Bạn chưa nhặt rương nào!"
|
|
|
|
[Header("Cài đặt Sao trên HUD")]
|
|
public GameObject hudStar1;
|
|
public GameObject hudStar2;
|
|
public GameObject hudStar3;
|
|
|
|
[Header("Cài đặt Sao trên Bảng Win")]
|
|
public GameObject winStar1;
|
|
public GameObject winStar2;
|
|
public GameObject winStar3;
|
|
|
|
[Header("Cài đặt Âm thanh")]
|
|
public string winSound = "UI_Win";
|
|
public string loseSound = "UI_Lose"; // Âm thanh thua
|
|
public string warningSound = "UI_Warning";
|
|
public string clickSound = "UI_Click";
|
|
|
|
[Header("Cấu hình Tag")]
|
|
public string playerTag = "Player";
|
|
|
|
private bool hasEnteredOnce = false; // Theo dõi lần chạm cổng đầu tiên
|
|
|
|
private void Start()
|
|
{
|
|
Time.timeScale = 1f;
|
|
|
|
if (winPanel != null) winPanel.SetActive(false);
|
|
if (losePanel != null) losePanel.SetActive(false);
|
|
if (welcomeUI != null) welcomeUI.SetActive(false);
|
|
if (warningUI != null) warningUI.SetActive(false);
|
|
|
|
UpdateStarsUI(0);
|
|
UpdateWinStarsUI(0);
|
|
}
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
if (other.CompareTag(playerTag))
|
|
{
|
|
PlayerInventory player = other.GetComponentInChildren<PlayerInventory>();
|
|
if (player == null) player = other.GetComponentInParent<PlayerInventory>();
|
|
|
|
if (player != null)
|
|
{
|
|
if (player.treasuresCollected > 0)
|
|
{
|
|
WinGame(player.treasuresCollected);
|
|
}
|
|
else
|
|
{
|
|
StopAllCoroutines();
|
|
// Nếu là lần đầu tiên -> Hiện Welcome. Nếu là lần sau -> Hiện Warning.
|
|
if (!hasEnteredOnce)
|
|
{
|
|
hasEnteredOnce = true;
|
|
StartCoroutine(ShowTempUI(welcomeUI));
|
|
}
|
|
else
|
|
{
|
|
StartCoroutine(ShowTempUI(warningUI));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void UpdateStarsUI(int count)
|
|
{
|
|
if (hudStar1 != null) hudStar1.SetActive(count >= 1);
|
|
if (hudStar2 != null) hudStar2.SetActive(count >= 2);
|
|
if (hudStar3 != null) hudStar3.SetActive(count >= 3);
|
|
}
|
|
|
|
private void UpdateWinStarsUI(int count)
|
|
{
|
|
if (winStar1 != null) winStar1.SetActive(count >= 1);
|
|
if (winStar2 != null) winStar2.SetActive(count >= 2);
|
|
if (winStar3 != null) winStar3.SetActive(count >= 3);
|
|
}
|
|
|
|
private void WinGame(int count)
|
|
{
|
|
if (winPanel != null)
|
|
{
|
|
winPanel.SetActive(true);
|
|
UpdateWinStarsUI(count);
|
|
}
|
|
|
|
AudioManager.PlayGlobal(winSound);
|
|
Time.timeScale = 0f;
|
|
Cursor.lockState = CursorLockMode.None;
|
|
Cursor.visible = true;
|
|
}
|
|
|
|
public void LoseGame()
|
|
{
|
|
if (losePanel != null)
|
|
{
|
|
losePanel.SetActive(true);
|
|
}
|
|
|
|
AudioManager.PlayGlobal(loseSound);
|
|
Time.timeScale = 0f;
|
|
Cursor.lockState = CursorLockMode.None;
|
|
Cursor.visible = true;
|
|
}
|
|
|
|
private IEnumerator ShowTempUI(GameObject ui)
|
|
{
|
|
if (ui == null) yield break;
|
|
|
|
// Tắt tất cả UI tạm thời khác trước khi bật cái mới để tránh đè chữ
|
|
if (welcomeUI != null) welcomeUI.SetActive(false);
|
|
if (warningUI != null) warningUI.SetActive(false);
|
|
|
|
ui.SetActive(true);
|
|
|
|
if (ui == warningUI)
|
|
{
|
|
AudioManager.PlayGlobal(warningSound);
|
|
}
|
|
|
|
yield return new WaitForSeconds(3f);
|
|
ui.SetActive(false);
|
|
}
|
|
|
|
public void RestartGame()
|
|
{
|
|
AudioManager.PlayGlobal(clickSound);
|
|
Time.timeScale = 1f;
|
|
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
|
|
}
|
|
|
|
public void QuitGame()
|
|
{
|
|
AudioManager.PlayGlobal(clickSound);
|
|
Application.Quit();
|
|
#if UNITY_EDITOR
|
|
UnityEditor.EditorApplication.isPlaying = false;
|
|
#endif
|
|
}
|
|
} |