public void FSMUpdate()
    {
        if (currentState == null)
        {
            return;
        }

        if (transformImmediately)
        {
            currentState.Exit();
            currentState = nextState;
            currentState.Enter();
            transformImmediately = false;
        }

        currentState.Excute();
        if (currentState.Status == FSMSateStatus.Success)
        {
            currentState.Exit();
            currentState = null;
            if (nextState != null)
            {
                currentState = nextState;
                currentState.Enter();
                nextState = null;
            }
            else
            {
                currentState = defaultState;
                currentState.Enter();
            }
        }
    }
Beispiel #2
0
 public void ConfigState(T _stateOwner, FSMState <T> _currState, FSMState <T> _globalState)
 {
     stateOwner  = _stateOwner;
     currState   = _currState;
     globalState = _globalState;
     currState.Enter(_stateOwner);
 }
Beispiel #3
0
    public bool ChangeState(FSMState <T> NewState, bool forceChange)
    {
        if (NewState == CurrentState && !forceChange)
        {
            return(false);
        }

#if DEBUG_STATES_ALL
        MessengerDebug.print("FSM(" + typeof(T) + "):Change State " + CurrentState + " to " + NewState);
#endif

        PreviousState = CurrentState;
        CurrentState  = NewState;

        if (PreviousState != null)
        {
            PreviousState.Exit(Owner);
        }

        if (CurrentState != null)
        {
            CurrentState.Enter(Owner);
        }

        return(true);
    }
Beispiel #4
0
    private void ApplyPendingChanges()
    {
        foreach (PendingChange change in pendingList)
        {
            switch (change.action)
            {
            case Action.Push:
                FSMState pushState = FindState(change.stateID);
                pushState.Enter();
                stateStack.Push(pushState);
                break;

            case Action.Pop:
                FSMState popState = stateStack.Pop();
                popState.Exit();
                break;

            case Action.Clear:
                while (!IsEmpty())
                {
                    FSMState state = stateStack.Pop();
                    state.Exit();
                }
                stateStack.Clear();
                break;
            }
        }
        pendingList.Clear();
    }
Beispiel #5
0
    /// <summary>
    /// 改变状态
    /// </summary>
    /// <param name="type"></param>
    public void ChangeState(FSMStateType type, params System.Object[] args)
    {
        if (CurrentType == FSMStateType.Dead)
        {
            return;
        }

        if (!map.ContainsKey(type))
        {
            Debuger.LogError("不包含此状态" + type);
        }

        FSMState state = null;

        map.TryGetValue(type, out state);
        if (state == null)
        {
            Debuger.LogError("获取到的状态为空:" + type);
            return;
        }

        if (state == currentState)
        {
            return;
        }
        if (currentState != null)
        {
            currentState.Exit();
        }
        currentState = state;
        currentType  = type;
        currentState.Enter(args);
    }
Beispiel #6
0
    private void SafePush(int stateID)
    {
        FSMState pushState = FindState(stateID);

        pushState.Enter();
        stateStack.Push(pushState);
    }
Beispiel #7
0
 void ChangeState(FSMState nextState)
 {
     ProcessUpdate = false;
     CurrentState.Leave(nextState);
     nextState.Enter(CurrentState);
     CurrentState  = nextState;
     ProcessUpdate = true;
 }
Beispiel #8
0
 public void Reset()
 {
     currentState = defaultState;
     if (currentState != null)
     {
         currentState.Enter();
     }
 }
Beispiel #9
0
 public void Update()
 {
     if (CurrentState.IsFinished)
     {
         CurrentState.Exit();
         CurrentState = DefaultState;
         CurrentState.Enter();
     }
     CurrentState.Execute();
 }
Beispiel #10
0
 public void ChangeState(FSMState <T> _newState)
 {
     if (currState != null && (_newState != currState))
     {
         currState.Exit(stateOwner);
         preState  = currState;
         currState = _newState;
         currState.Enter(stateOwner);
     }
 }
Beispiel #11
0
 public void ChangeState(FSMState <T> newState)
 {
     if (currentState != null && !reverting)
     {
         previousStates.Push(currentState);
     }
     reverting = false;
     currentState?.Exit(owner);
     currentState = newState;
     currentState?.Enter(owner);
 }
