Ejemplo n.º 1
0
        protected override int onUpdate(BehaviourTreeData wData)
        {
            TBTActionPrioritizedSelectorContext thisContext = getContext <TBTActionPrioritizedSelectorContext>(wData);
            int runningState = BehaviourTreeRunningStatus.FINISHED;

            if (thisContext.currentSelectedIndex != thisContext.lastSelectedIndex)
            {
                if (IsIndexValid(thisContext.lastSelectedIndex))
                {
                    BehaviourAction node = GetChild <BehaviourAction>(thisContext.lastSelectedIndex);
                    node.Transition(wData);
                }
                thisContext.lastSelectedIndex = thisContext.currentSelectedIndex;
            }
            if (IsIndexValid(thisContext.lastSelectedIndex))
            {
                BehaviourAction node = GetChild <BehaviourAction>(thisContext.lastSelectedIndex);
                runningState = node.Update(wData);
                if (BehaviourTreeRunningStatus.IsFinished(runningState))
                {
                    thisContext.lastSelectedIndex = -1;
                }
            }
            return(runningState);
        }
Ejemplo n.º 2
0
        protected override void onTransition(BehaviourTreeData wData)
        {
            TBTActionPrioritizedSelectorContext thisContext = getContext <TBTActionPrioritizedSelectorContext>(wData);
            BehaviourAction node = GetChild <BehaviourAction>(thisContext.lastSelectedIndex);

            if (node != null)
            {
                node.Transition(wData);
            }
            thisContext.lastSelectedIndex = -1;
        }
