Behaviour Tree
This commit is contained in:
39
Runtime/Engine/Code/AI/BehaviourNode.cs
Normal file
39
Runtime/Engine/Code/AI/BehaviourNode.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace RebootKit.Engine.AI {
|
||||
public class BehaviourNode {
|
||||
public enum Status {
|
||||
Success,
|
||||
Failure,
|
||||
Running
|
||||
}
|
||||
|
||||
public readonly string Name;
|
||||
|
||||
public readonly List<BehaviourNode> Children = new List<BehaviourNode>();
|
||||
|
||||
protected int m_CurrentChild;
|
||||
|
||||
public BehaviourNode(string name) {
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public void AddChild(BehaviourNode child) {
|
||||
Children.Add(child);
|
||||
}
|
||||
|
||||
public virtual Status Process(float dt) {
|
||||
return Children[m_CurrentChild].Process(dt);
|
||||
}
|
||||
|
||||
public virtual void Reset() {
|
||||
m_CurrentChild = 0;
|
||||
|
||||
for (int i = 0; i < Children.Count; i++) {
|
||||
Children[i].Reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
3
Runtime/Engine/Code/AI/BehaviourNode.cs.meta
Normal file
3
Runtime/Engine/Code/AI/BehaviourNode.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c1f6ae41eb91433f91a170b11173e7c3
|
||||
timeCreated: 1760018098
|
||||
19
Runtime/Engine/Code/AI/BehaviourTree.cs
Normal file
19
Runtime/Engine/Code/AI/BehaviourTree.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
namespace RebootKit.Engine.AI {
|
||||
public class BehaviourTree : BehaviourNode {
|
||||
public BehaviourTree(string name) : base(name) {
|
||||
}
|
||||
|
||||
public override Status Process(float dt) {
|
||||
while (m_CurrentChild < Children.Count) {
|
||||
Status status = Children[m_CurrentChild].Process(dt);
|
||||
if (status != Status.Success) {
|
||||
return status;
|
||||
}
|
||||
|
||||
m_CurrentChild += 1;
|
||||
}
|
||||
|
||||
return Status.Success;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Runtime/Engine/Code/AI/BehaviourTree.cs.meta
Normal file
3
Runtime/Engine/Code/AI/BehaviourTree.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4ceac2e33fd54beebadb6e844f2a7328
|
||||
timeCreated: 1760017438
|
||||
38
Runtime/Engine/Code/AI/IStrategy.cs
Normal file
38
Runtime/Engine/Code/AI/IStrategy.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
|
||||
namespace RebootKit.Engine.AI {
|
||||
public interface IStrategy {
|
||||
BehaviourNode.Status Process(float dt);
|
||||
void Reset() {
|
||||
}
|
||||
}
|
||||
|
||||
public class Condition : IStrategy {
|
||||
readonly Func<bool> m_Predicate;
|
||||
|
||||
public Condition(Func<bool> predicate) {
|
||||
m_Predicate = predicate;
|
||||
}
|
||||
|
||||
public BehaviourNode.Status Process(float dt) {
|
||||
if (m_Predicate()) {
|
||||
return BehaviourNode.Status.Success;
|
||||
}
|
||||
|
||||
return BehaviourNode.Status.Failure;
|
||||
}
|
||||
}
|
||||
|
||||
public class ActionStrategy : IStrategy {
|
||||
readonly Action m_Action;
|
||||
|
||||
public ActionStrategy(Action action) {
|
||||
m_Action = action;
|
||||
}
|
||||
|
||||
public BehaviourNode.Status Process(float dt) {
|
||||
m_Action();
|
||||
return BehaviourNode.Status.Success;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Runtime/Engine/Code/AI/IStrategy.cs.meta
Normal file
3
Runtime/Engine/Code/AI/IStrategy.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 85b48e6160ad4f039e9a5fc6524472fc
|
||||
timeCreated: 1760108175
|
||||
17
Runtime/Engine/Code/AI/Leaf.cs
Normal file
17
Runtime/Engine/Code/AI/Leaf.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace RebootKit.Engine.AI {
|
||||
public class Leaf : BehaviourNode {
|
||||
readonly IStrategy m_Strategy;
|
||||
|
||||
public Leaf(string name, IStrategy strategy) : base(name) {
|
||||
m_Strategy = strategy;
|
||||
}
|
||||
|
||||
public override Status Process(float dt) {
|
||||
return m_Strategy.Process(dt);
|
||||
}
|
||||
|
||||
public override void Reset() {
|
||||
m_Strategy.Reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Runtime/Engine/Code/AI/Leaf.cs.meta
Normal file
3
Runtime/Engine/Code/AI/Leaf.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ce3c5054b2fd45f6a5b26b74e735a622
|
||||
timeCreated: 1760108171
|
||||
37
Runtime/Engine/Code/AI/Selector.cs
Normal file
37
Runtime/Engine/Code/AI/Selector.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
|
||||
namespace RebootKit.Engine.AI {
|
||||
public class Selector : BehaviourNode {
|
||||
readonly Func<bool> m_Condition;
|
||||
|
||||
public Selector(string name, Func<bool> condition = null) : base(name) {
|
||||
m_Condition = condition;
|
||||
}
|
||||
|
||||
public override Status Process(float dt) {
|
||||
if (m_Condition != null && !m_Condition()) {
|
||||
return Status.Failure;
|
||||
}
|
||||
|
||||
if (m_CurrentChild < Children.Count) {
|
||||
Status status = Children[m_CurrentChild].Process(dt);
|
||||
|
||||
switch (status) {
|
||||
case Status.Success:
|
||||
Reset();
|
||||
return Status.Success;
|
||||
|
||||
case Status.Failure:
|
||||
m_CurrentChild += 1;
|
||||
return Status.Running;
|
||||
|
||||
case Status.Running:
|
||||
return Status.Running;
|
||||
}
|
||||
}
|
||||
|
||||
Reset();
|
||||
return Status.Failure;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Runtime/Engine/Code/AI/Selector.cs.meta
Normal file
3
Runtime/Engine/Code/AI/Selector.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ab99366791464e3789770f26559fa59a
|
||||
timeCreated: 1760101875
|
||||
37
Runtime/Engine/Code/AI/Sequence.cs
Normal file
37
Runtime/Engine/Code/AI/Sequence.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
|
||||
namespace RebootKit.Engine.AI {
|
||||
public class Sequence : BehaviourNode {
|
||||
readonly Func<bool> m_Condition;
|
||||
|
||||
public Sequence(string name, Func<bool> condition = null) : base(name) {
|
||||
m_Condition = condition;
|
||||
}
|
||||
|
||||
public override Status Process(float dt) {
|
||||
if (m_Condition != null && !m_Condition()) {
|
||||
return Status.Failure;
|
||||
}
|
||||
|
||||
if (m_CurrentChild < Children.Count) {
|
||||
Status status = Children[m_CurrentChild].Process(dt);
|
||||
|
||||
switch (status) {
|
||||
case Status.Success:
|
||||
m_CurrentChild += 1;
|
||||
return m_CurrentChild == Children.Count ? Status.Success : Status.Running;
|
||||
|
||||
case Status.Failure:
|
||||
Reset();
|
||||
return Status.Failure;
|
||||
|
||||
case Status.Running:
|
||||
return Status.Running;
|
||||
}
|
||||
}
|
||||
|
||||
Reset();
|
||||
return Status.Success;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Runtime/Engine/Code/AI/Sequence.cs.meta
Normal file
3
Runtime/Engine/Code/AI/Sequence.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c49882ec0f3b4f28a2ed3491967fa966
|
||||
timeCreated: 1760101518
|
||||
Reference in New Issue
Block a user