using UnityEngine; 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; /// /// Specify the statement to evaluate. /// /// protected abstract bool Statement(); /// /// Wrap the so it can be cached. /// internal bool GetStatement() { bool returnValue; if (_originSO.cacheResult && _isCached) returnValue = _cachedStatement; else { returnValue = Statement(); if (_originSO.cacheResult) { _isCached = true; _cachedStatement = returnValue; } } return returnValue; } 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 StateConditionSO _originSO; internal readonly StateMachine _stateMachine; internal readonly Condition _condition; internal readonly bool _expectedResult; public StateCondition(StateConditionSO originSO, StateMachine stateMachine, Condition condition, bool expectedResult) { _originSO = originSO; _stateMachine = stateMachine; _condition = condition; _condition._originSO = originSO; _expectedResult = expectedResult; } public bool IsMet() { bool statement = _condition.GetStatement(); bool isMet = statement == _expectedResult; #if UNITY_EDITOR _stateMachine._debugger.TransitionConditionResult(_originSO.name, statement, isMet); #endif return isMet; } } }