Skip to content

Commit b232079

Browse files
committed
Added ability to splice together BTs
1 parent 36b4208 commit b232079

File tree

2 files changed

+92
-8
lines changed

2 files changed

+92
-8
lines changed

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

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Generic;
2-
using Adnc.FluidBT.TaskParents;
32
using Adnc.FluidBT.TaskParents.Composites;
43
using Adnc.FluidBT.Tasks;
54
using NSubstitute;
@@ -12,7 +11,7 @@ public class TreeTest {
1211
private Tree _tree;
1312

1413
[SetUp]
15-
public void SetBehaviorTree () {
14+
public void BeforeEach () {
1615
_gameObject = new GameObject("BT");
1716
_tree = new Tree(_gameObject);
1817
}
@@ -29,6 +28,70 @@ public void It_should_set_the_root_node_Owner_by_default () {
2928
}
3029
}
3130

31+
public class SpliceMethod : TreeTest {
32+
private GameObject _owner;
33+
private Tree _treeAlt;
34+
35+
[SetUp]
36+
public void SetupTreeAlt () {
37+
_owner = new GameObject("BT");
38+
_treeAlt = new Tree(_owner);
39+
}
40+
41+
[TearDown]
42+
public void TeardownTreeAlt () {
43+
Object.DestroyImmediate(_owner);
44+
}
45+
46+
[Test]
47+
public void It_should_set_the_trees_root_as_a_child () {
48+
_tree.Splice(_tree.Root, _treeAlt);
49+
50+
Assert.AreEqual(_treeAlt.Root, _tree.Root.Children[0]);
51+
}
52+
53+
[Test]
54+
public void It_should_update_all_child_Owner_variables () {
55+
var task = Substitute.For<ITask>();
56+
task.Enabled.Returns(true);
57+
58+
_treeAlt.AddNode(_treeAlt.Root, task);
59+
var nodes = new List<ITask>{_treeAlt.Root, task};
60+
61+
_tree.Splice(_tree.Root, _treeAlt);
62+
63+
nodes.ForEach((node) => Assert.AreEqual(_gameObject, node.Owner));
64+
}
65+
66+
[Test]
67+
public void It_should_update_all_child_Owner_variables_with_nested_parents () {
68+
var task = Substitute.For<ITask>();
69+
task.Enabled.Returns(true);
70+
var sequence = new Sequence();
71+
72+
_treeAlt.AddNode(_treeAlt.Root, sequence);
73+
_treeAlt.AddNode(sequence, task);
74+
var nodes = new List<ITask>{sequence, _treeAlt.Root, task};
75+
76+
_tree.Splice(_tree.Root, _treeAlt);
77+
78+
nodes.ForEach((node) => Assert.AreEqual(_gameObject, node.Owner));
79+
}
80+
81+
[Test]
82+
public void It_should_update_all_child_ParentTree_variables () {
83+
var task = Substitute.For<ITask>();
84+
task.Enabled.Returns(true);
85+
86+
_treeAlt.AddNode(_treeAlt.Root, task);
87+
var nodes = new List<ITask>{_treeAlt.Root, task};
88+
89+
_tree.Splice(_tree.Root, _treeAlt);
90+
91+
nodes.ForEach((node) => Assert.AreEqual(_tree, node.ParentTree));
92+
}
93+
}
94+
3295
public class AddNodeMethod : TreeTest {
3396
[Test]
3497
public void Parent_adds_the_child_reference () {

Assets/FluidBehaviorTree/Scripts/BehaviorTree/Tree.cs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Adnc.FluidBT.TaskParents;
1+
using System.Collections.Generic;
2+
using Adnc.FluidBT.TaskParents;
23
using Adnc.FluidBT.Tasks;
34
using UnityEngine;
45

@@ -12,8 +13,13 @@ public class Tree {
1213

1314
public Tree (GameObject owner) {
1415
_owner = owner;
15-
Root.ParentTree = this;
16-
Root.Owner = owner;
16+
SyncNodes(Root);
17+
}
18+
19+
public void Tick () {
20+
if (Root.Update() != TaskStatus.Continue) {
21+
TickCount++;
22+
}
1723
}
1824

1925
public void Reset () {
@@ -26,9 +32,24 @@ public void AddNode (ITaskParent parent, ITask child) {
2632
child.Owner = _owner;
2733
}
2834

29-
public void Tick () {
30-
if (Root.Update() != TaskStatus.Continue) {
31-
TickCount++;
35+
public void Splice (ITaskParent parent, Tree tree) {
36+
parent.AddChild(tree.Root);
37+
38+
SyncNodes(tree.Root);
39+
}
40+
41+
private void SyncNodes (ITaskParent taskParent) {
42+
taskParent.Owner = _owner;
43+
taskParent.ParentTree = this;
44+
45+
foreach (var child in taskParent.Children) {
46+
child.Owner = _owner;
47+
child.ParentTree = this;
48+
49+
var parent = child as ITaskParent;
50+
if (parent != null) {
51+
SyncNodes(parent);
52+
}
3253
}
3354
}
3455
}

0 commit comments

Comments
 (0)