Continued working on AI Controller

This commit is contained in:
Caleb Sandford deQuincey
2025-10-30 16:50:35 +00:00
parent 1972134c81
commit b838293aa5
13 changed files with 73 additions and 13 deletions

View File

@@ -1,4 +1,5 @@
using UnityEngine;
using RPG.Core;
namespace RPG.Combat
{

View File

@@ -11,7 +11,7 @@ namespace RPG.Combat
[SerializeField] private float weaponRange = 2f;
[SerializeField] private float weaponDamage = 5f;
Health target;
private float timeSinceLastAttack = 0;
private float timeSinceLastAttack = Mathf.Infinity;
void Update()
{
timeSinceLastAttack += Time.deltaTime;

View File

@@ -1,5 +1,7 @@
using System;
using RPG.Combat;
using RPG.Core;
using RPG.Movement;
using UnityEngine;
using UnityEngine.AI;
@@ -8,21 +10,54 @@ namespace RPG.Control
public class AIController : MonoBehaviour,IAction
{
[SerializeField] float chaseDistance = 5f;
[SerializeField] float suspicionTime = 3f;
Fighter fighter;
Health health;
Mover mover;
GameObject player;
void Start()
Vector3 guardPosition;
float timeSinceLastSawPlayer = Mathf.Infinity;
private void Start()
{
fighter = GetComponent<Fighter>();
health = GetComponent<Health>();
player = GameObject.FindWithTag("Player");
mover = GetComponent<Mover>();
guardPosition = transform.position;
}
void Update()
private void Update()
{
if (health.IsDead()) return;
if (InAttackRangeOfPlayer() && fighter.CanAttack(player))
fighter.Attack(player);
{
timeSinceLastSawPlayer = 0;
AttackBehaviour();
}
else if (timeSinceLastSawPlayer < suspicionTime)
{
SuspicionBehaviour();
}
else
{
fighter.Cancel();
//fighter.Cancel();
GuardBehaviour();
}
timeSinceLastSawPlayer += Time.deltaTime;
}
private void GuardBehaviour()
{
mover.StartMoveAction(guardPosition);
}
private void SuspicionBehaviour()
{
GetComponent<ActionScheduler>().CancelCurrentAction();
}
private void AttackBehaviour()
{
fighter.Attack(player);
}
private bool InAttackRangeOfPlayer()
@@ -33,7 +68,13 @@ namespace RPG.Control
public void Cancel()
{
}
//Called bt Unity
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere(transform.position, chaseDistance);
}
}
}

View File

@@ -3,12 +3,14 @@ using UnityEngine;
using UnityEngine.InputSystem;
using RPG.Movement;
using RPG.Combat;
using RPG.Core;
using System;
namespace RPG.Control
{
public class PlayerController : MonoBehaviour
{
Health health;
private PlayerControls playerControls;
private InputAction moveAction;
private InputAction interactAction;
@@ -20,6 +22,10 @@ namespace RPG.Control
interactAction = playerControls.Player.Action;
mainCamera = Camera.main;
}
void Start()
{
health = GetComponent<Health>();
}
private void OnEnable()
{
moveAction.Enable();
@@ -33,6 +39,7 @@ namespace RPG.Control
}
private void Update()
{
if (health.IsDead()) return;
if (InteractWithCombat()) return;
if (InteractWithMovement()) return;
}

View File

@@ -14,5 +14,9 @@ namespace RPG.Core
}
currentAction = action;
}
public void CancelCurrentAction()
{
StartAction(null);
}
}
}

View File

@@ -1,7 +1,7 @@
using Unity.VisualScripting;
using UnityEngine;
namespace RPG.Combat
namespace RPG.Core
{
public class Health : MonoBehaviour
{
@@ -27,6 +27,7 @@ namespace RPG.Combat
if (isDead) return;
isDead = true;
GetComponent<Animator>().SetTrigger("die");
GetComponent<ActionScheduler>().CancelCurrentAction();
}
}
}

View File

@@ -12,6 +12,7 @@ namespace RPG.Movement
// Cache components for performance
private NavMeshAgent navMeshAgent;
private Animator animator;
Health health;
private void Awake()
{
@@ -19,9 +20,13 @@ namespace RPG.Movement
navMeshAgent = GetComponent<NavMeshAgent>();
animator = GetComponent<Animator>();
}
void Start()
{
health = GetComponent<Health>();
}
void Update()
{
navMeshAgent.enabled = !health.IsDead();
UpdateAnimator();
}
public void StartMoveAction(Vector3 destination)