Ejemplo n.º 3
0
        protected override bool onEvaluate(/*in*/ BehaviourTreeData wData)
        {
            TBTActionPrioritizedSelectorContext thisContext = getContext <TBTActionPrioritizedSelectorContext>(wData);

            thisContext.currentSelectedIndex = -1;
            int childCount = GetChildCount();

            for (int i = 0; i < childCount; ++i)
            {
                BehaviourAction node = GetChild <BehaviourAction>(i);
                if (node.Evaluate(wData))
                {
                    thisContext.currentSelectedIndex = i;
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 4
0
 void CheckTreeAvailablility()
 {
     _treeData = null;
     if (Selection.activeObject is BehaviourTreeData)
     {
         _treeData = Selection.activeObject as BehaviourTreeData;
     }
     else if (Selection.activeGameObject != null)
     {
         _tree = Selection.activeGameObject.GetComponent <BehaviourTree>();
         if (_tree != null)
         {
             _treeData        = _tree.treeData;
             _showFrameResult = true;
             UnregisterTreeUpdateCallback();
             _tree.onTreeUpdated += OnTreeUpdated;
         }
     }
     _hasTarget = _treeData != null;
 }
Ejemplo n.º 5
0
        override public void OnInspectorGUI()
        {
            BehaviourTreeData activeTreeData = BTEditor.activeTreeData;

            if (activeTreeData != null && behaviourTree != null && activeTreeData.GetInstanceID() == behaviourTree.GetInstanceID())
            {
                BehaviourNodeEditor node = BTEditor.lastActiveNode;

                if (node != null && node.nodeID < behaviourTree.nodeName.Count)
                {
                    switch (node.type)
                    {
                    case BehaviourNodeType.action:
                        EditorGUILayout.BeginHorizontal();
                        {
                            EditorGUILayout.PrefixLabel("Node ID: " + node.nodeID.ToString());
                        }
                        EditorGUILayout.EndHorizontal();

                        behaviourTree.nodeName[node.nodeID] = EditorGUILayout.TextField("Node Name", behaviourTree.nodeName[node.nodeID], EditorStyles.textField);
                        break;

                    default:
                        DrawDefaultInspector();
                        break;
                    }
                }
                else
                {
                    DrawDefaultInspector();
                }

                if (GUI.changed)
                {
                    EditorUtility.SetDirty(behaviourTree);
                }
            }

            Repaint();
        }
Ejemplo n.º 6
0
        public void OnGUI()
        {
            if (Selection.activeObject != null && Selection.activeObject is BehaviourTreeData)
            {
                if (activeTreeData == null || activeTreeData != Selection.activeObject || editorTree.root == null || lastSelectedObject != Selection.activeObject)
                {
                    activeTreeData = Selection.activeObject as BehaviourTreeData;
                    activeNode     = null;
                    lastActiveNode = null;
                    editorTree.buildTree(activeTreeData);
                }
            }

            lastSelectedObject = Selection.activeObject;

#if true
            //title and asset name
            GUILayout.Label("Behaviour Tree Editor", EditorStyles.boldLabel);

            EditorGUILayout.BeginHorizontal();
            {
                EditorGUILayout.PrefixLabel("Name");

                if (activeTreeData != null)
                {
                    GUILayout.Label(activeTreeData.name, EditorStyles.label);
                }
                else
                {
                    GUILayout.Label("none", EditorStyles.label);
                }
            }
            EditorGUILayout.EndHorizontal();
#endif

            editorTree.OnGUI(new Rect(0, 50, position.width, position.height - 50));

            if (activeNode != null)
            {
                lastActiveNode = activeNode;
            }

            activeNode = editorTree.clickedNode;

#if true
            //context menu
            Event evt = Event.current;

            if (evt.button == 1 && (activeNode != null || evt.rawType == EventType.MouseUp))
            {
                GenericMenu menu = new GenericMenu();

                if (activeNode == null)
                {
                    if (editorTree.root == null)
                    {
                        AddActionItem(menu);
                        menu.AddSeparator("");
                        AddInverterItem(menu);
                        menu.AddSeparator("");
                        AddPrioritySelectorItem(menu);
                        AddSequenceSelectorItem(menu);
                    }
                }
                else
                {
                    switch (activeNode.type)
                    {
                    case BehaviourNodeType.action:
                        AddDeleteNodeItem(menu);
                        break;

                    case BehaviourNodeType.inverter:
                        AddDeleteNodeItem(menu);
                        menu.AddSeparator("");
                        AddActionItem(menu);
                        menu.AddSeparator("");
                        AddPrioritySelectorItem(menu);
                        AddSequenceSelectorItem(menu);
                        break;

                    case BehaviourNodeType.prioritySelector:
                        AddDeleteNodeItem(menu);
                        menu.AddSeparator("");
                        AddActionItem(menu);
                        menu.AddSeparator("");
                        AddInverterItem(menu);
                        menu.AddSeparator("");
                        AddPrioritySelectorItem(menu);
                        AddSequenceSelectorItem(menu);
                        break;

                    case BehaviourNodeType.sequenceSelector:
                        AddDeleteNodeItem(menu);
                        menu.AddSeparator("");
                        AddActionItem(menu);
                        menu.AddSeparator("");
                        AddInverterItem(menu);
                        menu.AddSeparator("");
                        AddPrioritySelectorItem(menu);
                        break;
                    }
                }

                menu.ShowAsContext();
                evt.Use();
            }
#endif

            if (evt.commandName == "UndoRedoPerformed" && activeTreeData != null)
            {
                editorTree.buildTree(activeTreeData);
            }
        }
Ejemplo n.º 7
0
        public void buildTree(BehaviourTreeData treeData)
        {
            nodeList.Clear();

            dim = Vector2.zero;

            if (treeData.nodeDepth != null && treeData.nodeDepth.Count > 0)
            {
                Vector2[] leftRight = new Vector2[treeData.nodeDepth.Count];
                int       x         = 0;

                int oldDepth     = treeData.nodeDepth[treeData.nodeDepth.Count - 1];
                int deepestDepth = 0;

                //set type and y position and work out an x value for each node
                for (int i = treeData.nodeDepth.Count - 1; i >= 0; --i)
                {
                    int depth = treeData.nodeDepth[i];
                    deepestDepth = Mathf.Max(deepestDepth, depth);

                    BehaviourNodeEditor editorNode = new BehaviourNodeEditor();
                    editorNode.type       = treeData.nodeList[i];
                    editorNode.position.x = x;
                    editorNode.position.y = depth * yGap;

                    editorNode.nodeID = i;
                    editorNode.depth  = depth;

                    Vector2 position = leftRight[depth];

                    if (depth < oldDepth)
                    {
                        Vector2 childPos = leftRight[depth + 1];
                        editorNode.position.x = Mathf.Lerp(childPos.x, childPos.y, 0.5f);
                        position.x            = x + 1;
                    }
                    else if (depth > oldDepth)
                    {
                        for (int j = oldDepth + 1; j < depth; ++j)
                        {
                            leftRight[j] = new Vector2(x, x);
                        }

                        position = new Vector2(x, x);
                        --x;
                    }
                    else
                    {
                        position.x = x;
                        --x;
                    }

                    leftRight[depth] = position;

                    nodeList.Insert(0, editorNode);                                                             //as we're going backwards through the behaviour tree, we add nodes to the front of the list

                    oldDepth = depth;
                }

                //calculate x position
                x = Mathf.Abs(x + 1);

                dim.x = x * (xGap + nodeWidth);
                dim.y = deepestDepth * yGap + nodeHeight;

                for (int i = 0; i < nodeList.Count; ++i)
                {
                    BehaviourNodeEditor editorNode = nodeList[i];
                    editorNode.position.x += x;
                    editorNode.position.x *= nodeWidth + xGap;
                    nodeList[i]            = editorNode;
                }
            }

            root = null;

            if (nodeList.Count > 0)
            {
                root = nodeList[0];
            }
        }