Beispiel #1
0
 public void AddCondition(FSMCondition condition)
 {
     if (conditions.ContainsKey(condition.name))
     {
         UnityEngine.Debug.LogError("状态" + name + "已经包含名为:" + condition.name + "的动作!");
     }
     else
     {
         conditions.Add(condition.name, condition);
     }
 }
Beispiel #2
0
        private FSMCondition LoadCondition(string value)
        {
            var fsmCondition = new FSMCondition();

            fsmCondition.conditionNames     = new List <string> ();
            fsmCondition.conditionPrefixes  = new List <FSMCondition.EConditionPrefix> ();
            fsmCondition.conditionOperators = new List <FSMCondition.EConditionOperator> ();

            var stringBuilder = new StringBuilder();

            for (int i = 0; i < value.Length; i++)
            {
                var charTmp = value [i].ToString();
                switch (charTmp)
                {
                case "&":
                    i += 1;
                    fsmCondition.conditionNames.Add(stringBuilder.ToString());
                    stringBuilder = new StringBuilder();
                    stringBuilder.Append("&&");
                    break;

                case "|":
                    i += 1;
                    fsmCondition.conditionNames.Add(stringBuilder.ToString());
                    stringBuilder = new StringBuilder();
                    stringBuilder.Append("||");
                    break;

                default:
                    stringBuilder.Append(charTmp);
                    break;
                }
            }
            if (stringBuilder.Length > 0)
            {
                fsmCondition.conditionNames.Add(stringBuilder.ToString());
            }
            for (int i = 0; i < fsmCondition.conditionNames.Count; i++)
            {
                var conditionName = fsmCondition.conditionNames [i];
                var negative      = conditionName.IndexOf("!");
                var conditionAnd  = conditionName.IndexOf("&&");
                var conditionOr   = conditionName.IndexOf("||");
                if (negative != -1)
                {
                    fsmCondition.conditionPrefixes.Add(FSMCondition.EConditionPrefix.Negative);
                    conditionName = conditionName.Replace("!", "");
                }
                else
                {
                    fsmCondition.conditionPrefixes.Add(FSMCondition.EConditionPrefix.None);
                }
                if (conditionAnd != -1)
                {
                    fsmCondition.conditionOperators.Add(FSMCondition.EConditionOperator.And);
                    conditionName = conditionName.Replace("&&", "");
                }
                else if (conditionOr != -1)
                {
                    fsmCondition.conditionOperators.Add(FSMCondition.EConditionOperator.Or);
                    conditionName = conditionName.Replace("||", "");
                }
                else
                {
                    fsmCondition.conditionOperators.Add(FSMCondition.EConditionOperator.None);
                }
                fsmCondition.conditionNames [i] = conditionName;
            }
            return(fsmCondition);
        }
 public FSMStateData()
 {
     this.Condition  = new FSMCondition();
     this.StateName  = string.Empty;
     this.ListStates = new List <FSMStateData>();
 }
