using UnityEngine; using System.Collections; using Hallucinate.Audio; public class TreasureItem : MonoBehaviour { [Header("Cài đặt UI thông báo")] [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")] public string pickupSound = "Item_Pickup"; [Header("Cấu hình Tag")] public string playerTag = "Player"; private bool isCollected = false; private void Start() { if (notificationText != null) { notificationText.SetActive(false); } } private void OnTriggerEnter(Collider other) { if (!isCollected && other.CompareTag(playerTag)) { PlayerInventory player = other.GetComponentInChildren(); if (player == null) player = other.GetComponentInParent(); if (player != null) { isCollected = true; player.treasuresCollected++; FinishGate gate = Object.FindAnyObjectByType(); if (gate != null) { gate.UpdateStarsUI(player.treasuresCollected); } SetEnemiesAlertState(true); if (AudioManager.Instance != null) { AudioManager.Instance.Play(pickupSound, position: transform.position); } StartCoroutine(HandlePickupRoutine()); } } } private void SetEnemiesAlertState(bool state) { EnemyAI[] allEnemies = Object.FindObjectsByType(FindObjectsSortMode.None); foreach (EnemyAI enemy in allEnemies) { if (enemy != null) enemy.playerHasArtifact = state; } } private IEnumerator HandlePickupRoutine() { HideTreasureModel(); if (notificationText != null) { notificationText.SetActive(true); } yield return new WaitForSeconds(3f); // Tăng lên 3s để người chơi kịp đọc dòng chữ dài if (notificationText != null) { notificationText.SetActive(false); } gameObject.SetActive(false); } private void HideTreasureModel() { Collider col = GetComponent(); if (col != null) col.enabled = false; MeshRenderer[] renderers = GetComponentsInChildren(); foreach (MeshRenderer r in renderers) { r.enabled = false; } } }