Esempio n. 1
0
        public BehaviorTree BuildBehavior3TreeFromConfig(string path)
        {
            Behavior3TreeCfg cfg = LoadBehavior3TreeCfg(path);
            var tree             = new BehaviorTree();

            tree.Initialize();
            tree.Load(cfg);
            return(tree);
        }
Esempio n. 2
0
        public Behavior3TreeCfg LoadBehavior3TreeCfg(string path)
        {
            var res = new Behavior3TreeCfg();

            using (StreamReader reader = new StreamReader(path))
            {
                string json = reader.ReadToEnd();
                JsonMapper.ToObject <Behavior3TreeCfg>(json);
            }
            return(res);
        }
Esempio n. 3
0
        /**
         * This method loads a Behavior Tree from a data structure, populating this
         * object with the provided data. Notice that, the data structure must
         * follow the format specified by Behavior3JS. Consult the guide to know
         * more about this format.
         *
         * You probably want to use custom nodes in your BTs, thus, you need to
         * provide the `names` object, in which this method can find the nodes by
         * `names[NODE_NAME]`. This variable can be a namespace or a dictionary,
         * as long as this method can find the node by its name, for example:
         *
         *     //json
         *     ...
         *     'node1': {
         *       'name': MyCustomNode,
         *       'title': ...
         *     }
         *     ...
         *
         *     //code
         *     var bt = new b3.BehaviorTree();
         *     bt.load(data, {'MyCustomNode':MyCustomNode})
         *
         *
         * @method load
         * @param {Object} data The data structure representing a Behavior Tree.
         * @param {Object} [names] A namespace or dict containing custom nodes.
         **/

        public void Load(Behavior3TreeCfg data)
        {
            this.title       = data.title;
            this.description = data.description;
            this.properties  = data.properties;
            this.dumpinfo    = data;

            Dictionary <string, BaseNode> nodes = new Dictionary <string, BaseNode>();
            Behavior3NodeCfg spec;
            BaseNode         node;

            foreach (KeyValuePair <string, Behavior3NodeCfg> kv in data.nodes)
            {
                spec = kv.Value;
                node = Behavior3Factory.singleton.CreateBehavior3Instance(spec.name);
                if (node == null)
                {
                    throw new Exception("BehaviorTree.load: Invalid node name:" + spec.name);
                }
                node.Initialize(spec);
                nodes[spec.id] = node;
            }

            foreach (KeyValuePair <string, Behavior3NodeCfg> kv in data.nodes)
            {
                spec = kv.Value;
                node = nodes[spec.id];
                var category = node.GetCategory();
                if (category == Constants.COMPOSITE && spec.children.Count > 0)
                {
                    for (int i = 0; i < spec.children.Count; i++)
                    {
                        Composite comp = (Composite)node;
                        comp.AddChild(nodes[spec.children[i]]);
                    }
                }
                else if (category == Constants.DECORATOR && spec.child.Length > 0)
                {
                    Decorator dec = (Decorator)node;
                    dec.SetChild(nodes[spec.child]);
                }
            }

            this.root = nodes[data.root];
        }