protected override void UpdateEx(float interval)
        {
            if (Reslut == BehaviorState.Running)
            {
                var count = _list.Count;
                if (_target == null)
                {
                    var index = Random.Range(0, count);
                    _target = _list[index];
                }
                switch (_target.Behave(Entity, interval))
                {
                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;
                    LogHelper.PrintError("[BTRandom]BTRandom execute failure!");
                    break;

                default:
                    Reslut = BehaviorState.Failure;
                    LogHelper.PrintError("[BTRandom]BTRandom execute failure!");
                    break;
                }
            }
        }
Exemple #2
0
        private static void InitDict(string path)
        {
            _rootBehavior = null;
            _behaviorDict.Clear();
            _connectionDict.Clear();
            var json           = Resources.Load <TextAsset>(path);
            var content        = json.text.Replace("\r", "").Replace("\n", "");
            var table          = MiniJsonExtensions.hashtableFromJson(content);
            var nodeList       = table["nodes"] as ArrayList;
            var connectionList = table["connections"] as ArrayList;

            for (var i = 0; i < nodeList.Count; i++)
            {
                var nodeTable = nodeList[i] as Hashtable;
                if (int.TryParse(nodeTable["$id"].ToString(), out var id))
                {
                    var 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 (var i = 0; i < connectionList.Count; i++)
            {
                var connectionTable = connectionList[i] as Hashtable;
                var scurceNode      = connectionTable["_sourceNode"] as Hashtable;
                var targetNode      = connectionTable["_targetNode"] as Hashtable;
                if (int.TryParse(scurceNode["$ref"].ToString(), out var source) &&
                    int.TryParse(targetNode["$ref"].ToString(), out var target))
                {
                    if (!_connectionDict.TryGetValue(source, out var list))
                    {
                        _connectionDict[source] = new List <int>();
                        list = _connectionDict[source];
                    }
                    list.Add(target);
                }
                else
                {
                    LogHelper.PrintError("[BehaviorTreeFactory]try get source id and target id error!");
                }
            }
        }
 protected override void Reset()
 {
     //处理子节点;
     for (var i = 0; i < _list.Count; i++)
     {
         _list[i].ResetBehavior();
     }
     //处理自己;
     _target = null;
 }
Exemple #4
0
        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);
        }
Exemple #5
0
        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);
        }
Exemple #6
0
 public void Serialize(AbsBehavior node)
 {
     NextNode = node;
 }
Exemple #7
0
 public AbsDecorator(Hashtable table) : base(table)
 {
     NextNode = null;
 }
Exemple #8
0
 public BehaviorTree(AbsBehavior root, AbsEntity entity)
 {
     Root   = root;
     Entity = entity;
     Enable = false;
 }