Advertisement
TheLowestAnimal

StateMachine

May 23rd, 2022
581
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.AI;
  6.  
  7. namespace StateMachineLib
  8. {
  9.     public class StateMachine : MonoBehaviour
  10.     {
  11.         [SerializeField] public IState _defaultState;
  12.  
  13.         public IState _currentState;
  14.         private Dictionary<Type, List<Transition>> _transitions = new Dictionary<Type, List<Transition>>();
  15.         [SerializeField] public List<Transition> _currentTransitions = new List<Transition>();
  16.         [SerializeField] public List<Transition> _AnyTransitions = new List<Transition>();
  17.  
  18.         private List<Transition> _EmptyTransitions = new List<Transition>();
  19.  
  20.         //Debug Tools
  21.         #region
  22.         [Space]
  23.         [Header("Debug")]
  24.         public bool _LogStateDebug = false;
  25.         #endregion
  26.  
  27.  
  28.         //Delegates
  29.         public delegate void StateChange();
  30.         public StateChange _stateChanged;
  31.  
  32.         private void Awake()
  33.         {
  34.         }
  35.  
  36.  
  37.  
  38.         public void SetEnemyStats(GenericEnemy _genericEnemy)
  39.         {
  40.             //Grab Struct of Enemy stats from class reference
  41.         }
  42.  
  43.         public void Tick()
  44.         {
  45.             var transition = GetTransition();
  46.  
  47.             if (transition != null)
  48.                 SetState(transition.To);
  49.  
  50.             _currentState?.OnTick();
  51.         }
  52.  
  53.  
  54.         //We are not guranteed a   "current state" here
  55.         public void SetState(IState state)
  56.         {
  57.             if (_stateChanged != null)
  58.                 _stateChanged.Invoke();
  59.             else
  60.                 Debug.Log("_stateChanged is null");
  61.  
  62.             if (state == _currentState)
  63.                 return;
  64.             Debug.Log("Setting State To: " + state.GetType().ToString());
  65.  
  66.             _currentState?.OnExit();
  67.             _currentState = state;
  68.             _transitions.TryGetValue(_currentState.GetType(), out _currentTransitions);
  69.             if (_currentTransitions == null)
  70.                 _currentTransitions = _EmptyTransitions;
  71.  
  72.             _currentState.OnEnter();
  73.         }
  74.  
  75.         public class Transition
  76.         {
  77.             public Func<bool> Condition { get; }
  78.             public IState To { get; }
  79.  
  80.             public Transition(IState to, Func<bool> condition)
  81.             {
  82.                 To = to;
  83.                 Condition = condition;
  84.             }
  85.         }
  86.  
  87.         public void AddStateTransition(IState from, IState to, Func<bool> predicate)
  88.         {
  89.             if (_transitions.TryGetValue(from.GetType(), out var transitions) == false)
  90.             {
  91.                 Debug.LogWarning("transistions list was empty or null");
  92.                 transitions = new List<Transition>();
  93.                 _transitions[from.GetType()] = transitions;
  94.             }
  95.  
  96.             transitions.Add(new Transition(to, predicate));
  97.  
  98.             Debug.LogWarning("Transition Added..." + " From: " + LogState(from) + "To: " + LogState(to));
  99.         }
  100.  
  101.         public void AddAnyStateTransition(IState state, Func<bool> predicate)
  102.         {
  103.             _AnyTransitions.Add(new Transition(state, predicate));
  104.         }
  105.         private Transition GetTransition()
  106.         {
  107.             foreach (var transition in _AnyTransitions)
  108.             {
  109.                 if (transition.Condition())
  110.                 {
  111.                     return transition;
  112.                 }
  113.             }
  114.             foreach (var transition in _currentTransitions)
  115.             {
  116.                 if (transition.Condition())
  117.                 {
  118.                     return transition;
  119.                 }
  120.             }
  121.             return null;
  122.         }
  123.  
  124.         private string LogTransition(Transition transition)
  125.         {
  126.             return transition.To.GetType().ToString();
  127.         }
  128.  
  129.         private string LogState(IState state)
  130.         {
  131.             return state.GetType().ToString();
  132.         }
  133.     }
  134. }
  135.  
  136.  
  137.  
  138.  
Advertisement
RAW Paste Data Copied
Advertisement