// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2023 Kybernetik // using System.Collections.Generic; namespace Animancer.FSM { /// An object with a . /// /// Documentation: State Selectors /// /// https://kybernetik.com.au/animancer/api/Animancer.FSM/IPrioritizable /// public interface IPrioritizable : IState { float Priority { get; } } /************************************************************************************************************************/ public partial class StateMachine { /// A prioritised list of potential states for a to enter. /// /// Documentation: State Selectors /// /// /// public StateMachine<CharacterState> stateMachine; /// public CharacterState run; /// public CharacterState idle; /// /// private readonly StateMachine<CharacterState>.StateSelector /// Selector = new StateMachine<CharacterState>.StateSelector(); /// /// private void Awake() /// { /// Selector.Add(1, run); /// Selector.Add(0, idle); /// } /// /// public void RunOrIdle() /// { /// stateMachine.TrySetState(Selector.Values); /// // The "run" state has the highest priority so this will enter it if "run.CanEnterState" returns true. /// // Otherwise if "idle.CanEnterState" returns true it will enter that state instead. /// // If neither allows the transition, nothing happens and "stateMachine.TrySetState" returns false. /// } /// /// https://kybernetik.com.au/animancer/api/Animancer.FSM/StateSelector /// public class StateSelector : SortedList { public StateSelector() : base(ReverseComparer.Instance) { } /// Adds the `state` to this selector with its . public void Add(TPrioritizable state) where TPrioritizable : TState, IPrioritizable => Add(state.Priority, state); } } /************************************************************************************************************************/ /// An which reverses the default comparison. /// https://kybernetik.com.au/animancer/api/Animancer.FSM/ReverseComparer_1 public class ReverseComparer : IComparer { /// The singleton instance. public static readonly ReverseComparer Instance = new ReverseComparer(); /// No need to let users create other instances. private ReverseComparer() { } /// Uses with the parameters swapped. public int Compare(T x, T y) => Comparer.Default.Compare(y, x); } }