another approach for making the debugger nicer
This commit is contained in:
parent
b5d5a8f9f3
commit
b37c78d75b
4 changed files with 65 additions and 55 deletions
|
|
@ -13,23 +13,27 @@ public sealed class Tree(Node root)
|
|||
public EntityTable Blackboard { get; } = new();
|
||||
public Random Random { get; init; } = new();
|
||||
public AnimationPlayer? AnimationPlayer { get; init; }
|
||||
private int _tickNumber;
|
||||
|
||||
public Result Tick() => Process(0f);
|
||||
|
||||
public Result Process(double delta)
|
||||
{
|
||||
return Root.Tick(new()
|
||||
var result = Root.Tick(new()
|
||||
{
|
||||
Delta = delta,
|
||||
Blackboard = Blackboard,
|
||||
Random = Random,
|
||||
AnimationPlayer = AnimationPlayer,
|
||||
TickNumber = _tickNumber,
|
||||
});
|
||||
_tickNumber += 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
public void Halt() => Root.Halt();
|
||||
|
||||
public Godot.Collections.Dictionary DebugData => Root.DebugData;
|
||||
public Godot.Collections.Dictionary DebugData => Root.GetDebugData(_tickNumber - 1);
|
||||
}
|
||||
|
||||
public enum Result
|
||||
|
|
@ -53,9 +57,11 @@ public abstract class Node
|
|||
public NodeState State { get; private set; } = NodeState.Idle;
|
||||
public virtual Node[] Children => [];
|
||||
public virtual string? Name { get; init; }
|
||||
private int _lastTicked = -1;
|
||||
|
||||
public Result Tick(in Context context)
|
||||
{
|
||||
_lastTicked = context.TickNumber;
|
||||
if (State is not NodeState.Running) OnStart(context);
|
||||
var result = OnTick(in context);
|
||||
State = result.ToState();
|
||||
|
|
@ -88,7 +94,7 @@ public abstract class Node
|
|||
|
||||
/// <summary>
|
||||
/// Called whenever this node is halted forcibly,
|
||||
/// but only if its state is <see cref="NodeState.Running"/>.
|
||||
/// but only if its state is not <see cref="NodeState.Idle"/>.
|
||||
/// Therefore, this will never be called twice in a row.
|
||||
/// Before being ticked again, <see cref="OnStart"/> is guaranteed
|
||||
/// to be called first.
|
||||
|
|
@ -97,14 +103,15 @@ public abstract class Node
|
|||
{
|
||||
}
|
||||
|
||||
public Godot.Collections.Dictionary DebugData =>
|
||||
public Godot.Collections.Dictionary GetDebugData(int lastTick) =>
|
||||
new()
|
||||
{
|
||||
["type"] = GetType().Name,
|
||||
["name"] = Name ?? "",
|
||||
["state"] = State.ToString(),
|
||||
["is_stale"] = lastTick != _lastTicked,
|
||||
["children"] =
|
||||
new Godot.Collections.Array(Children.Select(c => Variant.CreateFrom(c.DebugData))),
|
||||
new Godot.Collections.Array(Children.Select(c => Variant.CreateFrom(c.GetDebugData(lastTick)))),
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -114,6 +121,7 @@ public readonly record struct Context
|
|||
public required EntityTable Blackboard { get; init; }
|
||||
public Random Random { get; init; }
|
||||
public AnimationPlayer? AnimationPlayer { get; init; }
|
||||
public int TickNumber { get; init; }
|
||||
}
|
||||
|
||||
public static class BehaviorTreeExtensions
|
||||
|
|
@ -148,10 +156,6 @@ public abstract class ListNode(Node[] children) : Node, IEnumerable<Node>
|
|||
|
||||
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)
|
||||
|
|
@ -181,13 +185,9 @@ public sealed class Sequence(Node[] children) : ListNode(children)
|
|||
}
|
||||
|
||||
LoopDone:
|
||||
// if (State is NodeState.Running && childIdx < _runningChildIdx)
|
||||
// {
|
||||
// Children[_runningChildIdx].Halt();
|
||||
// }
|
||||
if (State is NodeState.Running or NodeState.Failure)
|
||||
if (State is NodeState.Running && childIdx < _runningChildIdx)
|
||||
{
|
||||
for (var i = childIdx + 1; i < Children.Length; i++) Children[i].Halt();
|
||||
Children[_runningChildIdx].Halt();
|
||||
}
|
||||
|
||||
if (result is Result.Running) _runningChildIdx = childIdx;
|
||||
|
|
@ -195,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)
|
||||
|
|
@ -228,15 +228,11 @@ 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)
|
||||
|
|
@ -266,13 +262,9 @@ public sealed class Selector(Node[] children) : ListNode(children)
|
|||
}
|
||||
|
||||
LoopDone:
|
||||
// if (State is NodeState.Running && childIdx < _runningChildIdx)
|
||||
// {
|
||||
// Children[_runningChildIdx].Halt();
|
||||
// }
|
||||
if (State is NodeState.Running or NodeState.Success)
|
||||
if (State is NodeState.Running && childIdx < _runningChildIdx)
|
||||
{
|
||||
for (var i = childIdx + 1; i < Children.Length; i++) Children[i].Halt();
|
||||
Children[_runningChildIdx].Halt();
|
||||
}
|
||||
|
||||
if (result is Result.Running) _runningChildIdx = childIdx;
|
||||
|
|
@ -280,7 +272,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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue