using NodeCanvas.Framework;
using ParadoxNotion.Design;
using UnityEngine;
namespace NodeCanvas.StateMachines
{
/// Add this component on a gameobject to behave based on an FSM.
[AddComponentMenu("NodeCanvas/FSM Owner")]
public class FSMOwner : GraphOwner
{
///The current state name of the root fsm.
public string currentRootStateName => behaviour?.currentStateName;
///The previous state name of the root fsm.
public string previousRootStateName => behaviour?.previousStateName;
///The current deep state name of the fsm including sub fsms if any.
public string currentDeepStateName => GetCurrentState(true)?.name;
///The previous deep state name of the fsm including sub fsms if any.
public string previousDeepStateName => GetPreviousState(true)?.name;
///Returns the current fsm state optionally recursively by including SubFSMs.
public IState GetCurrentState(bool includeSubFSMs = true) {
if ( behaviour == null ) { return null; }
var current = behaviour.currentState;
if ( includeSubFSMs ) {
while ( current is NestedFSMState subState ) {
current = subState.currentInstance?.currentState;
}
}
return current;
}
///Returns the previous fsm state optionally recursively by including SubFSMs.
public IState GetPreviousState(bool includeSubFSMs = true) {
if ( behaviour == null ) { return null; }
var current = behaviour.currentState;
var previous = behaviour.previousState;
if ( includeSubFSMs ) {
while ( current is NestedFSMState subState ) {
current = subState.currentInstance?.currentState;
previous = subState.currentInstance?.previousState;
}
}
return previous;
}
///Enter a state of the root FSM by its name.
public IState TriggerState(string stateName) { return TriggerState(stateName, FSM.TransitionCallMode.Normal); }
public IState TriggerState(string stateName, FSM.TransitionCallMode callMode) {
return behaviour?.TriggerState(stateName, callMode);
}
///Get all root state names, excluding non-named states.
public string[] GetStateNames() {
return behaviour?.GetStateNames();
}
#if UNITY_EDITOR
protected override void OnDrawGizmos() {
UnityEditor.Handles.Label(transform.position + new Vector3(0, -0.3f, 0), currentDeepStateName, Styles.centerLabel);
}
#endif
}
}