public BehaviourTreeInstance(BehaviourTreeNode rootNode, Actor actor, int numberOfLoops)
 {
     this.rootNode      = rootNode;
     this.currentNode   = rootNode;
     this.Actor         = actor;
     this.numberOfLoops = numberOfLoops;
 }
    private BehaviourTree.Status SequenceTick()
    {
        foreach (BehaviourTreeNode child in children)
        {
            if (this.startFromFirstNodeEachTick || this.currentTickedNode == null || this.currentTickedNode == child)
            {
                BehaviourTree.Status childStatus = child.Tick();

                if (childStatus == BehaviourTree.Status.RUNNING)
                {
                    this.currentTickedNode = child;
                    return(childStatus);
                }

                if (childStatus == BehaviourTree.Status.FAILURE)
                {
                    this.currentTickedNode = null;
                    return(childStatus);
                }
            }
        }

        this.currentTickedNode = null;

        return(BehaviourTree.Status.SUCCESS);
    }
示例#3
0
    void RootWindowFunction(int windowID)
    {
        Event current = Event.current;

        if (current.type == EventType.MouseDown && current.button == 1)
        {
            this.selectedNode = FindNodeByID(BehaviourTreeEditorWindow.behaviourTree, windowID);

            GenericMenu menu = new GenericMenu();

            AddNewChildOption(menu, BehaviourTreeEditorWindow.behaviourTree.child == null);
            menu.ShowAsContext();

            current.Use();
        }
        else if (current.type == EventType.MouseDown && current.button == 0)
        {
            this.selectedNode = FindNodeByID(BehaviourTreeEditorWindow.behaviourTree, windowID);

            if (Selection.activeObject != BehaviourTreeEditorWindow.behaviourTree)
            {
                SelectNodeButDontChangeProjectView();
            }

            current.Use();
        }
    }
示例#4
0
 public override void AddChild(BehaviourTreeNode child)
 {
     if (this.child == null)
     {
         this.child = child;
     }
 }
示例#5
0
    void SetColor(BehaviourTreeNode node)
    {
        GUI.color = colorDefault;

        if (node is BehaviourTreeControlNode)
        {
            BehaviourTreeControlNode controlNode = (BehaviourTreeControlNode)node;

            switch (controlNode.type)
            {
            case BehaviourTreeControlNode.Type.SELECTOR: GUI.color = colorControlSelectorNode; break;

            case BehaviourTreeControlNode.Type.SEQUENCE: GUI.color = colorControlSequenceNode; break;

            case BehaviourTreeControlNode.Type.PARALLEL: GUI.color = colorControlParallelNode; break;

            default: GUI.color = colorDefault; break;
            }
        }
        else if (node is BehaviourTreeExecutionNode)
        {
            GUI.color = colorExecutionNode;
        }
        else if (node is BehaviourTreeDecoratorNode)
        {
            GUI.color = colorDecoratorNode;
        }
        else if (node is BehaviourTreeSubTreeNode)
        {
            GUI.color = colorBehaviourTreeNode;
        }
    }
示例#6
0
        public ExecutionResult Execute(BehaviourTreeInstance behaviourTreeInstance)
        {
            var state = behaviourTreeInstance.NodeAndState[this];

            if (state == BehaviourTreeInstance.NodeState.STATE_EXECUTING)
            {
                return(new ExecutionResult(true));
            }

            if (state == BehaviourTreeInstance.NodeState.STATE_COMPUTE_RESULT)
            {
                BehaviourTreeNode picked = ChooseByRandom(actionArrayAndLikelihood);

                behaviourTreeInstance.NodeAndState[this]   = BehaviourTreeInstance.NodeState.STATE_WAITING;
                behaviourTreeInstance.NodeAndState[picked] = BehaviourTreeInstance.NodeState.STATE_TO_BE_STARTED;

                foreach (var item in actionArrayAndLikelihood)
                {
                    if (item.Key != picked)
                    {
                        behaviourTreeInstance.NodeAndState[item.Key] = BehaviourTreeInstance.NodeState.STATE_DISCARDED;
                    }
                }
            }
            return(new ExecutionResult(true));
        }
