Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.AI;
- namespace StateMachineLib
- {
- public class StateMachine : MonoBehaviour
- {
- [SerializeField] public IState _defaultState;
- public IState _currentState;
- private Dictionary<Type, List<Transition>> _transitions = new Dictionary<Type, List<Transition>>();
- [SerializeField] public List<Transition> _currentTransitions = new List<Transition>();
- [SerializeField] public List<Transition> _AnyTransitions = new List<Transition>();
- private List<Transition> _EmptyTransitions = new List<Transition>();
- //Debug Tools
- #region
- [Space]
- [Header("Debug")]
- public bool _LogStateDebug = false;
- #endregion
- //Delegates
- public delegate void StateChange();
- public StateChange _stateChanged;
- private void Awake()
- {
- }
- public void SetEnemyStats(GenericEnemy _genericEnemy)
- {
- //Grab Struct of Enemy stats from class reference
- }
- public void Tick()
- {
- var transition = GetTransition();
- if (transition != null)
- SetState(transition.To);
- _currentState?.OnTick();
- }
- //We are not guranteed a "current state" here
- public void SetState(IState state)
- {
- if (_stateChanged != null)
- _stateChanged.Invoke();
- else
- Debug.Log("_stateChanged is null");
- if (state == _currentState)
- return;
- Debug.Log("Setting State To: " + state.GetType().ToString());
- _currentState?.OnExit();
- _currentState = state;
- _transitions.TryGetValue(_currentState.GetType(), out _currentTransitions);
- if (_currentTransitions == null)
- _currentTransitions = _EmptyTransitions;
- _currentState.OnEnter();
- }
- public class Transition
- {
- public Func<bool> Condition { get; }
- public IState To { get; }
- public Transition(IState to, Func<bool> condition)
- {
- To = to;
- Condition = condition;
- }
- }
- public void AddStateTransition(IState from, IState to, Func<bool> predicate)
- {
- if (_transitions.TryGetValue(from.GetType(), out var transitions) == false)
- {
- Debug.LogWarning("transistions list was empty or null");
- transitions = new List<Transition>();
- _transitions[from.GetType()] = transitions;
- }
- transitions.Add(new Transition(to, predicate));
- Debug.LogWarning("Transition Added..." + " From: " + LogState(from) + "To: " + LogState(to));
- }
- public void AddAnyStateTransition(IState state, Func<bool> predicate)
- {
- _AnyTransitions.Add(new Transition(state, predicate));
- }
- private Transition GetTransition()
- {
- foreach (var transition in _AnyTransitions)
- {
- if (transition.Condition())
- {
- return transition;
- }
- }
- foreach (var transition in _currentTransitions)
- {
- if (transition.Condition())
- {
- return transition;
- }
- }
- return null;
- }
- private string LogTransition(Transition transition)
- {
- return transition.To.GetType().ToString();
- }
- private string LogState(IState state)
- {
- return state.GetType().ToString();
- }
- }
- }
Advertisement
RAW Paste Data
Copied
Advertisement