using UOP1.StateMachine.ScriptableObjects;
namespace UOP1.StateMachine
{
///
/// Class that represents a conditional statement.
///
public abstract class Condition : IStateComponent
{
private bool _isCached = false;
private bool _cachedStatement = default;
internal StateConditionSO _originSO;
///
/// Use this property to access shared data from the that corresponds to this
///
protected StateConditionSO OriginSO => _originSO;
///
/// Specify the statement to evaluate.
///
///
protected abstract bool Statement();
///
/// Wrap the so it can be cached.
///
internal bool GetStatement()
{
if (!_isCached)
{
_isCached = true;
_cachedStatement = Statement();
}
return _cachedStatement;
}
internal void ClearStatementCache()
{
_isCached = false;
}
///
/// Awake is called when creating a new instance. Use this method to cache the components needed for the condition.
///
/// The this instance belongs to.
public virtual void Awake(StateMachine stateMachine) { }
public virtual void OnStateEnter() { }
public virtual void OnStateExit() { }
}
///
/// Struct containing a Condition and its expected result.
///
public readonly struct StateCondition
{
internal readonly StateMachine _stateMachine;
internal readonly Condition _condition;
internal readonly bool _expectedResult;
public StateCondition(StateMachine stateMachine, Condition condition, bool expectedResult)
{
_stateMachine = stateMachine;
_condition = condition;
_expectedResult = expectedResult;
}
public bool IsMet()
{
bool statement = _condition.GetStatement();
bool isMet = statement == _expectedResult;
#if UNITY_EDITOR
_stateMachine._debugger.TransitionConditionResult(_condition._originSO.name, statement, isMet);
#endif
return isMet;
}
}
}