Added: Unity Behavior Graph asset for behaviour demo
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
namespace AI.BehaviourTree
|
||||
{
|
||||
public abstract class Action : Node
|
||||
{
|
||||
protected Action()
|
||||
{
|
||||
children = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f74fc08720e6434d99bf774b6a6f3b09
|
||||
timeCreated: 1794503810
|
||||
@@ -0,0 +1,30 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace AI.BehaviourTree
|
||||
{
|
||||
public class BehaviourTree : MonoBehaviour
|
||||
{
|
||||
private Node rootNode;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
NodeStatus status = rootNode.Start();
|
||||
Debug.Log("Behaviour tree started");
|
||||
Debug.Log($"Behaviour tree start root node status: {status}");
|
||||
if (status != NodeStatus.Running) enabled = false;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
Debug.Log("Behaviour tree stopped");
|
||||
rootNode.Stop();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
NodeStatus status = rootNode.Execute();
|
||||
Debug.Log($"Behaviour tree execute root node status: {status}");
|
||||
if (status != NodeStatus.Running) enabled = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b2377a84a8744e51ad63470db235b306
|
||||
timeCreated: 1794502220
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace AI.BehaviourTree
|
||||
{
|
||||
public abstract class Composite : Node
|
||||
{
|
||||
protected Composite(params Node[] children)
|
||||
{
|
||||
this.children.AddRange(children);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f3fc40a957b743bbb38e5fc6ac76431a
|
||||
timeCreated: 1794503869
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace AI.BehaviourTree
|
||||
{
|
||||
public abstract class Condition : Action
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bb50593562d3477d9c9b37ff6c40c654
|
||||
timeCreated: 1794503879
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace AI.BehaviourTree
|
||||
{
|
||||
public abstract class Decorator : Node
|
||||
{
|
||||
protected Decorator(Node child)
|
||||
{
|
||||
this.children.Add(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0a8feb2ab491450fa977c32f0262f949
|
||||
timeCreated: 1794503911
|
||||
@@ -0,0 +1,23 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace AI.BehaviourTree
|
||||
{
|
||||
public abstract class Node
|
||||
{
|
||||
protected List<Node> children = new List<Node>();
|
||||
|
||||
public virtual NodeStatus Start() => NodeStatus.Running;
|
||||
public virtual NodeStatus Execute() => NodeStatus.Success;
|
||||
|
||||
public virtual void Stop()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public enum NodeStatus
|
||||
{
|
||||
Running,
|
||||
Success,
|
||||
Failure
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 53b501c551ff46848aaadb57d7118a3e
|
||||
timeCreated: 1794502273
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace AI.BehaviourTree
|
||||
{
|
||||
public class Selector : Composite
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 025f7db0aae240698cce07fb6743c2d9
|
||||
timeCreated: 1794503925
|
||||
@@ -0,0 +1,67 @@
|
||||
namespace AI.BehaviourTree
|
||||
{
|
||||
public class Sequence : Composite
|
||||
{
|
||||
Node _currentChild;
|
||||
int _currentChildIndex = 0;
|
||||
|
||||
public Sequence(params Node[] children) : base(children)
|
||||
{
|
||||
}
|
||||
|
||||
public override NodeStatus Start()
|
||||
{
|
||||
if (children.Count == 0) return NodeStatus.Failure;
|
||||
|
||||
_currentChildIndex = 0;
|
||||
_currentChild = children[_currentChildIndex];
|
||||
// if status == NodeStatus.Failure => whole sequence fails => return NodeStatus.Failure;
|
||||
// if status == NodeStatus.Success => run the next child
|
||||
|
||||
do
|
||||
{
|
||||
NodeStatus status = _currentChild?.Start() ?? NodeStatus.Failure;
|
||||
if (status == NodeStatus.Failure) return NodeStatus.Failure;
|
||||
if (status == NodeStatus.Running) return NodeStatus.Running;
|
||||
} while (RunNextChild());
|
||||
|
||||
return NodeStatus.Success;
|
||||
}
|
||||
|
||||
public override NodeStatus Execute()
|
||||
{
|
||||
NodeStatus status = _currentChild?.Execute() ?? NodeStatus.Failure;
|
||||
|
||||
if (status == NodeStatus.Failure) return NodeStatus.Failure;
|
||||
if (status == NodeStatus.Running) return NodeStatus.Running;
|
||||
|
||||
while (RunNextChild())
|
||||
{
|
||||
status = _currentChild?.Start() ?? NodeStatus.Failure;
|
||||
if (status == NodeStatus.Failure) return NodeStatus.Failure;
|
||||
if (status == NodeStatus.Running) return NodeStatus.Running;
|
||||
}
|
||||
|
||||
return NodeStatus.Success;
|
||||
}
|
||||
|
||||
public override void Stop()
|
||||
{
|
||||
_currentChild?.Stop();
|
||||
}
|
||||
|
||||
private bool RunNextChild()
|
||||
{
|
||||
_currentChild?.Stop();
|
||||
_currentChild = null;
|
||||
_currentChildIndex++;
|
||||
if (_currentChildIndex < children.Count)
|
||||
{
|
||||
_currentChild = children[_currentChildIndex];
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 71354184e3e64c428a329b5c493b8cc9
|
||||
timeCreated: 1794503979
|
||||
Reference in New Issue
Block a user