added animation playing in behavior trees

This commit is contained in:
Lilith Schier 2026-06-27 17:58:25 +02:00
parent d362d93b12
commit c9967f09d9
2 changed files with 50 additions and 11 deletions

View file

@ -11,6 +11,7 @@ public sealed class Tree(Node root)
public Node Root { get; } = root; public Node Root { get; } = root;
public EntityTable Blackboard { get; } = new(); public EntityTable Blackboard { get; } = new();
public Random Random { get; init; } = new(); public Random Random { get; init; } = new();
public AnimationPlayer? AnimationPlayer { get; init; }
public Result Tick() => Process(0f); public Result Tick() => Process(0f);
@ -21,6 +22,7 @@ public sealed class Tree(Node root)
Delta = delta, Delta = delta,
Blackboard = Blackboard, Blackboard = Blackboard,
Random = Random, Random = Random,
AnimationPlayer = AnimationPlayer,
}); });
} }
@ -95,6 +97,7 @@ public readonly record struct Context
public double Delta { get; init; } public double Delta { get; init; }
public required EntityTable Blackboard { get; init; } public required EntityTable Blackboard { get; init; }
public Random Random { get; init; } public Random Random { get; init; }
public AnimationPlayer? AnimationPlayer { get; init; }
} }
public static class BehaviorTreeExtensions public static class BehaviorTreeExtensions
@ -365,3 +368,39 @@ public sealed class Delay(double time) : Node
return Result.Running; return Result.Running;
} }
} }
public sealed class PlayAnimation(StringName animation) : Node
{
public bool WaitForFinish { get; set; } = false;
private bool _hasAnimationFinished;
protected override void OnStart(in Context context)
{
if (context.AnimationPlayer is not { } animationPlayer) throw new InvalidOperationException();
animationPlayer.Play(animation);
if (WaitForFinish)
{
_hasAnimationFinished = false;
animationPlayer.AnimationFinished += OnAnimationFinished;
}
}
protected override Result OnTick(in Context context)
{
if (context.AnimationPlayer is not { } animationPlayer) throw new InvalidOperationException();
if (!WaitForFinish) return Result.Success;
if (_hasAnimationFinished)
{
animationPlayer.AnimationFinished -= OnAnimationFinished;
return Result.Success;
}
return Result.Running;
}
private void OnAnimationFinished(StringName finishedAnimation)
{
if (finishedAnimation != animation) return;
_hasAnimationFinished = true;
}
}

View file

@ -26,7 +26,7 @@ public sealed partial class Birb : CharacterBody2D, IAttackable,
private float _hp; private float _hp;
private Behavior.Tree Tree { get; set; } = null!; private Bt.Tree Tree { get; set; } = null!;
private float TargetVelocity { get; set; } private float TargetVelocity { get; set; }
private MotionGoal _motionGoal = new MotionGoal.Stop(); private MotionGoal _motionGoal = new MotionGoal.Stop();
@ -59,19 +59,17 @@ public sealed partial class Birb : CharacterBody2D, IAttackable,
new Bt.Check(() => StabHitbox.GetOverlappingBodies().Contains(Target)), new Bt.Check(() => StabHitbox.GetOverlappingBodies().Contains(Target)),
new Bt.MemoSequence([ new Bt.MemoSequence([
new Bt.Action(Stop), new Bt.Action(Stop),
new Bt.Action(() => AnimationPlayer.Play("prepare_attack")), new Bt.PlayAnimation("prepare_attack"),
new Bt.Delay(0.4) { RandomVariation = 0.2 }, new Bt.Delay(0.4) { RandomVariation = 0.2 },
]), ]),
]), ]),
new Bt.Action(() => new Bt.PlayAnimation("stab"),
{ new Bt.Action(() => _state = State.Attacking),
AnimationPlayer.Play("stab"); new Bt.WaitUntil(() => _state is not State.Attacking),
_state = State.Attacking;
}),
new Bt.WaitUntil(() => _state is State.Idle),
]), ]),
new Bt.MemoSequence([ new Bt.MemoSequence([
new Bt.Delay(0.1) { RandomVariation = 0.05 }, new Bt.Delay(0.1) { RandomVariation = 0.05 },
new Bt.PlayAnimation("idle"),
new Bt.Action(() => new Bt.Action(() =>
{ {
var left = Target!.GlobalPosition + Vector2.Left * AttackDistance; var left = Target!.GlobalPosition + Vector2.Left * AttackDistance;
@ -81,7 +79,6 @@ public sealed partial class Birb : CharacterBody2D, IAttackable,
< GlobalPosition.DistanceSquaredTo(right) < GlobalPosition.DistanceSquaredTo(right)
? left ? left
: right; : right;
AnimationPlayer.Play("idle");
return WalkTo(targetPosition) return WalkTo(targetPosition)
? Bt.Result.Success ? Bt.Result.Success
: Bt.Result.Running; : Bt.Result.Running;
@ -91,11 +88,14 @@ public sealed partial class Birb : CharacterBody2D, IAttackable,
]), ]),
new Bt.MemoSequence([ new Bt.MemoSequence([
new Bt.Check(() => _state is State.Idle), new Bt.Check(() => _state is State.Idle),
new Bt.Action(() => AnimationPlayer.Play("idle")), new Bt.PlayAnimation("idle"),
new Bt.DoNothing(), new Bt.DoNothing(),
]), ]),
]) ])
); )
{
AnimationPlayer = AnimationPlayer,
};
public void OnReady() public void OnReady()
{ {