Merge branch 'main' of https://scove-vault.duckdns.org/scove/HALLUCINATION
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -1,20 +1,21 @@
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.SceneManagement;
|
using UnityEngine.SceneManagement;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using Hallucinate.Audio; // Import namespace hệ thống âm thanh của bạn
|
using Hallucinate.Audio;
|
||||||
|
|
||||||
public class FinishGate : MonoBehaviour
|
public class FinishGate : MonoBehaviour
|
||||||
{
|
{
|
||||||
[Header("Cài đặt UI Chính")]
|
[Header("Cài đặt UI Chính")]
|
||||||
public GameObject winPanel;
|
public GameObject winPanel;
|
||||||
public GameObject warningUI; // THÔNG BÁO: "Bạn chưa nhặt rương nào!"
|
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 (Giao diện chính lúc đang chạy)")]
|
[Header("Cài đặt Sao trên HUD")]
|
||||||
public GameObject hudStar1;
|
public GameObject hudStar1;
|
||||||
public GameObject hudStar2;
|
public GameObject hudStar2;
|
||||||
public GameObject hudStar3;
|
public GameObject hudStar3;
|
||||||
|
|
||||||
[Header("Cài đặt Sao trên Bảng Win (Kết thúc game)")]
|
[Header("Cài đặt Sao trên Bảng Win")]
|
||||||
public GameObject winStar1;
|
public GameObject winStar1;
|
||||||
public GameObject winStar2;
|
public GameObject winStar2;
|
||||||
public GameObject winStar3;
|
public GameObject winStar3;
|
||||||
@@ -24,53 +25,54 @@ public class FinishGate : MonoBehaviour
|
|||||||
public string warningSound = "UI_Warning";
|
public string warningSound = "UI_Warning";
|
||||||
public string clickSound = "UI_Click";
|
public string clickSound = "UI_Click";
|
||||||
|
|
||||||
[Header("Cấu hình Tag Hệ thống")]
|
[Header("Cấu hình Tag")]
|
||||||
[Tooltip("Tag của Người chơi/NPC dùng để kích hoạt Gate")]
|
|
||||||
public string playerTag = "Player";
|
public string playerTag = "Player";
|
||||||
|
|
||||||
|
private bool hasEnteredOnce = false; // Theo dõi lần chạm cổng đầu tiên
|
||||||
|
|
||||||
private void Start()
|
private void Start()
|
||||||
{
|
{
|
||||||
// Đảm bảo thời gian chạy bình thường khi load màn
|
|
||||||
Time.timeScale = 1f;
|
Time.timeScale = 1f;
|
||||||
|
|
||||||
// Ẩn các bảng UI khi vừa vào game
|
|
||||||
if (winPanel != null) winPanel.SetActive(false);
|
if (winPanel != null) winPanel.SetActive(false);
|
||||||
|
if (welcomeUI != null) welcomeUI.SetActive(false);
|
||||||
if (warningUI != null) warningUI.SetActive(false);
|
if (warningUI != null) warningUI.SetActive(false);
|
||||||
|
|
||||||
// Reset trạng thái sao về 0 lúc bắt đầu
|
|
||||||
UpdateStarsUI(0);
|
UpdateStarsUI(0);
|
||||||
UpdateWinStarsUI(0);
|
UpdateWinStarsUI(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnTriggerEnter(Collider other)
|
private void OnTriggerEnter(Collider other)
|
||||||
{
|
{
|
||||||
// KIỂM TRA 1: Phải đúng Tag quy định (Nên để là "Player")
|
|
||||||
if (other.CompareTag(playerTag))
|
if (other.CompareTag(playerTag))
|
||||||
{
|
{
|
||||||
// KIỂM TRA 2: Tìm linh hoạt component PlayerInventory ở cả con và cha
|
|
||||||
PlayerInventory player = other.GetComponentInChildren<PlayerInventory>();
|
PlayerInventory player = other.GetComponentInChildren<PlayerInventory>();
|
||||||
if (player == null) player = other.GetComponentInParent<PlayerInventory>();
|
if (player == null) player = other.GetComponentInParent<PlayerInventory>();
|
||||||
|
|
||||||
if (player != null)
|
if (player != null)
|
||||||
{
|
{
|
||||||
// TRƯỜNG HỢP 1: Đã đi đến cuối bản đồ nhặt được ít nhất 1 cổ vật -> CHIẾN THẮNG
|
|
||||||
if (player.treasuresCollected > 0)
|
if (player.treasuresCollected > 0)
|
||||||
{
|
{
|
||||||
Debug.Log($"<color=green>[Gate]</color> VỀ ĐÍCH HỢP LỆ! Hoàn thành với {player.treasuresCollected} cổ vật.");
|
|
||||||
WinGame(player.treasuresCollected);
|
WinGame(player.treasuresCollected);
|
||||||
}
|
}
|
||||||
// TRƯỜNG HỢP 2: Đi qua cổng lúc bắt đầu game (chưa nhặt được gì) -> HIỆN CẢNH BÁO
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Debug.Log("<color=yellow>[Gate]</color> NPC/Player chưa nhặt cổ vật! Hãy chạy đến cuối map.");
|
|
||||||
StopAllCoroutines();
|
StopAllCoroutines();
|
||||||
StartCoroutine(ShowTempUI(warningUI));
|
// 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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hàm public được gọi từ TreasureItem để cập nhật HUD ngay lập tức khi vừa nhặt đồ
|
|
||||||
public void UpdateStarsUI(int count)
|
public void UpdateStarsUI(int count)
|
||||||
{
|
{
|
||||||
if (hudStar1 != null) hudStar1.SetActive(count >= 1);
|
if (hudStar1 != null) hudStar1.SetActive(count >= 1);
|
||||||
@@ -78,7 +80,6 @@ public class FinishGate : MonoBehaviour
|
|||||||
if (hudStar3 != null) hudStar3.SetActive(count >= 3);
|
if (hudStar3 != null) hudStar3.SetActive(count >= 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hàm nội bộ cập nhật số sao trên bảng kết quả tổng kết
|
|
||||||
private void UpdateWinStarsUI(int count)
|
private void UpdateWinStarsUI(int count)
|
||||||
{
|
{
|
||||||
if (winStar1 != null) winStar1.SetActive(count >= 1);
|
if (winStar1 != null) winStar1.SetActive(count >= 1);
|
||||||
@@ -94,10 +95,7 @@ public class FinishGate : MonoBehaviour
|
|||||||
UpdateWinStarsUI(count);
|
UpdateWinStarsUI(count);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Phát âm thanh thắng cuộc toàn cục
|
|
||||||
AudioManager.PlayGlobal(winSound);
|
AudioManager.PlayGlobal(winSound);
|
||||||
|
|
||||||
// Dừng thời gian game và mở khóa chuột
|
|
||||||
Time.timeScale = 0f;
|
Time.timeScale = 0f;
|
||||||
Cursor.lockState = CursorLockMode.None;
|
Cursor.lockState = CursorLockMode.None;
|
||||||
Cursor.visible = true;
|
Cursor.visible = true;
|
||||||
@@ -107,9 +105,12 @@ public class FinishGate : MonoBehaviour
|
|||||||
{
|
{
|
||||||
if (ui == null) yield break;
|
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);
|
ui.SetActive(true);
|
||||||
|
|
||||||
// Chỉ phát âm thanh cảnh báo nếu đây là UI cảnh báo thiếu đồ
|
|
||||||
if (ui == warningUI)
|
if (ui == warningUI)
|
||||||
{
|
{
|
||||||
AudioManager.PlayGlobal(warningSound);
|
AudioManager.PlayGlobal(warningSound);
|
||||||
@@ -119,7 +120,6 @@ public class FinishGate : MonoBehaviour
|
|||||||
ui.SetActive(false);
|
ui.SetActive(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- CÁC HÀM ĐIỀU HƯỚNG UI BUTTONS ---
|
|
||||||
public void RestartGame()
|
public void RestartGame()
|
||||||
{
|
{
|
||||||
AudioManager.PlayGlobal(clickSound);
|
AudioManager.PlayGlobal(clickSound);
|
||||||
@@ -131,7 +131,6 @@ public class FinishGate : MonoBehaviour
|
|||||||
{
|
{
|
||||||
AudioManager.PlayGlobal(clickSound);
|
AudioManager.PlayGlobal(clickSound);
|
||||||
Application.Quit();
|
Application.Quit();
|
||||||
|
|
||||||
#if UNITY_EDITOR
|
#if UNITY_EDITOR
|
||||||
UnityEditor.EditorApplication.isPlaying = false;
|
UnityEditor.EditorApplication.isPlaying = false;
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -5,7 +5,8 @@ using Hallucinate.Audio;
|
|||||||
public class TreasureItem : MonoBehaviour
|
public class TreasureItem : MonoBehaviour
|
||||||
{
|
{
|
||||||
[Header("Cài đặt UI thông báo")]
|
[Header("Cài đặt UI thông báo")]
|
||||||
public GameObject notificationText; // Kéo Text "Đã nhặt Cổ vật" vào đây
|
[Tooltip("Kéo Text 'Đã nhặt được cổ vật hãy trốn thoát ra khỏi đây' vào đây")]
|
||||||
|
public GameObject notificationText;
|
||||||
|
|
||||||
[Header("Cài đặt Âm thanh")]
|
[Header("Cài đặt Âm thanh")]
|
||||||
public string pickupSound = "Item_Pickup";
|
public string pickupSound = "Item_Pickup";
|
||||||
@@ -13,12 +14,10 @@ public class TreasureItem : MonoBehaviour
|
|||||||
[Header("Cấu hình Tag")]
|
[Header("Cấu hình Tag")]
|
||||||
public string playerTag = "Player";
|
public string playerTag = "Player";
|
||||||
|
|
||||||
// Biến cờ để tránh việc va chạm 2 lần trong cùng 1 frame
|
|
||||||
private bool isCollected = false;
|
private bool isCollected = false;
|
||||||
|
|
||||||
private void Start()
|
private void Start()
|
||||||
{
|
{
|
||||||
// Đảm bảo UI thông báo luôn tắt khi bắt đầu game
|
|
||||||
if (notificationText != null)
|
if (notificationText != null)
|
||||||
{
|
{
|
||||||
notificationText.SetActive(false);
|
notificationText.SetActive(false);
|
||||||
@@ -27,7 +26,6 @@ public class TreasureItem : MonoBehaviour
|
|||||||
|
|
||||||
private void OnTriggerEnter(Collider other)
|
private void OnTriggerEnter(Collider other)
|
||||||
{
|
{
|
||||||
// 1. Chỉ xử lý khi chạm đúng Player và rương chưa bị nhặt
|
|
||||||
if (!isCollected && other.CompareTag(playerTag))
|
if (!isCollected && other.CompareTag(playerTag))
|
||||||
{
|
{
|
||||||
PlayerInventory player = other.GetComponentInChildren<PlayerInventory>();
|
PlayerInventory player = other.GetComponentInChildren<PlayerInventory>();
|
||||||
@@ -35,30 +33,22 @@ public class TreasureItem : MonoBehaviour
|
|||||||
|
|
||||||
if (player != null)
|
if (player != null)
|
||||||
{
|
{
|
||||||
// Khóa lại để không bị kích hoạt nhiều lần
|
|
||||||
isCollected = true;
|
isCollected = true;
|
||||||
|
|
||||||
// 2. Tăng số lượng rương đang giữ
|
|
||||||
player.treasuresCollected++;
|
player.treasuresCollected++;
|
||||||
Debug.Log($"<color=cyan>[Chest]</color> NHẶT THÀNH CÔNG! Số rương hiện tại: {player.treasuresCollected}");
|
|
||||||
|
|
||||||
// 3. Cập nhật sao trên HUD (Sử dụng hàm Unity 6: FindAnyObjectByType)
|
|
||||||
FinishGate gate = Object.FindAnyObjectByType<FinishGate>();
|
FinishGate gate = Object.FindAnyObjectByType<FinishGate>();
|
||||||
if (gate != null)
|
if (gate != null)
|
||||||
{
|
{
|
||||||
gate.UpdateStarsUI(player.treasuresCollected);
|
gate.UpdateStarsUI(player.treasuresCollected);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. Kích hoạt trạng thái truy đuổi cho toàn bộ Enemy AI
|
|
||||||
SetEnemiesAlertState(true);
|
SetEnemiesAlertState(true);
|
||||||
|
|
||||||
// 5. Chạy âm thanh nhặt đồ
|
|
||||||
if (AudioManager.Instance != null)
|
if (AudioManager.Instance != null)
|
||||||
{
|
{
|
||||||
AudioManager.Instance.Play(pickupSound, position: transform.position);
|
AudioManager.Instance.Play(pickupSound, position: transform.position);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 6. Xử lý hiện UI và làm biến mất rương an toàn
|
|
||||||
StartCoroutine(HandlePickupRoutine());
|
StartCoroutine(HandlePickupRoutine());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -66,48 +56,37 @@ public class TreasureItem : MonoBehaviour
|
|||||||
|
|
||||||
private void SetEnemiesAlertState(bool state)
|
private void SetEnemiesAlertState(bool state)
|
||||||
{
|
{
|
||||||
// Sử dụng hàm chuẩn Unity 6+
|
|
||||||
EnemyAI[] allEnemies = Object.FindObjectsByType<EnemyAI>(FindObjectsSortMode.None);
|
EnemyAI[] allEnemies = Object.FindObjectsByType<EnemyAI>(FindObjectsSortMode.None);
|
||||||
foreach (EnemyAI enemy in allEnemies)
|
foreach (EnemyAI enemy in allEnemies)
|
||||||
{
|
{
|
||||||
if (enemy != null)
|
if (enemy != null) enemy.playerHasArtifact = state;
|
||||||
{
|
|
||||||
enemy.playerHasArtifact = state;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private IEnumerator HandlePickupRoutine()
|
private IEnumerator HandlePickupRoutine()
|
||||||
{
|
{
|
||||||
// Ẩn rương đi trước (tắt hiển thị và va chạm)
|
|
||||||
HideTreasureModel();
|
HideTreasureModel();
|
||||||
|
|
||||||
// Bật UI thông báo "Đã nhặt Cổ vật"
|
|
||||||
if (notificationText != null)
|
if (notificationText != null)
|
||||||
{
|
{
|
||||||
notificationText.SetActive(true);
|
notificationText.SetActive(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Chờ 2 giây
|
yield return new WaitForSeconds(3f); // Tăng lên 3s để người chơi kịp đọc dòng chữ dài
|
||||||
yield return new WaitForSeconds(2f);
|
|
||||||
|
|
||||||
// Tắt UI thông báo
|
|
||||||
if (notificationText != null)
|
if (notificationText != null)
|
||||||
{
|
{
|
||||||
notificationText.SetActive(false);
|
notificationText.SetActive(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Khi UI đã xử lý xong, mới chính thức tắt hoàn toàn GameObject rương
|
|
||||||
gameObject.SetActive(false);
|
gameObject.SetActive(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void HideTreasureModel()
|
private void HideTreasureModel()
|
||||||
{
|
{
|
||||||
// Tắt va chạm chính của rương
|
|
||||||
Collider col = GetComponent<Collider>();
|
Collider col = GetComponent<Collider>();
|
||||||
if (col != null) col.enabled = false;
|
if (col != null) col.enabled = false;
|
||||||
|
|
||||||
// Tắt model hiển thị 3D của rương (bao gồm cả object cha và con)
|
|
||||||
MeshRenderer[] renderers = GetComponentsInChildren<MeshRenderer>();
|
MeshRenderer[] renderers = GetComponentsInChildren<MeshRenderer>();
|
||||||
foreach (MeshRenderer r in renderers)
|
foreach (MeshRenderer r in renderers)
|
||||||
{
|
{
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user