update
This commit is contained in:
@@ -37,6 +37,16 @@ public class EnemyAI : MonoBehaviour
|
||||
public float maxShootDelay = 3f;
|
||||
public float rotateSpeed = 50f;
|
||||
|
||||
[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 Mechanics")]
|
||||
public float dodgeForce = 8f; // Lực đẩy văng đi
|
||||
public float dodgeDuration = 0.5f; // Thời gian nhào lộn/né
|
||||
@@ -54,6 +64,7 @@ public class EnemyAI : MonoBehaviour
|
||||
{
|
||||
agent = GetComponent<NavMeshAgent>();
|
||||
rb = GetComponent<Rigidbody>();
|
||||
chatBubble = GetComponentInChildren<Hallucinate.UI.ChatBubble>(true);
|
||||
// Tự động tìm các điểm tuần tra nếu chưa gán
|
||||
if (patrolPoints == null || patrolPoints.Length == 0)
|
||||
{
|
||||
@@ -148,7 +159,14 @@ public class EnemyAI : MonoBehaviour
|
||||
new TaskNode(ActionInvestigate)
|
||||
});
|
||||
|
||||
// 4. Không có gì -> Tuần tra theo điểm
|
||||
// 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)
|
||||
});
|
||||
|
||||
// 5. Không có gì -> Tuần tra theo điểm
|
||||
var patrolNode = new TaskNode(ActionPatrol);
|
||||
|
||||
behaviorTreeRoot = new Selector(new List<Node>
|
||||
@@ -156,19 +174,46 @@ public class EnemyAI : MonoBehaviour
|
||||
laserSequence,
|
||||
chaseSequence,
|
||||
investigateSequence,
|
||||
talkSequence,
|
||||
patrolNode
|
||||
});
|
||||
}
|
||||
|
||||
#region CONDITIONS
|
||||
|
||||
private NodeState CheckCanTalkToNPC()
|
||||
{
|
||||
if (playerHasArtifact || 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 (canSeePlayer) StopConversation();
|
||||
return canSeePlayer ? NodeState.Success : NodeState.Failure;
|
||||
}
|
||||
|
||||
@@ -263,6 +308,78 @@ public class EnemyAI : MonoBehaviour
|
||||
Debug.Log("Laser Shot!");
|
||||
}
|
||||
|
||||
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
|
||||
#region DODGE MECHANIC
|
||||
|
||||
|
||||
Reference in New Issue
Block a user