Update
This commit is contained in:
@@ -34,6 +34,16 @@ public class EnemyAI : MonoBehaviour
|
||||
public string shootSound = "Enemy_Shoot";
|
||||
private bool hasSpottedPlayer; // Để chỉ kêu alert 1 lần
|
||||
|
||||
[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;
|
||||
|
||||
private float nextShootTime;
|
||||
private NavMeshAgent agent;
|
||||
|
||||
@@ -42,6 +52,7 @@ public class EnemyAI : MonoBehaviour
|
||||
private void Start()
|
||||
{
|
||||
agent = GetComponent<NavMeshAgent>();
|
||||
chatBubble = GetComponentInChildren<Hallucinate.UI.ChatBubble>(true);
|
||||
agent.speed = moveSpeed;
|
||||
|
||||
// Lưu lại vị trí ban đầu để làm tâm của khu vực tuần tra
|
||||
@@ -75,35 +86,69 @@ public class EnemyAI : MonoBehaviour
|
||||
|
||||
private void InitBehaviorTree()
|
||||
{
|
||||
// Player có artifact -> focus + shoot
|
||||
// Ưu tiên 1: Player có artifact -> focus + shoot (Cao nhất)
|
||||
var laserSequence = new Sequence(new List<Node>
|
||||
{
|
||||
new TaskNode(CheckHasArtifact),
|
||||
new TaskNode(ActionFocusAndShoot)
|
||||
});
|
||||
|
||||
// Thấy player -> chạy tới
|
||||
// Ưu tiên 2: Thấy player -> rượt đuổi
|
||||
var chaseSequence = new Sequence(new List<Node>
|
||||
{
|
||||
new TaskNode(CheckCanSeePlayer),
|
||||
new TaskNode(ActionMoveToPlayer)
|
||||
});
|
||||
|
||||
// Không thấy ai -> Tuần tra bằng NavMesh
|
||||
// Ư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
|
||||
var patrolNode = new TaskNode(ActionPatrol);
|
||||
|
||||
behaviorTreeRoot = new Selector(new List<Node>
|
||||
{
|
||||
laserSequence,
|
||||
chaseSequence,
|
||||
talkSequence,
|
||||
patrolNode
|
||||
});
|
||||
}
|
||||
|
||||
#region CONDITIONS
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
private NodeState CheckHasArtifact()
|
||||
{
|
||||
// Khi bị phát hiện hoặc player có artifact, ngắt lời ngay
|
||||
if (playerHasArtifact || hasSpottedPlayer) StopConversation();
|
||||
return playerHasArtifact ? NodeState.Success : NodeState.Failure;
|
||||
}
|
||||
|
||||
@@ -119,11 +164,12 @@ public class EnemyAI : MonoBehaviour
|
||||
{
|
||||
hasSpottedPlayer = true;
|
||||
AudioManager.Instance?.Play(alertSound, position: transform.position);
|
||||
StopConversation(); // Ngắt hội thoại khi thấy player
|
||||
}
|
||||
return NodeState.Success;
|
||||
}
|
||||
|
||||
hasSpottedPlayer = false; // Reset nếu player ra khỏi tầm mắt
|
||||
hasSpottedPlayer = false;
|
||||
return NodeState.Failure;
|
||||
}
|
||||
|
||||
@@ -131,6 +177,78 @@ public class EnemyAI : MonoBehaviour
|
||||
|
||||
#region ACTIONS
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
private NodeState ActionPatrol()
|
||||
{
|
||||
// Debug.Log("Patrolling...");
|
||||
|
||||
Reference in New Issue
Block a user