// Animancer // Copyright 2020 Kybernetik //
using UnityEngine;
namespace Animancer.FSM
{
///
/// Base class for states to be used in a .
///
[HelpURL(StateExtensions.APIDocumentationURL + "StateBehaviour_1")]
public abstract class StateBehaviour : MonoBehaviour, IState
where TState : StateBehaviour
{
/************************************************************************************************************************/
///
/// Determines whether the can enter this state. Always returns true
/// unless overridden.
///
public virtual bool CanEnterState(TState previousState) { return true; }
///
/// Determines whether the can exit this state. Always returns true
/// unless overridden.
///
public virtual bool CanExitState(TState nextState) { return true; }
/************************************************************************************************************************/
///
/// Asserts that this component is not already enabled, then enable it.
///
public virtual void OnEnterState()
{
Debug.Assert(!enabled, this + " was already enabled when entering its state", this);
enabled = true;
}
/************************************************************************************************************************/
///
/// Asserts that this component is not already disabled, then disable it.
///
public virtual void OnExitState()
{
if (this == null)
return;
Debug.Assert(enabled, this + " was already disabled when exiting its state", this);
enabled = false;
}
/************************************************************************************************************************/
#if UNITY_EDITOR
/// [Editor-Only]
/// Called by the Unity Editor in Edit Mode whenever an instance of this script is loaded or a value is changed
/// in the Inspector.
///
/// States start disabled and only the current state gets enabled at runtime.
///
protected virtual void OnValidate()
{
if (UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode)
return;
enabled = false;
}
#endif
/************************************************************************************************************************/
}
}