Represents time. Used to pass time values to behaviour tree nodes.
コード例 #1
0
        public void fails_when_required_number_of_children_fail()
        {
            Init(2, 2);

            var time = new TimeData();

            var mockChild1 = new Mock<IBehaviourTreeNode>();
            mockChild1
                .Setup(m => m.Tick(time))
                .Returns(BehaviourTreeStatus.Failure);

            var mockChild2 = new Mock<IBehaviourTreeNode>();
            mockChild2
                .Setup(m => m.Tick(time))
                .Returns(BehaviourTreeStatus.Failure);

            var mockChild3 = new Mock<IBehaviourTreeNode>();
            mockChild3
                .Setup(m => m.Tick(time))
                .Returns(BehaviourTreeStatus.Running);

            testObject.AddChild(mockChild1.Object);
            testObject.AddChild(mockChild2.Object);
            testObject.AddChild(mockChild3.Object);

            Assert.Equal(BehaviourTreeStatus.Failure, testObject.Tick(time));

            mockChild1.Verify(m => m.Tick(time), Times.Once());
            mockChild2.Verify(m => m.Tick(time), Times.Once());
            mockChild3.Verify(m => m.Tick(time), Times.Once());
        }
コード例 #2
0
        public BehaviourTreeStatus Tick(TimeData time)
        {
            var numChildrenSuceeded = 0;
            var numChildrenFailed = 0;

            foreach (var child in children)
            {
                var childStatus = child.Tick(time);
                switch (childStatus)
                {
                    case BehaviourTreeStatus.Success: ++numChildrenSuceeded; break;
                    case BehaviourTreeStatus.Failure: ++numChildrenFailed; break;
                }
            }

            if (numRequiredToSucceed > 0 && numChildrenSuceeded >= numRequiredToSucceed)
            {
                return BehaviourTreeStatus.Success;
            }

            if (numRequiredToFail > 0 && numChildrenFailed >= numRequiredToFail)
            {
                return BehaviourTreeStatus.Failure;
            }

            return BehaviourTreeStatus.Running;
        }
コード例 #3
0
        public BehaviourTreeStatus Tick(TimeData time)
        {
            if (childNode == null)
            {
                throw new ApplicationException("Succeeder must have a child node!");
            }

            childNode.Tick(time);
            return(BehaviourTreeStatus.Success);
        }
コード例 #4
0
        public BehaviourTreeStatus Tick(TimeData time)
        {
            var childStatus = childNode.Tick(time);

            if (childStatus == BehaviourTreeStatus.Failure)
            {
                return(BehaviourTreeStatus.Success);
            }

            return(BehaviourTreeStatus.Running);
        }
コード例 #5
0
        public IEnumerator <BehaviourTreeStatus> Tick(TimeData time)
        {
            currentStatus = BehaviourTreeStatus.Running;

            while (isRunning())
            {
                numChildrenSuceeded = 0;
                numChildrenFailed   = 0;

                foreach (var child in children)
                {
                    var childStatus = child.Tick(time);
                    childStatus.MoveNext();

                    if (childStatus.Current == BehaviourTreeStatus.Running)
                    {
                        // keep looping until we exit running mode or we
                        // run out of enum values.
                        yield return(childStatus.Current);

                        while (childStatus.MoveNext())
                        {
                            if (childStatus.Current != BehaviourTreeStatus.Running)
                            {
                                break;
                            }
                            yield return(childStatus.Current);
                        }
                    }
                    switch (childStatus.Current)
                    {
                    case BehaviourTreeStatus.Success: ++numChildrenSuceeded; break;

                    case BehaviourTreeStatus.Failure: ++numChildrenFailed; break;
                    }
                }

                if (numRequiredToSucceed > 0 && numChildrenSuceeded >= numRequiredToSucceed)
                {
                    currentStatus = BehaviourTreeStatus.Success;
                }
                else
                if (numRequiredToFail > 0 && numChildrenFailed >= numRequiredToFail)
                {
                    currentStatus = BehaviourTreeStatus.Failure;
                }
                else
                {
                    // Return Running if conditions were not met
                    currentStatus = BehaviourTreeStatus.Running;
                }
                yield return(currentStatus);
            }
        }
