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

138 lines
4.0 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-06 00:25:02 +07:00
using Hallucinate.Audio;
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;
2026-06-06 00:25:02 +07:00
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!"
2026-06-03 04:32:58 +07:00
2026-06-06 00:25:02 +07:00
[Header("Cài đặt Sao trên HUD")]
2026-06-04 17:49:04 +07:00
public GameObject hudStar1;
public GameObject hudStar2;
public GameObject hudStar3;
2026-06-06 00:25:02 +07:00
[Header("Cài đặt Sao trên Bảng Win")]
2026-06-04 17:49:04 +07:00
public GameObject winStar1;
public GameObject winStar2;
public GameObject winStar3;
2026-06-02 23:14:19 +07:00
2026-06-04 23:01:39 +07:00
[Header("Cài đặt Âm thanh")]
public string winSound = "UI_Win";
public string warningSound = "UI_Warning";
public string clickSound = "UI_Click";
2026-06-06 00:25:02 +07:00
[Header("Cấu hình Tag")]
2026-06-06 00:00:27 +07:00
public string playerTag = "Player";
2026-06-06 00:25:02 +07:00
private bool hasEnteredOnce = false; // Theo dõi lần chạm cổng đầu tiên
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);
2026-06-06 00:25:02 +07:00
if (welcomeUI != null) welcomeUI.SetActive(false);
2026-06-03 04:32:58 +07:00
if (warningUI != null) warningUI.SetActive(false);
2026-06-04 17:49:04 +07:00
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-06 00:00:27 +07:00
if (other.CompareTag(playerTag))
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)
{
WinGame(player.treasuresCollected);
}
else
{
StopAllCoroutines();
2026-06-06 00:25:02 +07:00
// 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));
}
2026-06-04 17:49:04 +07:00
}
2026-06-03 04:32:58 +07:00
}
2026-06-02 23:14:19 +07:00
}
}
2026-06-04 17:49:04 +07:00
public void UpdateStarsUI(int count)
{
2026-06-06 00:00:27 +07:00
if (hudStar1 != null) hudStar1.SetActive(count >= 1);
if (hudStar2 != null) hudStar2.SetActive(count >= 2);
if (hudStar3 != null) hudStar3.SetActive(count >= 3);
2026-06-04 17:49:04 +07:00
}
2026-06-06 00:00:27 +07:00
private void UpdateWinStarsUI(int count)
2026-06-03 04:32:58 +07:00
{
2026-06-06 00:00:27 +07:00
if (winStar1 != null) winStar1.SetActive(count >= 1);
if (winStar2 != null) winStar2.SetActive(count >= 2);
if (winStar3 != null) winStar3.SetActive(count >= 3);
2026-06-03 04:32:58 +07:00
}
2026-06-06 00:00:27 +07:00
private 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);
2026-06-06 00:00:27 +07:00
UpdateWinStarsUI(count);
2026-06-04 17:49:04 +07:00
}
2026-06-04 23:01:39 +07:00
2026-06-06 00:00:27 +07:00
AudioManager.PlayGlobal(winSound);
2026-06-02 23:14:19 +07:00
Time.timeScale = 0f;
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
2026-06-06 00:00:27 +07:00
private IEnumerator ShowTempUI(GameObject ui)
2026-06-04 17:49:04 +07:00
{
if (ui == null) yield break;
2026-06-06 00:00:27 +07:00
2026-06-06 00:25:02 +07:00
// 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);
2026-06-04 17:49:04 +07:00
ui.SetActive(true);
2026-06-04 23:01:39 +07:00
2026-06-06 00:00:27 +07:00
if (ui == warningUI)
{
AudioManager.PlayGlobal(warningSound);
}
2026-06-04 23:01:39 +07:00
2026-06-04 17:49:04 +07:00
yield return new WaitForSeconds(3f);
ui.SetActive(false);
}
2026-06-02 23:14:19 +07:00
public void RestartGame()
{
2026-06-06 00:00:27 +07:00
AudioManager.PlayGlobal(clickSound);
2026-06-02 23:14:19 +07:00
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-06 00:00:27 +07:00
AudioManager.PlayGlobal(clickSound);
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-06 00:00:27 +07:00
}