Skip to content

Commit 6e02c1a

Browse files
committed
Merge branch 'release/1.1'
2 parents 8015af8 + 3497dc3 commit 6e02c1a

File tree

9 files changed

+868
-157
lines changed

9 files changed

+868
-157
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,8 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using Adnc.FluidBT.Decorators;
4-
using Adnc.FluidBT.TaskParents;
5-
using Adnc.FluidBT.TaskParents.Composites;
6-
using Adnc.FluidBT.Tasks;
7-
using Adnc.FluidBT.Tasks.Actions;
8-
using UnityEngine;
1+
using UnityEngine;
92

103
namespace Adnc.FluidBT.Trees {
11-
public class BehaviorTreeBuilder {
12-
private readonly BehaviorTree _tree;
13-
private readonly List<ITaskParent> _pointer = new List<ITaskParent>();
14-
15-
private ITaskParent Pointer {
16-
get {
17-
if (_pointer.Count == 0) return null;
18-
return _pointer[_pointer.Count - 1];
19-
}
20-
}
21-
22-
public BehaviorTreeBuilder (GameObject owner) {
23-
_tree = new BehaviorTree(owner);
24-
_pointer.Add(_tree.Root);
25-
}
26-
27-
private BehaviorTreeBuilder ParentTask<T> (string name) where T : ITaskParent, new() {
28-
var parent = new T { Name = name };
29-
_tree.AddNode(Pointer, parent);
30-
_pointer.Add(parent);
31-
32-
return this;
33-
}
34-
35-
public BehaviorTreeBuilder Decorator (string name, Func<ITask, TaskStatus> logic) {
36-
var decorator = new DecoratorGeneric {
37-
updateLogic = logic,
38-
Name = name
39-
};
40-
41-
_tree.AddNode(Pointer, decorator);
42-
_pointer.Add(decorator);
43-
44-
return this;
45-
}
46-
47-
public BehaviorTreeBuilder Decorator (Func<ITask, TaskStatus> logic) {
48-
return Decorator("decorator", logic);
49-
}
50-
51-
public BehaviorTreeBuilder Inverter (string name = "inverter") {
52-
return ParentTask<Inverter>(name);
53-
}
54-
55-
public BehaviorTreeBuilder ReturnSuccess (string name = "return success") {
56-
return ParentTask<ReturnSuccess>(name);
57-
}
58-
59-
public BehaviorTreeBuilder ReturnFailure (string name = "return failure") {
60-
return ParentTask<ReturnFailure>(name);
61-
}
62-
63-
public BehaviorTreeBuilder Sequence (string name = "sequence") {
64-
return ParentTask<Sequence>(name);
65-
}
66-
67-
public BehaviorTreeBuilder Selector (string name = "selector") {
68-
return ParentTask<Selector>(name);
69-
}
70-
71-
public BehaviorTreeBuilder Parallel (string name = "parallel") {
72-
return ParentTask<Parallel>(name);
73-
}
74-
75-
public BehaviorTreeBuilder Do (string name, Func<TaskStatus> action) {
76-
_tree.AddNode(Pointer, new ActionGeneric {
77-
Name = name,
78-
updateLogic = action
79-
});
80-
81-
return this;
82-
}
83-
84-
public BehaviorTreeBuilder Do (Func<TaskStatus> action) {
85-
return Do("action", action);
86-
}
87-
88-
public BehaviorTreeBuilder Condition (string name, Func<bool> action) {
89-
_tree.AddNode(Pointer, new ConditionGeneric {
90-
Name = name,
91-
updateLogic = action
92-
});
93-
94-
return this;
95-
}
96-
97-
public BehaviorTreeBuilder Condition (Func<bool> action) {
98-
return Condition("condition", action);
99-
}
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-
}
114-
115-
public BehaviorTreeBuilder Wait (string name, int turns = 1) {
116-
_tree.AddNode(Pointer, new Wait {
117-
Name = name,
118-
turns = turns
119-
});
120-
121-
return this;
122-
}
123-
124-
public BehaviorTreeBuilder Wait (int turns = 1) {
125-
return Wait("wait", turns);
126-
}
127-
128-
public BehaviorTreeBuilder End () {
129-
_pointer.RemoveAt(_pointer.Count - 1);
130-
131-
return this;
132-
}
133-
134-
public BehaviorTree Build () {
135-
return _tree;
4+
public class BehaviorTreeBuilder : BehaviorTreeBuilderBase<BehaviorTreeBuilder> {
5+
public BehaviorTreeBuilder (GameObject owner) : base(owner) {
1366
}
1377
}
1388
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Adnc.FluidBT.Decorators;
4+
using Adnc.FluidBT.TaskParents;
5+
using Adnc.FluidBT.TaskParents.Composites;
6+
using Adnc.FluidBT.Tasks;
7+
using Adnc.FluidBT.Tasks.Actions;
8+
using UnityEngine;
9+
10+
namespace Adnc.FluidBT.Trees {
11+
/// <summary>
12+
/// This class can be extended with your own custom BehaviorTreeBuilder code
13+
/// </summary>
14+
/// <typeparam name="T"></typeparam>
15+
public abstract class BehaviorTreeBuilderBase<T> where T : BehaviorTreeBuilderBase<T> {
16+
protected readonly BehaviorTree _tree;
17+
protected readonly List<ITaskParent> _pointer = new List<ITaskParent>();
18+
19+
protected ITaskParent Pointer {
20+
get {
21+
if (_pointer.Count == 0) return null;
22+
return _pointer[_pointer.Count - 1];
23+
}
24+
}
25+
26+
protected BehaviorTreeBuilderBase (GameObject owner) {
27+
_tree = new BehaviorTree(owner);
28+
_pointer.Add(_tree.Root);
29+
}
30+
31+
protected T ParentTask<P> (string name) where P : ITaskParent, new() {
32+
var parent = new P { Name = name };
33+
_tree.AddNode(Pointer, parent);
34+
_pointer.Add(parent);
35+
36+
return (T)this;
37+
}
38+
39+
public T Decorator (string name, Func<ITask, TaskStatus> logic) {
40+
var decorator = new DecoratorGeneric {
41+
updateLogic = logic,
42+
Name = name
43+
};
44+
45+
_tree.AddNode(Pointer, decorator);
46+
_pointer.Add(decorator);
47+
48+
return (T)this;
49+
}
50+
51+
public T Decorator (Func<ITask, TaskStatus> logic) {
52+
return Decorator("decorator", logic);
53+
}
54+
55+
public T Inverter (string name = "inverter") {
56+
return ParentTask<Inverter>(name);
57+
}
58+
59+
public T ReturnSuccess (string name = "return success") {
60+
return ParentTask<ReturnSuccess>(name);
61+
}
62+
63+
public T ReturnFailure (string name = "return failure") {
64+
return ParentTask<ReturnFailure>(name);
65+
}
66+
67+
public T Sequence (string name = "sequence") {
68+
return ParentTask<Sequence>(name);
69+
}
70+
71+
public T Selector (string name = "selector") {
72+
return ParentTask<Selector>(name);
73+
}
74+
75+
public T Parallel (string name = "parallel") {
76+
return ParentTask<Parallel>(name);
77+
}
78+
79+
public T Do (string name, Func<TaskStatus> action) {
80+
_tree.AddNode(Pointer, new ActionGeneric {
81+
Name = name,
82+
updateLogic = action
83+
});
84+
85+
return (T)this;
86+
}
87+
88+
public T Do (Func<TaskStatus> action) {
89+
return Do("action", action);
90+
}
91+
92+
public T Condition (string name, Func<bool> action) {
93+
_tree.AddNode(Pointer, new ConditionGeneric {
94+
Name = name,
95+
updateLogic = action
96+
});
97+
98+
return (T)this;
99+
}
100+
101+
public T Condition (Func<bool> action) {
102+
return Condition("condition", action);
103+
}
104+
105+
public T RandomChance (string name, int chance, int outOf, int seed = 0) {
106+
_tree.AddNode(Pointer, new RandomChance {
107+
Name = name,
108+
chance = chance,
109+
outOf = outOf,
110+
seed = seed
111+
});
112+
113+
return (T)this;
114+
}
115+
116+
public T RandomChance (int chance, int outOf, int seed = 0) {
117+
return RandomChance("random chance", chance, outOf, seed);
118+
}
119+
120+
public T Wait (string name, int turns = 1) {
121+
_tree.AddNode(Pointer, new Wait {
122+
Name = name,
123+
turns = turns
124+
});
125+
126+
return (T)this;
127+
}
128+
129+
public T Wait (int turns = 1) {
130+
return Wait("wait", turns);
131+
}
132+
133+
public T Splice (BehaviorTree tree) {
134+
_tree.Splice(Pointer, tree);
135+
136+
return (T)this;
137+
}
138+
139+
public T End () {
140+
_pointer.RemoveAt(_pointer.Count - 1);
141+
142+
return (T)this;
143+
}
144+
145+
public BehaviorTree Build () {
146+
return _tree;
147+
}
148+
}
149+
}

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

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,21 @@ namespace Adnc.FluidBT.Tasks {
44
public class RandomChance : ConditionBase {
55
public float chance = 1;
66
public float outOf = 1;
7-
7+
public int seed;
8+
89
protected override bool OnUpdate () {
10+
var oldState = Random.state;
11+
12+
if (seed != 0) {
13+
Random.InitState(seed);
14+
}
15+
916
var percentage = chance / outOf;
1017
var rng = Random.value;
18+
19+
if (seed != 0) {
20+
Random.state = oldState;
21+
}
1122

1223
return rng <= percentage;
1324
}

Assets/FluidBehaviorTree/Scripts/Tasks/TaskBase.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ private void Init () {
7878
}
7979

8080
/// <summary>
81-
/// Run the first time this node is run or after a hard reset
81+
/// Triggers the first time this node is run or after a hard reset
8282
/// </summary>
8383
protected virtual void OnInit () {
8484
}

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Ash Blue
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

0 commit comments

Comments
 (0)