コード例 #6
0
        public BehaviourTreeStatus Tick(TimeData time)
        {
            foreach (var child in children)
            {
                var childStatus = child.Tick(time);
                if (childStatus != BehaviourTreeStatus.Success)
                {
                    return childStatus;
                }
            }

            return BehaviourTreeStatus.Success;
        }
コード例 #7
0
        public BehaviourTreeStatus Tick(TimeData time)
        {
            if ((maxIterations > -1 || maxIterations == 0) && iterations >= maxIterations)
            {
                iterations = 0;
                return(BehaviourTreeStatus.Success);
            }

            childNode.Tick(time);
            iterations++;

            return(BehaviourTreeStatus.Running);
        }
コード例 #8
0
        public BehaviourTreeStatus Tick(TimeData time)
        {
            foreach (var child in children)
            {
                var childStatus = child.Tick(time);
                if (childStatus != BehaviourTreeStatus.Failure)
                {
                    return(childStatus);
                }
            }

            return(BehaviourTreeStatus.Failure);
        }
コード例 #9
0
ファイル: ActionNode.cs プロジェクト: PlumpMath/coroutine-56
        public IEnumerator <BehaviourTreeStatus> Tick(TimeData time)
        {
            for (var e = fn(time); e.MoveNext();)
            {
                currentStatus = e.Current;
                yield return(currentStatus);

                // this will continue looping until complete and as long as
                // the function returns another enumerated value that is not success or failure
                if (isComplete())
                {
                    yield break;
                }
            }
        }
コード例 #10
0
        public void pass_through_running_of_child_node()
        {
            Init();

            var time = new TimeData();

            var mockChildNode = new Mock<IBehaviourTreeNode>();
            mockChildNode
                .Setup(m => m.Tick(time))
                .Returns(BehaviourTreeStatus.Running);

            testObject.AddChild(mockChildNode.Object);

            Assert.Equal(BehaviourTreeStatus.Running, testObject.Tick(time));

            mockChildNode.Verify(m => m.Tick(time), Times.Once());
        }
コード例 #11
0
        public void inverts_success_of_child_node()
        {
            Init();

            var time = new TimeData();

            var mockChildNode = new Mock<IBehaviourTreeNode>();
            mockChildNode
                .Setup(m => m.Tick(time))
                .Returns(BehaviourTreeStatus.Success);

            testObject.AddChild(mockChildNode.Object);

            Assert.Equal(BehaviourTreeStatus.Failure, testObject.Tick(time));

            mockChildNode.Verify(m => m.Tick(time), Times.Once());
        }
コード例 #12
0
        public BehaviourTreeStatus Tick(TimeData time)
        {
            for (int i = 0; i < children.Count; i++)
            {
                var childStatus = children[i].Tick(time);
                if (childStatus == BehaviourTreeStatus.Running)
                {
                    i--;
                    break;
                }
                if (childStatus != BehaviourTreeStatus.Failure)
                {
                    return(childStatus);
                }
            }

            return(BehaviourTreeStatus.Failure);
        }
コード例 #13
0
        public void can_run_action()
        {
            var time = new TimeData();

            var invokeCount = 0;
            var testObject =
                new ActionNode(
                    "some-action",
                    t =>
                    {
                        Assert.Equal(time, t);

                        ++invokeCount;
                        return BehaviourTreeStatus.Running;
                    }
                );

            Assert.Equal(BehaviourTreeStatus.Running, testObject.Tick(time));
            Assert.Equal(1, invokeCount);
        }
