Exemplo n.º 1
0
    public void Step()
    {
        for (int i = 0; i < mBTTrees.Count; i++)
        {
            BTTree    tree    = mBTTrees[i];
            EBTStatus pStatus = tree.Step();

            switch (pStatus)
            {
            case EBTStatus.BT_INITIAL:
                break;

            case EBTStatus.BT_RUNNING:
                break;

            case EBTStatus.BT_SUCCESS:
            case EBTStatus.BT_FAILURE:
            {
                mDeleteList.Add(tree);
            }
            break;
            }
        }

        for (int i = 0; i < mDeleteList.Count; i++)
        {
            BTTree tree = mDeleteList[i];
            mBTTrees.Remove(tree);
            tree.Clear();
        }
        mDeleteList.Clear();
    }
    // Use this for initialization
    void Awake()
    {
        //create a tree with just the attack node
        senses = GetComponentInChildren <AIPerceptor>();
        tree   = new BTTree
                 (
            new BTSelector(new List <BTTask>()
        {
            new BTSequence(new List <BTTask>()
            {
                new CheckIfPlayerUnderAttack(),
                new RLFightOrFlight()
            }),
            new FollowPlayerSequence(),
            new Idle()
        }
                           ));

        //setup the blackboard values
        tree.Blackboard = new Blackboard();
        senses          = GetComponentInChildren <AIPerceptor>();
        senses.SetBlackboard(tree.Blackboard);
        tree.Blackboard.SetValue("owner", this.gameObject);
        tree.Blackboard.SetValue("senses", senses);
        if (player)
        {
            tree.Blackboard.SetValue("player", player);
        }
        tree.BeginTree();
    }
Exemplo n.º 3
0
        public void Step()
        {
            for (int i = 0; i < m_BTTrees.Count; i++)
            {
                BTTree   tree    = m_BTTrees[i];
                BTStatus pStatus = tree.Step();

                switch (pStatus)
                {
                case BTStatus.Initial:
                    break;

                case BTStatus.Running:
                    break;

                case BTStatus.Success:
                case BTStatus.Failure:
                {
                    m_DeleteList.Add(tree);
                }
                break;
                }
            }

            for (int i = 0; i < m_DeleteList.Count; i++)
            {
                BTTree tree = m_DeleteList[i];
                m_BTTrees.Remove(tree);
                tree.Clear();
            }
            m_DeleteList.Clear();
        }
Exemplo n.º 4
0
    // Use this for initialization
    void Awake()
    {
        tree.Blackboard = new Blackboard();
        senses          = GetComponentInChildren <AIPerceptor>();

        //create the behaviour tree
        tree = new BTTree(
            new BTSelector(new List <BTTask>()
        {
            new Timer(
                new BTSequence(
                    new List <BTTask>()
            {
                new MoveTo(),
                new SelectNextLocation()
            }),
                5f),

            new RLAttack(
                new List <BTTask>()
            {
                new FireballAttack(),
                new WaterAttack(),
                new AirAttack(),
                new EarthAttack()
            })
        }));

        tree.Blackboard.SetValue("owner", this.gameObject);
        tree.Blackboard.SetValue("patrolPoints", GetComponent <Patrol>().GetPatrolPoints());
        tree.Blackboard.SetValue("moveToPos", GetComponent <Patrol>().GetPatrolPoints()[0].transform.position);
        tree.Blackboard.SetValue("distanceToArrival", 0.5f);
        tree.Blackboard.SetValue("senses", senses);
        tree.BeginTree();
    }
Exemplo n.º 5
0
 public override void onCreate(EntityInfo data)
 {
     base.onCreate(data);
     fsm = new MonsterFSM(this);
     onChangeState(StateType.spawn);
     treeAI = new MonsterBTAI();
     dt     = WorkingDataFactroy.createData(this);
 }
Exemplo n.º 6
0
 public void Remove(BTTree tree)
 {
     if (tree == null)
     {
         return;
     }
     m_DeleteList.Add(tree);
 }
Exemplo n.º 7
0
// Use this for initialization

    void Start()
    {
        database.characterManager = GetComponent <CharacterManager>();

        bt = BTTree.CloneTree(bt);

        bt.Initialize(database);
    }
