Exemplo n.º 1
0
    public FSMContorl(string contorl_name, IFSMManager manager)
    {
        m_contorl_name = contorl_name;

        m_fsm_manager = manager;

        m_state_pool = m_fsm_manager.InitState();

        IFSMState temp;

        var default_type = m_fsm_manager.GetDefaultStateType();

        if (!m_state_pool.TryGetValue(default_type, out temp))
        {
            throw new System.Exception("'" + m_contorl_name + "' not fount '" + default_type.Name + "' default state");
        }

        Cur_state = temp;

        var mes = new FSMStateMessage();

        mes.LastStateType = null;
        mes.CurStateType  = Cur_state.GetType();

        Cur_state.Enter(mes);
    }
Exemplo n.º 2
0
 /// <summary>
 /// Adds the state.
 /// </summary>
 /// <param name="state">State.</param>
 public void AddState(IFSMState state)
 {
     if (!mStates.Contains(state))
     {
         mStates.Add(state);
     }
 }
Exemplo n.º 3
0
    public void Update()
    {
        if (!Enable || WasShutdown)
        {
            return;
        }

        // Do transition to another state
        bool IsQueueStateDifferentThanCurrentState = (QueuedState != CurrentState);

        if (QueuedState != null && (IsQueueStateDifferentThanCurrentState || ForceStateChange))
        {
            if (CurrentState != null && QueuedState != null)
            {
                CurrentState.Exit(this, QueuedStateID);
            }

            PreviousState = CurrentState;   // Store prev state ...
            CurrentState  = QueuedState;    // Set to next state ...
            QueuedState   = null;           // Remove queued state ...

            CurrentState.Enter(this, PreviousStateID, Context);
        }
        // Queued state is current state ...
        else if (QueuedState == CurrentState)
        {
            QueuedState = null;
        }

        // Update current state ...
        if (CurrentState != null)
        {
            CurrentState.Update(this);
        }
    }
        public void FSMUpdate()
        {
            if (_StateStack.Count > 0)
            {
                IFSMState currState = _StateStack.Peek();
                currState.Update();
                currState.UpdateLate();

                // Unfocus update the other states
                for (int i = 0; i < _StateStack.Count - 1; ++i)
                {
                    _StateStack[i].UnfocusedUpdate();
                }

                                #if UNITY_EDITOR
                // Stack warning
                const int _StackCountWarning = 6;
                if (_StateStack.Count > _StackCountWarning)
                {
                    UnityEngine.Debug.LogWarning("FSM stack hit " +
                                                 _StateStack.Count.ToString());
                }
                                #endif
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// Removes the state.
 /// </summary>
 /// <param name="state">State.</param>
 public void RemoveState(IFSMState state)
 {
     if (mStates.Contains(state))
     {
         mStates.Remove(state);
     }
 }
Exemplo n.º 6
0
    public virtual void Shutdown()
    {
        WasShutdown = true;
        Enable      = false;

        if (RegisteredStates == null || RegisteredStates.Count <= 0)
        {
            return;
        }

        if (CurrentState != null)
        {
            CurrentState.Exit(this, -1);
        }

        foreach (KeyValuePair <int, IFSMState> KVP in RegisteredStates)
        {
            IFSMState ThisState = GetStateByID(KVP.Key);
            if (ThisState == null)
            {
                continue;
            }

            ThisState.Shutdown(this);
        }

        RegisteredStates.Clear();
        RegisteredStates = null;
    }
Exemplo n.º 7
0
        /// <summary>
        /// Enters the state.
        /// </summary>
        /// <param name="state">The state object.</param>
        public void EnterState(IFSMState state)
        {
            if (state == null)
            {
                return;
            }

            IFSMState previousState = m_currentState;

            m_currentState = state;

            if (previousState != null)
            {
                previousState.OnExit();
            }

            if (m_currentState != null)
            {
                m_currentState.OnEnter();
            }

            FSMEvent fsmEvent = new FSMEvent(FSMEvent.StateTransition);

            fsmEvent.previousState = previousState;
            fsmEvent.currentState  = m_currentState;
            DispatchEvent(fsmEvent);
        }
Exemplo n.º 8
0
 /// <summary>
 /// Update the fsm.
 /// </summary>
 public void Update()
 {
     if (mActiveState == null)
     {
         return;
     }
     for (int i = 0; i < mActiveState.stateTransitions.Count; ++i)
     {
         IFSMTransition transition = mActiveState.stateTransitions[i];
         if (transition == null)
         {
             continue;
         }
         if (transition.IsValid())
         {
             //1.exit current active state;
             mActiveState.OnExit();
             //2.get next state;
             mActiveState = transition.targetState;
             //3.handle tranition logic;
             transition.OnTransition();
             //4.execute new active state.
             if (mActiveState != null)
             {
                 mActiveState.OnEnter();
             }
             break;
         }
     }
     mActiveState.OnUpdate();
 }
Exemplo n.º 9
0
 public virtual void AddState(IFSMState <TState, TTrigger> state)
 {
     if (ContainsState(state.InnerState) == false)
     {
         states.Add(state);
     }
 }
Exemplo n.º 10
0
    public void SwitchTo(System.Type state)
    {
        if (m_cur_state.GetType() == state)
        {
            return;
        }

        IFSMState to_state;

        if (!m_state_pool.TryGetValue(state, out to_state))
        {
            throw new System.Exception("'" + m_contorl_name + "' not fount '" + state.Name + "' state");
        }

        var mes = new FSMStateMessage();

        mes.LastStateType = Cur_state.GetType();
        mes.CurStateType  = to_state.GetType();

        Cur_state.Exit(mes);

        Cur_state.Enter(mes);

        Cur_state = to_state;
    }
Exemplo n.º 11
0
    private void TransitionToState(FSMStateType StateName)
    {
        CurrentState.OnExit();
        CurrentState = GetState(StateName);
        CurrentState.OnEnter();

        Debug.Log("Transitioned to " + CurrentState.StateName);
    }
Exemplo n.º 12
0
            } // end TransitionPrev

            public void PerformTransition(IFSMState state) {
                if (null != currentState) currentState.DoBeforeLeaving();
                // end if
                previousState = currentState;
                currentState = state;
                if (null != currentState) currentState.DoBeforeEntering();
                // end if
            } // end PerformTransition
Exemplo n.º 13
0
 /// <summary>
 /// Add a new state.
 /// </summary>
 public void addState(string stateName, IFSMState state)
 {
     if (states == null)
     {
         states = new Dictionary <string, IFSMState> ();
     }
     states.Add(stateName, state);
 }
Exemplo n.º 14
0
 /// <summary>
 /// Start execute the fsm.
 /// </summary>
 public void Start()
 {
     if (entryState == null)
     {
         throw new Exception("the entry state is null!");
     }
     entryState.OnEnter();
     mActiveState = entryState;
 }
Exemplo n.º 15
0
 public override void Feed(IFSMState <EmptyFSMStateData> state, EmptyFSMStateData data = null)
 {
     base.Feed(state, data);
     this.CurrentState?.OnExit();
     this.CurrentType  = state;
     this.CurrentState = state;
     this.CurrentState.Feed(data);
     this.CurrentState.OnEnter();
 }
Exemplo n.º 16
0
    //------------------------------------------------------------------------------------------------------

    #region Constructors
    public FSM(object Owner)
    {
        Enable           = true;
        this.Owner       = Owner;
        QueuedState      = null;
        CurrentState     = null;
        PreviousState    = null;
        RegisteredStates = new Dictionary <int, IFSMState>();
    }
Exemplo n.º 17
0
    public void RegisterState(int Id, IFSMState State)
    {
        if (State == null || WasShutdown)
        {
            return;
        }

        RegisteredStates[Id] = State;
        State.Initialize(this);
    }
Exemplo n.º 18
0
 public void SwitchToState(int toKey)
 {
     if (_curState != null)
     {
         _curState.OnLeave(_context);
         _context.lastState = _curState;
     }
     _curState = _stateMap[toKey];
     _curState.OnEnter(_context);
 }
Exemplo n.º 19
0
        /// <summary>
        /// 设置触发器以过渡状态
        /// </summary>
        public void SetTrigger(string trigger, params object[] keys)
        {
            IFSMState targetState = _anyState[trigger];

            if (targetState == null && _curState != null)
            {
                targetState = _curState[trigger];
            }

            TurnState(_curState, targetState, keys);
        }
Exemplo n.º 20
0
        /// <summary>
        /// Removes the state.
        /// </summary>
        /// <param name="state">The state.</param>
        public void RemoveState(IFSMState state)
        {
            if (state == null)
            {
                return;
            }

            string stateName = state.stateName;

            RemoveState(stateName);
        }
Exemplo n.º 21
0
        public IFSMState[] GetAllStates()
        {
            int index = 0;

            IFSMState[] results = new IFSMState[statesDic.Count];
            foreach (KeyValuePair <string, IFSMState> state in statesDic)
            {
                results[index++] = state.Value;
            }
            return(results);
        }
Exemplo n.º 22
0
        /// <summary>
        /// 跳转状态
        /// </summary>
        private void TurnState(IFSMState oldState, IFSMState newState, params object[] keys)
        {
            if (newState != null && _stateList != null && _stateList.Contains(newState))
            {
                oldState?.OnExit();

                _curState = newState;

                newState.OnEnter(keys);
            }
        }
Exemplo n.º 23
0
        /// <summary>
        /// 设置过渡
        /// </summary>
        public void SetTransition(int transition, params object[] keys)
        {
            IFSMState targetState = _anyState[transition];

            if (targetState == null && _curState != null)
            {
                targetState = _curState[transition];
            }

            TurnState(_curState, targetState, keys);
        }
Exemplo n.º 24
0
    public void QueueState(int Id, System.Object Context, bool ForceStateChange)
    {
        if (WasShutdown)
        {
            return;
        }

        this.Context          = Context;
        this.ForceStateChange = ForceStateChange;
        this.QueuedState      = RegisteredStates[Id];
    }
    public override Key GetTypeByState(IFSMState <Data> stateType)
    {
        foreach (KeyValuePair <Key, IFSMState <Data> > state in this.StatesDatabase)
        {
            if (state.Value == stateType)
            {
                return(state.Key);
            }
        }

        return(default);
Exemplo n.º 26
0
        /// <summary>
        /// Pops all stacked states, and sets the given state as the new state
        /// </summary>
        public void NewState(IFSMState newState)
        {
            while (_StateStack.Count > 0)
            {
                IFSMState state = _StateStack.Peek();
                state.Exit();
                _StateStack.Pop();
            }

            _StateStack.Push(newState);
            newState.Enter();
        }
Exemplo n.º 27
0
        /// <summary>
        /// Adds the state.
        /// </summary>
        /// <param name="state">The state.</param>
        public void AddState(IFSMState state)
        {
            if (state == null)
            {
                return;
            }

            if (!m_stateDic.ContainsKey(state.stateName))
            {
                m_stateDic.Add(state.stateName, state);
            }
        }
Exemplo n.º 28
0
 public IEnumerator ChangeState(IFSMState newState)
 {
     StopAllCoroutines();
     //Verifica se o estado atual nao é nulo
     if (currentState != null)
     {
         //Se nao for, damos um exit
         yield return(StartCoroutine(currentState.Exit()));
     }
     currentState = newState;
     StartCoroutine(currentState.Enter());
 }
Exemplo n.º 29
0
 public void ChangeState(IFSMState <T> newState)
 {
     if (CurrentState != null)
     {
         CurrentState.Exit(Owner);
     }
     CurrentState = newState;
     if (CurrentState != null)
     {
         CurrentState.Enter(Owner);
     }
 }
Exemplo n.º 30
0
        /// <summary>
        /// Pushes the new State onto the stack.  The Current State loses focus.
        /// </summary>
        /// <param name="newState">The new state to push onto the stack</param>
        public void PushState(IFSMState newState, params object[] objs)
        {
            IFSMState currentState = _StateStack.Peek();

            if (currentState != null)
            {
                currentState.LostFocus();
            }

            _StateStack.Push(newState);
            newState.Enter();
        }