Merge branch 'main' of https://scove-vault.duckdns.org/scove/HALLUCINATION
This commit is contained in:
16
Assets/Scripts/AI NPC/AutoDestroy.cs
Normal file
16
Assets/Scripts/AI NPC/AutoDestroy.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class AutoDestroy : MonoBehaviour
|
||||
{
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/AI NPC/AutoDestroy.cs.meta
Normal file
2
Assets/Scripts/AI NPC/AutoDestroy.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 950ee3c6c086a3b4fa9a7f1e544c1651
|
||||
42
Assets/Scripts/AI NPC/ChatBubble.cs
Normal file
42
Assets/Scripts/AI NPC/ChatBubble.cs
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/AI NPC/ChatBubble.cs.meta
Normal file
2
Assets/Scripts/AI NPC/ChatBubble.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ea510cea4b9ed1547ae4725a2ded949a
|
||||
76
Assets/Scripts/AI NPC/GeminiService.cs
Normal file
76
Assets/Scripts/AI NPC/GeminiService.cs
Normal 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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/AI NPC/GeminiService.cs.meta
Normal file
2
Assets/Scripts/AI NPC/GeminiService.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a859fc8e9ec10a347a3704b6045ca7e8
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
198
Assets/Scripts/AI NPC/KamikazeAI.cs
Normal file
198
Assets/Scripts/AI NPC/KamikazeAI.cs
Normal file
@@ -0,0 +1,198 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
|
||||
[RequireComponent(typeof(NavMeshAgent))]
|
||||
public class KamikazeAI : MonoBehaviour
|
||||
{
|
||||
[Header("References")]
|
||||
public Transform player;
|
||||
|
||||
[Header("Detection")]
|
||||
public float detectRange = 15f;
|
||||
private bool canSeePlayer = false;
|
||||
|
||||
[Header("Movement & Random Patrol")]
|
||||
public float patrolSpeed = 2.5f;
|
||||
public float chaseSpeed = 7f;
|
||||
public float patrolRadius = 12f; // Bán kính của khu vực tuần tra ngẫu nhiên
|
||||
public float patrolWaitTime = 2f; // Thời gian đứng nghỉ trước khi đổi sang điểm ngẫu nhiên mới
|
||||
|
||||
private Vector3 startPosition; // Tâm của khu vực tuần tra (Vị trí ban đầu)
|
||||
private float currentWaitTime;
|
||||
private NavMeshAgent agent;
|
||||
private bool isExploding = false;
|
||||
public Node behaviorTreeRoot;
|
||||
public GameObject explosionEffectPrefab;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
agent = GetComponent<NavMeshAgent>();
|
||||
|
||||
// Lưu lại vị trí xuất phát để làm tâm, NPC sẽ chỉ đi loay hoay quanh khu vực này
|
||||
startPosition = transform.position;
|
||||
|
||||
InitBehaviorTree();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (isExploding) return;
|
||||
|
||||
if (player == null) FindPlayer();
|
||||
else CheckVision();
|
||||
|
||||
behaviorTreeRoot?.Evaluate();
|
||||
}
|
||||
|
||||
private void FindPlayer()
|
||||
{
|
||||
GameObject playerObj = GameObject.FindGameObjectWithTag("Player");
|
||||
if (playerObj != null) player = playerObj.transform;
|
||||
}
|
||||
|
||||
private void CheckVision()
|
||||
{
|
||||
if (Vector3.Distance(transform.position, player.position) <= detectRange)
|
||||
canSeePlayer = true;
|
||||
else
|
||||
canSeePlayer = false;
|
||||
}
|
||||
|
||||
private void InitBehaviorTree()
|
||||
{
|
||||
var explodeSequence = new Sequence(new List<Node>
|
||||
{
|
||||
new TaskNode(CheckIsCloseEnoughToExplode),
|
||||
new TaskNode(ActionTriggerExplosion)
|
||||
});
|
||||
|
||||
var chaseSequence = new Sequence(new List<Node>
|
||||
{
|
||||
new TaskNode(CheckCanSeePlayer),
|
||||
new TaskNode(ActionChase)
|
||||
});
|
||||
|
||||
// Hành động tuần tra ngẫu nhiên
|
||||
var patrolNode = new TaskNode(ActionRandomPatrol);
|
||||
|
||||
behaviorTreeRoot = new Selector(new List<Node>
|
||||
{
|
||||
explodeSequence,
|
||||
chaseSequence,
|
||||
patrolNode
|
||||
});
|
||||
}
|
||||
|
||||
#region CONDITIONS
|
||||
|
||||
private NodeState CheckCanSeePlayer()
|
||||
{
|
||||
return canSeePlayer ? NodeState.Success : NodeState.Failure;
|
||||
}
|
||||
|
||||
private NodeState CheckIsCloseEnoughToExplode()
|
||||
{
|
||||
if (player == null) return NodeState.Failure;
|
||||
float dist = Vector3.Distance(transform.position, player.position);
|
||||
return dist <= 3f ? NodeState.Success : NodeState.Failure;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ACTIONS
|
||||
|
||||
// HÀM TUẦN TRA NGẪU NHIÊN MỚI
|
||||
private NodeState ActionRandomPatrol()
|
||||
{
|
||||
Debug.Log("Wandering randomly...");
|
||||
agent.isStopped = false;
|
||||
agent.speed = patrolSpeed;
|
||||
|
||||
// Kiểm tra xem NPC đã đi đến điểm ngẫu nhiên hiện tại chưa
|
||||
if (!agent.pathPending && agent.remainingDistance <= agent.stoppingDistance)
|
||||
{
|
||||
currentWaitTime += Time.deltaTime;
|
||||
|
||||
// Đứng đợi hết thời gian quy định rồi mới tìm đường mới
|
||||
if (currentWaitTime >= patrolWaitTime)
|
||||
{
|
||||
// 1. Lấy một điểm ngẫu nhiên trong không gian hình cầu dựa trên bán kính
|
||||
Vector3 randomDirection = Random.insideUnitSphere * patrolRadius;
|
||||
randomDirection += startPosition; // Cộng với tâm ban đầu để giới hạn khu vực
|
||||
|
||||
NavMeshHit hit;
|
||||
// 2. Ép tọa độ ngẫu nhiên đó phải nằm TRÊN bề mặt xanh của NavMesh (tránh kẹt tường)
|
||||
// Số 1 ở cuối là Area Mask (thường là Walkable)
|
||||
if (NavMesh.SamplePosition(randomDirection, out hit, patrolRadius, 1))
|
||||
{
|
||||
agent.SetDestination(hit.position);
|
||||
}
|
||||
|
||||
currentWaitTime = 0f; // Reset thời gian chờ
|
||||
}
|
||||
}
|
||||
|
||||
return NodeState.Running;
|
||||
}
|
||||
|
||||
private NodeState ActionChase()
|
||||
{
|
||||
if (player == null) return NodeState.Failure;
|
||||
|
||||
Debug.Log("Kamikaze is rushing you!");
|
||||
agent.isStopped = false;
|
||||
agent.speed = chaseSpeed;
|
||||
agent.SetDestination(player.position);
|
||||
|
||||
return NodeState.Running;
|
||||
}
|
||||
|
||||
private NodeState ActionTriggerExplosion()
|
||||
{
|
||||
StartCoroutine(ExplosionRoutine());
|
||||
return NodeState.Success;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region EXPLOSION LOGIC
|
||||
|
||||
private IEnumerator ExplosionRoutine()
|
||||
{
|
||||
isExploding = true;
|
||||
agent.isStopped = true;
|
||||
agent.velocity = Vector3.zero;
|
||||
|
||||
Debug.Log("BOMB ARMED!");
|
||||
yield return new WaitForSeconds(1.5f);
|
||||
|
||||
if (player != null)
|
||||
{
|
||||
float distToPlayer = Vector3.Distance(transform.position, player.position);
|
||||
if (distToPlayer <= 4f)
|
||||
{
|
||||
Debug.Log("BOOM! Player took damage!");
|
||||
}
|
||||
}
|
||||
|
||||
if (explosionEffectPrefab != null)
|
||||
{
|
||||
Instantiate(explosionEffectPrefab, transform.position, Quaternion.identity);
|
||||
}
|
||||
|
||||
Destroy(gameObject);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
// Vẽ vùng giới hạn tuần tra màu xanh lá cây trên Scene để bạn dễ căn chỉnh độ rộng
|
||||
private void OnDrawGizmosSelected()
|
||||
{
|
||||
Gizmos.color = Color.green;
|
||||
// Nếu game đang chạy thì vẽ quanh tâm startPosition, nếu chưa chạy thì vẽ quanh vị trí hiện tại
|
||||
Vector3 center = Application.isPlaying ? startPosition : transform.position;
|
||||
Gizmos.DrawWireSphere(center, patrolRadius);
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/AI NPC/KamikazeAI.cs.meta
Normal file
2
Assets/Scripts/AI NPC/KamikazeAI.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6008ec58fb909034abd7293b55f0d558
|
||||
Reference in New Issue
Block a user