Skip to content

Commit 3d8bcf6

Browse files
committed
fix(repeaters): now handle composites and task hooks as expected
Repeater commands were not triggering tasks as expected and blowing out composites. Lacked a proper reset when the repeater was continunally looping.
1 parent b1b099b commit 3d8bcf6

File tree

11 files changed

+220
-77
lines changed

11 files changed

+220
-77
lines changed

Assets/FluidBehaviorTree/Runtime/Decorators/RepeatForever.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using CleverCrow.Fluid.BTs.Trees;
2-
using CleverCrow.Fluid.BTs.Decorators;
31
using CleverCrow.Fluid.BTs.Tasks;
42

53
namespace CleverCrow.Fluid.BTs.Decorators {
@@ -11,4 +9,4 @@ protected override TaskStatus OnUpdate () {
119
return TaskStatus.Continue;
1210
}
1311
}
14-
}
12+
}
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using CleverCrow.Fluid.BTs.Trees;
2-
using CleverCrow.Fluid.BTs.Decorators;
31
using CleverCrow.Fluid.BTs.Tasks;
42

53
namespace CleverCrow.Fluid.BTs.Decorators {
@@ -10,8 +8,8 @@ protected override TaskStatus OnUpdate () {
108
if (Child.Update() == TaskStatus.Failure) {
119
return TaskStatus.Failure;
1210
}
13-
11+
1412
return TaskStatus.Continue;
1513
}
1614
}
17-
}
15+
}

Assets/FluidBehaviorTree/Runtime/TaskParents/TaskParentBase.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public override TaskStatus Update () {
2525

2626
var status = OnUpdate();
2727
LastStatus = status;
28+
if (status != TaskStatus.Continue) {
29+
Reset();
30+
}
2831

2932
return status;
3033
}
@@ -33,7 +36,7 @@ private void UpdateTicks () {
3336
if (ParentTree == null) {
3437
return;
3538
}
36-
39+
3740
if (_lastTickCount != ParentTree.TickCount) {
3841
Reset();
3942
}
@@ -56,12 +59,12 @@ public virtual ITaskParent AddChild (ITask child) {
5659
if (!child.Enabled) {
5760
return this;
5861
}
59-
62+
6063
if (Children.Count < MaxChildren || MaxChildren < 0) {
6164
Children.Add(child);
6265
}
6366

6467
return this;
6568
}
6669
}
67-
}
70+
}

Assets/FluidBehaviorTree/Runtime/Tasks/TaskBase.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public override TaskStatus Update () {
2626
Init();
2727
_init = true;
2828
}
29-
29+
3030
if (!_start) {
3131
Start();
3232
_start = true;
@@ -51,7 +51,7 @@ private void UpdateTicks () {
5151
if (ParentTree == null) {
5252
return;
5353
}
54-
54+
5555
if (_lastTickCount != ParentTree.TickCount) {
5656
Reset();
5757
}
@@ -85,7 +85,7 @@ private void Init () {
8585
/// </summary>
8686
protected virtual void OnInit () {
8787
}
88-
88+
8989
private void Start () {
9090
OnStart();
9191
}
@@ -101,7 +101,7 @@ private void Exit () {
101101
OnExit();
102102
}
103103

104-
_exit = false;
104+
Reset();
105105
}
106106

107107
/// <summary>
@@ -110,4 +110,4 @@ private void Exit () {
110110
protected virtual void OnExit () {
111111
}
112112
}
113-
}
113+
}

