From b838293aa57ceec13e34057ea26d08b0b2a7e172 Mon Sep 17 00:00:00 2001 From: Caleb Sandford deQuincey Date: Thu, 30 Oct 2025 16:50:35 +0000 Subject: [PATCH] Continued working on AI Controller --- Assets/Scripts/Combat/CombatTarget.cs | 1 + Assets/Scripts/Combat/Fighter.cs | 2 +- Assets/Scripts/Control/AIController.cs | 51 +++++++++++++++++-- Assets/Scripts/Control/PlayerController.cs | 7 +++ Assets/Scripts/Core/ActionScheduler.cs | 4 ++ Assets/Scripts/{Combat => Core}/Health.cs | 3 +- .../Scripts/{Combat => Core}/Health.cs.meta | 0 Assets/Scripts/Movement/Mover.cs | 7 ++- .../Materials/PolyAdventureMaterial_02.mat | 2 +- .../PolyAdventureMaterial_Dark_01.mat | 2 +- .../PolyAdventureMaterial_Dark_02.mat | 2 +- .../PolyAdventureMaterial_Snow_01.mat | 2 +- .../PolyAdventureMaterial_Water_01.mat | 3 +- 13 files changed, 73 insertions(+), 13 deletions(-) rename Assets/Scripts/{Combat => Core}/Health.cs (87%) rename Assets/Scripts/{Combat => Core}/Health.cs.meta (100%) diff --git a/Assets/Scripts/Combat/CombatTarget.cs b/Assets/Scripts/Combat/CombatTarget.cs index 93d08ec6..cf2f93d8 100644 --- a/Assets/Scripts/Combat/CombatTarget.cs +++ b/Assets/Scripts/Combat/CombatTarget.cs @@ -1,4 +1,5 @@ using UnityEngine; +using RPG.Core; namespace RPG.Combat { diff --git a/Assets/Scripts/Combat/Fighter.cs b/Assets/Scripts/Combat/Fighter.cs index 1ffff13b..55f98d1d 100644 --- a/Assets/Scripts/Combat/Fighter.cs +++ b/Assets/Scripts/Combat/Fighter.cs @@ -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; diff --git a/Assets/Scripts/Control/AIController.cs b/Assets/Scripts/Control/AIController.cs index afd7c138..8a65c789 100644 --- a/Assets/Scripts/Control/AIController.cs +++ b/Assets/Scripts/Control/AIController.cs @@ -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(); + health = GetComponent(); player = GameObject.FindWithTag("Player"); + mover = GetComponent(); + 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().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); } } } \ No newline at end of file diff --git a/Assets/Scripts/Control/PlayerController.cs b/Assets/Scripts/Control/PlayerController.cs index 14b522a6..02021cdd 100644 --- a/Assets/Scripts/Control/PlayerController.cs +++ b/Assets/Scripts/Control/PlayerController.cs @@ -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(); + } 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; } diff --git a/Assets/Scripts/Core/ActionScheduler.cs b/Assets/Scripts/Core/ActionScheduler.cs index 947b8f8e..cd1d57ba 100644 --- a/Assets/Scripts/Core/ActionScheduler.cs +++ b/Assets/Scripts/Core/ActionScheduler.cs @@ -14,5 +14,9 @@ namespace RPG.Core } currentAction = action; } + public void CancelCurrentAction() + { + StartAction(null); + } } } \ No newline at end of file diff --git a/Assets/Scripts/Combat/Health.cs b/Assets/Scripts/Core/Health.cs similarity index 87% rename from Assets/Scripts/Combat/Health.cs rename to Assets/Scripts/Core/Health.cs index faba19fa..c143ecf8 100644 --- a/Assets/Scripts/Combat/Health.cs +++ b/Assets/Scripts/Core/Health.cs @@ -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().SetTrigger("die"); + GetComponent().CancelCurrentAction(); } } } diff --git a/Assets/Scripts/Combat/Health.cs.meta b/Assets/Scripts/Core/Health.cs.meta similarity index 100% rename from Assets/Scripts/Combat/Health.cs.meta rename to Assets/Scripts/Core/Health.cs.meta diff --git a/Assets/Scripts/Movement/Mover.cs b/Assets/Scripts/Movement/Mover.cs index 431a2aeb..3ed09def 100644 --- a/Assets/Scripts/Movement/Mover.cs +++ b/Assets/Scripts/Movement/Mover.cs @@ -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(); animator = GetComponent(); } - + void Start() + { + health = GetComponent(); + } void Update() { + navMeshAgent.enabled = !health.IsDead(); UpdateAnimator(); } public void StartMoveAction(Vector3 destination) diff --git a/Assets/Synty/PolygonAdventure/Materials/PolyAdventureMaterial_02.mat b/Assets/Synty/PolygonAdventure/Materials/PolyAdventureMaterial_02.mat index a611cbfe..4dc386cf 100644 --- a/Assets/Synty/PolygonAdventure/Materials/PolyAdventureMaterial_02.mat +++ b/Assets/Synty/PolygonAdventure/Materials/PolyAdventureMaterial_02.mat @@ -51,7 +51,7 @@ Material: m_Scale: {x: 1, y: 1} m_Offset: {x: 0, y: 0} - _MainTex: - m_Texture: {fileID: 0} + m_Texture: {fileID: 2800000, guid: 58ffb2344d9eb1349bb7784937c291a7, type: 3} m_Scale: {x: 1, y: 1} m_Offset: {x: 0, y: 0} - _MetallicGlossMap: diff --git a/Assets/Synty/PolygonAdventure/Materials/PolyAdventureMaterial_Dark_01.mat b/Assets/Synty/PolygonAdventure/Materials/PolyAdventureMaterial_Dark_01.mat index 14597866..a7303574 100644 --- a/Assets/Synty/PolygonAdventure/Materials/PolyAdventureMaterial_Dark_01.mat +++ b/Assets/Synty/PolygonAdventure/Materials/PolyAdventureMaterial_Dark_01.mat @@ -64,7 +64,7 @@ Material: m_Scale: {x: 1, y: 1} m_Offset: {x: 0, y: 0} - _MainTex: - m_Texture: {fileID: 0} + m_Texture: {fileID: 2800000, guid: 4f7d005a974d65443821d016a35f5b84, type: 3} m_Scale: {x: 1, y: 1} m_Offset: {x: 0, y: 0} - _MetallicGlossMap: diff --git a/Assets/Synty/PolygonAdventure/Materials/PolyAdventureMaterial_Dark_02.mat b/Assets/Synty/PolygonAdventure/Materials/PolyAdventureMaterial_Dark_02.mat index c14ef63f..8d0cd4a5 100644 --- a/Assets/Synty/PolygonAdventure/Materials/PolyAdventureMaterial_Dark_02.mat +++ b/Assets/Synty/PolygonAdventure/Materials/PolyAdventureMaterial_Dark_02.mat @@ -64,7 +64,7 @@ Material: m_Scale: {x: 1, y: 1} m_Offset: {x: 0, y: 0} - _MainTex: - m_Texture: {fileID: 0} + m_Texture: {fileID: 2800000, guid: c25f7aa77c9e3144ca364eaca83f2621, type: 3} m_Scale: {x: 1, y: 1} m_Offset: {x: 0, y: 0} - _MetallicGlossMap: diff --git a/Assets/Synty/PolygonAdventure/Materials/PolyAdventureMaterial_Snow_01.mat b/Assets/Synty/PolygonAdventure/Materials/PolyAdventureMaterial_Snow_01.mat index 541935c3..bb83b289 100644 --- a/Assets/Synty/PolygonAdventure/Materials/PolyAdventureMaterial_Snow_01.mat +++ b/Assets/Synty/PolygonAdventure/Materials/PolyAdventureMaterial_Snow_01.mat @@ -64,7 +64,7 @@ Material: m_Scale: {x: 1, y: 1} m_Offset: {x: 0, y: 0} - _MainTex: - m_Texture: {fileID: 0} + m_Texture: {fileID: 2800000, guid: e917c51eb5f9a2c4db7c985c75a72d5e, type: 3} m_Scale: {x: 1, y: 1} m_Offset: {x: 0, y: 0} - _MetallicGlossMap: diff --git a/Assets/Synty/PolygonAdventure/Materials/PolyAdventureMaterial_Water_01.mat b/Assets/Synty/PolygonAdventure/Materials/PolyAdventureMaterial_Water_01.mat index 4dae3ae3..6170e7ae 100644 --- a/Assets/Synty/PolygonAdventure/Materials/PolyAdventureMaterial_Water_01.mat +++ b/Assets/Synty/PolygonAdventure/Materials/PolyAdventureMaterial_Water_01.mat @@ -24,6 +24,7 @@ Material: disabledShaderPasses: - MOTIONVECTORS - DepthOnly + - SHADOWCASTER m_LockedProperties: m_SavedProperties: serializedVersion: 3 @@ -123,7 +124,7 @@ Material: - _ZWrite: 0 m_Colors: - _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} - _EmissionColorUI: {r: 1, g: 1, b: 1, a: 1} - _SpecColor: {r: 0.19999996, g: 0.19999996, b: 0.19999996, a: 1}