-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
868 additions
and
157 deletions.
There are no files selected for viewing
136 changes: 3 additions & 133 deletions
136
Assets/FluidBehaviorTree/Scripts/BehaviorTree/BehaviorTreeBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,138 +1,8 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Adnc.FluidBT.Decorators; | ||
using Adnc.FluidBT.TaskParents; | ||
using Adnc.FluidBT.TaskParents.Composites; | ||
using Adnc.FluidBT.Tasks; | ||
using Adnc.FluidBT.Tasks.Actions; | ||
using UnityEngine; | ||
using UnityEngine; | ||
|
||
namespace Adnc.FluidBT.Trees { | ||
public class BehaviorTreeBuilder { | ||
private readonly BehaviorTree _tree; | ||
private readonly List<ITaskParent> _pointer = new List<ITaskParent>(); | ||
|
||
private ITaskParent Pointer { | ||
get { | ||
if (_pointer.Count == 0) return null; | ||
return _pointer[_pointer.Count - 1]; | ||
} | ||
} | ||
|
||
public BehaviorTreeBuilder (GameObject owner) { | ||
_tree = new BehaviorTree(owner); | ||
_pointer.Add(_tree.Root); | ||
} | ||
|
||
private BehaviorTreeBuilder ParentTask<T> (string name) where T : ITaskParent, new() { | ||
var parent = new T { Name = name }; | ||
_tree.AddNode(Pointer, parent); | ||
_pointer.Add(parent); | ||
|
||
return this; | ||
} | ||
|
||
public BehaviorTreeBuilder Decorator (string name, Func<ITask, TaskStatus> logic) { | ||
var decorator = new DecoratorGeneric { | ||
updateLogic = logic, | ||
Name = name | ||
}; | ||
|
||
_tree.AddNode(Pointer, decorator); | ||
_pointer.Add(decorator); | ||
|
||
return this; | ||
} | ||
|
||
public BehaviorTreeBuilder Decorator (Func<ITask, TaskStatus> logic) { | ||
return Decorator("decorator", logic); | ||
} | ||
|
||
public BehaviorTreeBuilder Inverter (string name = "inverter") { | ||
return ParentTask<Inverter>(name); | ||
} | ||
|
||
public BehaviorTreeBuilder ReturnSuccess (string name = "return success") { | ||
return ParentTask<ReturnSuccess>(name); | ||
} | ||
|
||
public BehaviorTreeBuilder ReturnFailure (string name = "return failure") { | ||
return ParentTask<ReturnFailure>(name); | ||
} | ||
|
||
public BehaviorTreeBuilder Sequence (string name = "sequence") { | ||
return ParentTask<Sequence>(name); | ||
} | ||
|
||
public BehaviorTreeBuilder Selector (string name = "selector") { | ||
return ParentTask<Selector>(name); | ||
} | ||
|
||
public BehaviorTreeBuilder Parallel (string name = "parallel") { | ||
return ParentTask<Parallel>(name); | ||
} | ||
|
||
public BehaviorTreeBuilder Do (string name, Func<TaskStatus> action) { | ||
_tree.AddNode(Pointer, new ActionGeneric { | ||
Name = name, | ||
updateLogic = action | ||
}); | ||
|
||
return this; | ||
} | ||
|
||
public BehaviorTreeBuilder Do (Func<TaskStatus> action) { | ||
return Do("action", action); | ||
} | ||
|
||
public BehaviorTreeBuilder Condition (string name, Func<bool> action) { | ||
_tree.AddNode(Pointer, new ConditionGeneric { | ||
Name = name, | ||
updateLogic = action | ||
}); | ||
|
||
return this; | ||
} | ||
|
||
public BehaviorTreeBuilder Condition (Func<bool> action) { | ||
return Condition("condition", action); | ||
} | ||
|
||
public BehaviorTreeBuilder RandomChance (string name, int chance, int outOf) { | ||
_tree.AddNode(Pointer, new RandomChance { | ||
Name = name, | ||
chance = chance, | ||
outOf = outOf | ||
}); | ||
|
||
return this; | ||
} | ||
|
||
public BehaviorTreeBuilder RandomChance (int chance, int outOf) { | ||
return RandomChance("random chance", chance, outOf); | ||
} | ||
|
||
public BehaviorTreeBuilder Wait (string name, int turns = 1) { | ||
_tree.AddNode(Pointer, new Wait { | ||
Name = name, | ||
turns = turns | ||
}); | ||
|
||
return this; | ||
} | ||
|
||
public BehaviorTreeBuilder Wait (int turns = 1) { | ||
return Wait("wait", turns); | ||
} | ||
|
||
public BehaviorTreeBuilder End () { | ||
_pointer.RemoveAt(_pointer.Count - 1); | ||
|
||
return this; | ||
} | ||
|
||
public BehaviorTree Build () { | ||
return _tree; | ||
public class BehaviorTreeBuilder : BehaviorTreeBuilderBase<BehaviorTreeBuilder> { | ||
public BehaviorTreeBuilder (GameObject owner) : base(owner) { | ||
} | ||
} | ||
} |
149 changes: 149 additions & 0 deletions
149
Assets/FluidBehaviorTree/Scripts/BehaviorTree/BehaviorTreeBuilderBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Adnc.FluidBT.Decorators; | ||
using Adnc.FluidBT.TaskParents; | ||
using Adnc.FluidBT.TaskParents.Composites; | ||
using Adnc.FluidBT.Tasks; | ||
using Adnc.FluidBT.Tasks.Actions; | ||
using UnityEngine; | ||
|
||
namespace Adnc.FluidBT.Trees { | ||
/// <summary> | ||
/// This class can be extended with your own custom BehaviorTreeBuilder code | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
public abstract class BehaviorTreeBuilderBase<T> where T : BehaviorTreeBuilderBase<T> { | ||
protected readonly BehaviorTree _tree; | ||
protected readonly List<ITaskParent> _pointer = new List<ITaskParent>(); | ||
|
||
protected ITaskParent Pointer { | ||
get { | ||
if (_pointer.Count == 0) return null; | ||
return _pointer[_pointer.Count - 1]; | ||
} | ||
} | ||
|
||
protected BehaviorTreeBuilderBase (GameObject owner) { | ||
_tree = new BehaviorTree(owner); | ||
_pointer.Add(_tree.Root); | ||
} | ||
|
||
protected T ParentTask<P> (string name) where P : ITaskParent, new() { | ||
var parent = new P { Name = name }; | ||
_tree.AddNode(Pointer, parent); | ||
_pointer.Add(parent); | ||
|
||
return (T)this; | ||
} | ||
|
||
public T Decorator (string name, Func<ITask, TaskStatus> logic) { | ||
var decorator = new DecoratorGeneric { | ||
updateLogic = logic, | ||
Name = name | ||
}; | ||
|
||
_tree.AddNode(Pointer, decorator); | ||
_pointer.Add(decorator); | ||
|
||
return (T)this; | ||
} | ||
|
||
public T Decorator (Func<ITask, TaskStatus> logic) { | ||
return Decorator("decorator", logic); | ||
} | ||
|
||
public T Inverter (string name = "inverter") { | ||
return ParentTask<Inverter>(name); | ||
} | ||
|
||
public T ReturnSuccess (string name = "return success") { | ||
return ParentTask<ReturnSuccess>(name); | ||
} | ||
|
||
public T ReturnFailure (string name = "return failure") { | ||
return ParentTask<ReturnFailure>(name); | ||
} | ||
|
||
public T Sequence (string name = "sequence") { | ||
return ParentTask<Sequence>(name); | ||
} | ||
|
||
public T Selector (string name = "selector") { | ||
return ParentTask<Selector>(name); | ||
} | ||
|
||
public T Parallel (string name = "parallel") { | ||
return ParentTask<Parallel>(name); | ||
} | ||
|
||
public T Do (string name, Func<TaskStatus> action) { | ||
_tree.AddNode(Pointer, new ActionGeneric { | ||
Name = name, | ||
updateLogic = action | ||
}); | ||
|
||
return (T)this; | ||
} | ||
|
||
public T Do (Func<TaskStatus> action) { | ||
return Do("action", action); | ||
} | ||
|
||
public T Condition (string name, Func<bool> action) { | ||
_tree.AddNode(Pointer, new ConditionGeneric { | ||
Name = name, | ||
updateLogic = action | ||
}); | ||
|
||
return (T)this; | ||
} | ||
|
||
public T Condition (Func<bool> action) { | ||
return Condition("condition", action); | ||
} | ||
|
||
public T RandomChance (string name, int chance, int outOf, int seed = 0) { | ||
_tree.AddNode(Pointer, new RandomChance { | ||
Name = name, | ||
chance = chance, | ||
outOf = outOf, | ||
seed = seed | ||
}); | ||
|
||
return (T)this; | ||
} | ||
|
||
public T RandomChance (int chance, int outOf, int seed = 0) { | ||
return RandomChance("random chance", chance, outOf, seed); | ||
} | ||
|
||
public T Wait (string name, int turns = 1) { | ||
_tree.AddNode(Pointer, new Wait { | ||
Name = name, | ||
turns = turns | ||
}); | ||
|
||
return (T)this; | ||
} | ||
|
||
public T Wait (int turns = 1) { | ||
return Wait("wait", turns); | ||
} | ||
|
||
public T Splice (BehaviorTree tree) { | ||
_tree.Splice(Pointer, tree); | ||
|
||
return (T)this; | ||
} | ||
|
||
public T End () { | ||
_pointer.RemoveAt(_pointer.Count - 1); | ||
|
||
return (T)this; | ||
} | ||
|
||
public BehaviorTree Build () { | ||
return _tree; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/FluidBehaviorTree/Scripts/BehaviorTree/BehaviorTreeBuilderBase.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 Ash Blue | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
Oops, something went wrong.