コード例 #14
0
        public void runs_the_first_node_if_it_succeeds()
        {
            Init();

            var time = new TimeData();

            var mockChild1 = new Mock<IBehaviourTreeNode>();
            mockChild1
                .Setup(m => m.Tick(time))
                .Returns(BehaviourTreeStatus.Success);

            var mockChild2 = new Mock<IBehaviourTreeNode>();

            testObject.AddChild(mockChild1.Object);
            testObject.AddChild(mockChild2.Object);

            Assert.Equal(BehaviourTreeStatus.Success, testObject.Tick(time));

            mockChild1.Verify(m => m.Tick(time), Times.Once());
            mockChild2.Verify(m => m.Tick(time), Times.Never());
        }
コード例 #15
0
        public BehaviourTreeStatus Tick(TimeData time)
        {
            if (childNode == null)
            {
                throw new ApplicationException("InverterNode must have a child node!");
            }

            var result = childNode.Tick(time);
            if (result == BehaviourTreeStatus.Failure)
            {
                return BehaviourTreeStatus.Success;
            }
            else if (result == BehaviourTreeStatus.Success)
            {
                return BehaviourTreeStatus.Failure;
            }
            else
            {
                return result;
            }
        }
コード例 #16
0
 public BehaviourTreeStatus Tick(TimeData time)
 {
     for (int i = 0; i < children.Count; i++)
     {
         var childStatus = children[i].Tick(time);
         if (childStatus != BehaviourTreeStatus.Success)
         {
             if (childStatus == BehaviourTreeStatus.Running)
             {
                 i--;
                 break;
             }
             if (childStatus == BehaviourTreeStatus.Failure)
             {
                 return(childStatus);//this should break us out of the loop
             }
         }
     }
     //otherwise we go all the way through the loop
     return(BehaviourTreeStatus.Success);
 }
コード例 #17
0
        public void when_first_child_fails_then_entire_sequence_fails()
        {
            Init();

            var time = new TimeData();

            var mockChild1 = new Mock<IBehaviourTreeNode>();
            mockChild1
                .Setup(m => m.Tick(time))
                .Returns(BehaviourTreeStatus.Failure);

            var mockChild2 = new Mock<IBehaviourTreeNode>();

            testObject.AddChild(mockChild1.Object);
            testObject.AddChild(mockChild2.Object);

            Assert.Equal(BehaviourTreeStatus.Failure, testObject.Tick(time));

            mockChild1.Verify(m => m.Tick(time), Times.Once());
            mockChild2.Verify(m => m.Tick(time), Times.Never());
        }
コード例 #18
0
ファイル: InverterNode.cs プロジェクト: Cylindric/Sim
        public BehaviourTreeStatus Tick(TimeData time)
        {
            if (childNode == null)
            {
                throw new ApplicationException("InverterNode must have a child node!");
            }

            var result = childNode.Tick(time);

            if (result == BehaviourTreeStatus.Failure)
            {
                return(BehaviourTreeStatus.Success);
            }
            else if (result == BehaviourTreeStatus.Success)
            {
                return(BehaviourTreeStatus.Failure);
            }
            else
            {
                return(result);
            }
        }
コード例 #19
0
        public IEnumerator <BehaviourTreeStatus> Tick(TimeData time)
        {
            if (childNode == null)
            {
                throw new ApplicationException("InverterNode must have a child node!");
            }

            var result = childNode.Tick(time);

            result.MoveNext();
            currentStatus = result.Current;
            if (isRunning())
            {
                // keep looping until we exit running mode or we
                // run out of enum values.
                yield return(currentStatus);

                while (result.MoveNext())
                {
                    currentStatus = result.Current;
                    if (!isRunning())
                    {
                        break;
                    }
                }
            }
            if (isFailed())
            {
                currentStatus = BehaviourTreeStatus.Success;
            }
            else if (isSuccess())
            {
                currentStatus = BehaviourTreeStatus.Failure;
            }

            yield return(currentStatus);
        }
