Skip to content

Commit

Permalink
feat(decorators): add three new decorators
Browse files Browse the repository at this point in the history
RepeatForever, RepeatUntilSuccess, and RepeatUntilFailure are new decorators used to continue a task
indefinitely.  RepeatForever executes its child and returns TaskStatus.Continue, regardless of its
child's return status.  RepeatUntilSuccess returns TaskStatus.Success if its child returns success,
else it returns TaskStatus.Continue.  RepeatUntilFailure returns TaskStatus.Failure if its child
returns failure, else it returns TaskStatus.Continue.
  • Loading branch information
JesseTG committed Jun 4, 2019
1 parent fb39173 commit f376ea6
Show file tree
Hide file tree
Showing 13 changed files with 256 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ public BehaviorTreeBuilder ReturnFailure (string name = "return failure") {
return ParentTask<ReturnFailure>(name);
}

public BehaviorTreeBuilder RepeatUntilSuccess (string name = "repeat until success") {
return ParentTask<RepeatUntilSuccess>(name);
}

public BehaviorTreeBuilder RepeatUntilFailure (string name = "repeat until failure") {
return ParentTask<RepeatUntilFailure>(name);
}

public BehaviorTreeBuilder RepeatForever (string name = "repeat forever") {
return ParentTask<RepeatForever>(name);
}

public BehaviorTreeBuilder Sequence (string name = "sequence") {
return ParentTask<Sequence>(name);
}
Expand Down
12 changes: 12 additions & 0 deletions Assets/FluidBehaviorTree/Runtime/Decorators/RepeatForever.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using CleverCrow.Fluid.BTs.Trees;
using CleverCrow.Fluid.BTs.Decorators;
using CleverCrow.Fluid.BTs.Tasks;

namespace CleverCrow.Fluid.BTs.Decorators {
public class RepeatForever : DecoratorBase {
protected override TaskStatus OnUpdate () {
Child?.Update();
return TaskStatus.Continue;
}
}
}
11 changes: 11 additions & 0 deletions Assets/FluidBehaviorTree/Runtime/Decorators/RepeatForever.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions Assets/FluidBehaviorTree/Runtime/Decorators/RepeatUntilFailure.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using CleverCrow.Fluid.BTs.Trees;
using CleverCrow.Fluid.BTs.Decorators;
using CleverCrow.Fluid.BTs.Tasks;

namespace CleverCrow.Fluid.BTs.Decorators {
public class RepeatUntilFailure : DecoratorBase {
protected override TaskStatus OnUpdate () {
if (Child == null) {
return TaskStatus.Failure;
}

var childStatus = Child.Update();
var status = childStatus;

if (status == TaskStatus.Failure) {
return TaskStatus.Failure;
}
else {
return TaskStatus.Continue;
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions Assets/FluidBehaviorTree/Runtime/Decorators/RepeatUntilSuccess.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using CleverCrow.Fluid.BTs.Trees;
using CleverCrow.Fluid.BTs.Decorators;
using CleverCrow.Fluid.BTs.Tasks;

namespace CleverCrow.Fluid.BTs.Decorators {
public class RepeatUntilSuccess : DecoratorBase {
protected override TaskStatus OnUpdate () {
if (Child == null) {
return TaskStatus.Success;
}

var childStatus = Child.Update();
var status = childStatus;

if (status == TaskStatus.Success) {
return TaskStatus.Success;
}
else {
return TaskStatus.Continue;
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using CleverCrow.Fluid.BTs.Decorators;
using CleverCrow.Fluid.BTs.Tasks;
using NUnit.Framework;

namespace CleverCrow.Fluid.BTs.Testing {
public class RepeatForeverTest {
public class UpdateMethod {
[Test]
public void Returns_continue_on_child_failure () {
var repeater = new RepeatForever();
repeater.AddChild(A.TaskStub().WithUpdateStatus(TaskStatus.Failure).Build());

Assert.AreEqual(TaskStatus.Continue, repeater.Update());
}

[Test]
public void Returns_continue_on_child_success () {
var repeater = new RepeatForever();
repeater.AddChild(A.TaskStub().WithUpdateStatus(TaskStatus.Success).Build());

Assert.AreEqual(TaskStatus.Continue, repeater.Update());
}

[Test]
public void Returns_continue_on_child_continue () {
var repeater = new RepeatForever();
repeater.AddChild(A.TaskStub().WithUpdateStatus(TaskStatus.Continue).Build());

Assert.AreEqual(TaskStatus.Continue, repeater.Update());
}

[Test]
public void Does_not_crash_if_no_child () {
var repeater = new RepeatForever();

Assert.DoesNotThrow(() => repeater.Update());
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using CleverCrow.Fluid.BTs.Decorators;
using CleverCrow.Fluid.BTs.Tasks;
using NUnit.Framework;

namespace CleverCrow.Fluid.BTs.Testing {
public class RepeatUntilFailureTest {
public class UpdateMethod {
[Test]
public void Returns_failure_on_child_failure () {
var repeater = new RepeatUntilFailure();
repeater.AddChild(A.TaskStub().WithUpdateStatus(TaskStatus.Failure).Build());

Assert.AreEqual(TaskStatus.Failure, repeater.Update());
}

[Test]
public void Returns_continue_on_child_success () {
var repeater = new RepeatUntilFailure();
repeater.AddChild(A.TaskStub().WithUpdateStatus(TaskStatus.Success).Build());

Assert.AreEqual(TaskStatus.Continue, repeater.Update());
}

[Test]
public void Returns_continue_on_child_continue () {
var repeater = new RepeatUntilFailure();
repeater.AddChild(A.TaskStub().WithUpdateStatus(TaskStatus.Continue).Build());

Assert.AreEqual(TaskStatus.Continue, repeater.Update());
}

[Test]
public void Does_not_crash_if_no_child () {
var repeater = new RepeatUntilFailure();

Assert.DoesNotThrow(() => repeater.Update());
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using CleverCrow.Fluid.BTs.Decorators;
using CleverCrow.Fluid.BTs.Tasks;
using NUnit.Framework;

namespace CleverCrow.Fluid.BTs.Testing {
public class RepeatUntilSuccessTest {
public class UpdateMethod {
[Test]
public void Returns_continue_on_child_failure () {
var repeater = new RepeatUntilSuccess();
repeater.AddChild(A.TaskStub().WithUpdateStatus(TaskStatus.Failure).Build());

Assert.AreEqual(TaskStatus.Continue, repeater.Update());
}

[Test]
public void Returns_success_on_child_success () {
var repeater = new RepeatUntilSuccess();
repeater.AddChild(A.TaskStub().WithUpdateStatus(TaskStatus.Success).Build());

Assert.AreEqual(TaskStatus.Success, repeater.Update());
}

[Test]
public void Returns_continue_on_child_continue () {
var repeater = new RepeatUntilSuccess();
repeater.AddChild(A.TaskStub().WithUpdateStatus(TaskStatus.Continue).Build());

Assert.AreEqual(TaskStatus.Continue, repeater.Update());
}

[Test]
public void Does_not_crash_if_no_child () {
var repeater = new RepeatUntilSuccess();

Assert.DoesNotThrow(() => repeater.Update());
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f376ea6

Please sign in to comment.