// Animancer // Copyright 2020 Kybernetik // using UnityEngine; namespace Animancer.FSM { /// /// A state that can be used in a . /// public interface IState where TState : class, IState { /// Determines whether this state can be entered. bool CanEnterState(TState previousState); /// Determines whether this state can be exited. bool CanExitState(TState nextState); /// Called when this state is entered. void OnEnterState(); /// Called when this state is exited. void OnExitState(); } /************************************************************************************************************************/ /// /// A type of that knows which it is used in so it /// can be used with various extension methods in . /// public interface IOwnedState : IState where TState : class, IState { /// /// The that this state is used in. /// StateMachine OwnerStateMachine { get; } } /************************************************************************************************************************/ /// /// Various extension methods for . /// [HelpURL(APIDocumentationURL + "StateExtensions")] public static class StateExtensions { /************************************************************************************************************************/ /// The URL of the website where the Animancer API documentation is hosted. /// /// This is a duplicate of Strings.APIDocumentationURL so that the system does not /// have any dependencies on itself. /// public const string APIDocumentationURL = "https://kybernetik.com.au/animancer/api/Animancer.FSM/"; /************************************************************************************************************************/ /// /// Checks if the specified `state` is the in its /// . /// public static bool IsCurrentState(this TState state) where TState : class, IOwnedState { return state.OwnerStateMachine.CurrentState == state; } /************************************************************************************************************************/ /// /// Checks if it is currently possible to enter the specified `state`. This requires /// on the and /// on the specified `state` to both return true. /// public static bool CanEnterState(this TState state) where TState : class, IOwnedState { return state.OwnerStateMachine.CanSetState(state); } /************************************************************************************************************************/ /// /// Attempts to enter the specified `state` and returns true if successful. /// /// This method returns true immediately if the specified `state` is already the /// . To allow directly re-entering the same state, use /// instead. /// public static bool TryEnterState(this TState state) where TState : class, IOwnedState { return state.OwnerStateMachine.TrySetState(state); } /************************************************************************************************************************/ /// /// Attempts to enter the specified `state` and returns true if successful. /// /// This method does not check if the `state` is already the . /// To do so, use instead. /// public static bool TryReEnterState(this TState state) where TState : class, IOwnedState { return state.OwnerStateMachine.TryResetState(state); } /************************************************************************************************************************/ /// /// Calls on the then /// changes to the specified `state` and calls on it. /// /// This method does not check or /// . To do that, you should use instead. /// public static void ForceEnterState(this TState state) where TState : class, IOwnedState { state.OwnerStateMachine.ForceSetState(state); } /************************************************************************************************************************/ #pragma warning disable CS1587 // XML comment is not placed on a valid language element. // Copy this #region into a class which implements IOwnedState to give it the state extension methods as regular members. ///************************************************************************************************************************/ //#region State Extensions ///************************************************************************************************************************/ ///// ///// Checks if this state is the in its ///// . ///// //public bool IsCurrentState() //{ // return OwnerStateMachine.CurrentState == this; //} ///************************************************************************************************************************/ ///// ///// Checks if it is currently possible to enter this state. This requires ///// on the and ///// on this state to both return true. ///// //public bool CanEnterState() //{ // return OwnerStateMachine.CanSetState(this); //} ///************************************************************************************************************************/ ///// ///// Attempts to enter this state and returns true if successful. ///// ///// This method returns true immediately if this state is already the ///// . To allow directly re-entering the same state, use ///// instead. ///// //public bool TryEnterState() //{ // return OwnerStateMachine.TrySetState(this); //} ///************************************************************************************************************************/ ///// ///// Attempts to enter this state and returns true if successful. ///// ///// This method does not check if this state is already the . ///// To do so, use instead. ///// //public bool TryReEnterState() //{ // return OwnerStateMachine.TryResetState(this); //} ///************************************************************************************************************************/ ///// ///// Calls on the then ///// changes to the this state and calls on it. ///// ///// This method does not check or ///// . To do that, you should use instead. ///// //public void ForceEnterState() //{ // OwnerStateMachine.ForceSetState(this); //} ///************************************************************************************************************************/ //#endregion ///************************************************************************************************************************/ } }