Skip to content

Commit

Permalink
fix(repeaters): now handle composites and task hooks as expected
Browse files Browse the repository at this point in the history
Repeater commands were not triggering tasks as expected and blowing out composites. Lacked a proper
reset when the repeater was continunally looping.
  • Loading branch information
ashblue committed Jun 1, 2020
1 parent b1b099b commit 3d8bcf6
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 77 deletions.
4 changes: 1 addition & 3 deletions Assets/FluidBehaviorTree/Runtime/Decorators/RepeatForever.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using CleverCrow.Fluid.BTs.Trees;
using CleverCrow.Fluid.BTs.Decorators;
using CleverCrow.Fluid.BTs.Tasks;

namespace CleverCrow.Fluid.BTs.Decorators {
Expand All @@ -11,4 +9,4 @@ protected override TaskStatus OnUpdate () {
return TaskStatus.Continue;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using CleverCrow.Fluid.BTs.Trees;
using CleverCrow.Fluid.BTs.Decorators;
using CleverCrow.Fluid.BTs.Tasks;

namespace CleverCrow.Fluid.BTs.Decorators {
Expand All @@ -10,8 +8,8 @@ protected override TaskStatus OnUpdate () {
if (Child.Update() == TaskStatus.Failure) {
return TaskStatus.Failure;
}

return TaskStatus.Continue;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public override TaskStatus Update () {

var status = OnUpdate();
LastStatus = status;
if (status != TaskStatus.Continue) {
Reset();
}

return status;
}
Expand All @@ -33,7 +36,7 @@ private void UpdateTicks () {
if (ParentTree == null) {
return;
}

if (_lastTickCount != ParentTree.TickCount) {
Reset();
}
Expand All @@ -56,12 +59,12 @@ public virtual ITaskParent AddChild (ITask child) {
if (!child.Enabled) {
return this;
}

if (Children.Count < MaxChildren || MaxChildren < 0) {
Children.Add(child);
}

return this;
}
}
}
}
10 changes: 5 additions & 5 deletions Assets/FluidBehaviorTree/Runtime/Tasks/TaskBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override TaskStatus Update () {
Init();
_init = true;
}

if (!_start) {
Start();
_start = true;
Expand All @@ -51,7 +51,7 @@ private void UpdateTicks () {
if (ParentTree == null) {
return;
}

if (_lastTickCount != ParentTree.TickCount) {
Reset();
}
Expand Down Expand Up @@ -85,7 +85,7 @@ private void Init () {
/// </summary>
protected virtual void OnInit () {
}

private void Start () {
OnStart();
}
Expand All @@ -101,7 +101,7 @@ private void Exit () {
OnExit();
}

_exit = false;
Reset();
}

/// <summary>
Expand All @@ -110,4 +110,4 @@ private void Exit () {
protected virtual void OnExit () {
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,48 @@
namespace CleverCrow.Fluid.BTs.Testing {
public class RepeatForeverTest {
public class UpdateMethod {
private RepeatForever repeater;
private RepeatForever Setup (ITask child) {
var repeat = new RepeatForever();
repeat.AddChild(child);

[SetUp]
public void Setup_repeater () {
repeater = new RepeatForever();
return repeat;
}

[Test]
public void Returns_continue_on_child_failure () {
repeater.AddChild(A.TaskStub().WithUpdateStatus(TaskStatus.Failure).Build());
public class WhenChildReturnsFailure : UpdateMethod {
[Test]
public void It_should_return_continue () {
var stub = A.TaskStub().WithUpdateStatus(TaskStatus.Failure).Build();

Assert.AreEqual(TaskStatus.Continue, repeater.Update());
var repeater = Setup(stub);
var status = repeater.Update();

Assert.AreEqual(TaskStatus.Continue, status);
}
}

[Test]
public void Returns_continue_on_child_success () {
repeater.AddChild(A.TaskStub().WithUpdateStatus(TaskStatus.Success).Build());
public class WhenChildReturnsSuccess : UpdateMethod {
[Test]
public void It_should_return_continue () {
var stub = A.TaskStub().WithUpdateStatus(TaskStatus.Success).Build();

var repeater = Setup(stub);
var status = repeater.Update();

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

[Test]
public void Returns_continue_on_child_continue () {
repeater.AddChild(A.TaskStub().WithUpdateStatus(TaskStatus.Continue).Build());
public class WhenChildReturnsContinue : UpdateMethod {
[Test]
public void It_should_return_continue () {
var stub = A.TaskStub().WithUpdateStatus(TaskStatus.Continue).Build();

var repeater = Setup(stub);
var status = repeater.Update();

Assert.AreEqual(TaskStatus.Continue, repeater.Update());
Assert.AreEqual(TaskStatus.Continue, status);
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using CleverCrow.Fluid.BTs.TaskParents.Composites;
using CleverCrow.Fluid.BTs.Tasks;
using CleverCrow.Fluid.BTs.TaskParents;
using NSubstitute;
using NSubstitute.ReturnsExtensions;
using NUnit.Framework;

namespace CleverCrow.Fluid.BTs.Testing {
Expand Down Expand Up @@ -37,14 +35,6 @@ public void It_should_run_update_on_all_success_child_tasks () {
_sequence.Children.ForEach((c) => c.Received(1).Update());
}

[Test]
public void It_should_not_run_update_on_any_child_tasks_after_success () {
_sequence.Update();
_sequence.Update();

_sequence.Children.ForEach((c) => c.Received(1).Update());
}

[Test]
public void It_should_retick_a_continue_node_when_update_is_rerun () {
_childB.Update().Returns(TaskStatus.Continue);
Expand Down
61 changes: 50 additions & 11 deletions Assets/FluidBehaviorTree/Tests/Editor/TaskParents/TaskParentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,18 @@ public void SetTaskParent () {

public class TaskParentExample : TaskParentBase {
public int childCount = -1;
public int resetCount = 0;
public TaskStatus status = TaskStatus.Success;

protected override int MaxChildren => childCount;

protected override TaskStatus OnUpdate () {
return status;
}

public override void Reset () {
resetCount += 1;
}
}

public class TaskExample : ActionBase {
Expand All @@ -25,19 +35,48 @@ protected override TaskStatus OnUpdate () {
}
}

public class TriggeringReset : TaskParentTest {
[Test]
public void It_should_trigger_Reset_on_success () {
taskParent.status = TaskStatus.Success;

taskParent.Update();

Assert.AreEqual(1, taskParent.resetCount);
}

[Test]
public void It_should_trigger_Reset_on_failure () {
taskParent.status = TaskStatus.Failure;

taskParent.Update();

Assert.AreEqual(1, taskParent.resetCount);
}

[Test]
public void It_should_not_trigger_Reset_on_continue () {
taskParent.status = TaskStatus.Continue;

taskParent.Update();

Assert.AreEqual(0, taskParent.resetCount);
}
}

public class EnabledProperty : TaskParentTest {
[Test]
public void Returns_enabled_if_child () {
taskParent.AddChild(A.TaskStub().Build());

Assert.IsTrue(taskParent.Enabled);
}

[Test]
public void Returns_disabled_if_child_and_set_to_disabled () {
taskParent.AddChild(A.TaskStub().Build());
taskParent.Enabled = false;

Assert.IsFalse(taskParent.Enabled);
}
}
Expand All @@ -46,38 +85,38 @@ public class AddChildMethod : TaskParentTest {
[Test]
public void Adds_a_child () {
taskParent.AddChild(new TaskExample());

Assert.AreEqual(1, taskParent.Children.Count);
}

[Test]
public void Adds_two_children () {
taskParent.AddChild(new TaskExample());
taskParent.AddChild(new TaskExample());

Assert.AreEqual(2, taskParent.Children.Count);
}

[Test]
public void Ignores_overflowing_children () {
taskParent.childCount = 1;

taskParent.AddChild(new TaskExample());
taskParent.AddChild(new TaskExample());

Assert.AreEqual(1, taskParent.Children.Count);
}

[Test]
public void Does_not_add_disabled_children () {
var child = A.TaskStub()
.WithEnabled(false)
.Build();

taskParent.AddChild(child);

Assert.AreEqual(0, taskParent.Children.Count);
}
}
}
}
}
Loading

0 comments on commit 3d8bcf6

Please sign in to comment.