示例#7
0
    void DrawChildrenRecursively(BehaviourTreeNode node)
    {
        List <BehaviourTreeNode> children = node.GetChildren();

        for (int i = 0; i < node.ChildrenCount(); i++)
        {
            CreateNodeRect(children[i], node);

            Handles.BeginGUI();

            Vector2 begin = new Vector2(node.rect.center.x, node.rect.yMax);
            Vector2 end   = new Vector2(children[i].rect.center.x, children[i].rect.yMin + 2.0f);

            float xAverage = (node.rect.center.x + children[i].rect.center.x) / 2.0f;

            Vector2 beginTangente = new Vector2(xAverage, children[i].rect.yMin);
            Vector2 endTangente   = new Vector2(xAverage, node.rect.yMax);

            Handles.DrawBezier(begin, end, beginTangente, endTangente, colorLine, null, 5f);

            Handles.EndGUI();
        }

        for (int i = 0; i < node.ChildrenCount(); i++)
        {
            DrawChildrenRecursively(children[i]);
        }
    }
示例#8
0
 public SelectorNode(Func <BehaviourTreeInstance, ExecutionResult> conditionFunction,
                     BehaviourTreeNode actionIfTrue, BehaviourTreeNode actionIfFalse)
 {
     this.conditionFunction = conditionFunction;
     this.actionIfTrue      = actionIfTrue;
     this.actionIfFalse     = actionIfFalse;
 }
        public ExecutionResult ExecuteBehaviourTree()
        {
            if (Completed)
            {
                return(new ExecutionResult(true));
            }

            //find current node to be executed, or a running one, or root to launch, or root completed
            currentNode = FindCurrentNode(rootNode);

            if (currentNode == null)
            {
                numberOfRuns++;
                if (numberOfLoops == 0 || numberOfRuns < numberOfLoops)
                {
                    NodeAndState = new Dictionary <BehaviourTreeNode, NodeState>();
                    currentNode  = FindCurrentNode(rootNode);
                }
                else
                {
                    Completed = true;
                    return(new ExecutionResult(true));
                }
            }

            bool toBeStarted = false;

            if (NodeAndState.ContainsKey(currentNode))
            {
                toBeStarted = NodeAndState[currentNode] == BehaviourTreeInstance.NodeState.STATE_TO_BE_STARTED;
            }
            else
            {
                toBeStarted = true;
            }

            if (toBeStarted)
            {
                ExecutionResult result     = currentNode.Execute(this);
                var             afterState = NodeAndState[currentNode];

                if (afterState == BehaviourTreeInstance.NodeState.STATE_TO_BE_STARTED)
                {
                    NodeAndState[currentNode] = BehaviourTreeInstance.NodeState.STATE_COMPLETED;
                }
                return(result);
            }

            NodeState state = NodeAndState[currentNode];

            if (state == BehaviourTreeInstance.NodeState.STATE_COMPUTE_RESULT)
            {
                ExecutionResult result = currentNode.Execute(this);
                NodeAndState[currentNode] = BehaviourTreeInstance.NodeState.STATE_COMPLETED;
                return(result);
            }

            return(null);
        }
示例#10
0
        internal GraphNode OnNodeAdded(BehaviourTree asset, BehaviourTreeNode node)
        {
            var newNode = new BehaviourTreeEditorNode(asset, node, Presenter);

            Nodes.AddNode(newNode);

            return(newNode);
        }
        private GraphNode AddNewNode(BehaviourTreeNode node)
        {
            AddToAsset(node);

            TreeAsset.Nodes.Add(node);

            return(View.OnNodeAdded(TreeAsset, node));
        }
    public override void Init(BehaviourTreeAgent agent)
    {
        this.currentTickedNode = null;

        foreach (BehaviourTreeNode child in children)
        {
            child.Init(agent);
        }
    }
示例#13
0
    void DeleteRecursivelyBranchAssets(BehaviourTreeNode node)
    {
        DeleteNodeAsset(node);

        foreach (BehaviourTreeNode child in node.GetChildren())
        {
            DeleteRecursivelyBranchAssets(child);
        }
    }