Exemplo n.º 8
0
        public void ClearData(BTNode pNode, string key)
        {
            BTTree result = GetFirstTreeInParent(pNode);

            if (result != null)
            {
                result.SetData(key, null);
            }
        }
Exemplo n.º 9
0
        private string CommandCreateBTTreeForManager()
        {
            BTTree btTree = new BTTree();

            btTree.Root = CreateBTTree();
            Mgr <CatProject> .Singleton.BTTreeManager.AddBTTree("TestTree", btTree);

            return("BTTree has been created and inserted into BTTreeManager");
        }
Exemplo n.º 10
0
 public void Run(BTTree tree)
 {
     if (tree == null)
     {
         return;
     }
     tree.Clear();
     m_BTTrees.Add(tree);
 }
Exemplo n.º 11
0
// Use this for initialization

void Start () {

    database.characterManager = GetComponent<CharacterManager>();

    bt = BTTree.CloneTree (bt);

    bt.Initialize (database);

}
Exemplo n.º 12
0
        public object Execute()
        {
            BTTree btTree = new BTTree();

            btTree.Root = CreateBTTree();
            Mgr <CatProject> .Singleton.BTTreeManager.AddBTTree(m_btTreeName, btTree);

            return("BTTree has been created and inserted into BTTreeManager");
        }
Exemplo n.º 13
0
 public void Clear()
 {
     for (int i = 0; i < m_BTTrees.Count; i++)
     {
         BTTree tree = m_BTTrees[i];
         tree.Clear();
     }
     m_BTTrees.Clear();
 }
Exemplo n.º 14
0
        public void SaveData(BTNode pNode, string key, object value)
        {
            BTTree result = GetFirstTreeInParent(pNode);

            if (result != null)
            {
                result.SetData(key, value);
            }
        }
Exemplo n.º 15
0
        public ActorBase GetOwnerByNode(BTNode pNode)
        {
            BTTree tree = GetFirstTreeInParent(pNode);

            if (tree == null)
            {
                return(null);
            }
            return(tree.Owner);
        }
Exemplo n.º 16
0
        public object GetData(BTNode pNode, string key)
        {
            BTTree result = GetFirstTreeInParent(pNode);

            if (result != null)
            {
                return(result.GetData(key));
            }
            return(null);
        }
Exemplo n.º 17
0
        public object Execute()
        {
            BTTree btTree = Mgr <CatProject> .Singleton.BTTreeManager.LoadBTTree(m_btTreeName);

            if (btTree != null)
            {
                Mgr <MapEditor> .Singleton.m_btTreeEditor.OpenBTTree(btTree);
            }
            return("BTTree loaded and opened in viewer");
        }
Exemplo n.º 18
0
 /**
  * @brief set BTTree
  *   Warning: if it is observing a runtimepack, this action will stop
  *      the observation
  **/
 public void SetBTTree(BTTree _btree)
 {
     if (IsObservingRuntimePack())
     {
         m_observingRuntimePack = null;
     }
     m_btTree = _btree;
     CreateChart();
     AutoLayoutChart();
     Refresh();
 }
Exemplo n.º 19
0
        public BTTree GetFirstTreeInParent(BTNode pNode)
        {
            BTTree result = null;

            GetTreeInParents(pNode, ref result);
            if (result == null)
            {
                Debug.LogError("找不到BTree类型的Parent:" + pNode);
                return(null);
            }
            return(result);
        }
Exemplo n.º 20
0
        public object Execute()
        {
            BTTree btTree = Mgr <CatProject> .Singleton.BTTreeManager.LoadBTTree(m_btTreeName);

            if (btTree != null)
            {
                return("The btree has been loaded.");
            }
            else
            {
                return("Fail to load btree: " + m_btTreeName);
            }
        }
Exemplo n.º 21
0
        public void OnEditBTNodeClick(object sender, EventArgs e)
        {
            if (Mgr <CatProject> .Singleton != null && Mgr <CatProject> .Singleton.BTTreeManager != null)
            {
                string selected = (string)(contentList.SelectedItem);
                if (selected != null)
                {
                    BTTree btTree = Mgr <CatProject> .Singleton.BTTreeManager.LoadBTTree(selected);

                    Mgr <MapEditor> .Singleton.BTTreeEditor.OpenBTTree(btTree);

                    Mgr <MapEditor> .Singleton.BTTreeEditor.ShowDialog(this);
                }
            }
        }
