/// <summary>
        /// get state by name.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public SMState GetState(string name)
        {
            if (name == null)
            {
                return(null);
            }

            SMState state = null;

            if (_statesByName.ContainsKey(name))
            {
                state = _statesByName[name];
            }

            return(state);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Make transition from origin state to dest state.
        /// </summary>
        /// <param name="dest">dest state</param>
        /// <param name="duration">translate time</param>
        /// <returns></returns>
        public SMTransition MakeTransition(string dest, float duration)
        {
            SMState state = _sm.GetState(dest);

            if (state == null)
            {
                Debug.Log("can't find state named " + dest);
                return(null);
            }

            SMTransition transition = new SMTransition(this, state, duration, gameObject);

            transition._sm = _sm;
            _transitions.Add(transition);

            return(transition);
        }
        /// <summary>
        /// load state from file.
        /// </summary>
        /// <param name="asset">
        /// the state machine file.
        /// </param>
        /// <returns></returns>
        private bool LoadFromFile(TextAsset asset)
        {
            SMContext context = JsonConvert.DeserializeObject <SMContext>(asset.text);

            //!解析状态值
            for (int i = 0; i < context.states.Count; i++)
            {
                try
                {
                    SMState state = Activator.CreateInstance(Type.GetType(context.states[i]),
                                                             new object[] { context.states[i], gameObject }) as SMState;
                    _statesByName.Add(context.states[i], state);
                    _statesByHash.Add(state.HashCode, state);
                }
                catch
                {
                    Debug.Log("can't create state named " + context.states[i]);
                    continue;
                }
            }

            Default      = GetState(context.default_state);
            _activeState = Default;

            //!解析默认参数值
            foreach (string param in context.default_params)
            {
                ParseParamData(param);
            }

            //!解析transtitions
            for (int i = 0; i < context.transitions.Count; i++)
            {
                TranContext tranContext = context.transitions[i];
                SMState     origin      = GetState(tranContext.origin);

                if (origin == null)
                {
                    Debug.Log(string.Format("can't make transition from {0} to {1}",
                                            tranContext.origin, tranContext.dest));
                    continue;
                }

                SMTransition transition = origin.MakeTransition(tranContext.dest, tranContext.duration);

                if (transition == null)
                {
                    Debug.Log(string.Format("can't make transition from {0} to {1}",
                                            tranContext.origin, tranContext.dest));
                    continue;
                }

                for (int j = 0; j < context.transitions[i].conditions.Count; j++)
                {
                    string        fileName = tranContext.conditions[j].fileName;
                    List <string> args     = tranContext.conditions[j].paramDatas;

                    try
                    {
                        SMCondition condition = Activator.CreateInstance(Type.GetType(fileName), new object[] { args, gameObject }) as SMCondition;
                        transition.AddCondition(condition);
                    }
                    catch
                    {
                        Debug.Log(string.Format("can't make transition from {0} to {1}, condition {2} not found!",
                                                tranContext.origin, tranContext.dest, tranContext.conditions[j]));
                        origin.RemoveTransition(transition);
                    }
                }
            }

            return(true);
        }
        /// <summary>
        /// change the state
        /// </summary>
        /// <param name="state">
        /// the state for change. it's internal func.
        /// </param>
        internal void ChangeState(SMState state)
        {
            var old = ActiveState;

            ActiveState = state;
        }