/// <summary> /// 获取当前状态ID /// </summary> /// <returns></returns> public UInt32 GetCurrentStateID() { if (m_CurrentState == null) { return(CFSMState.cuInvalieStateId); } UInt32 uID = m_CurrentState.GetStateID(); return(uID); }
/// <summary> /// 获取一个状态 /// </summary> /// <param name="uStateId"></param> /// <returns></returns> protected CFSMState GetState(UInt32 uStateId) { Int32 nSize = m_StateList == null ? 0 : m_StateList.Count; for (Int32 i = 0; i < nSize; ++i) { CFSMState state = m_StateList[i]; if (state != null && state.GetStateID() == uStateId) { return(state); } } return(null); }
/// <summary> /// 添加一个状态 /// </summary> /// <param name="newState"></param> /// <returns></returns> public bool AddState(CFSMState newState) { if (newState == null) { return(false); } if (GetState(newState.GetStateID()) != null) // dup { return(false); } if (m_StateList == null) { m_StateList = new List <CFSMState>(); } m_StateList.Add(newState); return(true); }
/// <summary> /// 切换到一个状态 /// </summary> /// <param name="uNextState"></param> /// <param name="fsmEvent"></param> /// <returns></returns> public bool ChangeToState(UInt32 uNextState, CFSMEvent fsmEvent) { CFSMState nextFSMState = GetState(uNextState); if (nextFSMState == null) { return(false); } if (m_CurrentState != null) { if (m_CurrentState.GetStateID() == uNextState) { return(true); } if (m_CurrentState.OnExit(this, fsmEvent) == false) { BTDebug.Warning(string.Format("<BTFSM> State:{0} Exit Failed", m_CurrentState.GetStateName())); } } #if BTDEBUG if (m_bOpenDebug) { BTDebug.Log(string.Format("<BTFSM> FSM Trans From:{0} To:{1}", m_CurrentState == null ? "NULL" : m_CurrentState.GetStateName(), nextFSMState.GetStateName())); } #endif if (nextFSMState.OnEnter(this, fsmEvent) == false) { BTDebug.Warning(string.Format("<BTFSM> State:{0} Enter Failed", nextFSMState.GetStateName())); } m_CurrentState = nextFSMState; return(true); }