Files
BABA_YAGA/Assets/Scripts/Cho mon AI/TreasureItem.cs
2026-06-04 23:01:39 +07:00

70 lines
2.3 KiB
C#

using UnityEngine;
using System.Collections;
using Hallucinate.Audio;
public class TreasureItem : MonoBehaviour
{
[Header("Cài đặt UI thông báo")]
public GameObject notificationText; // Text "Đã nhặt Cổ vật"
[Header("Cài đặt Âm thanh")]
public string pickupSound = "Item_Pickup";
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
PlayerInventory player = other.GetComponentInChildren<PlayerInventory>();
if (player == null) player = other.GetComponentInParent<PlayerInventory>();
if (player != null)
{
// 1. Tăng số lượng rương đang giữ
player.treasuresCollected++;
Debug.Log($"<color=cyan>[Chest]</color> NHẶT THÀNH CÔNG! Số rương hiện tại: {player.treasuresCollected}");
// 2. Cập nhật sao trên HUD ngay lập tức (Tìm FinishGate để mượn hàm update)
FinishGate gate = Object.FindAnyObjectByType<FinishGate>();
if (gate != null)
{
gate.UpdateStarsUI(player.treasuresCollected);
}
// 3. Kích hoạt trạng thái truy đuổi cho toàn bộ Enemy AI
SetEnemiesAlertState(true);
// 4. Chạy âm thanh nhặt đồ
if (AudioManager.Instance != null)
{
AudioManager.Instance.Play(pickupSound, position: transform.position);
}
if (notificationText != null)
{
StopAllCoroutines();
StartCoroutine(ShowNotification());
}
// Biến mất rương
gameObject.SetActive(false);
}
}
}
private void SetEnemiesAlertState(bool state)
{
EnemyAI[] allEnemies = Object.FindObjectsByType<EnemyAI>(FindObjectsSortMode.None);
foreach (EnemyAI enemy in allEnemies)
{
enemy.playerHasArtifact = state;
}
}
IEnumerator ShowNotification()
{
notificationText.SetActive(true);
yield return new WaitForSeconds(2f);
notificationText.SetActive(false);
}
}