Skip to content

Commit

Permalink
feat(action): new wait time node
Browse files Browse the repository at this point in the history
New wait time node returns continue until the passed duration has expired
  • Loading branch information
ashblue committed May 31, 2019
1 parent c0eb5c7 commit bc49935
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,28 @@ public BehaviorTreeBuilder Do (string name, Func<TaskStatus> action) {
public BehaviorTreeBuilder Do (Func<TaskStatus> action) {
return Do("action", action);
}

/// <summary>
/// Return continue until time has passed
/// </summary>
/// <param name="name"></param>
/// <param name="time"></param>
/// <returns></returns>
public BehaviorTreeBuilder WaitTime (string name, float time = 1f) {
return AddNode(new WaitTime(new TimeMonitor()) {
Name = name,
time = time
});
}

/// <summary>
/// Return continue until time has passed
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public BehaviorTreeBuilder WaitTime (float time = 1f) {
return WaitTime("Wait Time", time);
}

public BehaviorTreeBuilder Condition (string name, Func<bool> action) {
return AddNode(new ConditionGeneric {
Expand Down
3 changes: 3 additions & 0 deletions Assets/FluidBehaviorTree/Runtime/Tasks/Actions/WaitTime.meta

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,5 @@
namespace CleverCrow.Fluid.BTs.Tasks.Actions {
public interface ITimeMonitor {
float DeltaTime { get; }
}
}

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,7 @@
using UnityEngine;

namespace CleverCrow.Fluid.BTs.Tasks.Actions {
public class TimeMonitor : ITimeMonitor {
public float DeltaTime => Time.deltaTime;
}
}

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,30 @@
namespace CleverCrow.Fluid.BTs.Tasks.Actions {
/// <summary>
/// Return continue until the time has passed
/// </summary>
public class WaitTime : ActionBase {
private ITimeMonitor _timeMonitor;
private float _timePassed;

public float time = 1;


public WaitTime (ITimeMonitor timeMonitor) {
_timeMonitor = timeMonitor;
}

protected override void OnStart () {
_timePassed = 0;
}

protected override TaskStatus OnUpdate () {
_timePassed += _timeMonitor.DeltaTime;

if (_timePassed < time) {
return TaskStatus.Continue;
}

return TaskStatus.Success;
}
}
}

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
Expand Up @@ -16,6 +16,7 @@ public void BeforeEach () {
}

public class SelectorRandomMethod : BehaviorTreeBuilderTest {
[Test]
public void It_should_add_a_random_selector () {
var tree = _builder
.SelectorRandom("random selector")
Expand Down Expand Up @@ -427,5 +428,29 @@ public void It_should_add_a_wait_node () {
Assert.AreEqual(1, _invokeCount);
}
}

public class WaitTimeMethod : BehaviorTreeBuilderTest {
[Test]
public void It_should_add_a_WaitTime_action () {
var tree = _builder
.WaitTime("Custom")
.Build();

var waitTime = tree.Root.Children[0] as WaitTime;

Assert.IsNotNull(waitTime);
}

[Test]
public void It_should_set_WaitTime_duration () {
var tree = _builder
.WaitTime(2f)
.Build();

var waitTime = tree.Root.Children[0] as WaitTime;

Assert.AreEqual(2f, waitTime.time);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using NSubstitute;
using NUnit.Framework;

namespace CleverCrow.Fluid.BTs.Tasks.Actions.Editors.Tests {
public class WaitTimeTest {
public class UpdateMethod {
[Test]
public void It_should_return_continue_if_time_has_not_passed () {
var timeMonitor = Substitute.For<ITimeMonitor>();
timeMonitor.DeltaTime.Returns(0);
var waitTime = new WaitTime(timeMonitor) {time = 1};

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

[Test]
public void It_should_return_success_if_time_has_passed () {
var timeMonitor = Substitute.For<ITimeMonitor>();
timeMonitor.DeltaTime.Returns(2);
var waitTime = new WaitTime(timeMonitor) {time = 1};

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

public class ResetMethod {
[Test]
public void It_should_reset_time () {
var timeMonitor = Substitute.For<ITimeMonitor>();
var waitTime = new WaitTime(timeMonitor) {time = 1};

timeMonitor.DeltaTime.Returns(2);
waitTime.Update();
waitTime.Reset();
timeMonitor.DeltaTime.Returns(0);

Assert.AreEqual(TaskStatus.Continue, waitTime.Update());

}
}
}
}

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

0 comments on commit bc49935

Please sign in to comment.