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

96 lines
2.6 KiB
C#
Raw Normal View History

2026-06-02 23:14:19 +07:00
using UnityEngine;
2026-06-03 04:32:58 +07:00
using System.Collections;
2026-06-06 00:00:27 +07:00
using Hallucinate.Audio;
2026-06-02 23:14:19 +07:00
public class TreasureItem : MonoBehaviour
{
2026-06-03 04:32:58 +07:00
[Header("Cài đặt UI thông báo")]
2026-06-06 00:25:02 +07:00
[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;
2026-06-03 04:32:58 +07:00
2026-06-04 23:01:39 +07:00
[Header("Cài đặt Âm thanh")]
public string pickupSound = "Item_Pickup";
2026-06-06 00:00:27 +07:00
[Header("Cấu hình Tag")]
public string playerTag = "Player";
private bool isCollected = false;
private void Start()
{
if (notificationText != null)
{
notificationText.SetActive(false);
}
}
2026-06-02 23:14:19 +07:00
private void OnTriggerEnter(Collider other)
{
2026-06-06 00:00:27 +07:00
if (!isCollected && 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-06 00:00:27 +07:00
isCollected = true;
2026-06-04 17:49:04 +07:00
player.treasuresCollected++;
FinishGate gate = Object.FindAnyObjectByType<FinishGate>();
if (gate != null)
{
gate.UpdateStarsUI(player.treasuresCollected);
}
SetEnemiesAlertState(true);
2026-06-04 23:01:39 +07:00
if (AudioManager.Instance != null)
{
AudioManager.Instance.Play(pickupSound, position: transform.position);
}
2026-06-06 00:00:27 +07:00
StartCoroutine(HandlePickupRoutine());
2026-06-03 04:32:58 +07:00
}
2026-06-04 17:49:04 +07:00
}
}
private void SetEnemiesAlertState(bool state)
{
EnemyAI[] allEnemies = Object.FindObjectsByType<EnemyAI>(FindObjectsSortMode.None);
foreach (EnemyAI enemy in allEnemies)
{
2026-06-06 00:25:02 +07:00
if (enemy != null) enemy.playerHasArtifact = state;
2026-06-02 23:14:19 +07:00
}
}
2026-06-03 04:32:58 +07:00
2026-06-06 00:00:27 +07:00
private IEnumerator HandlePickupRoutine()
2026-06-03 04:32:58 +07:00
{
2026-06-06 00:00:27 +07:00
HideTreasureModel();
if (notificationText != null)
{
notificationText.SetActive(true);
}
2026-06-06 00:25:02 +07:00
yield return new WaitForSeconds(3f); // Tăng lên 3s để người chơi kịp đọc dòng chữ dài
2026-06-06 00:00:27 +07:00
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;
}
2026-06-03 04:32:58 +07:00
}
2026-06-06 00:00:27 +07:00
}