// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2023 Kybernetik // using System; namespace Animancer.FSM { /// A static access point for the details of a state change in a . /// /// This system is thread-safe. /// /// Documentation: Changing States /// /// https://kybernetik.com.au/animancer/api/Animancer.FSM/StateChange_1 /// public struct StateChange : IDisposable where TState : class, IState { /************************************************************************************************************************/ [ThreadStatic] private static StateChange _Current; private StateMachine _StateMachine; private TState _PreviousState; private TState _NextState; /************************************************************************************************************************/ /// Is a of this type currently occurring? public static bool IsActive => _Current._StateMachine != null; /// The in which the current change is occurring. /// This will be null if no change is currently occurring. public static StateMachine StateMachine => _Current._StateMachine; /************************************************************************************************************************/ /// The state currently being changed from. /// [Assert-Only] /// is false so this property is likely being accessed on the wrong generic type. /// public static TState PreviousState { get { #if UNITY_ASSERTIONS if (!IsActive) throw new InvalidOperationException(StateExtensions.GetChangeError(typeof(TState), typeof(StateMachine<>))); #endif return _Current._PreviousState; } } /************************************************************************************************************************/ /// The state being changed into. /// [Assert-Only] /// is false so this property is likely being accessed on the wrong generic type. /// public static TState NextState { get { #if UNITY_ASSERTIONS if (!IsActive) throw new InvalidOperationException(StateExtensions.GetChangeError(typeof(TState), typeof(StateMachine<>))); #endif return _Current._NextState; } } /************************************************************************************************************************/ /// [Internal] /// Assigns the parameters as the details of the currently active change and creates a new /// containing the details of the previously active change so that disposing /// it will re-assign those previous details to be current again in case of recursive state changes. /// /// /// using (new StateChange<TState>(stateMachine, previousState, nextState)) /// { /// // Do the actual state change. /// } /// internal StateChange(StateMachine stateMachine, TState previousState, TState nextState) { this = _Current; _Current._StateMachine = stateMachine; _Current._PreviousState = previousState; _Current._NextState = nextState; } /************************************************************************************************************************/ /// [] /// Re-assigns the values of this change (which were the previous values from when it was created) to be the /// currently active change. See the constructor for recommended usage. /// /// /// Usually this will be returning to default values (nulls), but if one state change causes another then the /// second one ending will return to the first which will then return to the defaults. /// public void Dispose() { _Current = this; } /************************************************************************************************************************/ /// Returns a string describing the contents of this . public override string ToString() => IsActive ? $"{nameof(StateChange)}<{typeof(TState).FullName}" + $">({nameof(PreviousState)}='{_PreviousState}'" + $", {nameof(NextState)}='{_NextState}')" : $"{nameof(StateChange)}<{typeof(TState).FullName}(Not Currently Active)"; /// Returns a string describing the contents of the current . public static string CurrentToString() => _Current.ToString(); /************************************************************************************************************************/ } }