示例#14
0
    // Start is called before the first frame update
    void Start()
    {
        Blackboard = new Dictionary <string, object>();
        Blackboard.Add("enemy1", new Rect(25, 85, 10, 10));

        //initial behaviour is stopped
        startedBehaviour = false;
        mRoot            = new Repeater(this, new Sequencer(this, new BehaviourTreeNode[] { new BTEnemyMove(this, 25, 85) }));
    }
 public void Schedule(BehaviourTreeExecutionData <TBlackboard> data,
                      BehaviourTreeNode <TBlackboard> node,
                      ICompletionObserver <TBlackboard> observer = null)
 {
     if (observer != null)
     {
         node.Observer = observer;
     }
     data.Stack.AddBack(node);
 }
        private static void RemoveNode(BehaviourTreeExecutionData <TBlackboard> data,
                                       BehaviourTreeNode <TBlackboard> node)
        {
            var index = data.Stack.IndexOf(node);

            if (index >= 0)
            {
                data.Stack.RemoveAt(index);
            }
        }
示例#17
0
    void SubTreeCallback()
    {
        BehaviourTreeSubTreeNode newNode = (BehaviourTreeSubTreeNode)ScriptableObject.CreateInstance("BehaviourTreeSubTreeNode");

        newNode.ID = GetNextWindowID();
        this.selectedNode.AddChild(newNode);
        newNode.displayedName = "SUB TREE";
        AddNodeToAssets(newNode);
        SaveBehaviourTree();
        this.selectedNode = newNode;
    }
 public override void ReplaceChild(BehaviourTreeNode oldChild, BehaviourTreeNode newChild)
 {
     for (int i = 0; i < this.children.Count; i++)
     {
         if (this.children[i] == oldChild)
         {
             this.children[i] = newChild;
             return;
         }
     }
 }
示例#19
0
    void ExecutionCallback()
    {
        BehaviourTreeExecutionNode newNode = (BehaviourTreeExecutionNode)ScriptableObject.CreateInstance("BehaviourTreeExecutionNode");

        newNode.ID = GetNextWindowID();
        this.selectedNode.AddChild(newNode);
        newNode.displayedName = "EXECUTION";
        AddNodeToAssets(newNode);
        SaveBehaviourTree();
        this.selectedNode = newNode;
    }
示例#20
0
    void NewChildDecorator(BehaviourTreeDecoratorNode.Type type)
    {
        BehaviourTreeDecoratorNode newNode = (BehaviourTreeDecoratorNode)ScriptableObject.CreateInstance("BehaviourTreeDecoratorNode");

        newNode.type          = type;
        newNode.ID            = GetNextWindowID();
        newNode.displayedName = newNode.type.ToString();
        this.selectedNode.AddChild(newNode);
        AddNodeToAssets(newNode);
        SaveBehaviourTree();
        this.selectedNode = newNode;
    }
        public void Stop(BehaviourTreeExecutionData <TBlackboard> data,
                         BehaviourTreeNode <TBlackboard> node,
                         BehaviourTreeStatus status)
        {
            RemoveNode(data, node);
            data.Statuses[node.Id] = status;

            if (node.Observer != null)
            {
                node.Observer.OnComplete(this, data, status);
            }
        }
        public BehaviourTreeEditorNode(BehaviourTree tree, BehaviourTreeNode node, BehaviourTreeEditorPresenter presenter)
        {
            TreeAsset = tree;
            TreeNode  = node;
            Presenter = presenter;
            Position  = TreeNode.EditorPosition;

            Size = new Vector3(NodeWidth, NodeHeight);

            WindowTitle = GUIContent.none;
            WindowStyle = SpaceEditorStyles.GraphNodeBackground;
        }
        public void OnConnectNodes(BehaviourTreeNode parent, BehaviourTreeNode child)
        {
            if (_duringRecreate)
            {
                return;
            }

            var data = OnConnectNodesQueue.Post();

            data.Parent = parent;
            data.Child  = child;
        }
示例#24
0
    void DeleteNodeAsset(BehaviourTreeNode node)
    {
        if (node is BehaviourTreeExecutionNode)
        {
            BehaviourTreeExecutionNode execNode = (BehaviourTreeExecutionNode)node;
            if (execNode.task != null)
            {
                DestroyImmediate(execNode.task, true);
            }
        }

        DestroyImmediate(node, true);
        AssetDatabase.ImportAsset(behaviourTreeAssetFilesPath);
    }
