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

394 lines
12 KiB
C#
Raw Normal View History

2026-06-05 14:10:16 +07:00
using System.Collections;
2026-05-30 17:41:31 +07:00
using System.Collections.Generic;
using UnityEngine;
2026-06-05 17:16:11 +07:00
using UnityEngine.AI;
2026-05-30 17:41:31 +07:00
2026-06-05 17:16:11 +07:00
// Quy trình ưu tiên: Né đòn --> Bắn hạ (Artifact) --> Đuổi theo (Vector) --> Điều tra --> Đi tuần
2026-06-05 14:10:16 +07:00
[RequireComponent(typeof(NavMeshAgent))]
[RequireComponent(typeof(Rigidbody))]
2026-06-05 15:59:33 +07:00
[RequireComponent(typeof(FieldOfView))]
2026-05-30 17:41:31 +07:00
public class EnemyAI : MonoBehaviour
{
2026-06-05 17:16:11 +07:00
[Header("Tham chieu he thong")]
2026-05-30 17:41:31 +07:00
public Transform player;
2026-06-05 15:59:33 +07:00
private NavMeshAgent agent;
private Rigidbody rb;
private FieldOfView fov;
2026-06-03 13:42:09 +07:00
2026-06-05 17:16:11 +07:00
[Header("Toc do & Di chuyen")]
2026-06-05 15:59:33 +07:00
public float moveSpeed = 3f;
2026-06-05 17:16:11 +07:00
public float rotateSpeed = 10f;
[Header("He thong di tuan (Waypoints)")]
public Transform[] patrolWaypoints;
public int currentWaypointIndex = 0;
public float patrolWaitTime = 2f;
private float currentWaitTime = 0f;
[Header("Trang thai Co vat")]
2026-06-03 13:42:09 +07:00
public bool playerHasArtifact;
2026-06-05 17:16:11 +07:00
[Header("Vu khi Laser")]
2026-06-03 13:42:09 +07:00
public GameObject laserPrefab;
public Transform firePoint;
public float minShootDelay = 1f;
public float maxShootDelay = 3f;
2026-06-05 15:59:33 +07:00
private float nextShootTime;
2026-06-03 13:42:09 +07:00
2026-06-05 17:16:11 +07:00
[Header("Co che Ne don (Vat ly)")]
2026-06-05 16:06:59 +07:00
public float dodgeForce = 10f;
public float dodgeDuration = 0.2f;
public float dodgeCooldown = 1.2f;
2026-06-05 14:10:16 +07:00
private bool isDodging = false;
2026-06-05 17:16:11 +07:00
private float nextDodgeTime = 0f;
[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;
public Node rootNode;
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:10:16 +07:00
rb = GetComponent<Rigidbody>();
2026-06-05 15:59:33 +07:00
fov = GetComponent<FieldOfView>();
2026-06-04 23:01:39 +07:00
2026-06-05 17:16:11 +07:00
// Thiết lập Rigidbody chuẩn Unity 6 để sẵn sàng nhận lực né
rb.isKinematic = true;
2026-06-05 15:59:33 +07:00
rb.freezeRotation = true;
2026-06-05 17:16:11 +07:00
if (player == null)
{
GameObject playerObj = GameObject.FindGameObjectWithTag("Player");
if (playerObj != null) player = playerObj.transform;
}
chatBubble = GetComponentInChildren<Hallucinate.UI.ChatBubble>(true);
InitTree();
2026-06-05 14:10:16 +07:00
}
2026-06-05 17:16:11 +07:00
void InitTree()
2026-05-30 17:41:31 +07:00
{
2026-06-05 17:16:11 +07:00
// 1. Nhánh Né đòn (Khi player bấm chuột trái tấn công)
var dodgeSequence = new Sequence(new List<Node>
{
new TaskNode(CheckDodgeConditions),
new TaskNode(ActionDodge)
});
2026-06-05 15:59:33 +07:00
2026-06-05 17:16:11 +07:00
// 2. Nhánh Tấn công Laser (Khi player lấy được cổ vậ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 17:16:11 +07:00
// 3. Nhánh Đuổi theo (Sử dụng agent.Move di chuyển thẳng bằng Vector)
var chaseSequence = new Sequence(new List<Node>
2026-06-05 14:10:16 +07:00
{
2026-06-05 17:16:11 +07:00
new TaskNode(CheckCanSeePlayer),
new TaskNode(ActionChasePlayer)
2026-06-05 14:10:16 +07:00
});
2026-06-05 17:16:11 +07:00
// 4. Nhánh Điều tra (Khi mất dấu, đi kiểm tra vị trí cuối cùng)
var investigateSequence = new Sequence(new List<Node>
{
new TaskNode(CheckHasInvestigateTarget),
new TaskNode(ActionInvestigate)
});
var talkSequence = new Sequence(new List<Node>
{
new TaskNode(CheckCanTalkToNPC),
new TaskNode(ActionTalk)
});
// 5. Nhánh Đi tuần mặc định
var patrolAction = new TaskNode(ActionPatrol);
2026-06-03 13:42:09 +07:00
2026-06-05 17:16:11 +07:00
// Xây dựng cây tổng hợp theo thứ tự ưu tiên từ trên xuống dưới
rootNode = new Selector(new List<Node>
2026-05-30 17:41:31 +07:00
{
2026-06-05 17:16:11 +07:00
dodgeSequence,
2026-06-03 13:42:09 +07:00
laserSequence,
2026-06-05 17:16:11 +07:00
chaseSequence,
investigateSequence,
talkSequence,
patrolAction
2026-05-30 17:41:31 +07:00
});
}
2026-06-05 17:16:11 +07:00
void Update()
{
if (player == null) return;
rootNode?.Evaluate();
}
2026-06-05 15:59:33 +07:00
2026-06-05 17:16:11 +07:00
#region CONDITIONS (CAC HAM KIEM TRA)
private NodeState CheckDodgeConditions()
2026-06-05 15:59:33 +07:00
{
2026-06-05 17:16:11 +07:00
// Nếu đang trong quá trình né thì luôn cho phép chạy tiếp hành động né
if (isDodging) return NodeState.Success;
2026-06-05 15:59:33 +07:00
2026-06-05 17:16:11 +07:00
// Điều kiện kích hoạt né: Thấy player + Player nhấn chuột trái + Hết cooldown
2026-06-05 15:59:33 +07:00
if (fov.canSeePlayer && Input.GetMouseButtonDown(0) && Time.time >= nextDodgeTime)
{
2026-06-05 17:16:11 +07:00
return NodeState.Success;
2026-06-05 15:59:33 +07:00
}
return NodeState.Failure;
}
2026-06-03 13:42:09 +07:00
private NodeState CheckHasArtifact()
{
2026-06-05 17:16:11 +07:00
if (playerHasArtifact) 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-05 17:16:11 +07:00
2026-06-05 15:59:33 +07:00
return fov.canSeePlayer ? NodeState.Success : NodeState.Failure;
2026-06-05 14:10:16 +07:00
}
2026-06-03 13:42:09 +07:00
2026-06-05 15:59:33 +07:00
private NodeState CheckHasInvestigateTarget()
2026-06-05 14:10:16 +07:00
{
2026-06-05 15:59:33 +07:00
return fov.lastKnownPlayerPosition != Vector3.zero ? NodeState.Success : NodeState.Failure;
2026-06-03 13:42:09 +07:00
}
#endregion
2026-06-05 17:16:11 +07:00
#region ACTIONS (CAC HAM HANH DONG)
private NodeState ActionDodge()
{
if (!isDodging)
{
StartCoroutine(DodgeRollRoutine());
nextDodgeTime = Time.time + dodgeCooldown;
}
return NodeState.Running;
}
2026-06-03 13:42:09 +07:00
2026-06-05 15:59:33 +07:00
private IEnumerator DodgeRollRoutine()
{
isDodging = true;
2026-06-05 17:16:11 +07:00
agent.enabled = false; // Tắt định vị để nhường quyền cho Vật lý
rb.isKinematic = false; // Bật chế độ vật lý động
2026-06-05 15:59:33 +07:00
2026-06-05 17:16:11 +07:00
// Tính hướng né vuông góc với Player
2026-06-05 15:59:33 +07:00
Vector3 directionToPlayer = (player.position - transform.position).normalized;
Vector3 perpendicularDir = new Vector3(-directionToPlayer.z, 0, directionToPlayer.x);
Vector3 dodgeDirection = (Random.Range(0, 2) == 0 ? perpendicularDir : -perpendicularDir).normalized;
2026-06-05 17:16:11 +07:00
// Đẩy bằng lực Impulse vật lý thực tế
2026-06-05 15:59:33 +07:00
rb.AddForce(dodgeDirection * dodgeForce, ForceMode.Impulse);
yield return new WaitForSeconds(dodgeDuration);
2026-06-05 17:16:11 +07:00
// Trả lại quyền cho NavMeshAgent sau khi né xong
rb.linearVelocity = Vector3.zero; // Cú pháp chuẩn của Unity 6
2026-06-05 15:59:33 +07:00
rb.isKinematic = true;
agent.enabled = true;
isDodging = false;
}
2026-06-05 16:06:59 +07:00
private NodeState ActionChasePlayer()
{
2026-06-05 17:16:11 +07:00
agent.isStopped = false;
agent.speed = moveSpeed;
2026-06-05 16:06:59 +07:00
// 1. Tính toán hướng đi thẳng tắp (Bỏ trục Y)
Vector3 dir = player.position - transform.position;
2026-06-05 17:16:11 +07:00
dir.y = 0f;
2026-06-05 16:06:59 +07:00
dir.Normalize();
2026-06-05 17:16:11 +07:00
// 2. Di chuyển tịnh tiến bằng Vector tích hợp qua Agent.Move
2026-06-05 16:06:59 +07:00
Vector3 movement = dir * moveSpeed * Time.deltaTime;
2026-06-05 17:16:11 +07:00
agent.Move(movement);
2026-06-05 16:06:59 +07:00
2026-06-05 17:16:11 +07:00
// 3. Xoay mượt mà theo hướng di chuyển của Vector đúng ý bạn
2026-06-05 16:06:59 +07:00
if (dir != Vector3.zero)
{
Quaternion targetRotation = Quaternion.LookRotation(dir);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotateSpeed * Time.deltaTime);
}
return NodeState.Running;
}
2026-06-05 17:16:11 +07:00
private NodeState CheckCanTalkToNPC()
2026-05-30 17:41:31 +07:00
{
2026-06-05 17:16:11 +07:00
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)
2026-06-05 14:10:16 +07:00
{
2026-06-05 17:16:11 +07:00
if (hit.gameObject != gameObject && hit.CompareTag("Enemy"))
2026-06-05 15:59:33 +07:00
{
2026-06-05 17:16:11 +07:00
EnemyAI other = hit.GetComponent<EnemyAI>();
if (other != null && !other.isTalking && Time.time >= other.lastTalkTime + talkCooldown)
{
talkingPartner = other;
return NodeState.Success;
}
2026-06-05 15:59:33 +07:00
}
2026-06-04 15:41:01 +07:00
}
2026-06-05 17:16:11 +07:00
return NodeState.Failure;
}
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();
}
2026-05-30 17:41:31 +07:00
return NodeState.Running;
}
2026-06-05 17:16:11 +07:00
private void StartNPCConversation()
2026-06-05 14:10:16 +07:00
{
2026-06-05 17:16:11 +07:00
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);
});
2026-06-05 14:10:16 +07:00
2026-06-05 17:16:11 +07:00
// 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)
2026-06-05 14:10:16 +07:00
{
2026-06-05 17:16:11 +07:00
transform.rotation = Quaternion.LookRotation(dir);
2026-06-05 14:10:16 +07:00
}
2026-05-30 17:41:31 +07:00
}
2026-06-03 13:42:09 +07:00
private NodeState ActionFocusAndShoot()
2026-05-30 17:41:31 +07:00
{
2026-06-05 17:16:11 +07:00
agent.isStopped = true; // Đứng im bắn cố định khi có cổ vật
2026-06-04 21:26:19 +07:00
2026-06-05 17:16:11 +07:00
// Tập trung xoay người nhìn về phía Player
2026-06-04 15:41:01 +07:00
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
2026-06-05 17:16:11 +07:00
// Cơ chế đếm ngược bắn Laser ngẫu nhiên giống code cũ của bạn
2026-06-03 13:42:09 +07:00
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-05 17:16:11 +07:00
Debug.Log("Laser Shot!");
}
private NodeState ActionInvestigate()
{
agent.isStopped = false;
agent.speed = moveSpeed * 0.7f;
agent.SetDestination(fov.lastKnownPlayerPosition);
// Đi tới vị trí cuối cùng nhìn thấy Player
if (!agent.pathPending && agent.remainingDistance <= agent.stoppingDistance)
{
fov.lastKnownPlayerPosition = Vector3.zero; // Xóa vị trí nghi vấn để thoát trạng thái
return NodeState.Success;
}
return NodeState.Running;
}
private NodeState ActionPatrol()
{
if (patrolWaypoints == null || patrolWaypoints.Length == 0)
{
return NodeState.Failure;
}
agent.isStopped = false;
agent.speed = moveSpeed * 0.5f; // Đi tuần chậm rãi
var targetWaypoint = patrolWaypoints[currentWaypointIndex];
var distanceToTarget = Vector3.Distance(transform.position, targetWaypoint.position);
// Nếu đã đến gần điểm tuần tra الحالي
if (distanceToTarget < 1f)
{
currentWaitTime += Time.deltaTime;
// Chờ một khoảng thời gian trước khi chuyển sang điểm tiếp theo
if (currentWaitTime >= patrolWaitTime)
{
currentWaypointIndex = (currentWaypointIndex + 1) % patrolWaypoints.Length;
currentWaitTime = 0f;
}
}
agent.SetDestination(patrolWaypoints[currentWaypointIndex].position);
return NodeState.Running;
2026-06-03 13:42:09 +07:00
}
#endregion
2026-06-05 16:06:59 +07:00
}