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

375 lines
12 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using System.Linq;
[RequireComponent(typeof(NavMeshAgent))]
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(FieldOfView))]
public class EnemyAI : MonoBehaviour
{
[Header("References")]
public Transform player;
private NavMeshAgent agent;
private Rigidbody rb;
private FieldOfView fov;
[Header("Movement & Rotation")]
public float moveSpeed = 3f;
public float rotateSpeed = 50f;
[Header("Patrol Waypoints")]
public Transform[] patrolPoints;
public float patrolWaitTime = 2f;
private int currentPatrolIndex = 0;
private float currentWaitTime;
[Header("Artifact State")]
public bool playerHasArtifact;
[Header("Laser Weapon")]
public GameObject laserPrefab;
public Transform firePoint;
public float minShootDelay = 1f;
public float maxShootDelay = 3f;
private float nextShootTime;
[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;
[Header("Dodge Settings (Rigidbody)")]
public float dodgeForce = 8f;
public float dodgeDuration = 0.25f;
public float dodgeCooldown = 1.5f;
private bool isDodging = false;
private float nextDodgeTime;
// Gốc của Cây hành vi
public Node behaviorTreeRoot;
private void Start()
{
agent = GetComponent<NavMeshAgent>();
rb = GetComponent<Rigidbody>();
fov = GetComponent<FieldOfView>();
chatBubble = GetComponentInChildren<Hallucinate.UI.ChatBubble>(true);
agent.speed = moveSpeed;
// Tự động tìm tất cả điểm PatrolPoint trong Map
patrolPoints = GameObject.FindGameObjectsWithTag("PatrolPoint")
.Select(go => go.transform).ToArray();
// Cấu hình Rigidbody để không bị đổ ngã khi va chạm vật lý thông thường
rb.isKinematic = true;
rb.freezeRotation = true;
FindPlayer();
InitBehaviorTree();
}
private void Update()
{
if (player == null) FindPlayer();
// Thực thi cây hành vi liên tục mỗi khung hình
behaviorTreeRoot?.Evaluate();
}
private void FindPlayer()
{
GameObject playerObj = GameObject.FindGameObjectWithTag("Player");
if (playerObj != null) player = playerObj.transform;
}
private void InitBehaviorTree()
{
// Ưu tiên số 1: Kiểm tra và thực hiện né đòn
var dodgeNode = new TaskNode(CheckAndActionDodge);
// Ưu tiên số 2: Có cổ vật -> Đứng lại tập trung bắn hạ
var laserSequence = new Sequence(new List<Node>
{
new TaskNode(CheckHasArtifact),
new TaskNode(ActionFocusAndShoot)
});
// Ưu tiên số 3: Tương tác tầm nhìn (Đuổi theo hoặc Đi kiểm tra vết tích)
var trackingSelector = new Selector(new List<Node>
{
// Nhìn thấy trực tiếp -> dí theo
new Sequence(new List<Node> { new TaskNode(CheckCanSeePlayer), new TaskNode(ActionChasePlayer) }),
// Mất dấu -> đi đến vị trí cuối cùng để điều tra
new Sequence(new List<Node> { new TaskNode(CheckHasInvestigateTarget), new TaskNode(ActionInvestigate) })
});
// Ưu tiên số 4: 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 số 5: Mặc định đi tuần tra vòng quanh Map
var patrolNode = new TaskNode(ActionPatrol);
// Tạo cây tổng hợp theo thứ tự ưu tiên từ trên xuống dưới
behaviorTreeRoot = new Selector(new List<Node>
{
dodgeNode,
laserSequence,
trackingSelector,
talkSequence,
patrolNode
});
}
#region CONDITIONS & COMPOSITE NODES
private NodeState CheckAndActionDodge()
{
if (isDodging) return NodeState.Running;
// ĐIỀU KIỆN NÉ: Phải nhìn thấy Player VÀ Player nhấn chuột trái VÀ hết cooldown né
if (fov.canSeePlayer && Input.GetMouseButtonDown(0) && Time.time >= nextDodgeTime)
{
StartCoroutine(DodgeRollRoutine());
nextDodgeTime = Time.time + dodgeCooldown;
return NodeState.Running;
}
return NodeState.Failure;
}
private NodeState CheckCanTalkToNPC()
{
if (playerHasArtifact || fov.canSeePlayer) 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;
}
private NodeState CheckHasArtifact()
{
if (playerHasArtifact) StopConversation();
return playerHasArtifact ? NodeState.Success : NodeState.Failure;
}
private NodeState CheckCanSeePlayer()
{
if (fov.canSeePlayer) StopConversation();
return fov.canSeePlayer ? NodeState.Success : NodeState.Failure;
}
private NodeState CheckHasInvestigateTarget()
{
return fov.lastKnownPlayerPosition != Vector3.zero ? NodeState.Success : NodeState.Failure;
}
#endregion
#region ACTIONS
// Coroutine xử lý né bằng lực đẩy Rigidbody một cách thực tế
private IEnumerator DodgeRollRoutine()
{
isDodging = true;
agent.enabled = false; // Tắt định vị NavMesh để nhường quyền cho Vật lý
rb.isKinematic = false; // Bật chế độ vật lý động để nhận lực lực đẩy
// Tính toán hướng né: Vuông góc với hướng nhìn của Player (Tránh sang trái hoặc phải)
Vector3 directionToPlayer = (player.position - transform.position).normalized;
Vector3 perpendicularDir = new Vector3(-directionToPlayer.z, 0, directionToPlayer.x);
// Chọn ngẫu nhiên trái hoặc phải
Vector3 dodgeDirection = (Random.Range(0, 2) == 0 ? perpendicularDir : -perpendicularDir).normalized;
// Tác dụng lực đẩy Impulse tức thì
rb.AddForce(dodgeDirection * dodgeForce, ForceMode.Impulse);
yield return new WaitForSeconds(dodgeDuration);
// Kết thúc né: Trả lại quyền điều khiển cho NavMeshAgent
rb.linearVelocity = Vector3.zero; // Cú pháp chuẩn của Unity 6 (thay cho rb.velocity)
rb.isKinematic = true;
agent.enabled = true;
isDodging = false;
}
private NodeState ActionPatrol()
{
if (patrolPoints.Length == 0) return NodeState.Failure;
Debug.Log("Patrolling...");
agent.isStopped = false;
agent.speed = moveSpeed * 0.6f; // Đi tuần tra chậm rãi quay theo hướng đi tự động của NavMesh
agent.SetDestination(patrolPoints[currentPatrolIndex].position);
if (!agent.pathPending && agent.remainingDistance <= agent.stoppingDistance)
{
currentWaitTime += Time.deltaTime;
if (currentWaitTime >= patrolWaitTime)
{
currentPatrolIndex = (currentPatrolIndex + 1) % patrolPoints.Length;
currentWaitTime = 0f;
}
}
return NodeState.Running;
}
private NodeState ActionChasePlayer()
{
Debug.Log("Chasing Player!");
agent.isStopped = false;
agent.speed = moveSpeed; // Chạy nhanh hết tốc lực
agent.SetDestination(player.position);
return NodeState.Running;
}
private NodeState ActionInvestigate()
{
Debug.Log("Investigating Last Position...");
agent.isStopped = false;
agent.speed = moveSpeed * 0.8f;
agent.SetDestination(fov.lastKnownPlayerPosition);
if (!agent.pathPending && agent.remainingDistance <= agent.stoppingDistance)
{
// Đến nơi rồi mà không thấy ai, xóa vị trí cuối cùng để quay lại tuần tra
fov.lastKnownPlayerPosition = Vector3.zero;
return NodeState.Success;
}
return NodeState.Running;
}
private NodeState ActionFocusAndShoot()
{
Debug.Log("Focus and Shoot!");
agent.isStopped = true; // Dừng di chuyển để đứng ngắm bắn cố định
// Tự xoay người hướng thẳng về phía Player
Vector3 dir = player.position - transform.position;
dir.y = 0f;
if (dir != Vector3.zero)
{
Quaternion targetRotation = Quaternion.LookRotation(dir);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotateSpeed * Time.deltaTime);
}
// Đếm ngược thời gian bắn ngẫu nhiên
if (Time.time >= nextShootTime)
{
ShootLaser();
nextShootTime = Time.time + Random.Range(minShootDelay, maxShootDelay);
}
return NodeState.Running;
}
private void ShootLaser()
{
if (laserPrefab == null || firePoint == null) return;
Instantiate(laserPrefab, firePoint.position, firePoint.rotation);
}
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
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);
}
}
#endregion
}