Skip to content

Commit

Permalink
feat(behavior-tree): runtime trees can now be visually printed
Browse files Browse the repository at this point in the history
  • Loading branch information
ashblue committed Jun 3, 2019
1 parent 6367990 commit 11534c7
Show file tree
Hide file tree
Showing 18 changed files with 194 additions and 2 deletions.
8 changes: 8 additions & 0 deletions Assets/FluidBehaviorTree/Editor.meta

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

8 changes: 8 additions & 0 deletions Assets/FluidBehaviorTree/Editor/BehaviorTree.meta

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

21 changes: 21 additions & 0 deletions Assets/FluidBehaviorTree/Editor/BehaviorTree/BehaviorTreeDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using UnityEditor;
using UnityEngine;

namespace CleverCrow.Fluid.BTs.Trees.Editors {
[CustomPropertyDrawer(typeof(BehaviorTree))]
public class BehaviorTreeDrawer : PropertyDrawer {
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) {
EditorGUI.BeginProperty(position, label, property);

position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
GUI.enabled = Application.isPlaying;
if (GUI.Button(position, "View Tree")) {
var tree = fieldInfo.GetValue(property.serializedObject.targetObject) as IBehaviorTree;
BehaviorTreeWindow.ShowTree(tree, property.displayName);
}
GUI.enabled = true;

EditorGUI.EndProperty();
}
}
}

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

24 changes: 24 additions & 0 deletions Assets/FluidBehaviorTree/Editor/BehaviorTree/BehaviorTreeWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using UnityEditor;
using UnityEngine;

namespace CleverCrow.Fluid.BTs.Trees.Editors {
public class BehaviorTreeWindow : EditorWindow {
private BehaviorTreePrinter _printer;
private string _name;

public static void ShowTree (IBehaviorTree tree, string name) {
var window = GetWindow(typeof(BehaviorTreeWindow)) as BehaviorTreeWindow;
window.SetTree(tree, name);
}

private void SetTree (IBehaviorTree tree, string name) {
_printer = new BehaviorTreePrinter(tree);
_name = name;
}

private void OnGUI () {
GUILayout.Label($"Behavior Tree: {_name}", EditorStyles.boldLabel);
_printer?.Print();
}
}
}

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

3 changes: 3 additions & 0 deletions Assets/FluidBehaviorTree/Editor/BehaviorTree/Printer.meta

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
@@ -0,0 +1,13 @@
namespace CleverCrow.Fluid.BTs.Trees.Editors {
public class BehaviorTreePrinter {
private readonly PrintNode _root;

public BehaviorTreePrinter (IBehaviorTree tree) {
_root = new PrintNode(tree.Root);
}

public void Print () {
_root.Print();
}
}
}

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

39 changes: 39 additions & 0 deletions Assets/FluidBehaviorTree/Editor/BehaviorTree/Printer/PrintNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Collections.Generic;
using CleverCrow.Fluid.BTs.TaskParents;
using CleverCrow.Fluid.BTs.Tasks;
using UnityEngine;
using EditorGUILayout = UnityEditor.EditorGUILayout;

namespace CleverCrow.Fluid.BTs.Trees.Editors {
public class PrintNode {
private readonly ITask _task;
private readonly List<PrintNode> _children = new List<PrintNode>();

public PrintNode (ITask task) {
_task = task;

var parent = task as ITaskParent;
if (parent == null) return;
foreach (var child in parent.Children) {
_children.Add(new PrintNode(child));
}
}

public void Print () {
EditorGUILayout.BeginVertical();

var centeredStyle = GUI.skin.GetStyle("Label");
centeredStyle.alignment = TextAnchor.MiddleCenter;
EditorGUILayout.LabelField(_task.Name, centeredStyle);

EditorGUILayout.BeginHorizontal();
foreach (var child in _children) {
child.Print();
}
EditorGUILayout.EndHorizontal();

EditorGUILayout.EndVertical();

}
}
}

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

12 changes: 12 additions & 0 deletions Assets/FluidBehaviorTree/Editor/Fluid.BehaviorTree.Editor.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "Fluid.BehaviorTree.Editor",
"references": [
"Fluid.BehaviorTree"
],
"optionalUnityReferences": [],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false
}

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 @@ -3,7 +3,12 @@
using UnityEngine;

namespace CleverCrow.Fluid.BTs.Trees {
public class BehaviorTree {
public interface IBehaviorTree {
TaskRoot Root { get; }
}

[System.Serializable]
public class BehaviorTree : IBehaviorTree {
private readonly GameObject _owner;

public int TickCount { get; private set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using CleverCrow.Fluid.BTs.Tasks;
using CleverCrow.Fluid.BTs.Trees;
using UnityEngine;

namespace FluidBehaviorTree.Runtime {
public class BehaviorTreeSerializationTestDeleteMe : MonoBehaviour {
[SerializeField]
private BehaviorTree _tree;

private void Awake () {
_tree = new BehaviorTreeBuilder(gameObject)
.Sequence()
.Condition("Custom Condition", () => true)
.Do("Custom Action", () => TaskStatus.Success)
.Sequence("Nested Sequence")
.Condition("Custom Condition", () => true)
.Do("Custom Action", () => TaskStatus.Success)
.End()
.End()
.Build();
}

private void Update () {
// Update our tree every frame
_tree.Tick();
}
}
}

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 @@ -8,7 +8,7 @@ public abstract class TaskParentBase : ITaskParent {
public BehaviorTree ParentTree { get; set; }
public TaskStatus LastStatus { get; private set; }

public string Name { get; set; }
public virtual string Name { get; set; }
public bool Enabled { get; set; } = true;

public List<ITask> Children { get; } = new List<ITask>();
Expand Down
1 change: 1 addition & 0 deletions Assets/FluidBehaviorTree/Runtime/TaskParents/TaskRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CleverCrow.Fluid.BTs.TaskParents {
public class TaskRoot : TaskParentBase {
public override string Name { get; set; } = "Root";
protected override int MaxChildren { get; } = 1;

protected override TaskStatus OnUpdate () {
Expand Down

0 comments on commit 11534c7

Please sign in to comment.