Beispiel #12
0
    public void Configure(T owner, FSMState <T> initialState, FSMState <T> globalState)
    {
        Owner = owner;

        if (globalState != null)
        {
            GlobalState = globalState;
            GlobalState.Enter(Owner);
        }

        ChangeState(initialState);
    }
 public void ChangeState(FSMState <T> NewState)
 {
     PreviousState = CurrentState;
     if (PreviousState != null)
     {
         //Debug.Log(Owner + "EXITED STATE: " + PreviousState);
         PreviousState.Exit();
     }
     //Debug.Log(Owner + "ENTERED STATE: " + NewState);
     CurrentState = NewState;
     CurrentState.Enter(Owner);
 }
Beispiel #14
0
 public void ChangeState(FSMState <T> newState)
 {
     // NOTE: Right now, you can pass null to this.
     if (currentState != null)
     {
         currentState.Exit();
     }
     currentState = newState;
     if (currentState != null)
     {
         currentState.Enter(owner);
     }
 }
Beispiel #15
0
    public void SetGlobalState(string v)
    {
        if (globalState != null)
        {
            globalState.Exit();
        }

        if (allStates.ContainsKey(v))
        {
            globalState = allStates[v];
            globalState.Enter();
        }
    }
 public void ChangeState(FSMState <T> nextState)
 {
     previousState = currentState;
     if (currentState != null)
     {
         currentState.Exit(owner);
     }
     currentState = nextState;
     if (currentState != null)
     {
         currentState.Enter(owner);
     }
 }
Beispiel #17
0
    /// <summary>
    /// 处理下一个状态
    /// </summary>
    /// <param name="cmd"></param>
    protected void ChangeState(ICmd cmd)
    {
        if (NextState == null)
        {
            return;
        }
        CurrentState.Release();

        CurrentState.Exit();
        CurrentState = NextState;
        CurrentState.Enter(cmd);

        NextState = null;
    }
Beispiel #18
0
 public bool ChangeGlobalState(FSMState <T> NewGlobalState)
 {
     if (NewGlobalState != null)
     {
         GlobalState.Exit(Owner);
         NewGlobalState.Enter(Owner);
         GlobalState = NewGlobalState;
         return(true);
     }
     else
     {
         return(false);
     }
 }
Beispiel #19
0
    public void Update()
    {
        FSMTransition transition = current.VerifyTransitions();

        if (transition != null)
        {
            current.Exit();
            transition.Fire();
            current = current.NextState(transition);
            current.Enter();
        }
        else
        {
            current.Stay();
        }
    }
    // Examine transitions leading out from the current state
    // If a condition is activated, change the current state and
    // take all the actions linked to:
    // 1. Exit from the current state
    // 2. The activated transition
    // 3. Enter to the new state
    // If no transition is activated, take the actions associated
    // to staying in the current state

    public void Update()       // NOTE: this is NOT a MonoBehaviour
    {
        FSMTransition transition = current.VerifyTransitions();

        if (transition != null)
        {
            current.Exit();
            transition.Fire();
            current = current.NextState(transition);
            current.Enter();
        }
        else
        {
            current.Stay();
        }
    }
    public void  ChangeState(FSMState <T, U> NewState)
    {
        mPreviousState = mCurrentState;

        if (mCurrentState != null)
        {
            mCurrentState.Exit();
        }

        mCurrentState = NewState;

        if (mCurrentState != null)
        {
            mCurrentState.Enter();
        }
    }
Beispiel #22
0
    public void PerformTransition(Transition trans)
    {
        FSMState state = m_curState.GetState(trans);

        if (state != null)
        {
            Debug.Log("切换到状态 : " + trans.ToString());
            m_curState.Exit(m_owner);
            m_curState = state;
            m_curState.Enter(m_owner);
        }
        else
        {
            Debug.LogError("转换到 " + trans.ToString() + " 失败");
        }
    }
Beispiel #23
0
    public void  ChangeState(FSMState <T> newState)
    {
        _previousState = _currentState;

        if (_currentState != null)
        {
            _currentState.Exit(Owner);
        }

        _currentState = newState;

        if (_currentState != null)
        {
            _currentState.Enter(Owner);
        }
    }
    //ステート切り替えメソッド
    //現在のステートの終了メソッドを行ってから、現在のステートを切り替える
    public void  ChangeState(FSMState <T> NewState)
    {
        PreviousState = CurrentState;

        if (CurrentState != null)
        {
            CurrentState.Exit(Owner);
        }

        CurrentState = NewState;

        if (CurrentState != null)
        {
            CurrentState.Enter(Owner);
        }
    }
    public void ChangeState(FSMState <T> newState)
    {
        if (CurrentState == newState || newState == null)
        {
            return;
        }

        if (CurrentState != null)
        {
            CurrentState.Exit(Owner);
        }

        LastState = CurrentState;

        CurrentState = newState;
        CurrentState.Enter(Owner);
    }
Beispiel #26
0
 /// <summary>
 /// 通过命令跳转到相应状态
 /// </summary>
 /// <param name="cmd"></param>
 /// <param name="args"></param>
 public void ExecuteCmd(string cmd, params object[] args)
 {
     FSMState toState = CurrentState.GetDestState(cmd);
     if(toState == null)
     {
         return;
     }
     else if(toState == CurrentState)
     {
         CurrentState.Enter(args);
     }
     else
     {
         CurrentState.Exit();
         CurrentState = toState;
         CurrentState.Enter(args);
     }
 }
Beispiel #27
0
    public void SwitchTo(int code)
    {
        if (null != curState && curState.Code == code)
        {
            return;
        }
        if (null != curState)
        {
            curState.Exit();
        }

        curState = GetState(code);

        if (null != curState)
        {
            curState.Enter();
        }
    }
    public void  ChangeState(FSMState <T, U> NewState)
    {
        PreviousState = CurrentState;

        if (PreviousState == NewState)
        {
            return;
        }

        if (CurrentState != null)
        {
            CurrentState.Exit();
        }

        CurrentState = NewState;

        if (CurrentState != null)
        {
            CurrentState.Enter();
        }
    }
Beispiel #29
0
    public bool ChangeState(string id, object param = null)
    {
        if (id == null || !allStates.ContainsKey(id))
        {
            //DebugUtil.Log("fsm: state not exist: " + id);
            return(false);
        }

        FSMState <T> newState = allStates[id];

        if (newState == currentState)
        {
//			DebugUtil.Log("fsm: try to switch to same state " + id);
            return(true);
        }

        // currently changing to the target state.
        if (changingToState == id)
        {
            return(true);
        }

        changingToState = id;
        previousState   = currentState;
        if (currentState != null)
        {
            currentState.Exit();
        }
        currentState = newState;
        if (currentState != null)
        {
            currentState.SetParam(param);
            currentState.Enter();
        }
        stackHistory.Add(id);
        changingToState = null;
        return(true);
    }
Beispiel #30
0
    public void ChangeState(U newState)
    {
        _previousState = _currentState;

        if (_currentState != null)
        {
            _currentState.Exit(_owner);
        }

        if (_stateDictionary.ContainsKey(newState))
        {
            _currentState = _stateDictionary[newState];

            if (_currentState != null)
            {
                _currentState.Enter(_owner);
            }
        }
        else
        {
            Debug.LogError(newState.ToString() + " is not Registered.");
        }
    }
Beispiel #31
0
    public void PerformTransition(GlobalStateData.FSMTransistion trans)
    {
        // Check for NullTransition before changing the current state
        if (trans == GlobalStateData.FSMTransistion.None)
        {
            Debug.LogError(SCRIPT_NAME + ": Null transition is not allowed");
            return;
        }

        // Check if the currentState has the transition passed as argument
        GlobalStateData.FSMStateID id = currentState.GetOutputState(trans);
        if (id == GlobalStateData.FSMStateID.None)
        {
            Debug.LogError(SCRIPT_NAME + ": Current State does not have a target state for this transition");
            return;
        }


        // Update the currentStateID and currentState
        //currentStateID = id;
        foreach (FSMState state in fsmStates)
        {
            if (state.ID == id)
            {
                // Store previous state and call exit method.
                previousState = currentState;
                previousState.Exit();

                // Update current state and call enter method.
                currentState = state;
                currentState.Enter();

                break;
            }
        }
    }
Beispiel #32
0
	public void SwitchTo(int code)
	{
		if (null != curState && curState.Code == code) return;
		if (null != curState)
		{
			curState.Exit();
		}
		
		curState = GetState(code);
		
		if (null != curState)
		{
			curState.Enter();
		}
	}