Added Behavior Graph asset for AI simulations

This commit is contained in:
2026-04-28 18:33:07 +02:00
parent 8b1a42ed9d
commit 7e2030155c
34 changed files with 4361 additions and 8 deletions
+7
View File
@@ -0,0 +1,7 @@
namespace DefaultNamespace.BehaviorTree
{
public abstract class Action : Node
{
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a47380a9d61c417fae2871ebb4af4ca9
timeCreated: 1777392989
@@ -0,0 +1,14 @@
using System.Collections.Generic;
namespace DefaultNamespace.BehaviorTree
{
public abstract class Composite : Node
{
protected List<Node> Children;
public Composite(List<Node> children)
{
Children = children;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 03f624aecf9b4f06907bc2d216d810a7
timeCreated: 1777393016
@@ -0,0 +1,7 @@
namespace DefaultNamespace.BehaviorTree
{
public abstract class Condition : Action
{
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4a21ed8dbd354365a64b54f680885692
timeCreated: 1777393004
@@ -0,0 +1,12 @@
namespace DefaultNamespace.BehaviorTree
{
public abstract class Decorator : Node
{
protected Node Child;
public Decorator(Node child)
{
Child = child;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 907a63b689ce467d9b544d2cfeb23b50
timeCreated: 1777393029
+15
View File
@@ -0,0 +1,15 @@
namespace DefaultNamespace.BehaviorTree
{
public abstract class Node
{
public virtual Status OnStart() => Status.Running;
public virtual void OnEnd()
{
}
public virtual Status OnUpdate() => Status.Success;
}
public enum Status { Success, Failure, Running }
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 777a49e04b5645c69db83417be0665a5
timeCreated: 1777392427
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
namespace DefaultNamespace.BehaviorTree
{
public class Selector : Composite
{
private int _currentIndex;
private Node _currentNode;
public Selector(List<Node> children) : base(children)
{
}
public override Status OnStart()
{
if (Children.Count == 0) return Status.Failure;
_currentIndex = 0;
_currentNode = Children[_currentIndex];
return _currentNode?.OnStart() ?? Status.Failure;
}
public override Status OnUpdate()
{
Status status = _currentNode?.OnUpdate() ?? Status.Failure;
switch (status)
{
case Status.Failure:
_currentNode?.OnEnd();
_currentIndex++;
if (_currentIndex >= Children.Count) return Status.Failure;
_currentNode = Children[_currentIndex];
return _currentNode?.OnStart() ?? Status.Failure;
case Status.Success:
_currentNode?.OnEnd();
return Status.Success;
case Status.Running:
return Status.Running;
default:
throw new ArgumentOutOfRangeException();
}
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d6125671572a4bdca9faf360d9631007
timeCreated: 1777393713
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
namespace DefaultNamespace.BehaviorTree
{
public class Sequence : Composite
{
private int _currentIndex;
private Node _currentNode;
public Sequence(List<Node> children) : base(children)
{
}
public override Status OnStart()
{
if (Children.Count == 0) return Status.Failure;
_currentIndex = 0;
_currentNode = Children[_currentIndex];
return _currentNode?.OnStart() ?? Status.Failure;
}
public override Status OnUpdate()
{
Status status = _currentNode?.OnUpdate() ?? Status.Failure;
switch (status)
{
case Status.Success:
_currentNode?.OnEnd();
_currentIndex++;
if (_currentIndex >= Children.Count) return Status.Success;
_currentNode = Children[_currentIndex];
return _currentNode?.OnStart() ?? Status.Failure;
case Status.Failure:
_currentNode?.OnEnd();
return Status.Failure;
case Status.Running:
return Status.Running;
default:
throw new ArgumentOutOfRangeException();
}
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d710d34ec35547bda535ffd8af159ab1
timeCreated: 1777393258
+46
View File
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace DefaultNamespace.BehaviorTree
{
public class Tree : MonoBehaviour
{
private Node _rootNode;
// may be you want to run this tree anyways without looking for
// the current state, then throw the ifs away
private void OnEnable()
{
// create first node
_rootNode = new Sequence(new List<Node>());
//if (_rootNode.OnStart() == Status.Failure) enabled = false;
Debug.Log($"Root node status at start: {_rootNode.OnStart()}");
}
private void OnDisable()
{
_rootNode.OnEnd();
}
private void Update()
{
// Status status = _rootNode.OnUpdate();
//
// switch (status)
// {
// case Status.Success:
// break;
// case Status.Failure:
// break;
// case Status.Running:
// break;
// default:
// throw new ArgumentOutOfRangeException();
// }
Debug.Log($"Root node status at update: {_rootNode.OnUpdate()}");
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e3ebc182a7db40659155500d45db62e1
timeCreated: 1777392380