Exemplo n.º 22
0
 public void GetTreeInParents(BTNode pNode, ref BTTree result)
 {
     if (pNode == null)
     {
         return;
     }
     if (pNode.Parent is BTTree)
     {
         result = pNode.Parent as BTTree;
     }
     else
     {
         GetTreeInParents(pNode.Parent, ref result);
     }
 }
Exemplo n.º 23
0
        private void LoadBTTree(string _btTreeName)
        {
            if (Mgr <CatProject> .Singleton != null && Mgr <CatProject> .Singleton.BTTreeManager != null)
            {
                BTTree btTree = Mgr <CatProject> .Singleton.BTTreeManager.LoadBTTree(_btTreeName);

                if (btTree != null)
                {
                    OpenBTTree(btTree);
                }
                else
                {
                    Debug.Assert(false, "Cannot open BTTree: " + _btTreeName);
                }
            }
        }
Exemplo n.º 24
0
    void OnGUI()
    {
        _go = (GameObject)EditorGUI.ObjectField(new Rect(position.xMax - 350, position.yMin - 40, 300, 20), "BTTree GameObject", _go, typeof(GameObject), true);

        BTTree tree;

        if (_go == null || (tree = _go.GetComponent <BTTree>()) == null)
        {
            EditorGUI.LabelField(new Rect(position.xMax - 350, position.y - 17, 500, 20), "Find a GameObject with component inherited from BTTree.");
            return;
        }

        if (_previousTree == null || _previousTree.GetType() != tree.GetType())
        {
            BTNode root = tree.root == null?tree.Init() : tree.root;

            _levelToCount = new Dictionary <int, int>();
            _info         = ParseNodeInfo(root, 0, 0);

            _previousTree = tree;
        }

        int maxCount = 0;

        foreach (int count in _levelToCount.Values)
        {
            if (count > maxCount)
            {
                maxCount = count;
            }
        }

        _scrollPosition = GUI.BeginScrollView(new Rect(0, 0, position.width - 1, position.height - 1),
                                              _scrollPosition,
                                              new Rect(0, 0, maxCount * _offset.x + 50, _levelToCount.Keys.Count * _offset.y + 50));

        BeginWindows();

        _currentWindowId = 0;
        DrawNodeInfo(_info, null);

        EndWindows();

        GUI.EndScrollView();
    }
Exemplo n.º 25
0
        private void CreateNewBTTree()
        {
            string      treeName    = "UntitledBTTree";
            InputDialog inputDialog = new InputDialog();

            inputDialog.InitializeDate("Input a name for BTTree", "Input a name for BTTree", treeName);
            if (inputDialog.ShowDialog(this) == DialogResult.OK)
            {
                treeName = inputDialog.Result;
                if (Mgr <CatProject> .Singleton != null && Mgr <CatProject> .Singleton.BTTreeManager != null)
                {
                    while (Mgr <CatProject> .Singleton.BTTreeManager.HasBTTreeWithName(treeName))
                    {
                        inputDialog = new InputDialog();
                        inputDialog.InitializeDate("Tree " + treeName + "exists", "Input a new name for BTTree", treeName);
                        if (inputDialog.ShowDialog(this) == DialogResult.OK)
                        {
                            treeName = inputDialog.Result;
                        }
                        else
                        {
                            return;
                        }
                    }
                    BTTree btTree = Mgr <CatProject> .Singleton.BTTreeManager.CreateAndSaveEmptyBTTree(treeName);

                    OpenBTTree(btTree);
                }
                else
                {
                    Debug.Assert(false, "Cannot find CatProject instance.");
                    return;
                }
            }
            else
            {
                return;
            }
        }
Exemplo n.º 26
0
    // Start is called before the first frame update
    void Awake()
    {
        //singleton
        if (instance == null)
        {
            instance = this;
        }
        else if (instance != this)
        {
            Destroy(gameObject);
        }

        DontDestroyOnLoad(gameObject);

        rootSelector = new Selector();
        Sequence seq = new Sequence();

        rootSelector.AddChild(new Obj());
        rootSelector.AddChild(new Npc());
        rootSelector.AddChild(new Destination());


        rootSelector.AddChild(seq);
    }
