Skip to content

Commit

Permalink
Merge branch 'release/1.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
ashblue committed Aug 31, 2018
2 parents 8015af8 + 3497dc3 commit 6e02c1a
Show file tree
Hide file tree
Showing 9 changed files with 868 additions and 157 deletions.
136 changes: 3 additions & 133 deletions Assets/FluidBehaviorTree/Scripts/BehaviorTree/BehaviorTreeBuilder.cs
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) {
}
}
}
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;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@ namespace Adnc.FluidBT.Tasks {
public class RandomChance : ConditionBase {
public float chance = 1;
public float outOf = 1;

public int seed;

protected override bool OnUpdate () {
var oldState = Random.state;

if (seed != 0) {
Random.InitState(seed);
}

var percentage = chance / outOf;
var rng = Random.value;

if (seed != 0) {
Random.state = oldState;
}

return rng <= percentage;
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/FluidBehaviorTree/Scripts/Tasks/TaskBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private void Init () {
}

/// <summary>
/// Run the first time this node is run or after a hard reset
/// Triggers the first time this node is run or after a hard reset
/// </summary>
protected virtual void OnInit () {
}
Expand Down
21 changes: 21 additions & 0 deletions LICENSE
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.
Loading

0 comments on commit 6e02c1a

Please sign in to comment.