add unity behavior tree package & examples

add start of own implementation of a behavior tree
This commit is contained in:
2025-04-23 12:33:28 +02:00
parent 8a01366c35
commit d9fb5ee7bc
19 changed files with 232 additions and 17 deletions
+3
View File
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2c6bdc0b9db0433e939c2e77844106f1
timeCreated: 1745402938
+9
View File
@@ -0,0 +1,9 @@
namespace DefaultNamespace.BehaviorTree
{
public abstract class Action : Node
{
public Action()
{
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d658db48a0944051aa2a4a2fa02e089c
timeCreated: 1745403146
@@ -0,0 +1,17 @@
using System.Collections.Generic;
namespace DefaultNamespace.BehaviorTree
{
public class Composite : Node
{
public Composite(List<Node> nodes)
{
_nodes.AddRange(nodes);
}
public Composite(params Node[] nodes)
{
_nodes.AddRange(nodes);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 975b1d6c26eb459d9a8cdd3e042b6674
timeCreated: 1745403206
@@ -0,0 +1,7 @@
namespace DefaultNamespace.BehaviorTree
{
public abstract class Condition : Action
{
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a2d064fe5d964df7aa50ae36e8551561
timeCreated: 1745403415
@@ -0,0 +1,19 @@
namespace DefaultNamespace.BehaviorTree
{
public class Decorator : Node
{
public Node Node
{
get
{
if (_nodes != null && _nodes.Count > 0) return _nodes[0];
return null;
}
}
public Decorator(Node node)
{
_nodes.Add(node);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f064dd30dc4940009b32f253d2f6ec34
timeCreated: 1745403277
+31
View File
@@ -0,0 +1,31 @@
using System.Collections.Generic;
namespace DefaultNamespace.BehaviorTree
{
public abstract class Node
{
protected List<Node> _nodes = new();
public virtual void Start()
{
}
public virtual Status Update()
{
return Status.Success;
}
public virtual void End()
{
}
}
public enum Status
{
Success,
Failure,
Running,
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: bb191187e8a246b29358c9eca60e18ea
timeCreated: 1745403053
@@ -0,0 +1,42 @@
using System;
namespace DefaultNamespace.BehaviorTree
{
public class Sequence : Composite
{
protected int _currentIndex;
protected Node _currentNode;
public override void Start()
{
_currentIndex = 0;
_currentNode = _currentIndex >= _nodes.Count ? null : _nodes[_currentIndex];
_currentNode?.Start();
}
public override Status Update()
{
if (_currentNode == null) return Status.Failure;
Status status = _currentNode.Update();
switch (status)
{
case Status.Success:
_currentNode.End();
_currentIndex++;
if (_currentIndex >= _nodes.Count) return Status.Success;
_currentNode = _nodes[_currentIndex];
_currentNode.Start();
return Status.Running;
case Status.Failure:
_currentNode.End();
return Status.Failure;
case Status.Running:
return Status.Running;
default:
throw new ArgumentOutOfRangeException();
}
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 39476da0e0a54e429221ed45a8c2cf0f
timeCreated: 1745403452
+28
View File
@@ -0,0 +1,28 @@
using System;
using UnityEngine;
namespace DefaultNamespace.BehaviorTree
{
public class Tree : MonoBehaviour
{
private Node _rootNode;
private void OnEnable()
{
if (_rootNode == null) return;
_rootNode.Start();
}
private void Update()
{
if (_rootNode == null) return;
_rootNode.Update();
}
private void OnDisable()
{
if (_rootNode == null) return;
_rootNode.End();
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1de9a2c205bb40fb9770d0639fb89abe
timeCreated: 1745402959