tree based bt debugger

This commit is contained in:
Lilith Schier 2026-06-28 22:46:37 +02:00
parent c9967f09d9
commit b5d5a8f9f3
12 changed files with 271 additions and 19 deletions

View file

@ -2,6 +2,7 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Chickensoft.Collections;
namespace Dancer.Behavior;
@ -27,6 +28,8 @@ public sealed class Tree(Node root)
}
public void Halt() => Root.Halt();
public Godot.Collections.Dictionary DebugData => Root.DebugData;
}
public enum Result
@ -44,9 +47,12 @@ public enum NodeState
Failure,
}
[Tool]
public abstract class Node
{
public NodeState State { get; private set; } = NodeState.Idle;
public virtual Node[] Children => [];
public virtual string? Name { get; init; }
public Result Tick(in Context context)
{
@ -90,6 +96,16 @@ public abstract class Node
protected virtual void OnHalt()
{
}
public Godot.Collections.Dictionary DebugData =>
new()
{
["type"] = GetType().Name,
["name"] = Name ?? "",
["state"] = State.ToString(),
["children"] =
new Godot.Collections.Array(Children.Select(c => Variant.CreateFrom(c.DebugData))),
};
}
public readonly record struct Context
@ -128,10 +144,14 @@ public static class BehaviorTreeExtensions
public abstract class ListNode(Node[] children) : Node, IEnumerable<Node>
{
public Node[] Children { get; } = children;
public override Node[] Children { get; } = children;
public IEnumerator<Node> GetEnumerator() => ((IEnumerable<Node>)Children).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => Children.GetEnumerator();
protected override void OnHalt()
{
foreach (var child in Children) child.Halt();
}
}
public sealed class Sequence(Node[] children) : ListNode(children)
@ -161,9 +181,13 @@ public sealed class Sequence(Node[] children) : ListNode(children)
}
LoopDone:
if (State is NodeState.Running && childIdx < _runningChildIdx)
// if (State is NodeState.Running && childIdx < _runningChildIdx)
// {
// Children[_runningChildIdx].Halt();
// }
if (State is NodeState.Running or NodeState.Failure)
{
Children[_runningChildIdx].Halt();
for (var i = childIdx + 1; i < Children.Length; i++) Children[i].Halt();
}
if (result is Result.Running) _runningChildIdx = childIdx;
@ -171,7 +195,7 @@ public sealed class Sequence(Node[] children) : ListNode(children)
return result;
}
protected override void OnHalt() => Children[_runningChildIdx].Halt();
// protected override void OnHalt() => Children[_runningChildIdx].Halt();
}
public sealed class MemoSequence(Node[] children) : ListNode(children)
@ -204,11 +228,15 @@ public sealed class MemoSequence(Node[] children) : ListNode(children)
LoopDone:
if (result is Result.Running) _runningChildIdx = childIdx;
if (State is NodeState.Running or NodeState.Failure)
{
for (var i = childIdx + 1; i < Children.Length; i++) Children[i].Halt();
}
return result;
}
protected override void OnHalt() => Children[_runningChildIdx].Halt();
// protected override void OnHalt() => Children[_runningChildIdx].Halt();
}
public sealed class Selector(Node[] children) : ListNode(children)
@ -238,9 +266,13 @@ public sealed class Selector(Node[] children) : ListNode(children)
}
LoopDone:
if (State is NodeState.Running && childIdx < _runningChildIdx)
// if (State is NodeState.Running && childIdx < _runningChildIdx)
// {
// Children[_runningChildIdx].Halt();
// }
if (State is NodeState.Running or NodeState.Success)
{
Children[_runningChildIdx].Halt();
for (var i = childIdx + 1; i < Children.Length; i++) Children[i].Halt();
}
if (result is Result.Running) _runningChildIdx = childIdx;
@ -248,7 +280,7 @@ public sealed class Selector(Node[] children) : ListNode(children)
return result;
}
protected override void OnHalt() => Children[_runningChildIdx].Halt();
// protected override void OnHalt() => Children[_runningChildIdx].Halt();
}
public sealed class DoNothing : Node
@ -259,6 +291,7 @@ public sealed class DoNothing : Node
public abstract class DecoratorNode(Node child) : Node
{
public Node Child { get; } = child;
public override Node[] Children => [Child];
protected override void OnHalt() => Child.Halt();
}
@ -349,9 +382,11 @@ public sealed class Action(OnTickAction action) : Node
public sealed class Delay(double time) : Node
{
public double RandomVariation { get; set; }
public double RandomVariation { get; init; }
private double TimeLeft { get; set; }
public override string Name => $"Delay by {time:0.000}±{RandomVariation:0.000}s";
protected override void OnStart(in Context context)
{
TimeLeft = time;
@ -371,9 +406,15 @@ public sealed class Delay(double time) : Node
public sealed class PlayAnimation(StringName animation) : Node
{
/// <summary>
/// Set to true to have this node wait until the started animation has finished.
/// </summary>
public bool WaitForFinish { get; set; } = false;
private bool _hasAnimationFinished;
public override string Name => $"Play animation {animation}";
protected override void OnStart(in Context context)
{
if (context.AnimationPlayer is not { } animationPlayer) throw new InvalidOperationException();
@ -404,3 +445,8 @@ public sealed class PlayAnimation(StringName animation) : Node
_hasAnimationFinished = true;
}
}
public interface IHasBehaviorTree
{
public Tree Tree { get; }
}