Organize custom scripts and Shared under Assets/Scripts, and delete assembly definition files

This commit is contained in:
2026-07-01 20:36:56 +07:00
parent befc19bf37
commit 01048074ee
183 changed files with 180 additions and 3456 deletions

View File

@@ -0,0 +1,44 @@
using System.Collections.Generic;
using UnityEngine;
public enum NodeState
{
Success, Failure, Running
}
public abstract class Node
{
protected NodeState state;
public NodeState State => state;
public abstract NodeState Evaluate();
}
public class Selector : Node
{
protected List<Node> nodes = new List<Node>(); // children nodes
public Selector(List<Node> nodes)
{
this.nodes = nodes;
}
public override NodeState Evaluate()
{
foreach (var node in nodes)
{
switch (node.Evaluate())
{
case NodeState.Failure:
continue;
case NodeState.Success:
state = NodeState.Success;
return state;
case NodeState.Running:
state = NodeState.Running;
return state;
}
}
state = NodeState.Failure;
return state;
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 05bb68bbe2862134ab45f5267ec4b6bb

View File

@@ -0,0 +1,49 @@
using UnityEngine;
using TMPro;
using PrimeTween;
namespace Baba_yaga.UI
{
[UnityEngine.Scripting.APIUpdating.MovedFrom(true, sourceNamespace: "Hallucinate.UI", sourceAssembly: "Opsive.UltimateCharacterController")]
public class ChatBubble : MonoBehaviour
{
[SerializeField] private TextMeshProUGUI textDisplay;
[SerializeField] private CanvasGroup canvasGroup;
[SerializeField] private RectTransform bubbleRect;
private Transform mainCameraTransform;
private void Awake()
{
if (canvasGroup != null) canvasGroup.alpha = 0;
// gameObject.SetActive(false); // Bỏ dòng này để tránh tắt nhầm NPC gốc
}
private void LateUpdate()
{
// Tìm Camera nếu chưa có (Tránh lỗi Null nếu Camera chưa spawn hoặc bị xóa)
if (mainCameraTransform == null)
{
if (Camera.main != null) mainCameraTransform = Camera.main.transform;
else return;
}
// 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

@@ -0,0 +1,52 @@
using System.Collections;
using UnityEngine;
public class FieldOfView : MonoBehaviour
{
[Range(0, 360)]
public float viewAngle = 90f;
public float viewRadius = 20f;
public LayerMask obstacleLayerMask;
public LayerMask targetLayerMask;
[HideInInspector] public bool canSeePlayer = false;
[HideInInspector] public Vector3 lastKnownPlayerPosition;
void Start()
{
StartCoroutine(FindTargetWithDelay(0.1f));
}
IEnumerator FindTargetWithDelay(float delay)
{
while (true)
{
yield return new WaitForSeconds(delay);
FindVisibleTargets();
}
}
private void FindVisibleTargets()
{
canSeePlayer = false;
var colliders = Physics.OverlapSphere(transform.position, viewRadius, targetLayerMask);
for (int i = 0; i < colliders.Length; i++)
{
var target = colliders[i].transform;
var direction = (target.position - transform.position).normalized;
var angle = Vector3.Angle(transform.forward, direction);
if (angle < viewAngle / 2)
{
float distanceToTarget = Vector3.Distance(transform.position, target.position);
if (!Physics.Raycast(transform.position, direction, distanceToTarget, obstacleLayerMask))
{
canSeePlayer = true;
lastKnownPlayerPosition = target.position;
Debug.DrawLine(transform.position, target.position, Color.blue, 1f);
}
}
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 210b37cfe4a84a34a91d0a9e58856a60

View File

@@ -0,0 +1,135 @@
using System;
using System.Collections;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
namespace Baba_yaga.AI
{
[Serializable]
[UnityEngine.Scripting.APIUpdating.MovedFrom(true, sourceNamespace: "Hallucinate.AI", sourceAssembly: "Opsive.UltimateCharacterController")]
public class Part { public string text; }
[Serializable]
[UnityEngine.Scripting.APIUpdating.MovedFrom(true, sourceNamespace: "Hallucinate.AI", sourceAssembly: "Opsive.UltimateCharacterController")]
public class Content { public Part[] parts; }
[Serializable]
[UnityEngine.Scripting.APIUpdating.MovedFrom(true, sourceNamespace: "Hallucinate.AI", sourceAssembly: "Opsive.UltimateCharacterController")]
public class Candidate { public Content content; }
[Serializable]
[UnityEngine.Scripting.APIUpdating.MovedFrom(true, sourceNamespace: "Hallucinate.AI", sourceAssembly: "Opsive.UltimateCharacterController")]
public class GeminiResponse { public Candidate[] candidates; }
[UnityEngine.Scripting.APIUpdating.MovedFrom(true, sourceNamespace: "Hallucinate.AI", sourceAssembly: "Opsive.UltimateCharacterController")]
public class GeminiService : MonoBehaviour
{
public static GeminiService Instance { get; private set; }
private int activeRequests = 0;
private const int MAX_CONCURRENT_REQUESTS = 5;
[SerializeField] private string[] apiKeys = { "YOUR_KEY_1", "YOUR_KEY_2" };
private int currentKeyIndex = 0;
[SerializeField] private string geminiURL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent";
private float nextRequestTime = 0f;
private string[] fallbackDialogues = {
"{ \"text\": \"Nice weather, isn't it?\", \"speedMod\": 0.0, \"suspicionMod\": -5.0, \"aggressionMod\": 0.0, \"braveryMod\": 0.0, \"healthMod\": 0.0 }",
"{ \"text\": \"Did you hear something? Probably just a rat.\", \"speedMod\": 0.0, \"suspicionMod\": 2.0, \"aggressionMod\": 0.1, \"braveryMod\": -5.0, \"healthMod\": 0.0 }",
"{ \"text\": \"I'm so tired of this shift.\", \"speedMod\": -0.2, \"suspicionMod\": 0.0, \"aggressionMod\": -0.1, \"braveryMod\": 5.0, \"healthMod\": 0.0 }",
"{ \"text\": \"You looks strong, I should be careful.\", \"speedMod\": 0.1, \"suspicionMod\": 5.0, \"aggressionMod\": -0.2, \"braveryMod\": 10.0, \"healthMod\": 0.0 }"
};
private void Awake()
{
if (Instance == null) { Instance = this; DontDestroyOnLoad(gameObject); }
else { Destroy(gameObject); }
}
private string GetNextKey()
{
if (apiKeys == null || apiKeys.Length == 0) return "";
string key = apiKeys[currentKeyIndex];
currentKeyIndex = (currentKeyIndex + 1) % apiKeys.Length;
return key;
}
public void GetResponse(string persona, string prompt, Action<string> onComplete)
{
if (Time.time < nextRequestTime)
{
Debug.LogWarning("[Gemini] API is cooling down. Using fallback.");
onComplete?.Invoke(fallbackDialogues[UnityEngine.Random.Range(0, fallbackDialogues.Length)]);
return;
}
if (activeRequests >= MAX_CONCURRENT_REQUESTS)
{
onComplete?.Invoke(fallbackDialogues[UnityEngine.Random.Range(0, fallbackDialogues.Length)]);
return;
}
StartCoroutine(PostRequest(persona, prompt, onComplete));
}
private IEnumerator PostRequest(string persona, string prompt, Action<string> onComplete)
{
activeRequests++;
string jsonInstruction = " Respond ONLY with a JSON object: { " +
"'text': 'dialogue content', " +
"'speedMod': 0.0 (change movement speed), " +
"'suspicionMod': 0.0 (change suspicion level), " +
"'aggressionMod': 0.0 (0.1 to 0.5 makes NPC shoot faster, negative makes them slower), " +
"'braveryMod': 0.0 (positive makes them less likely to panic, negative makes them scared), " +
"'healthMod': 0.0 (positive heals NPC, negative damages them) " +
"}. Keep values realistic.";
string escapedPersona = persona.Replace("\"", "\\\"");
string escapedPrompt = prompt.Replace("\"", "\\\"");
var jsonBody = $@"{{
""systemInstruction"": {{""parts"": [{{ ""text"": ""{escapedPersona} {jsonInstruction}"" }}]}},
""contents"": [{{""parts"": [{{ ""text"": ""{escapedPrompt}"" }}]}}],
""generationConfig"": {{
""maxOutputTokens"": 150,
""temperature"": 0.8,
""responseMimeType"": ""application/json""
}}
}}";
var requestURL = $"{geminiURL}?key={GetNextKey()}";
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)
{
var response = JsonUtility.FromJson<GeminiResponse>(request.downloadHandler.text);
if (response?.candidates?.Length > 0 && response.candidates[0].content?.parts?.Length > 0)
{
onComplete?.Invoke(response.candidates[0].content.parts[0].text);
}
}
else
{
Debug.LogError($"[Gemini] API Error: {request.error}");
if (request.responseCode == 429)
{
nextRequestTime = Time.time + 60f;
}
onComplete?.Invoke(fallbackDialogues[UnityEngine.Random.Range(0, fallbackDialogues.Length)]);
}
}
activeRequests--;
}
}
}

View File

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

View 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);
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 6008ec58fb909034abd7293b55f0d558

View File

@@ -0,0 +1,47 @@
using System.Collections.Generic;
public class Sequence : Node
{
private List<Node> nodes = new List<Node>();
public Sequence(List<Node> nodes)
{
this.nodes = nodes;
}
public override NodeState Evaluate()
{
var isAnyChildRunning = false;
foreach (var node in nodes)
{
switch (node.Evaluate())
{
case NodeState.Failure:
state = NodeState.Failure;
return state;
case NodeState.Success:
continue;
case NodeState.Running:
isAnyChildRunning = true;
continue;
}
}
state = isAnyChildRunning ? NodeState.Running : NodeState.Success;
return state;
}
}
public class TaskNode : Node
{
public delegate NodeState TaskDelegate();
private TaskDelegate action;
public TaskNode(TaskDelegate action)
{
this.action = action;
}
public override NodeState Evaluate()
{
return action();
}
}

View File

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