public void Is_Key_Set_Or_Not()
        {
            var child = new MockNode();
            var sut   = new BlackboardCondition <TestType>("Key1", Operator.IS_SET).Decorate(new Log("yahaha"));
            var bt    = new Root().Decorate(sut);


            bt.Blackboard.AddObserver("Key1", (t, d) => UnityEngine.Debug.Log($"Raise event {t},{d.GetType()}"));
            bt.Start();

            Assert.AreEqual(Node.NodeStatus.Inactive, child.CurrentStatus, "is not set, child should be INACTIVED");
            bt.Blackboard.Set("Key1", new TestType());

            bt.Clock.Tick(1f);

            Assert.AreEqual(Node.NodeStatus.Active, child.CurrentStatus, "is set, child should be ACTIVED");
        }
예제 #2
0
        public void ShouldListenToEvents_WhenUsingChildBlackboard()
        {
            Blackboard rootBlackboard = new Blackboard("rootbb", clock);
            Blackboard blackboard     = new Blackboard("childbb", rootBlackboard, clock);

            // our mock nodes we want to query for status
            MockNode firstChild  = new MockNode(false); // false -> fail when aborted
            MockNode secondChild = new MockNode(false);

            // conditions for each subtree that listen the BB for events
            var firstCondition  = new BlackboardCondition <bool>("branch1", Operator.IS_EQUAL, true, ObserverAborts.BOTH).Decorate(firstChild);
            var secondCondition = new BlackboardCondition <bool>("branch2", Operator.IS_EQUAL, true, ObserverAborts.BOTH).Decorate(secondChild);

            // set up the tree
            var selector     = new Selector().OpenBranch(firstCondition, secondCondition);
            var behaviorTree = new TestRoot(blackboard, clock).Decorate(selector);

            // intially we want to activate branch2
            rootBlackboard.Set("branch2", true);
            UnityEngine.Debug.Log(blackboard.Get("branch2").Value);

            // start the tree
            behaviorTree.Start();

            // tick the timer to ensure the blackboard notifies the nodes
            clock.Tick(0.1f);


            // verify the second child is running
            Assert.AreEqual(Node.NodeStatus.Inactive, firstChild.CurrentStatus);
            Assert.AreEqual(Node.NodeStatus.Active, secondChild.CurrentStatus);

            // change keys so the first conditions get true, too
            rootBlackboard.Set("branch1", true);
            UnityEngine.Debug.Log(blackboard.Get("branch1").Value);

            // tick the timer to ensure the blackboard notifies the nodes
            clock.Tick(0.1f);

            // now we should be in branch1
            Assert.AreEqual(Node.NodeStatus.Active, firstChild.CurrentStatus);
            Assert.AreEqual(Node.NodeStatus.Inactive, secondChild.CurrentStatus);
        }