Esempio n. 1
0
        public void translation(string name)
        {
            if (!this.states_.ContainsKey(name))//if no target return!
            {
                return;
            }

            StateBase target = this.states_[name];//target state

            while (!string.IsNullOrEmpty(target.defSubState) && this.states_.ContainsKey(target.defSubState))
            {
                target = this.states_[target.defSubState];
            }

            //if current, reset
            if (target == this.currState_[this.currState_.Count - 1])
            {
                target.over();
                target.start();
                return;
            }



            StateBase publicState = null;

            List <StateBase> stateList = new List <StateBase>();

            StateBase tempState  = target;
            string    fatherName = tempState.fatherName;

            //do loop
            while (tempState != null)
            {
                //reiterator current list
                for (var i = this.currState_.Count - 1; i >= 0; i--)
                {
                    StateBase state = this.currState_[i] as StateBase;
                    //if has public
                    if (state == tempState)
                    {
                        publicState = state;
                        break;
                    }
                }

                //end
                if (publicState != null)
                {
                    break;
                }

                //else push state_list
                stateList.Insert(0, tempState);
                //state_list.unshift(temp_state);

                if (fatherName != "")
                {
                    tempState  = this.states_[fatherName] as StateBase;
                    fatherName = tempState.fatherName;
                }
                else
                {
                    tempState = null;
                }
            }
            //if no public return
            if (publicState == null)
            {
                return;
            }

            List <StateBase> newCurrState = new List <StateBase>();
            bool             under        = true;

            //-- 析构状态
            for (int i2 = this.currState_.Count - 1; i2 >= 0; --i2)
            {
                StateBase state2 = this.currState_[i2] as StateBase;
                if (state2 == publicState)
                {
                    under = false;
                }
                if (under)
                {
                    state2.over();
                }
                else
                {
                    newCurrState.Insert(0, state2);
                }
            }


            //-- 构建状态
            for (int i3 = 0; i3 < stateList.Count; ++i3)
            {
                StateBase state3 = stateList[i3] as StateBase;
                state3.start();

                newCurrState.Add(state3);
            }

            this.currState_ = newCurrState;
            string outs = "";

            for (int i = 0; i < currState_.Count; ++i)
            {
                outs += ":" + currState_[i].name;
            }

            //			Debug.LogWarning (outs);
        }
Esempio n. 2
0
File: FSM.cs Progetto: baby2nana/DRY
        public void translation(string stateName)
        {
            //init state 需要在 states 字典里 不然无法 init
            if (!this.states_.ContainsKey(stateName))
            {
                return;
            }

            //获取到状态的末端子状态,如果是当前状态的最后一个,那么状态结束
            StateBase target = this.states_[stateName];

            while (!string.IsNullOrEmpty(target.defsubState) && this.states_.ContainsKey(target.defsubState))
            {
                target = this.states_[target.defsubState];
            }

            if (target == this.currStates_[this.currStates_.Count - 1])
            {
                target.over();
                target.start();
                return;
            }

            StateBase        publicState = null;
            List <StateBase> stateList   = new List <StateBase>();

            StateBase tempState  = target;
            string    fatherName = target.fatherName;

            //do loop
            while (tempState != null)
            {
                foreach (var state in this.currStates_)
                {
                    if (state == tempState)
                    {
                        publicState = state;
                        break;
                    }
                }

                //end
                if (publicState != null)
                {
                    break;
                }
                stateList.Insert(0, tempState);
                if (fatherName != "")
                {
                    tempState  = this.states_[fatherName] as StateBase;
                    fatherName = tempState.fatherName;
                }
                else
                {
                    tempState = null;
                }
            }
            if (publicState == null)
            {
                Debug.Log("public state is null...");
                return;
            }

            List <StateBase> newCurrState = new List <StateBase>();
            bool             under        = true;

            //析构状态
            foreach (var state in this.currStates_)
            {
                if (state == publicState)
                {
                    under = false;
                }

                if (under)
                {
                    state.over();
                }
                else
                {
                    newCurrState.Insert(0, state);
                }
            }
            //构建状态
            foreach (var state in stateList)
            {
                state.start();
                newCurrState.Add(state);
            }

            this.currStates_ = newCurrState;

            //存储当前所有状态名字
            this.currStateName = printCurrState();
        }