Beispiel #4
0
        public FsmStateMachine(XmlDocument xmlDoc)
        {
            this.states = new Dictionary <string, State>();
            XmlNode root = xmlDoc.SelectSingleNode("StateMachine");

            fsmName = root.Attributes["name"].Value;
            XmlNodeList states = root.SelectNodes("State");

            for (int i = 0, size = states.Count; i < size; i++)
            {
                XmlNode stateNode = states[i];
                State   state     = new State(stateNode.Attributes["name"].Value);
                state.isDefault = stateNode.Attributes["isDefault"].Value == "true";
                XmlNodeList actions = stateNode.SelectSingleNode("Actions").SelectNodes("Action");
                for (int j = 0; j < actions.Count; j++)
                {
                    XmlNode   actionNode = actions[j];
                    string    className  = actionNode.Attributes["name"].Value;
                    FSMAction action     = (FSMAction)Reflection.GetExecutingAssembly().CreateInstance(className);
                    action.name = className;
                    XmlNodeList paramsNodes = actionNode.SelectSingleNode("params").SelectNodes("param");
                    for (int k = 0; k < paramsNodes.Count; k++)
                    {
                        XmlNode   paramNode    = paramsNodes[k];
                        string    propertyName = paramNode.Attributes["key"].Value;
                        Type      tp           = action.GetType();
                        FieldInfo pInfo        = tp.GetField(propertyName);
                        if (null == pInfo)
                        {
                            UnityEngine.Debug.LogError("获取条件名为: " + action.name + "的属性 " + paramNode.Attributes["key"].Value + "不存在");
                            return;
                        }
                        pInfo.SetValue(action, paramNode.Attributes["value"].Value);
                    }
                    state.AddAction(action);
                }

                XmlNodeList transitions = stateNode.SelectSingleNode("Transitions").SelectNodes("Transition");
                for (int j = 0; j < transitions.Count; j++)
                {
                    XmlNode     transitionNode  = transitions[j];
                    Transition  trans           = new Transition(transitionNode.Attributes["name"].Value, state.name, transitionNode.Attributes["toState"].Value);
                    XmlNodeList conditionsNodes = transitionNode.SelectSingleNode("Conditions").SelectNodes("Condition");
                    for (int k = 0; k < conditionsNodes.Count; k++)
                    {
                        XmlNode      conditionNode = conditionsNodes[k];
                        string       className     = conditionNode.Attributes["name"].Value;
                        FSMCondition condition     = (FSMCondition)Reflection.GetExecutingAssembly().CreateInstance(className);
                        condition.name = className;
                        XmlNodeList paramsNodes = conditionNode.SelectSingleNode("params").SelectNodes("param");
                        for (int m = 0; m < paramsNodes.Count; m++)
                        {
                            XmlNode   paramNode = paramsNodes[m];
                            FieldInfo pInfo     = condition.GetType().GetField(paramNode.Attributes["key"].Value);
                            if (null == pInfo)
                            {
                                UnityEngine.Debug.LogError("获取条件名为: " + condition.name + "的属性 " + paramNode.Attributes["key"].Value + "不存在");
                                return;
                            }
                            // UnityEngine.Debug.Log("获取条件名为: " + condition.name + "的属性 " + paramNode.Attributes["key"].Value + "不存在");
                            pInfo.SetValue(condition, paramNode.Attributes["value"].Value);
                        }
                        trans.AddCondition(condition);
                    }
                    state.AddTransition(trans);
                }

                AddState(state);
            }
        }
Beispiel #5
0
        private FSMCondition LoadCondition(string value)
        {
            var fsmCondition = new FSMCondition();

            fsmCondition.conditionNames     = new List <string> ();
            fsmCondition.conditionPrefixes  = new List <FSMCondition.EConditionPrefix> ();
            fsmCondition.conditionOperators = new List <FSMCondition.EConditionOperator> ();

            var stringBuilder = new StringBuilder();

            for (int i = 0; i < value.Length; i++)
            {
                var charTmp = value [i].ToString();
                switch (charTmp)
                {
                case "&":
                    i += 1;
                    stringBuilder.Append("&&");
                    fsmCondition.conditionNames.Add(stringBuilder.ToString());
                    stringBuilder = new StringBuilder();
                    break;

                case "|":
                    i += 1;
                    stringBuilder.Append("||");
                    fsmCondition.conditionNames.Add(stringBuilder.ToString());
                    stringBuilder = new StringBuilder();
                    break;

                default:
                    stringBuilder.Append(charTmp);
                    break;
                }
            }
            if (stringBuilder.Length > 0)
            {
                fsmCondition.conditionNames.Add(stringBuilder.ToString());
            }
            for (int i = 0; i < fsmCondition.conditionNames.Count; i++)
            {
                var conditionName = fsmCondition.conditionNames [i];
                var prefix        = conditionName [0].ToString();
                var suffix        = conditionName.Substring(conditionName.Length - 2, 2);
                if (prefix == "!")
                {
                    fsmCondition.conditionPrefixes.Add(FSMCondition.EConditionPrefix.Negative);
                    conditionName = conditionName.Substring(1, conditionName.Length - 1);
                }
                else
                {
                    fsmCondition.conditionPrefixes.Add(FSMCondition.EConditionPrefix.None);
                }
                if (suffix == "&&")
                {
                    fsmCondition.conditionOperators.Add(FSMCondition.EConditionOperator.And);
                    conditionName = conditionName.Substring(0, conditionName.Length - 2);
                }
                if (suffix == "||")
                {
                    fsmCondition.conditionOperators.Add(FSMCondition.EConditionOperator.Or);
                    conditionName = conditionName.Substring(0, conditionName.Length - 2);
                }
                else
                {
                    fsmCondition.conditionOperators.Add(FSMCondition.EConditionOperator.None);
                }
                fsmCondition.conditionNames [i] = conditionName;
            }
            return(fsmCondition);
        }