Exemple #1
0
        void SetTreeForward(Tree tree, BTAction action)
        {
            try
            {
                int index = (int)System.Enum.Parse(tree.LibraryActions, action.Name);

                MethodInfo start = action.GetType().GetMethod("Start",
                                                              BindingFlags.NonPublic | BindingFlags.Instance);

                MethodInfo init = action.GetType().GetMethod("Init",
                                                             BindingFlags.NonPublic | BindingFlags.Instance,
                                                             null,
                                                             new Type[] { typeof(Tree) },
                                                             null);

                MethodInfo Tick = action.GetType().GetMethod("Tick",
                                                             BindingFlags.NonPublic | BindingFlags.Instance,
                                                             null,
                                                             new Type[] { typeof(Tree) },
                                                             null);

                MethodInfo Reset = action.GetType().GetMethod("Reset",
                                                              BindingFlags.NonPublic | BindingFlags.Instance,
                                                              null,
                                                              new Type[] { typeof(Tree) },
                                                              null);

                if (start != null)
                {
                    start.Invoke(action, null);
                }

                if (init != null)
                {
                    tree.SetInitForward(index, Delegate.CreateDelegate(typeof(TickForward), action, init) as TickForward);
                }

                if (Tick != null)
                {
                    tree.SetTickForward(index, Delegate.CreateDelegate(typeof(TickForward), action, Tick) as TickForward);
                }

                if (Reset != null)
                {
                    tree.SetResetForward(index, Delegate.CreateDelegate(typeof(ResetForward), action, Reset) as ResetForward);
                }
            }
            catch (Exception)
            {
                //Debug.LogWarning(e);
            }
        }
        static BTAgent Resolver(string btPath)
        {
            BTAgent agent = null;

            TextAsset asset = UnityEngine.Resources.Load(btPath) as TextAsset;

            if (asset != null && !string.IsNullOrEmpty(asset.text))
            {
                try
                {
                    XmlDocument       xmlDoc   = new XmlDocument();
                    XmlReaderSettings settings = new XmlReaderSettings();
                    settings.IgnoreComments = true;
                    XmlReader reader = XmlReader.Create(new StringReader(asset.text), settings);
                    xmlDoc.Load(reader);

                    XmlElement root    = xmlDoc.SelectSingleNode("Tree") as XmlElement;
                    string     library = XmlUtil.GetAttributeString(root, "Library");
                    string     tree    = XmlUtil.GetAttributeString(root, "Tree");

                    agent = new BTAgent(btPath, library, tree);
                    agent.Tick();

                    //Profiler.BeginSample("XmlNode");
                    XmlNodeList xmlNodeList = root.GetElementsByTagName("Action");
                    foreach (XmlNode node in xmlNodeList)
                    {
                        XmlElement e        = node as XmlElement;
                        string     typeName = XmlUtil.GetAttributeString(e, "Type");

                        BTAction action = agent.GetAction(typeName);

                        if (action == null)
                        {
                            action = Reflecter.Instance.CreateAction(typeName);
                            agent.RegisterAction(action);
                        }

                        if (action != null)
                        {
                            InitData(btPath, action, e);
                        }
                    }
                }
                catch (System.Exception e)
                {
                    Debug.LogError("[" + btPath + "]" + "<" + e + ">");
                }
            }

            return(agent);
        }
Exemple #3
0
        public BTAction CreateAction(string actionName)
        {
            BehaveAction behave = m_BehaveActions.Find(ret => ret.name.Equals(actionName));

            System.Type actionType = (behave != null ? behave.type : null);

            if (actionType == null)
            {
                Debug.LogError("Can't find action : " + "[" + actionName + "]");
                return(null);
            }
            BTAction action = System.Activator.CreateInstance(actionType) as BTAction;

            action.SetName(actionName);
            return(action);
        }
Exemple #4
0
        public void RegisterAction(BTAction action)
        {
            if (action == null)
            {
                return;
            }

            SetTreeForward(m_Tree, action);

            if (m_Agent != null && !m_Agent.Equals(null))
            {
                action.SetAgent(m_Agent);
            }

            m_Actions.Add(action);
        }
Exemple #5
0
        public BTAction Clone()
        {
            BTAction action = System.Activator.CreateInstance(GetType()) as BTAction;

            action.m_Name = m_Name;

            foreach (KeyValuePair <string, object> kvp in m_TreeDataList)
            {
                object           obj    = System.Activator.CreateInstance(kvp.Value.GetType());
                List <FieldInfo> fields = BTResolver.s_Fields[m_Name];
                for (int i = 0; i < fields.Count; i++)
                {
                    fields[i].SetValue(obj, fields[i].GetValue(kvp.Value));
                }

                action.m_TreeDataList.Add(kvp.Key, obj);
            }

            return(action);
        }
        static void InitData(string btPath, BTAction action, XmlElement e)
        {
            if (!s_Fields.ContainsKey(action.Name))
            {
                s_Fields.Add(action.Name, new List <FieldInfo>());
            }

            if (action != null && e != null)
            {
                //foreach (FieldInfo field in action.GetType().GetFields())
                //{
                //    foreach (object attribute in field.GetCustomAttributes(true))
                //    {
                //        if (typeof(BehaveAttribute).IsInstanceOfType(attribute))
                //        {
                //            string error = SetValue(field, action, e);
                //            if (error != "")
                //            {
                //                //Debug.LogError("Action : " + action.Name + "["+ error +"]");
                //            }
                //            continue;
                //        }
                //    }
                //}

                //Profiler.BeginSample("Resolver");
                object[] members = action.GetType().GetCustomAttributes(true);
                for (int i = 0; i < members.Length; i++)
                {
                    if (typeof(BehaveAction).IsInstanceOfType(members[i]))
                    {
                        BehaveAction behave = members[i] as BehaveAction;
                        XmlNodeList  nodes  = e.GetElementsByTagName("Data");
                        for (int j = 0; j < nodes.Count; j++)
                        {
                            XmlElement d = nodes[j] as XmlElement;
                            if (behave.dataType != null)
                            {
                                object      obj      = System.Activator.CreateInstance(behave.dataType);
                                string      dataName = XmlUtil.GetAttributeString(d, "Name");
                                FieldInfo[] fields   = obj.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public);
                                for (int k = 0; k < fields.Length; k++)
                                {
                                    FieldInfo field = fields[k];
                                    object[]  attrs = field.GetCustomAttributes(true);
                                    for (int n = 0; n < attrs.Length; n++)
                                    {
                                        if (typeof(BehaveAttribute).IsInstanceOfType(attrs[n]))
                                        {
                                            if (s_Fields[action.Name].Find(ret => ret.Name == field.Name) == null)
                                            {
                                                s_Fields[action.Name].Add(field);
                                            }

                                            string error = SetValue(field, obj, d);
                                            if (error != "")
                                            {
                                                Debug.LogError(btPath + "-" + "Action:" + action.Name + "-" + "Data : " + dataName + "-" + "[" + error + "]");
                                            }
                                            continue;
                                        }
                                    }
                                }
                                action.AddData(dataName, obj);
                            }
                        }
                    }
                }
                //Profiler.EndSample();
            }
        }