This commit is contained in:
2026-06-05 14:57:25 +07:00
parent 9499efe518
commit 05187d12a7
10 changed files with 654 additions and 104 deletions

View File

@@ -0,0 +1,42 @@
using UnityEngine;
using TMPro;
using PrimeTween;
namespace Hallucinate.UI
{
public class ChatBubble : MonoBehaviour
{
[SerializeField] private TextMeshProUGUI textDisplay;
[SerializeField] private CanvasGroup canvasGroup;
[SerializeField] private RectTransform bubbleRect;
private Transform mainCameraTransform;
private void Awake()
{
mainCameraTransform = Camera.main.transform;
canvasGroup.alpha = 0;
gameObject.SetActive(false);
}
private void LateUpdate()
{
// Billboard effect
transform.LookAt(transform.position + mainCameraTransform.rotation * Vector3.forward, mainCameraTransform.rotation * Vector3.up);
}
public void Show(string text, float duration = 4f)
{
gameObject.SetActive(true);
textDisplay.text = text;
// Animation using PrimeTween
PrimeTween.Sequence.Create()
.Group(Tween.Alpha(canvasGroup, 1f, 0.3f))
.Group(Tween.Scale(bubbleRect, Vector3.zero, Vector3.one, 0.4f, Ease.OutBack))
.Chain(Tween.Delay(duration))
.Chain(Tween.Alpha(canvasGroup, 0f, 0.5f))
.OnComplete(() => gameObject.SetActive(false));
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: ea510cea4b9ed1547ae4725a2ded949a

View File

@@ -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...");

View File

@@ -0,0 +1,76 @@
using System;
using System.Collections;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
namespace Hallucinate.AI
{
[Serializable]
public class Part { public string text; }
[Serializable]
public class Content { public Part[] parts; }
[Serializable]
public class Candidate { public Content content; }
[Serializable]
public class GeminiResponse { public Candidate[] candidates; }
public class GeminiService : MonoBehaviour
{
public static GeminiService Instance { get; private set; }
[SerializeField] private string apiKey = "AQ.Ab8RN6I2hU_p8yHiPNNHtWzYBiLugbPP22gC6lzTWaYEWj4v0g"; // Replace with your key
[SerializeField] private string geminiURL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-flash-latest:generateContent";
private void Awake()
{
if (Instance == null) { Instance = this; DontDestroyOnLoad(gameObject); }
else { Destroy(gameObject); }
}
public void GetResponse(string persona, string prompt, Action<string> onComplete)
{
StartCoroutine(PostRequest(persona, prompt, onComplete));
}
private IEnumerator PostRequest(string persona, string prompt, Action<string> onComplete)
{
var jsonBody = $@"{{
""systemInstruction"": {{""parts"": [{{ ""text"": ""{persona}"" }}]}},
""contents"": [{{""parts"": [{{ ""text"": ""{prompt}"" }}]}}]
}}";
var requestURL = $"{geminiURL}?key={apiKey}";
using (var request = new UnityWebRequest(requestURL, "POST"))
{
byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonBody);
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
request.downloadHandler = new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.Success)
{
try
{
var response = JsonUtility.FromJson<GeminiResponse>(request.downloadHandler.text);
if (response?.candidates != null && response.candidates.Length > 0)
{
onComplete?.Invoke(response.candidates[0].content.parts[0].text);
}
}
catch (Exception e) { Debug.LogError($"[Gemini] JSON Parse Error: {e.Message}"); }
}
else
{
Debug.LogError($"[Gemini] API Error: {request.error}");
}
}
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: a859fc8e9ec10a347a3704b6045ca7e8

View File

@@ -1,44 +1,13 @@
using System;
using System.Collections;
using System.Text;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Networking;
using System.Collections;
using Hallucinate.Audio;
[Serializable]
public class Part
{
public string text;
}
[Serializable]
public class Content
{
public Part[] parts;
}
[Serializable]
public class Candidate
{
public Content content;
}
[Serializable]
public class GeminiResponse
{
public Candidate[] candidates;
}
using Hallucinate.AI;
public class GerminiNPC : MonoBehaviour
{
[SerializeField]
private string apiKey = "AQ.Ab8RN6I2hU_p8yHiPNNHtWzYBiLugbPP22gC6lzTWaYEWj4v0g";
[SerializeField]
private string germiniURL =
"https://generativelanguage.googleapis.com/v1beta/models/gemini-flash-latest:generateContent";
public string npcPersona =
private string npcPersona =
"Ngươi là một lão thợ rèn cọc cằn tên là Tom, ngươi rất ghét những kẻ mang phế liệu đến tiệm của mình. Chỉ trả lời ngắn gọn trong 2 câu, theo phong cách trung cổ.";
public string playerHeldItem = "Thanh kiếm rỉ sét";
@@ -95,46 +64,17 @@ public class GerminiNPC : MonoBehaviour
private IEnumerator GetGerminiReponse()
{
var jsonBody = $@"{{
""systemInstruction"": {{""parts"": [{{ ""text"": ""{npcPersona}"" }}]}},
""contents"": [{{""parts"": [{{ ""text"": ""Ta muốn bán cho ông món đồ này: {playerHeldItem}""}}]}}]
}}";
string prompt = $"Ta muốn bán cho ông món đồ này: {playerHeldItem}";
Hallucinate.AI.GeminiService.Instance.GetResponse(npcPersona, prompt, (response) => {
Debug.Log($"<color=green>Tom:</color> {response}");
AudioManager.Instance?.Play(responseSound, position: transform.position);
// Nếu có ChatBubble gắn kèm thì hiển thị luôn
var bubble = GetComponentInChildren<Hallucinate.UI.ChatBubble>(true);
if (bubble != null) bubble.Show(response);
});
// 1. Sửa tham số thành ?key= (trước đó là ?ket=)
var requestURL = $"{germiniURL}?key={apiKey}";
// 2. Sử dụng requestURL (có chứa key) thay vì germiniURL gốc
using (var request = new UnityWebRequest(requestURL, "POST"))
{
var bodyRaw = Encoding.UTF8.GetBytes(jsonBody);
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
request.downloadHandler = new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.ProtocolError || request.result == UnityWebRequest.Result.ConnectionError)
{
Debug.LogError($"[Gemini Error] {request.error} - Response: {request.downloadHandler.text}");
}
else
{
var responseTEXT = request.downloadHandler.text;
try
{
var geminiResponse = JsonUtility.FromJson<GeminiResponse>(responseTEXT);
if (geminiResponse != null && geminiResponse.candidates != null && geminiResponse.candidates.Length > 0)
{
var npcResponse = geminiResponse.candidates[0].content.parts[0].text;
Debug.Log($"<color=green>Tom:</color> {npcResponse}");
AudioManager.Instance?.Play(responseSound, position: transform.position);
}
}
catch (Exception e)
{
Debug.LogError($"[JSON Parse Error] {e.Message}");
}
}
}
yield break;
}
}