Skip to content

Commit 484a1e5

Browse files
committed
Merge branch 'feature/random-condition' into develop
2 parents f4d8fbf + 1fc11c4 commit 484a1e5

File tree

6 files changed

+81
-0
lines changed

6 files changed

+81
-0
lines changed

Assets/FluidBehaviorTree/Scripts/BehaviorTree/BehaviorTreeBuilder.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@ public BehaviorTreeBuilder Condition (string name, Func<bool> action) {
9797
public BehaviorTreeBuilder Condition (Func<bool> action) {
9898
return Condition("condition", action);
9999
}
100+
101+
public BehaviorTreeBuilder RandomChance (string name, int chance, int outOf) {
102+
_tree.AddNode(Pointer, new RandomChance {
103+
Name = name,
104+
chance = chance,
105+
outOf = outOf
106+
});
107+
108+
return this;
109+
}
110+
111+
public BehaviorTreeBuilder RandomChance (int chance, int outOf) {
112+
return RandomChance("random chance", chance, outOf);
113+
}
100114

101115
public BehaviorTreeBuilder End () {
102116
_pointer.RemoveAt(_pointer.Count - 1);

Assets/FluidBehaviorTree/Scripts/BehaviorTree/Editor/BehaviorTreeBuilderTest.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,5 +369,27 @@ public void It_should_create_a_ReturnFailure () {
369369
Assert.AreEqual(1, _invokeCount);
370370
}
371371
}
372+
373+
public class RandomChanceMethod : BehaviorTreeBuilderTest {
374+
[Test]
375+
public void It_should_add_a_random_chance () {
376+
var tree = _builder
377+
.Sequence()
378+
.RandomChance("random", 1, 3)
379+
.Do("action", () => {
380+
_invokeCount++;
381+
return TaskStatus.Success;
382+
})
383+
.Build();
384+
385+
var sequence = tree.Root.Children[0] as Sequence;
386+
var condition = sequence.Children[0] as RandomChance;
387+
388+
Assert.AreEqual(sequence.Children.Count, 2);
389+
Assert.IsNotNull(condition);
390+
Assert.AreEqual(TaskStatus.Success, tree.Tick());
391+
Assert.AreEqual(1, _invokeCount);
392+
}
393+
}
372394
}
373395
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Adnc.FluidBT.Tasks;
2+
using NUnit.Framework;
3+
4+
namespace Adnc.FluidBT.Testing {
5+
public class RandomChanceTest {
6+
[Test]
7+
public void It_should_create_a_random_chance () {
8+
var randomChance = new RandomChance {
9+
chance = 1,
10+
outOf = 2
11+
};
12+
13+
Assert.IsNotNull(randomChance.Update());
14+
}
15+
}
16+
}

Assets/FluidBehaviorTree/Scripts/Tasks/Conditions/Editor/RandomChanceTest.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using UnityEngine;
2+
3+
namespace Adnc.FluidBT.Tasks {
4+
public class RandomChance : ConditionBase {
5+
public float chance = 1;
6+
public float outOf = 1;
7+
8+
protected override bool OnUpdate () {
9+
var percentage = chance / outOf;
10+
var rng = Random.value;
11+
12+
return rng <= percentage;
13+
}
14+
}
15+
}

Assets/FluidBehaviorTree/Scripts/Tasks/Conditions/RandomChance.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)