示例#25
0
    void NewParentDecorator(BehaviourTreeDecoratorNode.Type type)
    {
        BehaviourTreeDecoratorNode newNode = (BehaviourTreeDecoratorNode)ScriptableObject.CreateInstance("BehaviourTreeDecoratorNode");

        newNode.type          = type;
        newNode.ID            = GetNextWindowID();
        newNode.displayedName = newNode.type.ToString().Replace('_', ' ');
        BehaviourTreeNode parent = FindParentOfNodeByID(BehaviourTreeEditorWindow.behaviourTree, this.selectedNode.ID);

        parent.ReplaceChild(this.selectedNode, newNode);
        newNode.AddChild(this.selectedNode);
        AddNodeToAssets(newNode);
        SaveBehaviourTree();
        this.selectedNode = newNode;
    }
        private void ShowMainContextMenu(System.Action <Type> callback)
        {
            GenericMenu menu = new GenericMenu();

            foreach (Type type in Reflection.GetSubclasses <BehaviourTreeNode>())
            {
                string title = string.Format("{0}/{1}", BehaviourTreeNode.GetNodeTypeName(type), type.Name);
                menu.AddItem(new GUIContent(title), false, CreateNewNodeCallback(callback, type));
            }

            menu.AddSeparator(string.Empty);
            menu.AddItem(new GUIContent("Refresh"), false, RecreateNodes);

            menu.ShowAsContext();
        }
示例#27
0
    void MoveLastCallback()
    {
        BehaviourTreeNode parent = FindParentOfNodeByID(BehaviourTreeEditorWindow.behaviourTree, this.selectedNode.ID);

        int selectedNodeIndice = parent.GetChildren().IndexOf(this.selectedNode);

        if (selectedNodeIndice < parent.ChildrenCount() - 1)
        {
            BehaviourTreeNode node = parent.GetChildren()[selectedNodeIndice];
            parent.GetChildren().Remove(node);
            parent.GetChildren().Insert(parent.ChildrenCount(), node);
        }

        SaveBehaviourTree();
    }
示例#28
0
    void DeleteBranchCallback()
    {
        if (!EditorUtility.DisplayDialog("Delete branch ?",
                                         "Are you sure you want to delete the node " + this.selectedNode.displayedName + " and his children ?",
                                         "Yes, delete this branch", "No"))
        {
            return;
        }

        BehaviourTreeNode parent = FindParentOfNodeByID(BehaviourTreeEditorWindow.behaviourTree, this.selectedNode.ID);

        parent.RemoveChild(selectedNode);
        DeleteRecursivelyBranchAssets(selectedNode);
        AssetDatabase.Refresh();
        SaveBehaviourTree();
    }
示例#29
0
        protected BehaviourTreeNode <T>[] BuildChildren <T>(ref int index)
        {
            var childrenNodes = new BehaviourTreeNode <T> [childrenCount];

            for (var i = 0; i < childrenCount; i++)
            {
                var port          = GetOutputPort(string.Format(ChildrenPortNameFormat, i));
                var connectedNode = port.Connection.node as BehaviourTreeGraphNode;
                if (connectedNode != null)
                {
                    childrenNodes[i] = connectedNode.Build <T>(ref index);
                }
            }

            return(childrenNodes);
        }
示例#30
0
    void ControlWindowFunction(int windowID)
    {
        BehaviourTreeControlNode node = (BehaviourTreeControlNode)FindNodeByID(BehaviourTreeEditorWindow.behaviourTree, windowID);

        Event current = Event.current;

        if (current.type == EventType.MouseDown && current.button == 1)
        {
            this.selectedNode = node;

            if (Selection.activeObject != BehaviourTreeEditorWindow.behaviourTree)
            {
                SelectNodeButDontChangeProjectView();
            }

            GenericMenu menu = new GenericMenu();

            AddNewChildOption(menu, true);
            AddInsertNewParentOptions(menu);
            AddMoveOption(menu);
            if (this.selectedNode.ChildrenCount() <= 1)
            {
                menu.AddItem(new GUIContent("Delete Node"), false, DeleteNodeCallback);
                menu.AddDisabledItem(new GUIContent("Delete Branch"));
            }
            else
            {
                menu.AddDisabledItem(new GUIContent("Delete Node"));
                menu.AddItem(new GUIContent("Delete Branch"), false, DeleteBranchCallback);
            }

            menu.ShowAsContext();

            current.Use();
        }
        else if (current.type == EventType.MouseDown && current.button == 0)
        {
            this.selectedNode = node;

            if (Selection.activeObject != BehaviourTreeEditorWindow.behaviourTree)
            {
                SelectNodeButDontChangeProjectView();
            }

            current.Use();
        }
    }