/// <summary> /// Adds transition to given state after checking /// </summary> /// <param name="transitionState">State that is to be transitioned to with transition</param> /// <param name="trans">transition selected to transition to above state</param> public void AddTransitionState(FSMStateID transitionState, FSMTransitions trans) { if (transitionState == FSMStateID.none) { Debug.LogError("Null Transition not allowed"); return; } if (transitions.ContainsKey(trans)) { Debug.LogError(trans.ToString() + " transition already listed as Transitionable"); return; } transitions.Add(trans, transitionState); Debug.Log("Transition state " + transitionState.ToString() + " has been added as a transitionable state of " + stateID.ToString() + " using transition " + trans.ToString()); }
/// <summary> /// Sets new state based off input transition /// </summary> /// <param name="trans">Transition to be set to given state</param> public void SetTransition(FSMTransitions trans) { if (trans == FSMTransitions.none) { Debug.LogError("Null transition is not allowed"); return; } FSMStateID newID = currentState.CheckTransition(trans); if (newID == FSMStateID.none) { Debug.LogError("There is no state bound to " + trans.ToString()); return; } foreach (var state in FSMStates) { if (newID == state.StateID) { Debug.Log("State changed to " + newID.ToString()); currentState.OnStateExit(playerTransform, gameObject); currentState = state; currentState.OnStateEnter(playerTransform, gameObject); return; } } Debug.LogError("Unidentified error with state transition"); }
// ----------------------------------------------------------------------------------- // // ----------------------------------------------------------------------------------- public void AddTransition(FSMTransition trans, FSMStateID id) { // Check if anyone of the args is invalid if (trans == FSMTransition.NullTransition) { Debug.LogError("FSMState ERROR: NullTransition is not allowed for a real FSMTransition"); return; } if (id == FSMStateID.NullStateID) { Debug.LogError("FSMState ERROR: NullStateID is not allowed for a real ID"); return; } // Since this is a Deterministic FSM, // check if the current FSMTransition was already inside the map if (map.ContainsKey(trans)) { Debug.LogError("FSMState ERROR: State " + FSMStateID.ToString() + " already has FSMTransition " + trans.ToString() + "Impossible to assign to another state"); return; } map.Add(trans, id); }
/// <summary> /// Removes FSMState from list of currently avalable FSM States /// </summary> /// <param name="stateID">State which is to be removed</param> public void RemoveFSMState(FSMStateID stateID) { foreach (var item in FSMStates) { if (item.StateID == stateID) { FSMStates.Remove(item); return; } } Debug.LogError("Error: " + stateID.ToString() + " Is not present within FSMStates"); }
public FSMState GetFSMState(FSMStateID stateID) { if (FSMStates.Exists(x => x.StateID == stateID)) { return(FSMStates.Find(x => x.StateID == stateID)); } else { Debug.LogError("Error: " + stateID.ToString() + " Is not present within FSMStates"); return(null); } }
/// <summary> /// 将stateID状态设置为已完成,statid应该是保存在previousStates中的,现在取消这个回退 /// </summary> /// <param name="stateID"></param> public void FinishState(FSMStateID stateID) { FSMState state = states.Find(s => s.StateID == stateID); if (state != null) { state.isFinished = true; } else { Debug.LogWarning("Cant't finish state: " + stateID.ToString() + "because can't find it"); } }
/// <summary> /// 执行状态转换,当前+条件->下一个 /// </summary> public void PerformTransition(FSMTransitionID trans) { // 判空 if (trans == FSMTransitionID.NullTransition) { Debug.LogError("FSM ERROR: NullTransition is not allowed for a real transition"); return; } // 尝试获取下一个状态ID FSMStateID nextStateID = currentState.GetOutputState(trans); if (nextStateID == FSMStateID.NullState) { Debug.LogError("FSM ERROR: State " + currentStateID.ToString() + " does not have a target state " + " for transition " + trans.ToString()); return; } // 获取下一个状态 FSMState nextState = null; if (nextStateID == FSMStateID.Default) { nextState = defaultState; } else { nextState = states.Find(s => s.StateID == nextStateID); } // 更新状态 currentState.DoBeforeLeaving(); if (!currentState.isFinished) { SavePreviousState(); } currentState = nextState; currentStateID = nextState.StateID; currentState.DoBeforeEntering(); }
// ----------------------------------------------------------------------------------- // // ----------------------------------------------------------------------------------- public void DeleteState(FSMStateID id) { // Check for NullState before deleting if (id == FSMStateID.NullStateID) { Debug.LogError("FSM ERROR: NullStateID is not allowed for a real state"); return; } // Search the List and delete the state if it's inside it foreach (FSMState state in states) { if (state.state == id) { states.Remove(state); return; } } Debug.LogError("FSM ERROR: Impossible to delete state " + id.ToString() + ". It was not on the list of states"); }
/// <summary> /// 删除ID对应的状态 /// </summary> public void RemoveState(FSMStateID id) { // 判空 if (id == FSMStateID.NullState) { Debug.LogError("FSM ERROR: NullStateID is not allowed for a real state"); return; } FSMState state = states.Find(s => s.StateID == id); if (state != null) { states.Remove(state); } else { Debug.LogError("FSM ERROR: Impossible to delete state " + id.ToString() + ". It was not on the list of states"); } }
// ----------------------------------------------------------------------------------- // // ----------------------------------------------------------------------------------- public void PerformTransition(FSMTransition trans) { // Check for NullTransition before changing the current state if (trans == FSMTransition.NullTransition) { Debug.LogError("FSM ERROR: NullTransition is not allowed for a real transition"); return; } // Check if the currentState has the transition passed as argument FSMStateID id = currentState.GetOutputState(trans); if (id == FSMStateID.NullStateID) { Debug.LogError("FSM ERROR: State " + currentStateID.ToString() + " does not have a target state " + " for transition " + trans.ToString()); return; } // Update the currentStateID and currentState currentStateID = id; foreach (FSMState state in states) { if (state.state == currentStateID) { // Do the post processing of the state before setting the new one currentState.DoBeforeLeaving(); currentState = state; // Reset the state to its desired condition before it can reason or act currentState.DoBeforeEntering(); break; } } }
/// <summary> /// 删除映射对 /// </summary> public void RemoveTransition(FSMTransitionID transitionID) { // 判空 if (transitionID == FSMTransitionID.NullTransition) { Debug.LogError("FSMState ERROR: NullTransition is not allowed"); return; } // 确实存在 if (map.ContainsKey(transitionID)) { map.Remove(transitionID); RemoveTransitionObject(transitionID); return; } Debug.LogError("FSMState ERROR: Transition " + transitionID.ToString() + " passed to " + stateID.ToString() + " was not on the state's transition list"); }
/// <summary> /// Modifies a transition of the state to transition to a different state /// </summary> /// <param name="transitionState">New state that is to be transitioned to when calling this transition</param> /// <param name="trans">previously added transition that is to be modified</param> /// <returns></returns> public bool EditTransitionState(FSMStateID transitionState, FSMTransitions trans) { if (transitions.ContainsKey(trans)) { transitions[trans] = transitionState; Debug.Log("Transition " + trans.ToString() + " of state " + stateID.ToString() + " has been modified to transition to stateID " + transitionState.ToString()); return(true); } else { Debug.LogError("Current state " + stateID.ToString() + " does not contain transition " + trans.ToString() + ". Did you mean to use AddTransitionState()"); return(false); } }