Example #1
0
        public static void Serialize(this Node root, BehaviorTreeConfig config)
        {
            config.Clear();
            BehaviorNodeConfig          rootNp = config.AddRootNode(root.GetType().Name);
            EQueue <Node>               queue  = new EQueue <Node>();
            EQueue <BehaviorNodeConfig> npQue  = new EQueue <BehaviorNodeConfig>();

            rootNp.describe = root.Description;
            queue.Enqueue(root);
            npQue.Enqueue(rootNp);
            while (queue.Count > 0)
            {
                Node cur = queue.Dequeue();
                BehaviorNodeConfig np = npQue.Dequeue();
                foreach (Node child in cur.GetChildren)
                {
                    BehaviorNodeConfig childNp = GetNodeConfigFromNode(child);
                    queue.Enqueue(child);
                    npQue.Enqueue(childNp);
                    config.AddChild(np, childNp);
                }
            }
            //             PrintNode(root);
            //             PrintConfigNode(config.RootNodeConfig);
        }
Example #2
0
        public BTEditorTree(BehaviorTreeConfig config)
        {
            Type rootType = typeof(Game).Assembly.GetType($"Model.{config.RootNodeProto.Name}");
            Node root     = (Node)Activator.CreateInstance(rootType, config.RootNodeProto);

            root.Id = BTEditor.NodeIdStartIndex;
            EQueue <NodeProto> protoStack = new EQueue <NodeProto>();
            EQueue <Node>      nodeStack  = new EQueue <Node>();

            protoStack.Enqueue(config.RootNodeProto);
            nodeStack.Enqueue(root);
            while (protoStack.Count > 0)
            {
                NodeProto p    = protoStack.Dequeue();
                Node      node = nodeStack.Dequeue();

                foreach (KeyValuePair <string, object> argsItem in p.Args.Dict())
                {
                    FieldInfo fieldInfo = node.GetType().GetField(argsItem.Key, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                    fieldInfo.SetValue(node, argsItem.Value);
                }
                foreach (NodeProto child in p.Children)
                {
                    Type t         = typeof(Game).Assembly.GetType($"Model.{child.Name}");
                    Node childNode = (Node)Activator.CreateInstance(t, child);
                    AddChild(childNode, node);
                    protoStack.Enqueue(child);
                    nodeStack.Enqueue(childNode);
                }
            }
            this.BTConfig = config;
            _root         = root;
        }
Example #3
0
        public static T GetChildInType <T>(this Node root) where T : Node
        {
            EQueue <Node> nodeStack = new EQueue <Node>();

            nodeStack.Enqueue(root);
            while (nodeStack.Count > 0)
            {
                Node c = nodeStack.Dequeue();
                foreach (Node child in c.GetChildren)
                {
                    if (child.GetType() == typeof(T))
                    {
                        return(child as T);
                    }
                    nodeStack.Enqueue(child);
                }
            }
            return(null);
        }
Example #4
0
        public static T[] GetChildrenInType <T>(this Node root) where T : Node
        {
            EQueue <Node> nodeStack = new EQueue <Node>();
            List <T>      list      = new List <T>();

            nodeStack.Enqueue(root);
            while (nodeStack.Count > 0)
            {
                Node c = nodeStack.Dequeue();
                foreach (Node child in c.GetChildren)
                {
                    if (child.GetType() == typeof(T))
                    {
                        list.Add(child as T);
                    }
                    nodeStack.Enqueue(child);
                }
            }
            return(list.ToArray());
        }