void LateUpdate() { if (current == null) { return; } GsStateTransition chosenTransition = null; for (int i = 0; i < AnyStateTransitions.Length; ++i) { GsStateTransition tr = AnyStateTransitions[i]; if (tr != null && (chosenTransition == null || tr.Priority > chosenTransition.Priority) && (tr.Cond != null && tr.Cond.IsSatisfied(this, current.State, tr)) ) { chosenTransition = tr; } } //any state transition priority > current state if (chosenTransition == null && current != null && current.Transitions != null) { for (int i = 0; i < current.Transitions.Length; ++i) { GsStateTransition tr = current.Transitions[i]; if (tr != null && (chosenTransition == null || tr.Priority > chosenTransition.Priority) && (tr.Cond != null && (tr.Cond.IsSatisfied(this, current.State, tr))) ) { chosenTransition = tr; } } } if (chosenTransition != null) { leaveCurrentState(); StateEntity nextStateEntity = stateDic[chosenTransition.nextStateName]; if (onStateChanged != null) { onStateChanged(current.State, nextStateEntity.State, chosenTransition); } enterState(nextStateEntity); } else { current.State.OnStateUpdate(Time.deltaTime); } }
public override bool IsSatisfied(GsStateMachine stateMachine, GsState ownerState, GsStateTransition transition) { string fromState = ownerState.name; string toState = transition.nextStateName; if (!transition.canTransitionToSelf && ownerState.name == transition.nextStateName) { return(false); } //last frame is not reached and this frame reach exit time then exit time condition meet //or return false if (exitTimeEnable) { bool nowResult = ownerState.runningTime > exitTime * ownerState.length - 0.1f; if (!cachedResult && nowResult) { cachedResult = nowResult; } else { cachedResult = nowResult; return(false); } } //遍历checker, 任何一个不满足就 return false 跳出 for (int i = 0; i < checkers.Length; i++) { GsConditionChecker checker = checkers[i]; Parameter param = stateMachine.parameters[checker.paramName]; bool boolParam = param.boolValue; int intParam = param.intValue; float floatParam = param.floatValue; switch (param.paramType) { case ParamType.ParamType_Bool: if (checker.checkMode == GsConditionChecker.CheckMode.CheckMode_If && boolParam == false) { return(false); } else if (checker.checkMode == GsConditionChecker.CheckMode.CheckMode_IfNot && boolParam == true) { return(false); } break; case ParamType.ParamType_Trigger: if (boolParam == false) { return(false); } param.boolValue = false; break; case ParamType.ParamType_Int: if (checker.checkMode == GsConditionChecker.CheckMode.CheckMode_Equals && intParam != (int)checker.threshold) { return(false); } else if (checker.checkMode == GsConditionChecker.CheckMode.CheckMode_NotEqual && intParam == (int)checker.threshold) { return(false); } else if (checker.checkMode == GsConditionChecker.CheckMode.CheckMode_Greater && intParam <= (int)checker.threshold) { return(false); } else if (checker.checkMode == GsConditionChecker.CheckMode.CheckMode_Less && intParam >= (int)checker.threshold) { return(false); } break; case ParamType.ParamType_Float: if (checker.checkMode == GsConditionChecker.CheckMode.CheckMode_Equals && !Mathf.Approximately(floatParam, checker.threshold)) { return(false); } else if (checker.checkMode == GsConditionChecker.CheckMode.CheckMode_NotEqual && Mathf.Approximately(floatParam, checker.threshold)) { return(false); } else if (checker.checkMode == GsConditionChecker.CheckMode.CheckMode_Greater && floatParam <= checker.threshold) { return(false); } else if (checker.checkMode == GsConditionChecker.CheckMode.CheckMode_Less && floatParam >= checker.threshold) { return(false); } break; default: break; } } return(true); }
//Determines whether the condition is satisfied. public abstract bool IsSatisfied(GsStateMachine stateMachine, GsState ownerState, GsStateTransition transition);