HKI Core
AIStateController.cs
Go to the documentation of this file.
1 using UnityEngine;
2 
3 namespace HKI.Core.AI.StateMachine
4 {
10  public class AIStateController : MonoBehaviour
11  {
12  // Public variables
14  [SerializeField] AIState StartState = null;
15  [SerializeField] AIState CurrentState = null;
16  [SerializeField] AIState RemainInState = null;
17 
18  [HideInInspector] public float StateTimeElapsed = 0.0f;
19 
20  [HideInInspector] public Transform ThisTransform = null;
21 
22  // Private variables
23  bool isAiActive = false;
24 
25  // Awake function
26  void Awake()
27  {
28  ThisTransform = transform;
29  }
30 
31  // OnEnable
32  void OnEnable()
33  {
34  if(AIStateControllerContainer != null)
35  AIStateControllerContainer.Add(this);
36  }
37 
38  // OnDisable
39  void OnDisable()
40  {
41  if(AIStateControllerContainer != null)
42  AIStateControllerContainer.Remove(this);
43  }
44 
45  // Update function
46  public void UpdateState()
47  {
48  if (!isAiActive || CurrentState == null)
49  return;
50 
51  CurrentState.UpdateState(this);
52  }
53 
54  // Interface functions
55  public void SetupAI(bool isAiActive)
56  {
57  this.isAiActive = isAiActive;
58 
59  CurrentState = StartState;
60  }
61 
62  public void TransitionToState(AIState nextState)
63  {
64  if (nextState != RemainInState)
65  {
66  CurrentState = nextState;
67  OnExitState();
68  }
69  }
70 
71  public bool CheckIfCountDownElapsed(float duration)
72  {
73  StateTimeElapsed += Time.deltaTime;
74 
75  return (StateTimeElapsed >= duration);
76  }
77 
78  // Helper function
79  void OnExitState()
80  {
81  StateTimeElapsed = 0.0f;
82  }
83  }
84 }
This is obsolete code it'll be removed later. For now it's still included for exemplary purposes for ...
Definition: AIState.cs:11
This is obsolete code it'll be removed later. For now it's still included for exemplary purposes for ...
This is obsolete code it'll be removed later. For now it's still included for exemplary purposes for ...
void UpdateState(AIStateController controller)
Definition: AIState.cs:18