Assets/FluidBehaviorTree/Tests/Editor/Decorators/RepeatForeverTest.cs

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,48 @@
55
namespace CleverCrow.Fluid.BTs.Testing {
66
public class RepeatForeverTest {
77
public class UpdateMethod {
8-
private RepeatForever repeater;
8+
private RepeatForever Setup (ITask child) {
9+
var repeat = new RepeatForever();
10+
repeat.AddChild(child);
911

10-
[SetUp]
11-
public void Setup_repeater () {
12-
repeater = new RepeatForever();
12+
return repeat;
1313
}
1414

15-
[Test]
16-
public void Returns_continue_on_child_failure () {
17-
repeater.AddChild(A.TaskStub().WithUpdateStatus(TaskStatus.Failure).Build());
15+
public class WhenChildReturnsFailure : UpdateMethod {
16+
[Test]
17+
public void It_should_return_continue () {
18+
var stub = A.TaskStub().WithUpdateStatus(TaskStatus.Failure).Build();
1819

19-
Assert.AreEqual(TaskStatus.Continue, repeater.Update());
20+
var repeater = Setup(stub);
21+
var status = repeater.Update();
22+
23+
Assert.AreEqual(TaskStatus.Continue, status);
24+
}
2025
}
2126

22-
[Test]
23-
public void Returns_continue_on_child_success () {
24-
repeater.AddChild(A.TaskStub().WithUpdateStatus(TaskStatus.Success).Build());
27+
public class WhenChildReturnsSuccess : UpdateMethod {
28+
[Test]
29+
public void It_should_return_continue () {
30+
var stub = A.TaskStub().WithUpdateStatus(TaskStatus.Success).Build();
31+
32+
var repeater = Setup(stub);
33+
var status = repeater.Update();
2534

26-
Assert.AreEqual(TaskStatus.Continue, repeater.Update());
35+
Assert.AreEqual(TaskStatus.Continue, status);
36+
}
2737
}
2838

29-
[Test]
30-
public void Returns_continue_on_child_continue () {
31-
repeater.AddChild(A.TaskStub().WithUpdateStatus(TaskStatus.Continue).Build());
39+
public class WhenChildReturnsContinue : UpdateMethod {
40+
[Test]
41+
public void It_should_return_continue () {
42+
var stub = A.TaskStub().WithUpdateStatus(TaskStatus.Continue).Build();
43+
44+
var repeater = Setup(stub);
45+
var status = repeater.Update();
3246

33-
Assert.AreEqual(TaskStatus.Continue, repeater.Update());
47+
Assert.AreEqual(TaskStatus.Continue, status);
48+
}
3449
}
3550
}
3651
}
37-
}
52+
}

Assets/FluidBehaviorTree/Tests/Editor/TaskParents/Composites/TaskSequenceTest.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using CleverCrow.Fluid.BTs.TaskParents.Composites;
22
using CleverCrow.Fluid.BTs.Tasks;
3-
using CleverCrow.Fluid.BTs.TaskParents;
43
using NSubstitute;
5-
using NSubstitute.ReturnsExtensions;
64
using NUnit.Framework;
75

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

40-
[Test]
41-
public void It_should_not_run_update_on_any_child_tasks_after_success () {
42-
_sequence.Update();
43-
_sequence.Update();
44-
45-
_sequence.Children.ForEach((c) => c.Received(1).Update());
46-
}
47-
4838
[Test]
4939
public void It_should_retick_a_continue_node_when_update_is_rerun () {
5040
_childB.Update().Returns(TaskStatus.Continue);

Assets/FluidBehaviorTree/Tests/Editor/TaskParents/TaskParentTest.cs

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,18 @@ public void SetTaskParent () {
1515

1616
public class TaskParentExample : TaskParentBase {
1717
public int childCount = -1;
18+
public int resetCount = 0;
19+
public TaskStatus status = TaskStatus.Success;
1820

1921
protected override int MaxChildren => childCount;
22+
23+
protected override TaskStatus OnUpdate () {
24+
return status;
25+
}
26+
27+
public override void Reset () {
28+
resetCount += 1;
29+
}
2030
}
2131

2232
public class TaskExample : ActionBase {
@@ -25,19 +35,48 @@ protected override TaskStatus OnUpdate () {
2535
}
2636
}
2737

38+
public class TriggeringReset : TaskParentTest {
39+
[Test]
40+
public void It_should_trigger_Reset_on_success () {
41+
taskParent.status = TaskStatus.Success;
42+
43+
taskParent.Update();
44+
45+
Assert.AreEqual(1, taskParent.resetCount);
46+
}
47+
48+
[Test]
49+
public void It_should_trigger_Reset_on_failure () {
50+
taskParent.status = TaskStatus.Failure;
51+
52+
taskParent.Update();
53+
54+
Assert.AreEqual(1, taskParent.resetCount);
55+
}
56+
57+
[Test]
58+
public void It_should_not_trigger_Reset_on_continue () {
59+
taskParent.status = TaskStatus.Continue;
60+
61+
taskParent.Update();
62+
63+
Assert.AreEqual(0, taskParent.resetCount);
64+
}
65+
}
66+
2867
public class EnabledProperty : TaskParentTest {
2968
[Test]
3069
public void Returns_enabled_if_child () {
3170
taskParent.AddChild(A.TaskStub().Build());
32-
71+
3372
Assert.IsTrue(taskParent.Enabled);
3473
}
35-
74+
3675
[Test]
3776
public void Returns_disabled_if_child_and_set_to_disabled () {
3877
taskParent.AddChild(A.TaskStub().Build());
3978
taskParent.Enabled = false;
40-
79+
4180
Assert.IsFalse(taskParent.Enabled);
4281
}
4382
}
@@ -46,38 +85,38 @@ public class AddChildMethod : TaskParentTest {
4685
[Test]
4786
public void Adds_a_child () {
4887
taskParent.AddChild(new TaskExample());
49-
88+
5089
Assert.AreEqual(1, taskParent.Children.Count);
5190
}
52-
91+
5392
[Test]
5493
public void Adds_two_children () {
5594
taskParent.AddChild(new TaskExample());
5695
taskParent.AddChild(new TaskExample());
5796

5897
Assert.AreEqual(2, taskParent.Children.Count);
5998
}
60-
99+
61100
[Test]
62101
public void Ignores_overflowing_children () {
63102
taskParent.childCount = 1;
64-
103+
65104
taskParent.AddChild(new TaskExample());
66105
taskParent.AddChild(new TaskExample());
67106

68107
Assert.AreEqual(1, taskParent.Children.Count);
69108
}
70-
109+
71110
[Test]
72111
public void Does_not_add_disabled_children () {
73112
var child = A.TaskStub()
74113
.WithEnabled(false)
75114
.Build();
76-
115+
77116
taskParent.AddChild(child);
78-
117+
79118
Assert.AreEqual(0, taskParent.Children.Count);
80119
}
81120
}
82121
}
83-
}
122+
}

0 commit comments

Comments
 (0)