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

117 lines
3.8 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:00:27 +07:00
public GameObject notificationText; // Kéo Text "Đã nhặt Cổ vật" vào đây
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";
// Biến cờ để tránh việc va chạm 2 lần trong cùng 1 frame
private bool isCollected = false;
private void Start()
{
// Đảm bảo UI thông báo luôn tắt khi bắt đầu game
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
// 1. Chỉ xử lý khi chạm đúng Player và rương chưa bị nhặt
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
// Khóa lại để không bị kích hoạt nhiều lần
isCollected = true;
// 2. Tăng số lượng rương đang giữ
2026-06-04 17:49:04 +07:00
player.treasuresCollected++;
Debug.Log($"<color=cyan>[Chest]</color> NHẶT THÀNH CÔNG! Số rương hiện tại: {player.treasuresCollected}");
2026-06-06 00:00:27 +07:00
// 3. Cập nhật sao trên HUD (Sử dụng hàm Unity 6: FindAnyObjectByType)
2026-06-04 17:49:04 +07:00
FinishGate gate = Object.FindAnyObjectByType<FinishGate>();
if (gate != null)
{
gate.UpdateStarsUI(player.treasuresCollected);
}
2026-06-06 00:00:27 +07:00
// 4. Kích hoạt trạng thái truy đuổi cho toàn bộ Enemy AI
2026-06-04 17:49:04 +07:00
SetEnemiesAlertState(true);
2026-06-06 00:00:27 +07:00
// 5. Chạy âm thanh nhặt đồ
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
// 6. Xử lý hiện UI và làm biến mất rương an toàn
StartCoroutine(HandlePickupRoutine());
2026-06-03 04:32:58 +07:00
}
2026-06-04 17:49:04 +07:00
}
}
private void SetEnemiesAlertState(bool state)
{
2026-06-06 00:00:27 +07:00
// Sử dụng hàm chuẩn Unity 6+
2026-06-04 17:49:04 +07:00
EnemyAI[] allEnemies = Object.FindObjectsByType<EnemyAI>(FindObjectsSortMode.None);
foreach (EnemyAI enemy in allEnemies)
{
2026-06-06 00:00:27 +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
// Ẩn rương đi trước (tắt hiển thị và va chạm)
HideTreasureModel();
// Bật UI thông báo "Đã nhặt Cổ vật"
if (notificationText != null)
{
notificationText.SetActive(true);
}
// Chờ 2 giây
2026-06-03 04:32:58 +07:00
yield return new WaitForSeconds(2f);
2026-06-06 00:00:27 +07:00
// Tắt UI thông báo
if (notificationText != null)
{
notificationText.SetActive(false);
}
// Khi UI đã xử lý xong, mới chính thức tắt hoàn toàn GameObject rương
gameObject.SetActive(false);
}
private void HideTreasureModel()
{
// Tắt va chạm chính của rương
Collider col = GetComponent<Collider>();
if (col != null) col.enabled = false;
// Tắt model hiển thị 3D của rương (bao gồm cả object cha và con)
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
}