using System.Collections; using System.Collections.Generic; using UnityEngine; namespace MoreMountains.Tools { [System.Serializable] public class AIActionsList : MMReorderableArray { } [System.Serializable] public class AITransitionsList : MMReorderableArray { } /// /// A State is a combination of one or more actions, and one or more transitions. An example of a state could be "_patrolling until an enemy gets in range_". /// [System.Serializable] public class AIState { /// the name of the state (will be used as a reference in Transitions public string StateName; [MMReorderableAttribute(null, "Action", null)] public AIActionsList Actions; [MMReorderableAttribute(null, "Transition", null)] public AITransitionsList Transitions;/* /// a list of actions to perform in this state public List Actions; /// a list of transitions to evaluate to exit this state public List Transitions;*/ protected AIBrain _brain; /// /// Sets this state's brain to the one specified in parameters /// /// public virtual void SetBrain(AIBrain brain) { _brain = brain; } /// /// On enter state we pass that info to our actions and decisions /// public virtual void EnterState() { foreach (AIAction action in Actions) { action.OnEnterState(); } foreach (AITransition transition in Transitions) { if (transition.Decision != null) { transition.Decision.OnEnterState(); } } } /// /// On exit state we pass that info to our actions and decisions /// public virtual void ExitState() { foreach (AIAction action in Actions) { action.OnExitState(); } foreach (AITransition transition in Transitions) { if (transition.Decision != null) { transition.Decision.OnExitState(); } } } /// /// Performs this state's actions /// public virtual void PerformActions() { if (Actions.Count == 0) { return; } for (int i=0; i /// Tests this state's transitions /// public virtual void EvaluateTransitions() { if (Transitions.Count == 0) { return; } for (int i = 0; i < Transitions.Count; i++) { if (Transitions[i].Decision != null) { if (Transitions[i].Decision.Decide()) { if (!string.IsNullOrEmpty(Transitions[i].TrueState)) { _brain.TransitionToState(Transitions[i].TrueState); break; } } else { if (!string.IsNullOrEmpty(Transitions[i].FalseState)) { _brain.TransitionToState(Transitions[i].FalseState); break; } } } } } } }