Exemplo n.º 27
0
    void OnGUI()
    {
        _go = (GameObject) EditorGUI.ObjectField(new Rect(position.xMax - 350, position.yMin-40, 300, 20), "BTTree GameObject", _go, typeof(GameObject), true);

        BTTree tree;
        if (_go == null || (tree = _go.GetComponent<BTTree>()) == null) {
            EditorGUI.LabelField(new Rect(position.xMax - 350, position.y-17, 500, 20), "Find a GameObject with component inherited from BTTree.");
            return ;
        }

        if (_previousTree == null || _previousTree.GetType() != tree.GetType()) {
            BTNode root = tree.root == null ? tree.Init() : tree.root;

            _levelToCount = new Dictionary<int, int>();
            _info = ParseNodeInfo(root, 0, 0);

            _previousTree = tree;
        }

        int maxCount = 0;
        foreach (int count in _levelToCount.Values) {
            if (count > maxCount) {
                maxCount = count;
            }
        }

        _scrollPosition = GUI.BeginScrollView(new Rect(0, 0, position.width - 1, position.height - 1),
                                              _scrollPosition,
                                              new Rect(0, 0, maxCount * _offset.x + 50, _levelToCount.Keys.Count * _offset.y + 50));

        BeginWindows();

        _currentWindowId = 0;
        DrawNodeInfo(_info, null);

        EndWindows();

        GUI.EndScrollView();
    }
Exemplo n.º 28
0
 void OnEnable()
 {
     _previousTree = null;
 }
