Skip to content

Commit

Permalink
Add new variable types
Browse files Browse the repository at this point in the history
Add SharedVector2 SharedVector2Int  SharedVector3Int
  • Loading branch information
AkiKurisu committed Jun 5, 2024
1 parent 9d01356 commit fe34111
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Editor/Core/GraphView/AdvancedBlackBoard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ private void InitRequestDelegate()
menu.AddItem(new GUIContent("Int"), false, () => AddSharedVariableWithNotify(new SharedInt()));
menu.AddItem(new GUIContent("Float"), false, () => AddSharedVariableWithNotify(new SharedFloat()));
menu.AddItem(new GUIContent("Bool"), false, () => AddSharedVariableWithNotify(new SharedBool()));
menu.AddItem(new GUIContent("Vector2"), false, () => AddSharedVariableWithNotify(new SharedVector2()));
menu.AddItem(new GUIContent("Vector2Int"), false, () => AddSharedVariableWithNotify(new SharedVector2Int()));
menu.AddItem(new GUIContent("Vector3"), false, () => AddSharedVariableWithNotify(new SharedVector3()));
menu.AddItem(new GUIContent("Vector3Int"), false, () => AddSharedVariableWithNotify(new SharedVector3Int()));
menu.AddItem(new GUIContent("String"), false, () => AddSharedVariableWithNotify(new SharedString()));
menu.AddItem(new GUIContent("Object"), false, () => AddSharedVariableWithNotify(new SharedObject()));
menu.ShowAsContext();
Expand Down
3 changes: 3 additions & 0 deletions Runtime/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Kurisu.AkiBT.Editor")]
[assembly: InternalsVisibleTo("Kurisu.AkiBT.DSL")]
11 changes: 11 additions & 0 deletions Runtime/AssemblyInfo.cs.meta

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

2 changes: 1 addition & 1 deletion Runtime/Core/BehaviorTreeSO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Kurisu.AkiBT
public class BehaviorTreeSO : ScriptableObject, IBehaviorTree
{
[SerializeReference, HideInInspector]
protected Root root = new();
protected internal Root root = new();
public Root Root => root;
Object IBehaviorTree._Object => this;
public List<SharedVariable> SharedVariables => sharedVariables;
Expand Down
52 changes: 47 additions & 5 deletions Runtime/Core/Model/BehaviorTreeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ namespace Kurisu.AkiBT
{
public class BehaviorTreeBuilder : IVariableSource
{
private readonly GameObject bindObject;
private readonly Stack<NodeBehavior> nodeStack;
private readonly Stack<NodeBehavior> pointers;
public List<SharedVariable> SharedVariables { get; }
public BehaviorTreeBuilder(GameObject bindObject)
public BehaviorTreeBuilder()
{
this.bindObject = bindObject;
nodeStack = new();
SharedVariables = new();
pointers = new();
Expand Down Expand Up @@ -85,9 +83,10 @@ public BehaviorTreeBuilder EndChild()
/// <summary>
/// Build behavior tree
/// </summary>
/// <param name="bindObject"></param>
/// <param name="behaviorTree"></param>
/// <returns></returns>
public bool Build(out BehaviorTree behaviorTree)
public bool Build(GameObject bindObject, out BehaviorTree behaviorTree)
{
if (pointers.Count > 0)
{
Expand All @@ -108,13 +107,56 @@ public bool Build(out BehaviorTree behaviorTree)
nodeStack.Pop();
}
}
behaviorTree = bindObject.AddComponent<BehaviorTree>();
if (!bindObject.TryGetComponent(out behaviorTree))
behaviorTree = bindObject.AddComponent<BehaviorTree>();
behaviorTree.SharedVariables.AddRange(SharedVariables);
behaviorTree.Root = new Root() { Child = nodeStack.Pop() };
SharedVariables.Clear();
return true;
}
/// <summary>
/// Build behavior tree so
/// </summary>
/// <returns></returns>
public BehaviorTreeSO Build()
{
var instance = ScriptableObject.CreateInstance<BehaviorTreeSO>();
if (Build(instance)) return instance;
UObject.Destroy(instance);
return null;
}
/// <summary>
/// Build behavior tree so
/// </summary>
/// <param name="behaviorTreeSO"></param>
/// <returns></returns>
public bool Build(BehaviorTreeSO behaviorTreeSO)
{
if (pointers.Count > 0)
{
Debug.LogWarning("The number of calls to EndChild is not consistent with StartChild");
}
pointers.Clear();
int excess = nodeStack.Count - 1;
if (excess == -1)
{
Debug.LogError("Build failed, no node is added to the builder");
return false;
}
if (excess > 0)
{
for (int i = 0; i < excess; ++i)
{
nodeStack.Pop();
}
}
behaviorTreeSO.SharedVariables.Clear();
behaviorTreeSO.SharedVariables.AddRange(SharedVariables);
behaviorTreeSO.root = new Root() { Child = nodeStack.Pop() };
SharedVariables.Clear();
return true;
}
/// <summary>
/// Get new <see cref="SharedFloat"/>, also write it to the public variables of the behavior tree
/// </summary>
/// <param name="key"></param>
Expand Down
37 changes: 37 additions & 0 deletions Runtime/Core/Model/Variable/SharedVector2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using UnityEngine;
namespace Kurisu.AkiBT
{
[Serializable]
public class SharedVector2 : SharedVariable<Vector2>
{
public SharedVector2(Vector2 value)
{
this.value = value;
}
public SharedVector2()
{

}
protected override SharedVariable<Vector2> CloneT()
{
return new SharedVector2() { Value = value };
}
}
[Serializable]
public class SharedVector2Int : SharedVariable<Vector2Int>
{
public SharedVector2Int(Vector2Int value)
{
this.value = value;
}
public SharedVector2Int()
{

}
protected override SharedVariable<Vector2Int> CloneT()
{
return new SharedVector2Int() { Value = value };
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Core/Model/Variable/SharedVector2.cs.meta

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

16 changes: 16 additions & 0 deletions Runtime/Core/Model/Variable/SharedVector3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,20 @@ protected override SharedVariable<Vector3> CloneT()
return new SharedVector3() { Value = value };
}
}
[Serializable]
public class SharedVector3Int : SharedVariable<Vector3Int>
{
public SharedVector3Int(Vector3Int value)
{
this.value = value;
}
public SharedVector3Int()
{

}
protected override SharedVariable<Vector3Int> CloneT()
{
return new SharedVector3Int() { Value = value };
}
}
}

0 comments on commit fe34111

Please sign in to comment.