private static void InitDict(string path)
        {
            _rootBehavior = null;
            _behaviorDict.Clear();
            _connectionDict.Clear();
            TextAsset json           = Resources.Load <TextAsset>(path);
            string    content        = json.text.Replace("\r", "").Replace("\n", "");
            Hashtable table          = MiniJsonExtensions.hashtableFromJson(content);
            ArrayList nodeList       = table["nodes"] as ArrayList;
            ArrayList connectionList = table["connections"] as ArrayList;

            for (int i = 0; i < nodeList.Count; i++)
            {
                Hashtable nodeTable = nodeList[i] as Hashtable;
                var       id        = 0;
                if (int.TryParse(nodeTable["$id"].ToString(), out id))
                {
                    AbsBehavior absBehavior = CreateBehavior(nodeTable, id);
                    _behaviorDict[id] = absBehavior;
                    if (_rootBehavior == null)
                    {
                        _rootBehavior = absBehavior;
                    }
                    else
                    {
                        if (absBehavior.Id < _rootBehavior.Id)
                        {
                            _rootBehavior = absBehavior;
                        }
                    }
                }
                else
                {
                    LogHelper.PrintError("[BehaviorTreeFactory]try get node id error!");
                }
            }
            for (int i = 0; i < connectionList.Count; i++)
            {
                Hashtable connectionTable = connectionList[i] as Hashtable;
                int       source          = 0;
                int       target          = 0;
                Hashtable scurceNode      = connectionTable["_sourceNode"] as Hashtable;
                Hashtable targetNode      = connectionTable["_targetNode"] as Hashtable;
                if (int.TryParse(scurceNode["$ref"].ToString(), out source) &&
                    int.TryParse(targetNode["$ref"].ToString(), out target))
                {
                    List <int> list;
                    if (!_connectionDict.TryGetValue(source, out list))
                    {
                        _connectionDict[source] = new List <int>();
                        list = _connectionDict[source];
                    }
                    list.Add(target);
                }
                else
                {
                    LogHelper.PrintError("[BehaviorTreeFactory]try get source id and target id error!");
                }
            }
        }
예제 #2
0
        protected override void UpdateEx()
        {
            if (Reslut == BehaviorState.Running)
            {
                int count = _list.Count;
                if (_target == null)
                {
                    int index = Random.Range(0, count);
                    _target = _list[index];
                }
                switch (_target.Behave(Entity))
                {
                case BehaviorState.Running:
                case BehaviorState.Finish:
                    Reslut = BehaviorState.Running;
                    break;

                case BehaviorState.Success:
                    Reslut = BehaviorState.Finish;
                    break;

                case BehaviorState.Failure:
                case BehaviorState.Reset:
                    Reslut = BehaviorState.Failure;
                    LogUtil.LogUtility.PrintError("[BTRandom]BTRandom execute failure!");
                    break;

                default:
                    Reslut = BehaviorState.Failure;
                    LogUtil.LogUtility.PrintError("[BTRandom]BTRandom execute failure!");
                    break;
                }
            }
        }
예제 #3
0
        private static AbsBehavior CreateBehavior(string type)
        {
            AbsBehavior behavior = null;

            string[] str = type.Split(".".ToCharArray());
            if (str.Length < 3)
            {
                LogUtil.LogUtility.PrintError(string.Format("[BehaviorTreeFactory]create Behavior error,type is {0}", type));
                return(behavior);
            }
            switch (str[2])
            {
            case "BTLog":
                break;

            case "BTSequence":
                break;

            case "BTRandom":
                break;

            case "BTSelector":
                break;

            default:
                break;
            }
            return(behavior);
        }
예제 #4
0
 protected override void Reset()
 {
     //处理子节点;
     for (int i = 0; i < _list.Count; i++)
     {
         _list[i].ResetBehavior();
     }
     //处理自己;
     _target = null;
 }
        private static AbsBehavior CreateBehavior(Hashtable table, int id)
        {
            AbsBehavior behavior = null;
            var         type     = table["$type"].ToString();
            var         str      = type.Split(".".ToCharArray());

            if (3 == str.Length)
            {
                var name   = "Framework." + str[2];
                var target = Type.GetType(name);
                behavior = Activator.CreateInstance(target, table) as AbsBehavior;
            }
            if (behavior != null)
            {
                behavior.Id = id;
            }
            return(behavior);
        }
        public static BehaviorTree CreateBehaviorTree(AbsEntity entity, string path)
        {
            InitDict(path);
            if (_rootBehavior == null)
            {
                LogHelper.PrintError("[BehaviorTreeFactory]Root Behavior is null!");
                return(null);
            }
            GenerateConnect(new List <AbsBehavior>()
            {
                _rootBehavior
            });
            var tree = new BehaviorTree(_rootBehavior, entity);

            _rootBehavior = null;
            _behaviorDict.Clear();
            _connectionDict.Clear();
            return(tree);
        }
예제 #7
0
        private static AbsBehavior CreateBehavior(Hashtable table, int id)
        {
            AbsBehavior behavior = null;
            string      type     = table["$type"].ToString();

            switch (type)
            {
            //顺序执行节点;
            case "NodeCanvas.BehaviourTrees.BTSequence":
                behavior = new BTSequence(table);
                break;

            //随机执行节点;
            case "NodeCanvas.BehaviourTrees.BTRandom":
                behavior = new BTRandom(table);
                break;

            //选择执行节点;
            case "NodeCanvas.BehaviourTrees.BTSelector":
                behavior = new BTSelector(table);
                break;

            //等级条件执行节点;
            case "NodeCanvas.BehaviourTrees.BTLevel":
                behavior = new BTLevel(table);
                break;

            //日志执行节点;
            case "NodeCanvas.BehaviourTrees.BTLog":
                behavior = new BTLog(table);
                break;

            default:
                break;
            }
            if (behavior != null)
            {
                behavior.Id = id;
            }
            return(behavior);
        }
예제 #8
0
 public void Serialize(AbsBehavior node)
 {
     _nextNode = node;
 }
예제 #9
0
 public AbsDecorator(Hashtable table) : base(table)
 {
     _nextNode = null;
 }
예제 #10
0
 public BehaviorTree(AbsBehavior root, AbsEntity entity)
 {
     Root   = root;
     Entity = entity;
     Enable = false;
 }
예제 #11
0
 public BehaviorTree(AbsBehavior root, BaseEntity entity)
 {
     _root   = root;
     _entity = entity;
     _enable = false;
 }