Skip to content

Commit 260d694

Browse files
committed
Merge branch 'feature/behavior-tree' into develop
2 parents 72b556e + d9d6c59 commit 260d694

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+420
-284
lines changed

Assets/FluidBehaviorTree/Editor/Builders/TaskStubBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public TaskStubBuilder WithUpdateStatus (TaskStatus status) {
2222
public ITask Build () {
2323
var task = Substitute.For<ITask>();
2424
task.Enabled.Returns(_enabled);
25-
task.Update().Returns(_status);
25+
task.Update().ReturnsForAnyArgs(_status);
2626

2727
return task;
2828
}
Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,55 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using Adnc.FluidBT.TaskParents;
1+
using Adnc.FluidBT.TaskParents;
42
using Adnc.FluidBT.Tasks;
3+
using UnityEngine;
54

65
namespace Adnc.FluidBT.Trees {
76
public class BehaviorTree {
8-
private bool _setup;
9-
7+
private readonly GameObject _owner;
8+
9+
public int TickCount { get; private set; }
10+
1011
public TaskRoot Root { get; } = new TaskRoot();
1112

12-
public ITask Current { get; set; }
13-
14-
public readonly HashSet<object> nodes = new HashSet<object>();
15-
16-
public readonly List<IEventAwake> nodeAwake = new List<IEventAwake>();
17-
18-
public BehaviorTree () {
19-
Current = Root;
20-
nodes.Add(Root);
13+
public BehaviorTree (GameObject owner) {
14+
_owner = owner;
15+
SyncNodes(Root);
2116
}
22-
23-
public void AddNode (ITaskParent parent, ITask child) {
24-
if (parent == null) {
25-
throw new ArgumentNullException(nameof(parent));
26-
}
27-
28-
if (child == null) {
29-
throw new ArgumentNullException(nameof(child));
30-
}
31-
32-
if (!nodes.Contains(parent)) {
33-
throw new ArgumentException("Cannot add a node to a parent that is not in the BT");
34-
17+
18+
public void Tick () {
19+
if (Root.Update() != TaskStatus.Continue) {
20+
TickCount++;
3521
}
22+
}
3623

37-
if (nodes.Contains(child)) {
38-
throw new ArgumentException("Cannot set a child node that has already been added");
39-
}
24+
public void Reset () {
25+
TickCount++;
26+
}
4027

28+
public void AddNode (ITaskParent parent, ITask child) {
4129
parent.AddChild(child);
42-
nodes.Add(child);
43-
child.Owner = this;
44-
45-
var item = child as IEventAwake;
46-
if (item != null) {
47-
nodeAwake.Add(item);
48-
}
30+
child.ParentTree = this;
31+
child.Owner = _owner;
4932
}
5033

51-
public void Setup () {
52-
if (!_setup) {
53-
_setup = true;
54-
} else {
55-
return;
56-
}
34+
public void Splice (ITaskParent parent, BehaviorTree tree) {
35+
parent.AddChild(tree.Root);
5736

58-
nodeAwake.ForEach((n) => {
59-
n.Awake();
60-
});
37+
SyncNodes(tree.Root);
6138
}
6239

63-
public void Update () {
64-
Current.Update();
40+
private void SyncNodes (ITaskParent taskParent) {
41+
taskParent.Owner = _owner;
42+
taskParent.ParentTree = this;
43+
44+
foreach (var child in taskParent.Children) {
45+
child.Owner = _owner;
46+
child.ParentTree = this;
47+
48+
var parent = child as ITaskParent;
49+
if (parent != null) {
50+
SyncNodes(parent);
51+
}
52+
}
6553
}
6654
}
6755
}

Assets/FluidBehaviorTree/Scripts/BehaviorTree/BehaviorTree.cs.meta

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

0 commit comments

Comments
 (0)