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

60 lines
2.0 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-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-04 17:49:04 +07:00
public GameObject notificationText; // Text "Đã nhặt Cổ vật"
2026-06-03 04:32:58 +07:00
2026-06-02 23:14:19 +07:00
private void OnTriggerEnter(Collider other)
{
2026-06-04 17:49:04 +07:00
if (other.CompareTag("Player"))
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-04 17:49:04 +07:00
// 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);
if (notificationText != null)
{
StopAllCoroutines();
StartCoroutine(ShowNotification());
}
// Biến mất rương
gameObject.SetActive(false);
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)
{
enemy.playerHasArtifact = state;
2026-06-02 23:14:19 +07:00
}
}
2026-06-03 04:32:58 +07:00
IEnumerator ShowNotification()
{
notificationText.SetActive(true);
yield return new WaitForSeconds(2f);
notificationText.SetActive(false);
}
2026-06-04 17:49:04 +07:00
}