Files
BABA_YAGA/Assets/Scripts/Cho mon AI/TreasureItem.cs
manhduyhoang90 a7e07a007f Update ui
2026-06-06 00:25:02 +07:00

96 lines
2.6 KiB
C#

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<PlayerInventory>();
if (player == null) player = other.GetComponentInParent<PlayerInventory>();
if (player != null)
{
isCollected = true;
player.treasuresCollected++;
FinishGate gate = Object.FindAnyObjectByType<FinishGate>();
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<EnemyAI>(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<Collider>();
if (col != null) col.enabled = false;
MeshRenderer[] renderers = GetComponentsInChildren<MeshRenderer>();
foreach (MeshRenderer r in renderers)
{
r.enabled = false;
}
}
}