Exemplo n.º 29
0
 void OnEnable()
 {
     _previousTree = null;
 }
    void OnGUI()
    {
        //draw behaviour tree
        scrollPosition = GUI.BeginScrollView(new Rect(0, 0, position.width - 240, position.height), scrollPosition, new Rect(0, 0, maxSize.x, maxSize.y));
        Texture2D text1 = new Texture2D(1, 1);

        text1.SetPixel(0, 0, Color.white);
        text1.Apply();
        //Texture2D text2 = new Texture2D( 1 , 1 );
        //text2.SetPixel( 0 , 0 , Color.gray );
        //text2.Apply();

        for (int i = 0; i < 1000; ++i)
        {
            GUI.DrawTexture(new Rect(0, i * NODE_HEIGH, 2 * _Instance.position.width, NODE_HEIGH), text1);
            //++i;
            //GUI.DrawTexture( new Rect( 0 , i * NODE_HEIGH , 2 * _Instance.position.width , NODE_HEIGH ) , text2 );
        }

        if (currentTree != null && currentTree.treeRoot != null)
        {
            int xx = 0, yy = 0;
            currentTree.treeRoot.RenderNode(xx, ref yy);
        }
        GUI.EndScrollView();

        //draw editor
        GUI.BeginGroup(new Rect(position.width - GUI_WIDTH, 0, 300, 1000));
        int           x = 20, y = 20;
        List <BTTree> treeList = BehaviourTreeFactory._Instance.GetAllTrees();

        if (GUI.Button(new Rect(x, y, 200, 40), "Load Json"))
        {
            selectedNode     = null;
            currentNode      = null;
            currentTree      = null;
            currentTreeIndex = -1;
            lastTreeIndex    = -1;
            creatNodeId      = -1;

            BehaviourTreeFactory._Instance.EditorLoad();
        }
        y += 60;
        if (GUI.Button(new Rect(x, y, 200, 40), "Save Json"))
        {
            BehaviourTreeFactory._Instance.EditorSave();
            AssetDatabase.Refresh();
        }
        y += 40;
        GUI.Label(new Rect(x, y, 200, 20), "==========================");
        y        += 20;
        inputName = GUI.TextField(new Rect(x, y + 10, 200, 20), inputName);
        y        += 40;
        if (GUI.Button(new Rect(x, y, 200, 40), "creat tree"))
        {
            if (inputName != "")
            {
                currentNode = null;
                BTTree newTree = new BTTree();
                newTree.treeName = inputName;
                BehaviourTreeFactory._Instance.AddTree(newTree);
                currentTree = newTree;
                tickRateStr = "";
                treeList    = BehaviourTreeFactory._Instance.GetAllTrees();
                for (int i = 0; i < treeList.Count; ++i)
                {
                    if (treeList[i].treeName == newTree.treeName)
                    {
                        currentTreeIndex = i;
                        break;
                    }
                }
                lastTreeIndex = currentTreeIndex;
                Repaint();
            }
        }

        if (treeList.Count != 0)
        {
            y += 40;
            GUI.Label(new Rect(x, y, 200, 20), "==========================");

            y += 20;
            string[] treeNames = new string[treeList.Count];
            for (int i = 0; i < treeNames.Length; ++i)
            {
                treeNames[i] = treeList[i].treeName;
            }
            currentTreeIndex = EditorGUI.Popup(new Rect(x, y, 200, 45), currentTreeIndex, treeNames);
            if (currentTreeIndex != lastTreeIndex)
            {
                lastTreeIndex = currentTreeIndex;
                currentTree   = treeList[currentTreeIndex];
                tickRateStr   = "";
                currentNode   = null;
            }
            y += 40;
            if (GUI.Button(new Rect(x, y, 200, 40), "remove tree"))
            {
                currentNode = null;
                BehaviourTreeFactory._Instance.RemoveTree(currentTree);
                currentTree      = null;
                currentTreeIndex = -1;
                lastTreeIndex    = -1;
                treeList         = BehaviourTreeFactory._Instance.GetAllTrees();
                Repaint();
            }
        }
        if (currentTree != null)
        {
            y += 40;
            GUI.Label(new Rect(x, y, 200, 20), "==========================");
            y += 20;
            GUI.Label(new Rect(x, y, 100, 20), "Tree name : ");
            currentTree.treeName = GUI.TextField(new Rect(x + 100, y, 100, 20), currentTree.treeName);
            y += 20;

            GUI.Label(new Rect(x, y, 100, 20), "Tree Tick Rate : ");
            if (tickRateStr == "")
            {
                tickRateStr = currentTree.tickRate.ToString();
            }
            tickRateStr          = GUI.TextField(new Rect(x + 100, y, 100, 20), tickRateStr);
            currentTree.tickRate = double.Parse(tickRateStr);

            if (currentTree.treeRoot == null)
            {
                creatNodeId = EditorGUI.Popup(new Rect(x, y + 20, 200, 40), creatNodeId, BehaviourTreeFactory._Instance.GetCompostieNodeNamesList());
                y          += 40;
                if (creatNodeId != -1)
                {
                    if (GUI.Button(new Rect(x, y, 200, 40), "Create Root"))
                    {
                        BTNode newNode = BehaviourTreeFactory._Instance.CreateRoot(creatNodeId);
                        currentTree.treeRoot = newNode;
                        currentNode          = newNode;
                        y += 40;
                    }
                }
            }
        }
        if (currentNode != null)
        {
            y += 40;
            GUI.Label(new Rect(x, y, 200, 20), "==========================");
            y += 20;
            GUI.Label(new Rect(x, y, 300, 20), "Node Type : " + currentNode.GetType().FullName);
            y += 20;
            GUI.Label(new Rect(x, y, 200, 20), "Node Name : " + currentNode.GetType().Name);
            y += 20;

            GUI.Label(new Rect(x, y, 100, 20), "Node Description : ");
            y += 20;
            string tempNodeDestricption = currentNode.GetNodeDestricption();
            tempNodeDestricption = GUI.TextField(new Rect(x, y, 200, 40), tempNodeDestricption);
            currentNode.SetNodeDestricption(tempNodeDestricption);
            y += 40;

            GUI.Label(new Rect(x, y, 100, 20), "Reverse Result : ");
            bool tempReverseResult = currentNode.IsReverseResult();
            tempReverseResult = GUI.Toggle(new Rect(x + 100, y, 100, 20), tempReverseResult, "");
            currentNode.SetReverseResult(tempReverseResult);

            y += 15;
            currentNode.RenderSelfProperty(x, y);
        }
        GUI.EndGroup();
    }
Exemplo n.º 31
0
 /**
  * @brief open BTTree
  **/
 public void OpenBTTree(BTTree _btTree)
 {
     InitializeAll();
     btTreeViewer.SetBTTree(_btTree);
 }