/// <summary> /// Create each state switch to the given state's transition ,and add them to the transition list. /// </summary> /// <param name="nextFsmStateName">the state which them will switch to</param> /// <param name="fsmTransitionConditionArray">Some transition conditions</param> public void CreateAnyFSMStateToFSMStateTransition(string nextFsmStateName, IFSMTransitionCondition[] fsmTransitionConditionArray) { string nextFSMStateNameHash = HashTool.StringToHash(nextFsmStateName); FSMState nextFSMState = null; bool nextFSMStateExist = m_FSMStateDic.TryGetValue(nextFSMStateNameHash, out nextFSMState); if (nextFSMStateExist == false) { Debug.LogErrorFormat("The nextFSMStateName:{0} is not exist", nextFsmStateName); return; } foreach (KeyValuePair <string, FSMState> kv in m_FSMStateDic) { FSMState fsmState = kv.Value; if (fsmState != m_Entry && fsmState != m_Exit && fsmState != m_EndFSMState && fsmState != nextFSMState) { fsmState.AddTransition(nextFSMState, fsmTransitionConditionArray); } } }
/// <summary> /// Create a transition which represent the state switch to another state,and add it to the transition list. /// </summary> /// <param name="fsmStateName">The state name</param> /// <param name="nextFSMStateName">The name of the next state</param> /// <param name="fsmTransitionConditionArray">Some transition conditions</param> public FSMTransition CreateFSMStateToAnotherFSMStateTransition(string fsmStateName, string nextFSMStateName, IFSMTransitionCondition[] fsmTransitionConditionArray) { //convert the state name to hash value,to improve performance string fsmStateNameHash = HashTool.StringToHash(fsmStateName); FSMState fsmState = null; bool fsmStateExist = m_FSMStateDic.TryGetValue(fsmStateNameHash, out fsmState); if (fsmStateExist == false) { Debug.LogErrorFormat("The fsmStateName:{0} is not exist", fsmStateName); return(null); } //convert the state name to hash value,to improve performance string nextFSMStateNameHash = HashTool.StringToHash(nextFSMStateName); FSMState nextFSMState = null; bool nextFSMStateExist = m_FSMStateDic.TryGetValue(nextFSMStateNameHash, out nextFSMState); if (nextFSMStateExist == false) { Debug.LogErrorFormat("The nextFSMStateName:{0} is not exist", nextFSMStateName); return(null); } return(fsmState.AddTransition(nextFSMState, fsmTransitionConditionArray)); }
/// <summary> /// Set the end state /// </summary> /// <param name="fsmStateName">The state name</param> /// <param name="endStateToExitStateFSMTransitionConditionArray">Some conditions of endState switch to the exitState</param> public void SetEndState(string fsmStateName, IFSMTransitionCondition[] endStateToExitStateFSMTransitionConditionArray) { if (fsmStateName == null || fsmStateName == "") { Debug.LogError("the fsmStateName can not be null or Empty"); return; } FSMState fsmState = null; //convert the state name to hash value,to improve performance string fsmStateNameHash = HashTool.StringToHash(fsmStateName); bool exist = m_FSMStateDic.TryGetValue(fsmStateNameHash, out fsmState); if (exist == true) { this.m_EndFSMState = fsmState; m_Exit = new FSMExit("FSMExit"); AddState(m_Exit); this.m_EndFSMState.AddTransition(m_Exit, endStateToExitStateFSMTransitionConditionArray); } else { Debug.LogErrorFormat("The {0} is not exist", fsmStateName); } }
/// <summary> /// Set the default state /// </summary> /// <param name="fsmStateName">The state name</param> public void SetDefaultState(string fsmStateName) { if (fsmStateName == null || fsmStateName == "") { Debug.LogError("the fsmStateName can not be null or Empty"); return; } FSMState fsmState = null; //convert the state name to hash value,to improve performance string fsmStateNameHash = HashTool.StringToHash(fsmStateName); bool exist = m_FSMStateDic.TryGetValue(fsmStateNameHash, out fsmState); if (exist == true) { this.m_DefaultFSMState = fsmState; } else { Debug.LogErrorFormat("The {0} is not exist", fsmStateName); } }
/// <summary> /// Delete the specific transition in the state /// </summary> /// <param name="fsmStateName">The state name</param> /// <param name="fsmTransition">The specific transition</param> public void DeleteFSMStateToAnotherFSMStateTransition(string fsmStateName, FSMTransition fsmTransition) { //convert the state name to hash value,to improve performance string fsmStateNameHash = HashTool.StringToHash(fsmStateName); FSMState fsmState = null; bool fsmStateExist = m_FSMStateDic.TryGetValue(fsmStateNameHash, out fsmState); if (fsmStateExist == true) { fsmState.DeleteTransition(fsmTransition); } else { Debug.LogErrorFormat("The fsmStateName:{0} is not exist", fsmStateName); } }
/// <summary> /// Delete state from the dictionary according to its name /// </summary> /// <param name="fsmStateName">the state name</param> public void DeleteState(string fsmStateName) { if (fsmStateName == null || fsmStateName == "") { Debug.LogError("the fsmStateName can not be null or empty"); return; } //convert the state name to hash value,to improve performance string fsmStateNameHash = HashTool.StringToHash(fsmStateName); if (m_FSMStateDic.ContainsKey(fsmStateNameHash) == true) { m_FSMStateDic.Remove(fsmStateNameHash); } else { Debug.LogErrorFormat("The finite state machine do not have the state:{0}", fsmStateName); } }
/// <summary> /// Get the state name,then convert it to hash string as the key, and the state as the value, /// Add them to the dictionary /// </summary> /// <param name="fsmState">the state</param> public void AddState(FSMState fsmState) { if (fsmState == null) { Debug.LogError("the fsmState can not be null"); return; } string fsmStateName = fsmState.FSMStateName; //convert the state name to hash value,to improve performance string fsmStateNameHash = HashTool.StringToHash(fsmStateName); if (m_FSMStateDic.ContainsKey(fsmStateNameHash) == false) { m_FSMStateDic.Add(fsmStateNameHash, fsmState); } else { Debug.LogErrorFormat("The finite state machine already have the state:{0}", fsmState.FSMStateName); } }