// Animancer // https://kybernetik.com.au/animancer // Copyright 2021 Kybernetik // using System; namespace Animancer.FSM { /// https://kybernetik.com.au/animancer/api/Animancer.FSM/StateMachine_1 partial class StateMachine { /// A with a . /// /// Documentation: Default States /// /// https://kybernetik.com.au/animancer/api/Animancer.FSM/WithDefault /// public class WithDefault : StateMachine { /************************************************************************************************************************/ private TState _DefaultState; /// The starting state and main state to return to when nothing else is active. /// /// If the is null when setting this value, it calls /// to enter the specified state immediately. /// /// For a character, this would typically be their Idle state. /// public TState DefaultState { get => _DefaultState; set { _DefaultState = value; if (CurrentState == null && value != null) ForceSetState(value); } } /************************************************************************************************************************/ /// Calls with the . /// This delegate is cached to avoid allocating garbage when used in Animancer Events. public readonly Action ForceSetDefaultState; /************************************************************************************************************************/ /// Creates a new . public WithDefault() { // Silly C# doesn't allow instance delegates to be assigned using field initializers. ForceSetDefaultState = () => ForceSetState(_DefaultState); } /************************************************************************************************************************/ /// Creates a new and sets the . public WithDefault(TState defaultState) : this() { _DefaultState = defaultState; ForceSetState(defaultState); } /************************************************************************************************************************/ /// /// Attempts to enter the and returns true if successful. /// /// This method returns true immediately if the specified is already the /// . To allow directly re-entering the same state, use /// instead. /// public bool TrySetDefaultState() => TrySetState(DefaultState); /************************************************************************************************************************/ /// /// Attempts to enter the and returns true if successful. /// /// This method does not check if the is already the . /// To do so, use instead. /// public bool TryResetDefaultState() => TryResetState(DefaultState); /************************************************************************************************************************/ } } }