Skip to content

Commit

Permalink
Add BehaviorTreeBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
AkiKurisu committed Feb 20, 2024
1 parent 96db8fd commit 991a1ee
Show file tree
Hide file tree
Showing 34 changed files with 1,807 additions and 28 deletions.
52 changes: 52 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- [AkiGroupAttribute](#akigroupattribute)
- [ForceSharedAttribute](#forcesharedattribute)
- [WrapFieldAttribute](#wrapfieldattribute)
- [Runtime Build BehaviorTree](#runtime-build-behaviortree)
- [SharedVariable](#sharedvariable)
- [How to use](#how-to-use)
- [API Reference](#api-reference)
Expand Down Expand Up @@ -328,6 +329,57 @@ public class SetFloat : Action
}
```

## Runtime Build BehaviorTree

Use `BehaviorTreeBuilder` to build a behaviorTree on the builder pattern.

Use `BeginChild` to start writing child or children.

Use `EndChild` to end writing.

Example:
```C#
var builder = new BehaviorTreeBuilder(gameObject);
//Create and set value of local variable
builder.NewObject<NavMeshAgent>("NavAgent",navmeshAgent);
//Create and bind global variable
builder.NewFloat("Distance").IsGlobal = true;
bool success = builder.BeginChild(new Sequence() { abortOnConditionChanged = true })
.Append(new FloatComparison()
{
operation = FloatComparison.Operation.GreaterThan,
float1 = builder.NewFloat("Distance"),
float2 = new(4f)
})
.BeginChild()
.BeginChild(new Sequence())
.Append(new NavmeshSetDestination()
{
agent = builder.NewObject<NavMeshAgent>("NavAgent"),
destination = builder.NewVector3("EnemyPosition")
})
.Append(new SetBool()
{
boolValue = new(true),
storeResult = builder.NewBool("IsFollowing")
})
.EndChild()
.EndChild()
.BeginChild(new Sequence())
.Append(new SetBool()
{
boolValue = new(false),
storeResult = builder.NewBool("IsFollowing")
})
.EndChild(new NavmeshStopAgent()
{
agent = builder.NewObject<UnityEngine.AI.NavMeshAgent>("NavAgent"),
isStopped = new(true)
})
.EndChild()
.Build(out BehaviorTree behaviorTree);
```

## SharedVariable

### How to use
Expand Down
2 changes: 1 addition & 1 deletion Runtime/BuiltIn/Composite/Rotator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Kurisu.AkiBT
{
[AkiInfo("Composite : Rotator, update child nodes in order, each Update will only update the current node" +
[AkiInfo("Composite: Rotator, update child nodes in order, each Update will only update the current node" +
", after the node finishes running, the next Update will continue to update the next node")]
public class Rotator : Composite
{
Expand Down
2 changes: 1 addition & 1 deletion Runtime/BuiltIn/Composite/Selector.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using UnityEngine;
namespace Kurisu.AkiBT
{
[AkiInfo("Composite : Select, traverse the child nodes in turn," +
[AkiInfo("Composite: Select, traverse the child nodes in turn," +
" if it returns Failure, continue to update the next one, otherwise return Success")]
public class Selector : Composite
{
Expand Down
2 changes: 1 addition & 1 deletion Runtime/BuiltIn/Composite/Sequence.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using UnityEngine;
namespace Kurisu.AkiBT
{
[AkiInfo("Composite : Sequence, traversing the child nodes in turn, if it returns Success" +
[AkiInfo("Composite: Sequence, traversing the child nodes in turn, if it returns Success" +
", continue to update the next one, otherwise it returns Failure")]
public class Sequence : Composite
{
Expand Down
2 changes: 1 addition & 1 deletion Runtime/BuiltIn/Composite/WeightedRandom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using UnityEngine;
namespace Kurisu.AkiBT
{
[AkiInfo("Composite : Weighted random, randomly selected according to the weight" +
[AkiInfo("Composite: Weighted random, randomly selected according to the weight" +
", wait for the node to finish running and reselect the next node")]
public class WeightedRandom : Composite
{
Expand Down
42 changes: 41 additions & 1 deletion Runtime/Core/BehaviorTreeExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,31 @@ public static SharedVariable<string> GetSharedString(this IVariableSource variab
{
return variableScope.GetSharedVariable(variableName) as SharedVariable<string>;
}
public static SharedVariable<T> GetSharedObject<T>(this IVariableSource variableScope, string variableName) where T : Object
{
return variableScope.GetSharedVariable(variableName) as SharedVariable<T>;
}
public static SharedVariable<Object> GetSharedObject(this IVariableSource variableScope, string variableName)
{
return variableScope.GetSharedVariable(variableName) as SharedVariable<Object>;
}
public static T GetValue<T>(this IVariableSource variableScope, string variableName) where T : unmanaged
{
var variable = variableScope.GetSharedVariable(variableName);
if (variable is SharedVariable<T> tVariable) return tVariable.Value;
return default;
}
public static T GetObject<T>(this IVariableSource variableScope, string variableName) where T : Object
{
var variable = variableScope.GetSharedVariable(variableName);
if (variable is SharedVariable<T> tVariable) return tVariable.Value;
if (variable is SharedObject sharedObject) return sharedObject.Value as T;
if (variable is SharedVariable<Object> sharedObject) return sharedObject.Value as T;
return null;
}
public static string GetString(this IVariableSource variableScope, string variableName)
{
var variable = variableScope.GetSharedVariable(variableName);
if (variable is SharedVariable<string> sVariable) return sVariable.Value;
return null;
}
/// <summary>
Expand Down Expand Up @@ -83,6 +103,26 @@ public static bool TryGetSharedString(this IVariableSource variableScope, string
sharedTVariable = null;
return false;
}
public static bool TryGetSharedObject(this IVariableSource variableScope, string variableName, out SharedVariable<Object> sharedObject)
{
if (variableScope.TryGetSharedVariable(variableName, out SharedVariable sharedVariable))
{
sharedObject = sharedVariable as SharedVariable<Object>;
return sharedObject != null;
}
sharedObject = null;
return false;
}
public static bool TryGetSharedObject<T>(this IVariableSource variableScope, string variableName, out SharedVariable<T> sharedTObject) where T : Object
{
if (variableScope.TryGetSharedVariable(variableName, out SharedVariable sharedVariable))
{
sharedTObject = sharedVariable as SharedVariable<T>;
return sharedTObject != null;
}
sharedTObject = null;
return false;
}
/// <summary>
/// Map variable to global variables
/// </summary>
Expand Down
Loading

0 comments on commit 991a1ee

Please sign in to comment.