コード例 #20
0
        public BehaviourTreeStatus Tick(TimeData time)
        {
            var numChildrenSuceeded = 0;
            var numChildrenFailed   = 0;


            if (!iscompleted)
            {
                while (current != null)
                {
                    var status = current.Value.Action.Tick(time);
                    switch (status)
                    {
                    case BehaviourTreeStatus.Success: ++numChildrenSuceeded; break;

                    case BehaviourTreeStatus.Failure: ++numChildrenFailed; break;
                    }

                    current = current.Next;
                }

                iscompleted = true;
            }

            if (numRequiredToSucceed > 0 && numChildrenSuceeded >= numRequiredToSucceed)
            {
                return(BehaviourTreeStatus.Success);
            }

            if (numRequiredToFail > 0 && numChildrenFailed >= numRequiredToFail)
            {
                return(BehaviourTreeStatus.Failure);
            }

            return(BehaviourTreeStatus.Failure);
        }
コード例 #21
0
        public void can_run_all_children_in_order()
        {
            Init();

            var time = new TimeData();

            var callOrder = 0;

            var mockChild1 = new Mock<IBehaviourTreeNode>();
            mockChild1
                .Setup(m => m.Tick(time))
                .Returns(BehaviourTreeStatus.Success)
                .Callback(() =>
                 {
                     Assert.Equal(1, ++callOrder);
                 });

            var mockChild2 = new Mock<IBehaviourTreeNode>();
            mockChild2
                .Setup(m => m.Tick(time))
                .Returns(BehaviourTreeStatus.Success)
                .Callback(() =>
                {
                    Assert.Equal(2, ++callOrder);
                });

            testObject.AddChild(mockChild1.Object);
            testObject.AddChild(mockChild2.Object);

            Assert.Equal(BehaviourTreeStatus.Success, testObject.Tick(time));

            Assert.Equal(2, callOrder);

            mockChild1.Verify(m => m.Tick(time), Times.Once());
            mockChild2.Verify(m => m.Tick(time), Times.Once());
        }
コード例 #22
0
        public BehaviourTreeStatus Tick(TimeData time)
        {
            if (!iscompleted)
            {
                bool hasrunning = false;

                while (current != null)
                {
                    var status = current.Value.Action.Tick(time);

                    if (status == BehaviourTreeStatus.Success)
                    {
                        iscompleted = true;
                        return(status);
                    }
                    else if (status == BehaviourTreeStatus.Running)
                    {
                        hasrunning = true;
                    }

                    current = current.Next;
                }

                //if has running node, continue wait
                if (hasrunning)
                {
                    //reset current position to first
                    current = children.First;
                    return(BehaviourTreeStatus.Running);
                }

                iscompleted = true;
            }

            return(BehaviourTreeStatus.Failure);
        }
コード例 #23
0
        public void continues_to_run_if_required_number_children_neither_succeed_or_fail()
        {
            Init(2, 2);

            var time = new TimeData();

            var mockChild1 = new Mock<IBehaviourTreeNode>();
            mockChild1
                .Setup(m => m.Tick(time))
                .Returns(BehaviourTreeStatus.Success);

            var mockChild2 = new Mock<IBehaviourTreeNode>();
            mockChild2
                .Setup(m => m.Tick(time))
                .Returns(BehaviourTreeStatus.Failure);

            testObject.AddChild(mockChild1.Object);
            testObject.AddChild(mockChild2.Object);

            Assert.Equal(BehaviourTreeStatus.Running, testObject.Tick(time));

            mockChild1.Verify(m => m.Tick(time), Times.Once());
            mockChild2.Verify(m => m.Tick(time), Times.Once());
        }
