using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; namespace CleverCrow.Fluid.FSMs { [Serializable] public class Fsm : IFsm { private readonly Dictionary _stateDic = new Dictionary(); public GameObject Owner { get; } public IState CurrentState { get; private set; } public IState DefaultState { get; set; } public UnityEvent EventExit { get; } = new UnityEvent(); public Fsm (GameObject owner) { Owner = owner; } public IState GetState (Enum id) { return _stateDic[id]; } public void AddState (IState state) { _stateDic[state.Id] = state; } public void SetState (Enum id) { CurrentState?.Exit(); CurrentState = GetState(id); CurrentState.Enter(); } public void Tick () { if (CurrentState == null && DefaultState != null) { SetState(DefaultState.Id); } CurrentState?.Update(); } public void Reset () { SetState(DefaultState.Id); } public void Exit () { CurrentState.Exit(); CurrentState = null; EventExit.Invoke(); } } }