2020-03-08 12:32:41 +01:00
using System ;
using UnityEngine ;
using UnityAtoms.BaseAtoms ;
namespace UnityAtoms.FSM
{
/// <summary>
/// Controls a transition from a FromState to a ToState.
/// </summary>
[Serializable]
public class Transition : IAtomListener < bool >
{
public string FromState { get = > _fromState . Value ; }
public string ToState { get = > _toState . Value ; }
public string Command { get = > _command . Value ; }
[SerializeField]
2020-03-22 01:22:08 +01:00
private StringReference _fromState = default ;
2020-03-08 12:32:41 +01:00
[SerializeField]
2020-03-22 01:22:08 +01:00
private StringReference _toState = default ;
2020-03-08 12:32:41 +01:00
[SerializeField]
2020-03-22 01:22:08 +01:00
private StringReference _command = default ;
2020-03-08 12:32:41 +01:00
[SerializeField]
2020-03-22 01:22:08 +01:00
private BoolVariable _testCondition = default ;
2020-03-08 12:32:41 +01:00
[SerializeField]
2020-03-22 01:22:08 +01:00
private bool _raiseEventToCompleteTransition = default ;
2020-03-08 12:32:41 +01:00
private FiniteStateMachine _fsmReference ;
2020-03-11 21:11:27 +01:00
private Action _onComplete ;
2020-03-08 12:32:41 +01:00
private void Complete ( )
{
2020-03-11 21:11:27 +01:00
_onComplete ( ) ;
_fsmReference = null ;
_onComplete = null ;
2020-03-08 12:32:41 +01:00
}
2020-03-11 21:11:27 +01:00
public void Begin ( FiniteStateMachine fsm , Action onComplete )
2020-03-08 12:32:41 +01:00
{
_fsmReference = fsm ;
2020-03-11 21:11:27 +01:00
_onComplete = onComplete ;
2020-03-08 12:32:41 +01:00
2020-03-08 20:41:22 +01:00
if ( _raiseEventToCompleteTransition )
2020-03-08 12:32:41 +01:00
{
if ( _fsmReference . CompleteCurrentTransition = = null )
{
2020-03-08 20:41:22 +01:00
Debug . LogWarning ( "Complete Current Transition on State Machine needs to pe specified when using Raise Event To Complete Transition. Ignoring and completing transition immediatly." ) ;
2020-03-08 12:32:41 +01:00
}
else
{
_fsmReference . CompleteCurrentTransition . RegisterListener ( this ) ;
return ;
}
}
Complete ( ) ;
}
public void OnEventRaised ( bool completeTransition )
{
if ( completeTransition )
{
_fsmReference . CompleteCurrentTransition . UnregisterListener ( this ) ;
Complete ( ) ;
}
}
public bool TestCondition ( ) = > _testCondition = = null | | _testCondition . Value ;
}
}