コード例 #24
0
        public void when_first_child_is_running_second_child_is_supressed()
        {
            Init();

            var time = new TimeData();

            var mockChild1 = new Mock<IBehaviourTreeNode>();
            mockChild1
                .Setup(m => m.Tick(time))
                .Returns(BehaviourTreeStatus.Running);

            var mockChild2 = new Mock<IBehaviourTreeNode>();

            testObject.AddChild(mockChild1.Object);
            testObject.AddChild(mockChild2.Object);

            Assert.Equal(BehaviourTreeStatus.Running, testObject.Tick(time));

            mockChild1.Verify(m => m.Tick(time), Times.Once());
            mockChild2.Verify(m => m.Tick(time), Times.Never());
        }
コード例 #25
0
        // A Selector may be contaminated with other nodes (Condition) or IParentBeahviourTreeNodes over which
        // it's behaviour as a selector is applied. The assumption here is that any item in it's child list
        // should have  selector behaviour applied unless the first one is a condition Action.
        public IEnumerator <BehaviourTreeStatus> Tick(TimeData time)
        {
            int skipOne = 0;
            // check if this is a conditional action
            IBehaviourTreeNode firstNode = children[0];

            if (firstNode is ConditionNode)
            {
                ConditionNode firstChild = (ConditionNode)firstNode;
                var           status     = firstChild.Tick(time);
                status.MoveNext();

                if (status.Current != BehaviourTreeStatus.Success)
                {
                    // Return Failure and Exit the Node
                    currentStatus = BehaviourTreeStatus.Failure;
                    yield return(currentStatus);

                    yield break;
                }
                else
                {
                    ++skipOne;
                }
            }

            List <IBehaviourTreeNode> remainingChildren = children.Skip(skipOne).ToList <IBehaviourTreeNode>();

            if (isRandom)
            {
                remainingChildren = Randomize(remainingChildren);
            }

            foreach (var child in remainingChildren)
            {
                var childStatus = child.Tick(time);
                childStatus.MoveNext();
                currentStatus = childStatus.Current;
                if (isRunning())
                {
                    // keep looping until we exit running mode or we
                    // run out of enum values.
                    yield return(currentStatus);

                    while (childStatus.MoveNext())
                    {
                        currentStatus = childStatus.Current;
                        if (!isRunning())
                        {
                            break;
                        }
                    }
                }
                // On exit above, status should be success or fail
                // if success, then exit selector
                if (isSuccess())
                {
                    yield return(currentStatus);

                    yield break;
                }
            }

            yield return(currentStatus);
        }
コード例 #26
0
 public BehaviourTreeStatus Tick(TimeData time)
 {
     return(fn(time));
 }
コード例 #27
0
 public BehaviourTreeStatus Tick(TimeData time)
 {
     return fn(time);
 }
コード例 #28
0
        public void stops_on_the_first_node_when_it_is_running()
        {
            Init();

            var time = new TimeData();

            var mockChild1 = new Mock<IBehaviourTreeNode>();
            mockChild1
                .Setup(m => m.Tick(time))
                .Returns(BehaviourTreeStatus.Running);

            var mockChild2 = new Mock<IBehaviourTreeNode>();

            testObject.AddChild(mockChild1.Object);
            testObject.AddChild(mockChild2.Object);

            Assert.Equal(BehaviourTreeStatus.Running, testObject.Tick(time));

            mockChild1.Verify(m => m.Tick(time), Times.Once());
            mockChild2.Verify(m => m.Tick(time), Times.Never());
        }
コード例 #29
0
 public BehaviourTreeStatus Tick(TimeData time)
 {
     return(fn(time) ? BehaviourTreeStatus.Success : BehaviourTreeStatus.Failure);
 }
コード例 #30
0
 public BehaviourTreeStatus Tick(TimeData time)
 {
     Status = fn(time);
     return(Status);
 }
コード例 #31
0
 public IEnumerator <BehaviourTreeStatus> Tick(TimeData time)
 {
     currentStatus = fn(time) ? BehaviourTreeStatus.Success : BehaviourTreeStatus.Failure;
     yield return(currentStatus);
 }