diff --git a/src/behavior/Behavior.cs b/src/behavior/Behavior.cs index 09a4dee..db8d458 100644 --- a/src/behavior/Behavior.cs +++ b/src/behavior/Behavior.cs @@ -11,6 +11,7 @@ public sealed class Tree(Node root) public Node Root { get; } = root; public EntityTable Blackboard { get; } = new(); public Random Random { get; init; } = new(); + public AnimationPlayer? AnimationPlayer { get; init; } public Result Tick() => Process(0f); @@ -21,6 +22,7 @@ public sealed class Tree(Node root) Delta = delta, Blackboard = Blackboard, Random = Random, + AnimationPlayer = AnimationPlayer, }); } @@ -95,6 +97,7 @@ public readonly record struct Context public double Delta { get; init; } public required EntityTable Blackboard { get; init; } public Random Random { get; init; } + public AnimationPlayer? AnimationPlayer { get; init; } } public static class BehaviorTreeExtensions @@ -365,3 +368,39 @@ public sealed class Delay(double time) : Node 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; + } +} diff --git a/src/creatures/birb/Birb.cs b/src/creatures/birb/Birb.cs index 039ded8..6d14100 100644 --- a/src/creatures/birb/Birb.cs +++ b/src/creatures/birb/Birb.cs @@ -26,7 +26,7 @@ public sealed partial class Birb : CharacterBody2D, IAttackable, private float _hp; - private Behavior.Tree Tree { get; set; } = null!; + private Bt.Tree Tree { get; set; } = null!; private float TargetVelocity { get; set; } 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.MemoSequence([ 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.Action(() => - { - AnimationPlayer.Play("stab"); - _state = State.Attacking; - }), - new Bt.WaitUntil(() => _state is State.Idle), + new Bt.PlayAnimation("stab"), + new Bt.Action(() => _state = State.Attacking), + new Bt.WaitUntil(() => _state is not State.Attacking), ]), new Bt.MemoSequence([ new Bt.Delay(0.1) { RandomVariation = 0.05 }, + new Bt.PlayAnimation("idle"), new Bt.Action(() => { var left = Target!.GlobalPosition + Vector2.Left * AttackDistance; @@ -81,7 +79,6 @@ public sealed partial class Birb : CharacterBody2D, IAttackable, < GlobalPosition.DistanceSquaredTo(right) ? left : right; - AnimationPlayer.Play("idle"); return WalkTo(targetPosition) ? Bt.Result.Success : Bt.Result.Running; @@ -91,11 +88,14 @@ public sealed partial class Birb : CharacterBody2D, IAttackable, ]), new Bt.MemoSequence([ new Bt.Check(() => _state is State.Idle), - new Bt.Action(() => AnimationPlayer.Play("idle")), + new Bt.PlayAnimation("idle"), new Bt.DoNothing(), ]), ]) - ); + ) + { + AnimationPlayer = AnimationPlayer, + }; public void OnReady() {