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 UnityEngine;
using RPG.Core;
namespace RPG.Combat namespace RPG.Combat
{ {

View File

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

View File

@@ -1,5 +1,7 @@
using System;
using RPG.Combat; using RPG.Combat;
using RPG.Core; using RPG.Core;
using RPG.Movement;
using UnityEngine; using UnityEngine;
using UnityEngine.AI; using UnityEngine.AI;
@@ -8,21 +10,54 @@ namespace RPG.Control
public class AIController : MonoBehaviour,IAction public class AIController : MonoBehaviour,IAction
{ {
[SerializeField] float chaseDistance = 5f; [SerializeField] float chaseDistance = 5f;
[SerializeField] float suspicionTime = 3f;
Fighter fighter; Fighter fighter;
Health health;
Mover mover;
GameObject player; GameObject player;
void Start() Vector3 guardPosition;
float timeSinceLastSawPlayer = Mathf.Infinity;
private void Start()
{ {
fighter = GetComponent<Fighter>(); fighter = GetComponent<Fighter>();
health = GetComponent<Health>();
player = GameObject.FindWithTag("Player"); player = GameObject.FindWithTag("Player");
mover = GetComponent<Mover>();
guardPosition = transform.position;
} }
void Update() private void Update()
{ {
if (health.IsDead()) return;
if (InAttackRangeOfPlayer() && fighter.CanAttack(player)) if (InAttackRangeOfPlayer() && fighter.CanAttack(player))
fighter.Attack(player); {
timeSinceLastSawPlayer = 0;
AttackBehaviour();
}
else if (timeSinceLastSawPlayer < suspicionTime)
{
SuspicionBehaviour();
}
else 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() private bool InAttackRangeOfPlayer()
@@ -33,7 +68,13 @@ namespace RPG.Control
public void Cancel() 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 UnityEngine.InputSystem;
using RPG.Movement; using RPG.Movement;
using RPG.Combat; using RPG.Combat;
using RPG.Core;
using System; using System;
namespace RPG.Control namespace RPG.Control
{ {
public class PlayerController : MonoBehaviour public class PlayerController : MonoBehaviour
{ {
Health health;
private PlayerControls playerControls; private PlayerControls playerControls;
private InputAction moveAction; private InputAction moveAction;
private InputAction interactAction; private InputAction interactAction;
@@ -20,6 +22,10 @@ namespace RPG.Control
interactAction = playerControls.Player.Action; interactAction = playerControls.Player.Action;
mainCamera = Camera.main; mainCamera = Camera.main;
} }
void Start()
{
health = GetComponent<Health>();
}
private void OnEnable() private void OnEnable()
{ {
moveAction.Enable(); moveAction.Enable();
@@ -33,6 +39,7 @@ namespace RPG.Control
} }
private void Update() private void Update()
{ {
if (health.IsDead()) return;
if (InteractWithCombat()) return; if (InteractWithCombat()) return;
if (InteractWithMovement()) return; if (InteractWithMovement()) return;
} }

View File

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

View File

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

View File

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

View File

@@ -51,7 +51,7 @@ Material:
m_Scale: {x: 1, y: 1} m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0} m_Offset: {x: 0, y: 0}
- _MainTex: - _MainTex:
m_Texture: {fileID: 0} m_Texture: {fileID: 2800000, guid: 58ffb2344d9eb1349bb7784937c291a7, type: 3}
m_Scale: {x: 1, y: 1} m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0} m_Offset: {x: 0, y: 0}
- _MetallicGlossMap: - _MetallicGlossMap:

View File

@@ -64,7 +64,7 @@ Material:
m_Scale: {x: 1, y: 1} m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0} m_Offset: {x: 0, y: 0}
- _MainTex: - _MainTex:
m_Texture: {fileID: 0} m_Texture: {fileID: 2800000, guid: 4f7d005a974d65443821d016a35f5b84, type: 3}
m_Scale: {x: 1, y: 1} m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0} m_Offset: {x: 0, y: 0}
- _MetallicGlossMap: - _MetallicGlossMap:

View File

@@ -64,7 +64,7 @@ Material:
m_Scale: {x: 1, y: 1} m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0} m_Offset: {x: 0, y: 0}
- _MainTex: - _MainTex:
m_Texture: {fileID: 0} m_Texture: {fileID: 2800000, guid: c25f7aa77c9e3144ca364eaca83f2621, type: 3}
m_Scale: {x: 1, y: 1} m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0} m_Offset: {x: 0, y: 0}
- _MetallicGlossMap: - _MetallicGlossMap:

View File

@@ -64,7 +64,7 @@ Material:
m_Scale: {x: 1, y: 1} m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0} m_Offset: {x: 0, y: 0}
- _MainTex: - _MainTex:
m_Texture: {fileID: 0} m_Texture: {fileID: 2800000, guid: e917c51eb5f9a2c4db7c985c75a72d5e, type: 3}
m_Scale: {x: 1, y: 1} m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0} m_Offset: {x: 0, y: 0}
- _MetallicGlossMap: - _MetallicGlossMap:

View File

@@ -24,6 +24,7 @@ Material:
disabledShaderPasses: disabledShaderPasses:
- MOTIONVECTORS - MOTIONVECTORS
- DepthOnly - DepthOnly
- SHADOWCASTER
m_LockedProperties: m_LockedProperties:
m_SavedProperties: m_SavedProperties:
serializedVersion: 3 serializedVersion: 3
@@ -123,7 +124,7 @@ Material:
- _ZWrite: 0 - _ZWrite: 0
m_Colors: m_Colors:
- _BaseColor: {r: 0.6985294, g: 0.82535493, b: 1, a: 0.24705882} - _BaseColor: {r: 0.6985294, g: 0.82535493, b: 1, a: 0.24705882}
- _Color: {r: 1, g: 1, b: 1, a: 1} - _Color: {r: 0.6985294, g: 0.82535493, b: 1, a: 0.24705882}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 0} - _EmissionColor: {r: 0, g: 0, b: 0, a: 0}
- _EmissionColorUI: {r: 1, g: 1, b: 1, a: 1} - _EmissionColorUI: {r: 1, g: 1, b: 1, a: 1}
- _SpecColor: {r: 0.19999996, g: 0.19999996, b: 0.19999996, a: 1} - _SpecColor: {r: 0.19999996, g: 0.19999996, b: 0.19999996, a: 1}