added behavior trees!
This commit is contained in:
parent
4d0ad13bf3
commit
d362d93b12
9 changed files with 510 additions and 197 deletions
|
|
@ -1,11 +1,12 @@
|
|||
using System;
|
||||
using Chickensoft.LogicBlocks;
|
||||
using Chickensoft.LogicBlocks.Auto;
|
||||
using System.Diagnostics;
|
||||
using Bt = Dancer.Behavior;
|
||||
|
||||
namespace Dancer;
|
||||
|
||||
[Meta(typeof(IAutoNode))]
|
||||
public sealed partial class Birb : CharacterBody2D, IAttackable, IProvide<IHitboxOwner>, IHitboxOwner
|
||||
public sealed partial class Birb : CharacterBody2D, IAttackable,
|
||||
IProvide<IHitboxOwner>, IHitboxOwner
|
||||
{
|
||||
public override void _Notification(int what) => this.Notify(what);
|
||||
|
||||
|
|
@ -19,20 +20,83 @@ public sealed partial class Birb : CharacterBody2D, IAttackable, IProvide<IHitbo
|
|||
[Node] private AnimatedSprite2D Body { get; set; } = null!;
|
||||
[Node] private AnimationPlayer AnimationPlayer { get; set; } = null!;
|
||||
|
||||
[Node] private Hitbox StabHitbox { get; set; } = null!;
|
||||
|
||||
public Node2D? Target { get; set; }
|
||||
|
||||
private float _hp;
|
||||
|
||||
private Logic Ai { get; set; } = null!;
|
||||
private Behavior.Tree Tree { get; set; } = null!;
|
||||
private float TargetVelocity { get; set; }
|
||||
|
||||
private MotionGoal _motionGoal = new MotionGoal.Stop();
|
||||
|
||||
|
||||
private State _state;
|
||||
|
||||
private enum State
|
||||
{
|
||||
Idle,
|
||||
Attacking,
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
_hp = MaxHp;
|
||||
HealthBar.MinValue = 0f;
|
||||
HealthBar.MaxValue = MaxHp;
|
||||
Ai = new();
|
||||
Ai.Set(this);
|
||||
Ai.Start<Logic.Root.Idle>();
|
||||
Tree = CreateTree();
|
||||
}
|
||||
|
||||
private Bt.Tree CreateTree() => new(
|
||||
new Bt.Selector([
|
||||
new Bt.Sequence([
|
||||
new Bt.Check(() => Target is not null),
|
||||
new Bt.Action(() => FaceToward(Target!.GlobalPosition)),
|
||||
new Bt.Selector([
|
||||
new Bt.MemoSequence([
|
||||
new Bt.Sequence([
|
||||
new Bt.Check(() => StabHitbox.GetOverlappingBodies().Contains(Target)),
|
||||
new Bt.MemoSequence([
|
||||
new Bt.Action(Stop),
|
||||
new Bt.Action(() => AnimationPlayer.Play("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.MemoSequence([
|
||||
new Bt.Delay(0.1) { RandomVariation = 0.05 },
|
||||
new Bt.Action(() =>
|
||||
{
|
||||
var left = Target!.GlobalPosition + Vector2.Left * AttackDistance;
|
||||
var right = Target!.GlobalPosition + Vector2.Right * AttackDistance;
|
||||
var targetPosition =
|
||||
GlobalPosition.DistanceSquaredTo(left)
|
||||
< GlobalPosition.DistanceSquaredTo(right)
|
||||
? left
|
||||
: right;
|
||||
AnimationPlayer.Play("idle");
|
||||
return WalkTo(targetPosition)
|
||||
? Bt.Result.Success
|
||||
: Bt.Result.Running;
|
||||
}),
|
||||
]),
|
||||
]),
|
||||
]),
|
||||
new Bt.MemoSequence([
|
||||
new Bt.Check(() => _state is State.Idle),
|
||||
new Bt.Action(() => AnimationPlayer.Play("idle")),
|
||||
new Bt.DoNothing(),
|
||||
]),
|
||||
])
|
||||
);
|
||||
|
||||
public void OnReady()
|
||||
{
|
||||
this.Provide();
|
||||
|
|
@ -42,13 +106,42 @@ public sealed partial class Birb : CharacterBody2D, IAttackable, IProvide<IHitbo
|
|||
|
||||
public void OnPhysicsProcess(double delta)
|
||||
{
|
||||
Ai.Input(new Logic.Input.OnPhysicsProcess(delta));
|
||||
Tree.Process(delta);
|
||||
|
||||
if (!IsOnFloor()) Velocity += GetGravity() * (float)delta;
|
||||
var velocity = Velocity;
|
||||
switch (_motionGoal)
|
||||
{
|
||||
case MotionGoal.Stop:
|
||||
if (!IsOnFloor()) break;
|
||||
velocity.X = Mathf.MoveToward(Velocity.X, 0, (float)delta * Acceleration);
|
||||
break;
|
||||
case MotionGoal.WalkTo(var position):
|
||||
if (!IsOnFloor()) break;
|
||||
var xDifference = position.X - GlobalPosition.X;
|
||||
var distance = Mathf.Abs(xDifference);
|
||||
var breakableSpeed = Mathf.Sqrt(2 * Acceleration * distance);
|
||||
var speedRequiredToReachGoalNow = distance / (float)delta;
|
||||
var speed = Mathf.Min(Speed, Mathf.Min(speedRequiredToReachGoalNow, breakableSpeed));
|
||||
var targetVelocity = Mathf.Sign(xDifference) * speed;
|
||||
velocity.X = Mathf.MoveToward(Velocity.X, targetVelocity, (float)delta * Acceleration);
|
||||
break;
|
||||
default:
|
||||
throw new UnreachableException();
|
||||
}
|
||||
|
||||
if (!IsOnFloor()) velocity += GetGravity() * (float)delta;
|
||||
|
||||
Velocity = velocity;
|
||||
MoveAndSlide();
|
||||
}
|
||||
|
||||
private abstract record MotionGoal
|
||||
{
|
||||
public sealed record Stop : MotionGoal;
|
||||
|
||||
public sealed record WalkTo(Vector2 Position) : MotionGoal;
|
||||
}
|
||||
|
||||
public void OnProcess(double delta)
|
||||
{
|
||||
HealthBar.Value = _hp;
|
||||
|
|
@ -58,40 +151,32 @@ public sealed partial class Birb : CharacterBody2D, IAttackable, IProvide<IHitbo
|
|||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="delta"></param>
|
||||
/// <param name="pos"></param>
|
||||
/// <param name="position"></param>
|
||||
/// <returns>If the goal will be reached this tick or not.</returns>
|
||||
public bool WalkToward(double delta, Vector2 pos)
|
||||
public bool WalkTo(Vector2 position)
|
||||
{
|
||||
var xDifference = pos.X - GlobalPosition.X;
|
||||
var distance = Mathf.Abs(xDifference);
|
||||
var breakableSpeed = Mathf.Sqrt(2 * Acceleration * distance);
|
||||
var speedRequiredToReachGoalNow = distance / (float)delta;
|
||||
var speed = Mathf.Min(Speed, Mathf.Min(speedRequiredToReachGoalNow, breakableSpeed));
|
||||
var targetVelocity = Mathf.Sign(xDifference) * speed;
|
||||
Velocity = Velocity with
|
||||
{
|
||||
X = Mathf.MoveToward(Velocity.X, targetVelocity, (float)delta * Acceleration),
|
||||
};
|
||||
_motionGoal = new MotionGoal.WalkTo(position);
|
||||
if (!IsOnFloor()) return false;
|
||||
var xDifference = position.X - GlobalPosition.X;
|
||||
var speedRequiredToReachGoalNow = Mathf.Abs(xDifference) * Engine.PhysicsTicksPerSecond;
|
||||
return speedRequiredToReachGoalNow < Speed;
|
||||
}
|
||||
|
||||
public void Stop(double delta)
|
||||
{
|
||||
Velocity = Velocity with
|
||||
{
|
||||
X = Mathf.MoveToward(Velocity.X, 0, (float)delta * Acceleration),
|
||||
};
|
||||
}
|
||||
public void Stop() => _motionGoal = new MotionGoal.Stop();
|
||||
|
||||
public void FaceToward(Vector2 pos)
|
||||
{
|
||||
Body.Scale = new(pos.X > GlobalPosition.X ? 1 : -1, 1);
|
||||
}
|
||||
|
||||
public void OnDetectionBodyEntered(Node2D body) => Ai.Input(new Logic.Input.TargetFound(body));
|
||||
public void OnChaseBodyExited(Node2D body) => Ai.Input(new Logic.Input.TargetLost(body));
|
||||
public void OnAttackFinished() => Ai.Input(new Logic.Input.AttackFinished());
|
||||
public void OnDetectionBodyEntered(Node2D body) => Target = body;
|
||||
|
||||
public void OnChaseBodyExited(Node2D body)
|
||||
{
|
||||
if (Target == body) Target = null;
|
||||
}
|
||||
|
||||
public void OnAttackFinished() => _state = State.Idle;
|
||||
|
||||
public void GetAttacked(in Attack attack)
|
||||
{
|
||||
|
|
@ -104,6 +189,7 @@ public sealed partial class Birb : CharacterBody2D, IAttackable, IProvide<IHitbo
|
|||
|
||||
public void OnHitboxAdded(Hitbox hitbox) => hitbox.BodyHit += OnBodyHit;
|
||||
public void OnHitboxRemoved(Hitbox hitbox) => hitbox.BodyHit -= OnBodyHit;
|
||||
|
||||
private void OnBodyHit(Hitbox hitbox, Node2D body)
|
||||
{
|
||||
if (body is not IAttackable attackable) return;
|
||||
|
|
@ -113,150 +199,4 @@ public sealed partial class Birb : CharacterBody2D, IAttackable, IProvide<IHitbo
|
|||
Damage = 1f,
|
||||
});
|
||||
}
|
||||
|
||||
[Meta]
|
||||
public sealed partial class Logic : AutoBlock
|
||||
{
|
||||
public sealed record Data
|
||||
{
|
||||
public float DurationLeft { get; set; }
|
||||
public Node2D? Target { get; set; }
|
||||
}
|
||||
|
||||
public Logic()
|
||||
{
|
||||
Set(new Data());
|
||||
Preallocate<Root>();
|
||||
}
|
||||
|
||||
public static class Input
|
||||
{
|
||||
public readonly record struct AttackFinished;
|
||||
|
||||
public readonly record struct TargetFound(Node2D Target);
|
||||
|
||||
public readonly record struct TargetLost(Node2D Target);
|
||||
|
||||
public readonly record struct OnPhysicsProcess(double Delta);
|
||||
}
|
||||
|
||||
[Meta, StateDiagram]
|
||||
public abstract partial record Root : LogicBlockState
|
||||
{
|
||||
[Meta]
|
||||
public sealed partial record Idle : Root, IGet<Input.TargetFound>, IGet<Input.OnPhysicsProcess>
|
||||
{
|
||||
public Idle()
|
||||
{
|
||||
this.OnEnter(() => { Get<Birb>().AnimationPlayer.Play("idle"); });
|
||||
}
|
||||
|
||||
public Type On(in Input.TargetFound input)
|
||||
{
|
||||
Get<Data>().Target = input.Target;
|
||||
return To<Fight.Think>();
|
||||
}
|
||||
|
||||
public Type On(in Input.OnPhysicsProcess input)
|
||||
{
|
||||
Get<Birb>().Stop(input.Delta);
|
||||
return ToSelf();
|
||||
}
|
||||
}
|
||||
|
||||
[Meta]
|
||||
public abstract partial record Fight : Root, IGet<Input.TargetLost>
|
||||
{
|
||||
public Type On(in Input.TargetLost input)
|
||||
{
|
||||
var data = Get<Data>();
|
||||
if (input.Target != data.Target) return ToSelf();
|
||||
data.Target = null;
|
||||
return To<Idle>();
|
||||
}
|
||||
|
||||
[Meta]
|
||||
public sealed partial record Think : Fight, IGet<Input.OnPhysicsProcess>
|
||||
{
|
||||
public Think()
|
||||
{
|
||||
this.OnEnter(() =>
|
||||
{
|
||||
Get<Data>().DurationLeft = (float)GD.RandRange(0f, 0.4f);
|
||||
Get<Birb>().AnimationPlayer.Play("idle");
|
||||
});
|
||||
}
|
||||
|
||||
public Type On(in Input.OnPhysicsProcess input)
|
||||
{
|
||||
var data = Get<Data>();
|
||||
var birb = Get<Birb>();
|
||||
data.DurationLeft -= (float)input.Delta;
|
||||
if (data.DurationLeft > 0f) return ToSelf();
|
||||
|
||||
birb.FaceToward(data.Target!.GlobalPosition);
|
||||
birb.Stop(input.Delta);
|
||||
return data.Target!.GlobalPosition.DistanceTo(birb.GlobalPosition) < birb.AttackDistance
|
||||
? To<PrepareStrike>()
|
||||
: To<StepToPosition>();
|
||||
}
|
||||
}
|
||||
|
||||
[Meta]
|
||||
public sealed partial record StepToPosition : Fight, IGet<Input.OnPhysicsProcess>
|
||||
{
|
||||
public Type On(in Input.OnPhysicsProcess input)
|
||||
{
|
||||
var birb = Get<Birb>();
|
||||
var data = Get<Data>();
|
||||
birb.FaceToward(data.Target!.GlobalPosition);
|
||||
|
||||
var left = data.Target!.GlobalPosition + Vector2.Left * birb.AttackDistance;
|
||||
var right = data.Target!.GlobalPosition + Vector2.Right * birb.AttackDistance;
|
||||
var targetPosition =
|
||||
birb.GlobalPosition.DistanceSquaredTo(left)
|
||||
< birb.GlobalPosition.DistanceSquaredTo(right)
|
||||
? left
|
||||
: right;
|
||||
return birb.WalkToward(input.Delta, targetPosition)
|
||||
? To<Think>()
|
||||
: ToSelf();
|
||||
}
|
||||
}
|
||||
|
||||
[Meta]
|
||||
public sealed partial record PrepareStrike : Fight, IGet<Input.OnPhysicsProcess>
|
||||
{
|
||||
public PrepareStrike()
|
||||
{
|
||||
this.OnEnter(() =>
|
||||
{
|
||||
|
||||
Get<Data>().DurationLeft = (float)GD.RandRange(0.1f, 0.7f);
|
||||
Get<Birb>().AnimationPlayer.Play("prepare_attack");
|
||||
});
|
||||
}
|
||||
|
||||
public Type On(in Input.OnPhysicsProcess input)
|
||||
{
|
||||
var data = Get<Data>();
|
||||
var birb = Get<Birb>();
|
||||
if (data.Target!.GlobalPosition.DistanceTo(birb.GlobalPosition) >
|
||||
birb.AttackDistance * 1.2) return To<Think>();
|
||||
birb.FaceToward(data.Target!.GlobalPosition);
|
||||
birb.Stop(input.Delta);
|
||||
data.DurationLeft -= (float)input.Delta;
|
||||
return data.DurationLeft > 0f ? ToSelf() : To<Stab>();
|
||||
}
|
||||
}
|
||||
|
||||
[Meta]
|
||||
public sealed partial record Stab : Fight, IGet<Input.AttackFinished>
|
||||
{
|
||||
public Stab() => this.OnEnter(() => Get<Birb>().AnimationPlayer.Play("stab"));
|
||||
public Type On(in Input.AttackFinished input) => To<Think>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@
|
|||
size = Vector2(34, 2)
|
||||
|
||||
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_feb5d"]
|
||||
radius = 6.0
|
||||
|
||||
[sub_resource type="CircleShape2D" id="CircleShape2D_lyo8d"]
|
||||
radius = 60.0
|
||||
|
|
@ -80,6 +81,7 @@ tracks/1/keys = {
|
|||
|
||||
[sub_resource type="Animation" id="Animation_1i6dk"]
|
||||
resource_name = "prepare_attack"
|
||||
length = 24.000010357628017
|
||||
step = 0.1
|
||||
tracks/0/type = "value"
|
||||
tracks/0/imported = false
|
||||
|
|
@ -178,7 +180,6 @@ _data = {
|
|||
collision_layer = 4
|
||||
collision_mask = 65
|
||||
script = ExtResource("1_lyo8d")
|
||||
AttackDistance = 45.0
|
||||
metadata/_edit_group_ = true
|
||||
|
||||
[node name="Body" type="AnimatedSprite2D" parent="." unique_id=581143480]
|
||||
|
|
@ -190,14 +191,13 @@ offset = Vector2(9, -4)
|
|||
|
||||
[node name="StabHitbox" type="Area2D" parent="Body" unique_id=1373491990]
|
||||
unique_name_in_owner = true
|
||||
process_mode = 4
|
||||
visible = false
|
||||
position = Vector2(20, 9)
|
||||
collision_layer = 16
|
||||
collision_mask = 2
|
||||
script = ExtResource("3_bu42x")
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="Body/StabHitbox" unique_id=1331922714]
|
||||
position = Vector2(19, 9)
|
||||
shape = SubResource("RectangleShape2D_8bh2b")
|
||||
debug_color = Color(0.99607843, 0, 0.23137255, 0.41960785)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue