Newer
Older
TheVengeance-Project-IADE-Unity2D / Assets / Scripts / NPC / FSM / State.cs
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace MyCollections.AI.FinitStateMachine
  5. {
  6. [CreateAssetMenu(menuName = "Finite State Machine/State")]
  7. public class State : ScriptableObject
  8. {
  9. [SerializeField] private List<Action> entryActions;
  10. [SerializeField] private List<Action> actions;
  11. [SerializeField] private List<Transition> transitions = new List<Transition>();
  12. [SerializeField] private Action exitAction;
  13. public List<Action> GetentryActions() => entryActions;
  14. public List<Action> GetActions() => actions;
  15. public List<Transition> GetTransitions() => transitions;
  16. public Action GetExitAction() => exitAction;
  17. //SetUp custom Properties of the state
  18. public virtual void StartState()
  19. {
  20. }
  21. //Runs the entry actions of the state
  22. public void EnterState(FSM fsm)
  23. {
  24. StartState();
  25. if (entryActions.Count > 0)
  26. {
  27. foreach (Action action in entryActions)
  28. {
  29. action.Run(fsm);
  30. }
  31. }
  32. }
  33. //Gets a triggered transition if any of the transition is true
  34. public Transition GetTriggeredTransition(FSM fsm)
  35. {
  36. if (transitions.Count > 0)
  37. {
  38. foreach (Transition transition in transitions)
  39. {
  40. //Debug.Log($"Transition {transition.name}: {transition.IsTriggered(fsm)}");
  41. if (transition.IsTriggered(fsm))
  42. {
  43. return transition;
  44. }
  45. }
  46. }
  47. return null;
  48. }
  49. }
  50. }