Files
BABA_YAGA/Assets/Scripts/AI NPC/EnemyAI.cs

340 lines
10 KiB
C#
Raw Normal View History

2026-05-30 17:41:31 +07:00
using System.Collections.Generic;
using UnityEngine;
2026-06-04 15:41:01 +07:00
using UnityEngine.AI; // Cần thiết để dùng NavMesh
2026-06-04 23:01:39 +07:00
using Hallucinate.Audio;
2026-05-30 17:41:31 +07:00
2026-06-04 15:41:01 +07:00
[RequireComponent(typeof(NavMeshAgent))] // Tự động thêm component này nếu chưa có
2026-05-30 17:41:31 +07:00
public class EnemyAI : MonoBehaviour
{
2026-06-03 13:42:09 +07:00
[Header("References")]
2026-05-30 17:41:31 +07:00
public Transform player;
2026-06-03 13:42:09 +07:00
[Header("Detection")]
2026-05-30 17:41:31 +07:00
public float detectRange = 10f;
public float moveSpeed = 3f;
public float rotateSpeed = 50f;
2026-06-03 13:42:09 +07:00
2026-06-04 15:41:01 +07:00
[Header("Patrol Area")]
public float patrolRadius = 15f; // Bán kính khu vực tuần tra
public float patrolWaitTime = 2f; // Thời gian đứng chờ trước khi đi điểm khác
private Vector3 startPosition;
private float currentWaitTime;
2026-06-03 13:42:09 +07:00
[Header("Artifact")]
public bool playerHasArtifact;
[Header("Laser")]
public GameObject laserPrefab;
public Transform firePoint;
public float minShootDelay = 1f;
public float maxShootDelay = 3f;
2026-06-04 23:01:39 +07:00
[Header("Audio")]
public string alertSound = "Enemy_Alert";
public string shootSound = "Enemy_Shoot";
private bool hasSpottedPlayer; // Để chỉ kêu alert 1 lần
2026-06-05 14:57:25 +07:00
[Header("Conversation")]
public string npcName = "Guard";
public string persona = "You are a grumpy guard protecting gold.";
public float talkRange = 4f;
public float talkCooldown = 30f;
private float lastTalkTime;
private bool isTalking;
private EnemyAI talkingPartner;
private Hallucinate.UI.ChatBubble chatBubble;
2026-06-03 13:42:09 +07:00
private float nextShootTime;
2026-06-04 15:41:01 +07:00
private NavMeshAgent agent;
2026-05-30 17:41:31 +07:00
public Node behaviorTreeRoot;
2026-06-03 13:42:09 +07:00
private void Start()
2026-05-30 17:41:31 +07:00
{
2026-06-04 15:41:01 +07:00
agent = GetComponent<NavMeshAgent>();
2026-06-05 14:57:25 +07:00
chatBubble = GetComponentInChildren<Hallucinate.UI.ChatBubble>(true);
2026-06-04 15:41:01 +07:00
agent.speed = moveSpeed;
2026-06-04 23:01:39 +07:00
2026-06-04 15:41:01 +07:00
// Lưu lại vị trí ban đầu để làm tâm của khu vực tuần tra
startPosition = transform.position;
2026-06-04 23:01:39 +07:00
2026-06-03 13:42:09 +07:00
nextShootTime = Time.time + Random.Range(minShootDelay, maxShootDelay);
2026-05-30 17:41:31 +07:00
InitBehaviorTree();
2026-06-04 15:41:01 +07:00
FindPlayer();
2026-05-30 17:41:31 +07:00
}
2026-06-03 13:42:09 +07:00
private void Update()
2026-05-30 17:41:31 +07:00
{
2026-06-04 15:41:01 +07:00
// Nếu mất reference (Player chết hoặc chưa spawn), liên tục tìm lại
if (player == null)
{
FindPlayer();
}
// Chỉ chạy AI nếu đã tìm thấy player (hoặc bạn có thể cho tuần tra ngay cả khi chưa có player tùy logic game)
2026-06-03 13:42:09 +07:00
behaviorTreeRoot?.Evaluate();
2026-05-30 17:41:31 +07:00
}
2026-06-04 15:41:01 +07:00
private void FindPlayer()
{
GameObject playerObj = GameObject.FindGameObjectWithTag("Player");
if (playerObj != null)
{
player = playerObj.transform;
}
}
2026-06-03 13:42:09 +07:00
private void InitBehaviorTree()
2026-05-30 17:41:31 +07:00
{
2026-06-05 14:57:25 +07:00
// Ưu tiên 1: Player có artifact -> focus + shoot (Cao nhất)
2026-06-03 13:42:09 +07:00
var laserSequence = new Sequence(new List<Node>
{
new TaskNode(CheckHasArtifact),
new TaskNode(ActionFocusAndShoot)
});
2026-06-05 14:57:25 +07:00
// Ưu tiên 2: Thấy player -> rượt đuổi
2026-05-30 17:41:31 +07:00
var chaseSequence = new Sequence(new List<Node>
{
new TaskNode(CheckCanSeePlayer),
new TaskNode(ActionMoveToPlayer)
});
2026-06-03 13:42:09 +07:00
2026-06-05 14:57:25 +07:00
// Ưu tiên 3: Gần NPC khác -> nói chuyện (Mới)
var talkSequence = new Sequence(new List<Node>
{
new TaskNode(CheckCanTalkToNPC),
new TaskNode(ActionTalk)
});
// Ưu tiên cuối: Tuần tra
2026-06-04 15:41:01 +07:00
var patrolNode = new TaskNode(ActionPatrol);
2026-06-03 13:42:09 +07:00
2026-05-30 17:41:31 +07:00
behaviorTreeRoot = new Selector(new List<Node>
{
2026-06-03 13:42:09 +07:00
laserSequence,
2026-05-30 17:41:31 +07:00
chaseSequence,
2026-06-05 14:57:25 +07:00
talkSequence,
2026-06-04 15:41:01 +07:00
patrolNode
2026-05-30 17:41:31 +07:00
});
}
2026-06-03 13:42:09 +07:00
#region CONDITIONS
2026-06-05 14:57:25 +07:00
private NodeState CheckCanTalkToNPC()
{
if (playerHasArtifact || hasSpottedPlayer) return NodeState.Failure;
if (Time.time < lastTalkTime + talkCooldown) return NodeState.Failure;
if (isTalking) return NodeState.Success;
// Tìm NPC gần nhất
Collider[] hitColliders = Physics.OverlapSphere(transform.position, talkRange);
foreach (var hit in hitColliders)
{
if (hit.gameObject != gameObject && hit.CompareTag("Enemy"))
{
EnemyAI other = hit.GetComponent<EnemyAI>();
if (other != null && !other.isTalking && Time.time >= other.lastTalkTime + talkCooldown)
{
talkingPartner = other;
return NodeState.Success;
}
}
}
return NodeState.Failure;
}
2026-06-03 13:42:09 +07:00
private NodeState CheckHasArtifact()
{
2026-06-05 14:57:25 +07:00
// Khi bị phát hiện hoặc player có artifact, ngắt lời ngay
if (playerHasArtifact || hasSpottedPlayer) StopConversation();
2026-06-04 15:41:01 +07:00
return playerHasArtifact ? NodeState.Success : NodeState.Failure;
2026-06-03 13:42:09 +07:00
}
private NodeState CheckCanSeePlayer()
{
2026-06-04 15:41:01 +07:00
if (player == null) return NodeState.Failure;
2026-06-03 13:42:09 +07:00
2026-06-04 15:41:01 +07:00
float distance = Vector3.Distance(transform.position, player.position);
2026-06-03 13:42:09 +07:00
if (distance <= detectRange)
{
2026-06-04 23:01:39 +07:00
if (!hasSpottedPlayer)
{
hasSpottedPlayer = true;
AudioManager.Instance?.Play(alertSound, position: transform.position);
2026-06-05 14:57:25 +07:00
StopConversation(); // Ngắt hội thoại khi thấy player
2026-06-04 23:01:39 +07:00
}
2026-06-03 13:42:09 +07:00
return NodeState.Success;
}
2026-06-05 14:57:25 +07:00
hasSpottedPlayer = false;
2026-06-03 13:42:09 +07:00
return NodeState.Failure;
}
#endregion
#region ACTIONS
2026-06-05 14:57:25 +07:00
private NodeState ActionTalk()
{
if (talkingPartner == null) return NodeState.Failure;
if (!isTalking)
{
isTalking = true;
agent.isStopped = true;
// Xoay về phía bạn
FaceTarget(talkingPartner.transform.position);
// Bắt đầu hội thoại qua Gemini
StartNPCConversation();
}
return NodeState.Running;
}
private void StartNPCConversation()
{
string prompt = $"You are {npcName}. You are talking to your fellow guard {talkingPartner.npcName}. " +
"Keep it short (1 sentence). Topic: gold security or complaining about work.";
Hallucinate.AI.GeminiService.Instance.GetResponse(persona, prompt, (response) => {
if (chatBubble != null) chatBubble.Show(response);
// Hẹn giờ kết thúc hội thoại
Invoke(nameof(EndConversation), 5f);
});
// Thông báo cho bạn diễn cũng dừng lại để "nghe"
talkingPartner.OnPartnerTalked(this);
}
public void OnPartnerTalked(EnemyAI partner)
{
isTalking = true;
talkingPartner = partner;
agent.isStopped = true;
FaceTarget(partner.transform.position);
// Chờ bạn nói xong mới phản hồi (Tùy chọn: có thể thêm logic phản hồi ở đây)
Invoke(nameof(EndConversation), 6f);
}
private void EndConversation()
{
isTalking = false;
lastTalkTime = Time.time;
if (agent != null) agent.isStopped = false;
talkingPartner = null;
}
private void StopConversation()
{
if (!isTalking) return;
CancelInvoke(nameof(EndConversation));
EndConversation();
if (chatBubble != null) chatBubble.Show("Suỵt! Có gì đó không ổn...", 2f);
}
private void FaceTarget(Vector3 targetPos)
{
Vector3 dir = targetPos - transform.position;
dir.y = 0;
if (dir != Vector3.zero)
{
transform.rotation = Quaternion.LookRotation(dir);
}
}
2026-06-04 15:41:01 +07:00
private NodeState ActionPatrol()
2026-05-30 17:41:31 +07:00
{
2026-06-04 17:49:04 +07:00
// Debug.Log("Patrolling...");
2026-06-04 21:26:19 +07:00
if (!agent.isActiveAndEnabled || !agent.isOnNavMesh) return NodeState.Failure;
2026-06-04 15:41:01 +07:00
agent.isStopped = false; // Đảm bảo NPC được phép di chuyển
agent.speed = moveSpeed * 0.5f; // Đi dạo nên đi chậm lại một chút
2026-06-03 13:42:09 +07:00
2026-06-04 15:41:01 +07:00
// Kiểm tra xem NPC đã đến điểm đích chưa
if (!agent.pathPending && agent.remainingDistance <= agent.stoppingDistance)
{
currentWaitTime += Time.deltaTime;
// Chờ một lúc rồi mới chọn điểm mới
if (currentWaitTime >= patrolWaitTime)
{
// Tìm một điểm ngẫu nhiên trong bán kính cho trước
Vector3 randomDirection = Random.insideUnitSphere * patrolRadius;
randomDirection += startPosition;
NavMeshHit hit;
2026-06-04 23:01:39 +07:00
2026-06-04 15:41:01 +07:00
// Đảm bảo điểm ngẫu nhiên nằm trên bề mặt NavMesh hợp lệ
if (NavMesh.SamplePosition(randomDirection, out hit, patrolRadius, 1))
{
agent.SetDestination(hit.position);
}
currentWaitTime = 0f;
}
}
2026-06-03 13:42:09 +07:00
2026-05-30 17:41:31 +07:00
return NodeState.Running;
}
private NodeState ActionMoveToPlayer()
{
2026-06-04 15:41:01 +07:00
if (player == null) return NodeState.Failure;
2026-06-03 13:42:09 +07:00
2026-06-04 17:49:04 +07:00
// Debug.Log("Chasing Player");
2026-06-04 23:01:39 +07:00
2026-06-04 21:26:19 +07:00
if (!agent.isActiveAndEnabled || !agent.isOnNavMesh) return NodeState.Failure;
2026-06-04 15:41:01 +07:00
agent.isStopped = false;
agent.speed = moveSpeed; // Phục hồi tốc độ rượt đuổi
agent.SetDestination(player.position);
2026-06-03 13:42:09 +07:00
2026-05-30 17:41:31 +07:00
return NodeState.Running;
}
2026-06-03 13:42:09 +07:00
private NodeState ActionFocusAndShoot()
2026-05-30 17:41:31 +07:00
{
2026-06-04 15:41:01 +07:00
if (player == null) return NodeState.Failure;
2026-06-03 13:42:09 +07:00
2026-06-04 17:49:04 +07:00
// Debug.Log("Focus and Shoot!");
2026-06-04 23:01:39 +07:00
2026-06-04 21:26:19 +07:00
if (!agent.isActiveAndEnabled || !agent.isOnNavMesh) return NodeState.Failure;
2026-06-04 15:41:01 +07:00
// Dừng NavMeshAgent lại để đứng bắn, tránh bị trượt
agent.isStopped = true;
2026-06-03 13:42:09 +07:00
2026-06-04 15:41:01 +07:00
// Focus player
Vector3 dir = player.position - transform.position;
2026-06-03 13:42:09 +07:00
dir.y = 0f;
if (dir != Vector3.zero)
{
2026-06-04 15:41:01 +07:00
Quaternion targetRotation = Quaternion.LookRotation(dir);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotateSpeed * Time.deltaTime);
2026-05-30 17:41:31 +07:00
}
2026-06-03 13:42:09 +07:00
// Shoot with random delay
if (Time.time >= nextShootTime)
2026-05-30 17:41:31 +07:00
{
2026-06-03 13:42:09 +07:00
ShootLaser();
2026-06-04 15:41:01 +07:00
nextShootTime = Time.time + Random.Range(minShootDelay, maxShootDelay);
2026-05-30 17:41:31 +07:00
}
2026-06-03 13:42:09 +07:00
return NodeState.Running;
2026-05-30 17:41:31 +07:00
}
2026-06-03 13:42:09 +07:00
private void ShootLaser()
{
2026-06-04 15:41:01 +07:00
if (laserPrefab == null || firePoint == null) return;
Instantiate(laserPrefab, firePoint.position, firePoint.rotation);
2026-06-04 23:01:39 +07:00
AudioManager.Instance?.Play(shootSound, position: transform.position);
2026-06-04 17:49:04 +07:00
// Debug.Log("Laser Shot!");
2026-06-03 13:42:09 +07:00
}
#endregion
2026-05-30 17:41:31 +07:00
}