Skip to content

Commit

Permalink
fix(behavior-tree): recursive global position changes don't double up
Browse files Browse the repository at this point in the history
  • Loading branch information
ashblue committed Jun 6, 2019
1 parent e72cc54 commit e762e8e
Show file tree
Hide file tree
Showing 23 changed files with 389 additions and 347 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ public class BehaviorTreePrinter {
private readonly VisualTask _root;

public BehaviorTreePrinter (IBehaviorTree tree) {
var container = new TreeContainerVertical();
var container = new GraphContainerVertical();
// @TODO Temporary start position for debugging, replace with a proper scroll view
container.SetGlobalPosition(300, 0);
_root = new VisualTask(tree.Root, container);
Expand Down

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,27 @@
namespace CleverCrow.Fluid.BTs.Trees.Editors {
public class GraphBox : IGraphBox {
public float LocalPositionX { get; private set; }
public float LocalPositionY { get; private set; }

public float GlobalPositionX { get; private set; }
public float GlobalPositionY { get; private set; }

public float Width { get; private set; }
public float Height { get; private set; }

public void SetSize (float width, float height) {
Width = width;
Height = height;
}

public void SetLocalPosition (float x, float y) {
LocalPositionX = x;
LocalPositionY = y;
}

public void AddGlobalPosition (float x, float y) {
GlobalPositionX += x;
GlobalPositionY += y;
}
}
}

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,56 @@
using System.Collections.Generic;

namespace CleverCrow.Fluid.BTs.Trees.Editors {
public class GraphContainerHorizontal : IGraphContainer {
protected readonly List<IGraphBox> _childContainers = new List<IGraphBox>();

public float LocalPositionX { get; private set; }
public float LocalPositionY { get; private set; }
public float GlobalPositionX { get; private set; }
public float GlobalPositionY { get; private set; }
public float Width { get; protected set; }
public float Height { get; protected set; }
public List<IGraphBox> ChildContainers => _childContainers;

public void SetLocalPosition (float x, float y) {
LocalPositionX = x;
LocalPositionY = y;
}

public virtual void AddBox (IGraphBox child) {
CalculateChild(child);
_childContainers.Add(child);
}

private void CalculateChild (IGraphBox child) {
child.SetLocalPosition(Width, 0);
child.AddGlobalPosition(GlobalPositionX + child.LocalPositionX, GlobalPositionY + child.LocalPositionY);

Width += child.Width;
if (child.Height > Height) Height = child.Height;
}

public void SetSize (float width, float height) {
throw new System.NotImplementedException();
}

public void SetGlobalPosition (float x, float y) {
GlobalPositionX = x;
GlobalPositionY = y;
}

public void AddGlobalPosition (float x, float y) {
GlobalPositionX += x;
GlobalPositionY += y;

foreach (var child in ChildContainers) {
child.AddGlobalPosition(x, y);
}
}

public override string ToString () {
return
$"Size: {Width}, {Height}; Local: {LocalPositionX}, {LocalPositionY}; Global: {GlobalPositionX}, {GlobalPositionY};";
}
}
}

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,16 @@
namespace CleverCrow.Fluid.BTs.Trees.Editors {
public class GraphContainerVertical : GraphContainerHorizontal {
public override void AddBox (IGraphBox child) {
CalculateChild(child);
_childContainers.Add(child);
}

private void CalculateChild (IGraphBox child) {
child.SetLocalPosition(0, Height);
child.AddGlobalPosition(GlobalPositionX + child.LocalPositionX, GlobalPositionY + child.LocalPositionY);

Height += child.Height;
if (child.Width > Width) Width = child.Width;
}
}
}

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,15 @@
namespace CleverCrow.Fluid.BTs.Trees.Editors {
public interface IGraphBox {
float LocalPositionX { get; }
float LocalPositionY { get; }
float GlobalPositionX { get; }
float GlobalPositionY { get; }

float Width { get; }
float Height { get; }

void SetSize (float width, float height);
void SetLocalPosition (float x, float y);
void AddGlobalPosition (float x, float y);
}
}

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,5 @@
namespace CleverCrow.Fluid.BTs.Trees.Editors {
public interface IGraphContainer : IGraphBox {
void AddBox (IGraphBox container);
}
}

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

141 changes: 0 additions & 141 deletions Assets/FluidBehaviorTree/Editor/BehaviorTree/Printer/TreeContainer.cs

This file was deleted.

This file was deleted.

19 changes: 7 additions & 12 deletions Assets/FluidBehaviorTree/Editor/BehaviorTree/Printer/VisualTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,28 @@ public class VisualTask {
private float _height = 50;

private List<VisualTask> _children = new List<VisualTask>();
private ITreeBox _box;
private IGraphBox _box;
private ITask _task;

public VisualTask (ITask task, ITreeContainer parentContainer) {
public VisualTask (ITask task, IGraphContainer parentContainer) {
_task = task;

var container = new TreeContainerVertical();
var childContainer = new TreeContainerHorizontal();
var container = new GraphContainerVertical();

_box = new TreeBox();
_box = new GraphBox();
_box.SetSize(50, 50);

parentContainer.AddBox(container);
container.AddBox(_box);

if (task.Children != null) {
var childContainer = new GraphContainerHorizontal();
foreach (var child in task.Children) {
_children.Add(new VisualTask(child, childContainer));
}
container.AddBox(childContainer);
}

container.AddBox(childContainer);

Debug.Log(task.Name);
Debug.Log($"Parent Global Position: {parentContainer.GlobalPositionX}, {parentContainer.GlobalPositionY}");
Debug.Log($"Container Global Position: {container.GlobalPositionX}, {container.GlobalPositionY}");
Debug.Log($"Box Global Position: {_box.GlobalPositionX}, {_box.GlobalPositionY}");
parentContainer.AddBox(container);
}

public void Print () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,23 @@ private void Awake () {
.Sequence()
.Condition("Custom Condition", () => true)
.Do("Custom Action A", () => TaskStatus.Success)
// .Sequence("Nested Sequence")
// .Condition("Custom Condition", () => true)
// .Sequence("Nested Sequence")
// .Condition("Custom Condition", () => true)
// .Do("Custom Action", () => TaskStatus.Success)
// .End()
// .Do("Custom Action", () => TaskStatus.Success)
// .End()
// .Do("Custom Action B", () => TaskStatus.Success)
.Sequence("Nested Sequence")
.Sequence("Nested Sequence")
.Condition("Custom Condition", () => true)
.Do("Custom Action", () => TaskStatus.Success)
.End()
.Condition("Custom Condition", () => true)
.Sequence("Nested Sequence")
.Condition("Custom Condition", () => true)
.Do("Custom Action", () => TaskStatus.Success)
.Sequence("Nested Sequence")
.Condition("Custom Condition", () => true)
.Do("Custom Action", () => TaskStatus.Success)
.End()
.End()
.Do("Custom Action", () => TaskStatus.Success)
.End()
.Do("Custom Action B", () => TaskStatus.Success)
.End()
.Build();
}
Expand Down
Loading

0 comments on commit e762e8e

Please sign in to comment.