birb stab
This commit is contained in:
parent
00b76b6b25
commit
96bec5a6b7
11 changed files with 309 additions and 120 deletions
|
|
@ -9,51 +9,62 @@ public sealed partial class Birb : CharacterBody2D, IAttackable
|
|||
{
|
||||
public override void _Notification(int what) => this.Notify(what);
|
||||
|
||||
[Export] public float Speed { get; set; } = 60f;
|
||||
[Export] public float AttackDistance { get; set; } = 32f;
|
||||
[Export] public float ThinkTime { get; set; } = 0.2f;
|
||||
[Export] public float Speed { get; set; } = 90f;
|
||||
[Export] public float AttackDistance { get; set; } = 30f;
|
||||
[Export] public float Weight { get; set; } = 20f;
|
||||
[Export] public float MaxHp { get; set; } = 4f;
|
||||
[Export] public float Acceleration { get; set; } = 700f;
|
||||
|
||||
[Node] private ProgressBar HealthBar { get; set; } = null!;
|
||||
[Node] private AnimatedSprite2D BodySprite { get; set; } = null!;
|
||||
[Node] private AnimatedSprite2D Body { get; set; } = null!;
|
||||
[Node] private AnimationPlayer AnimationPlayer { get; set; } = null!;
|
||||
|
||||
private float _hp;
|
||||
private float _timeThought;
|
||||
private Node2D? _target;
|
||||
private float _targetVelocity;
|
||||
|
||||
public override void _Ready()
|
||||
private Logic Ai { get; set; } = null!;
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
_hp = MaxHp;
|
||||
HealthBar.MinValue = 0f;
|
||||
HealthBar.MaxValue = MaxHp;
|
||||
Ai = new();
|
||||
Ai.Set(this);
|
||||
Ai.Start<Logic.Root.Idle>();
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
public void OnReady()
|
||||
{
|
||||
_timeThought += (float)delta;
|
||||
if (_timeThought >= ThinkTime)
|
||||
{
|
||||
_timeThought -= ThinkTime;
|
||||
if (_target is not null)
|
||||
{
|
||||
var xDifference = _target.Position.X - Position.X;
|
||||
var distance = Mathf.Abs(xDifference);
|
||||
if (Mathf.Abs(distance - AttackDistance) > Speed * ThinkTime)
|
||||
_targetVelocity = Mathf.Sign(xDifference) * Mathf.Sign(distance - AttackDistance) * Speed;
|
||||
else
|
||||
_targetVelocity = 0f;
|
||||
BodySprite.FlipH = xDifference < 0;
|
||||
}
|
||||
else
|
||||
_targetVelocity = 0f;
|
||||
}
|
||||
SetPhysicsProcess(true);
|
||||
SetProcess(true);
|
||||
}
|
||||
|
||||
Velocity = Velocity with
|
||||
{
|
||||
X = Mathf.MoveToward(Velocity.X, _targetVelocity, (float)delta * 500f)
|
||||
};
|
||||
public void OnPhysicsProcess(double delta)
|
||||
{
|
||||
// _timeThought += (float)delta;
|
||||
// if (_timeThought >= ThinkTime)
|
||||
// {
|
||||
// _timeThought -= ThinkTime;
|
||||
// if (_target is not null)
|
||||
// {
|
||||
// var xDifference = _target.Position.X - Position.X;
|
||||
// var distance = Mathf.Abs(xDifference);
|
||||
// if (Mathf.Abs(distance - AttackDistance) > Speed * ThinkTime)
|
||||
// _targetVelocity = Mathf.Sign(xDifference) * Mathf.Sign(distance - AttackDistance) * Speed;
|
||||
// else
|
||||
// _targetVelocity = 0f;
|
||||
// BodySprite.FlipH = xDifference < 0;
|
||||
// }
|
||||
// else
|
||||
// _targetVelocity = 0f;
|
||||
// }
|
||||
//
|
||||
// Velocity = Velocity with
|
||||
// {
|
||||
// X = Mathf.MoveToward(Velocity.X, _targetVelocity, (float)delta * 500f)
|
||||
// };
|
||||
|
||||
Ai.Input(new Logic.Input.OnPhysicsProcess(delta));
|
||||
|
||||
if (!IsOnFloor()) Velocity += GetGravity() * (float)delta;
|
||||
|
||||
|
|
@ -66,13 +77,44 @@ public sealed partial class Birb : CharacterBody2D, IAttackable
|
|||
HealthBar.Visible = _hp < MaxHp;
|
||||
}
|
||||
|
||||
public void OnDetectionBodyEntered(Node2D body) => _target = body;
|
||||
|
||||
public void OnChaseBodyExited(Node2D body)
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="delta"></param>
|
||||
/// <param name="pos"></param>
|
||||
/// <returns>If the goal will be reached this tick or not.</returns>
|
||||
public bool WalkToward(double delta, Vector2 pos)
|
||||
{
|
||||
if (body == _target) _target = null;
|
||||
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)
|
||||
};
|
||||
return speedRequiredToReachGoalNow < Speed;
|
||||
}
|
||||
|
||||
public void Stop(double delta)
|
||||
{
|
||||
Velocity = Velocity with
|
||||
{
|
||||
X = Mathf.MoveToward(Velocity.X, 0, (float)delta * Acceleration)
|
||||
};
|
||||
}
|
||||
|
||||
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 GetAttacked(in Attack attack)
|
||||
{
|
||||
_hp -= attack.Damage;
|
||||
|
|
@ -85,84 +127,143 @@ public sealed partial class Birb : CharacterBody2D, IAttackable
|
|||
{
|
||||
public sealed record Data
|
||||
{
|
||||
public required Birb Birb { get; set; }
|
||||
public float DurationLeft { get; set; }
|
||||
public Node2D? Target { get; set; }
|
||||
}
|
||||
|
||||
public Logic(Birb birb)
|
||||
public Logic()
|
||||
{
|
||||
Set(new Data { Birb = birb });
|
||||
Set(new Data());
|
||||
Preallocate<Root>();
|
||||
}
|
||||
|
||||
public static class Input
|
||||
{
|
||||
public readonly record struct TimerFinished;
|
||||
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
|
||||
{
|
||||
public virtual void OnPhysicsProcess(double delta)
|
||||
[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 sealed partial record Idle : Root;
|
||||
|
||||
[Meta]
|
||||
public abstract partial record Fight : Root
|
||||
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.TimerFinished>
|
||||
public sealed partial record Think : Fight, IGet<Input.OnPhysicsProcess>
|
||||
{
|
||||
public Think()
|
||||
{
|
||||
this.OnEnter(() => Get<Data>().DurationLeft = (float)GD.RandRange(0.3, 0.6));
|
||||
this.OnEnter(() =>
|
||||
{
|
||||
Get<Data>().DurationLeft = (float)GD.RandRange(0f, 0.4f);
|
||||
Get<Birb>().AnimationPlayer.Play("idle");
|
||||
});
|
||||
}
|
||||
|
||||
public override void OnPhysicsProcess(double delta)
|
||||
public Type On(in Input.OnPhysicsProcess input)
|
||||
{
|
||||
var data = Get<Data>();
|
||||
data.DurationLeft -= (float)delta;
|
||||
if (data.DurationLeft <= 0f) Input(new Input.TimerFinished());
|
||||
}
|
||||
var birb = Get<Birb>();
|
||||
data.DurationLeft -= (float)input.Delta;
|
||||
if (data.DurationLeft > 0f) return ToSelf();
|
||||
|
||||
public Type On(in Input.TimerFinished input)
|
||||
{
|
||||
var data = Get<Data>();
|
||||
if (data.Target is not { } target) return To<Idle>();
|
||||
return target.GlobalPosition.DistanceTo(data.Birb.GlobalPosition) <
|
||||
data.Birb.AttackDistance
|
||||
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;
|
||||
|
||||
[Meta]
|
||||
public sealed partial record PrepareStrike : Fight, IGet<Input.TimerFinished>
|
||||
public sealed partial record StepToPosition : Fight, IGet<Input.OnPhysicsProcess>
|
||||
{
|
||||
public PrepareStrike()
|
||||
{
|
||||
this.OnEnter(() => Get<Data>().DurationLeft = (float)GD.RandRange(0.7, 0.9));
|
||||
}
|
||||
|
||||
public override void OnPhysicsProcess(double delta)
|
||||
public Type On(in Input.OnPhysicsProcess input)
|
||||
{
|
||||
var birb = Get<Birb>();
|
||||
var data = Get<Data>();
|
||||
data.DurationLeft -= (float)delta;
|
||||
if (data.DurationLeft <= 0f) Input(new Input.TimerFinished());
|
||||
}
|
||||
birb.FaceToward(data.Target!.GlobalPosition);
|
||||
|
||||
public Type On(in Input.TimerFinished input) => To<Stab>();
|
||||
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 Stab : Fight;
|
||||
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>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -11,7 +11,7 @@ height = 14.0
|
|||
radius = 52.0
|
||||
|
||||
[sub_resource type="CircleShape2D" id="CircleShape2D_ba6cv"]
|
||||
radius = 110.0
|
||||
radius = 160.0
|
||||
|
||||
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_lyo8d"]
|
||||
bg_color = Color(0.15, 0.15, 0.15, 0.34509805)
|
||||
|
|
@ -25,7 +25,7 @@ step = 0.1
|
|||
tracks/0/type = "value"
|
||||
tracks/0/imported = false
|
||||
tracks/0/enabled = true
|
||||
tracks/0/path = NodePath("BodySprite:animation")
|
||||
tracks/0/path = NodePath("Body:animation")
|
||||
tracks/0/interp = 1
|
||||
tracks/0/loop_wrap = true
|
||||
tracks/0/keys = {
|
||||
|
|
@ -37,7 +37,7 @@ tracks/0/keys = {
|
|||
tracks/1/type = "value"
|
||||
tracks/1/imported = false
|
||||
tracks/1/enabled = true
|
||||
tracks/1/path = NodePath("BodySprite:frame")
|
||||
tracks/1/path = NodePath("Body:frame")
|
||||
tracks/1/interp = 1
|
||||
tracks/1/loop_wrap = true
|
||||
tracks/1/keys = {
|
||||
|
|
@ -54,7 +54,7 @@ step = 0.1
|
|||
tracks/0/type = "value"
|
||||
tracks/0/imported = false
|
||||
tracks/0/enabled = true
|
||||
tracks/0/path = NodePath("BodySprite:animation")
|
||||
tracks/0/path = NodePath("Body:animation")
|
||||
tracks/0/interp = 1
|
||||
tracks/0/loop_wrap = true
|
||||
tracks/0/keys = {
|
||||
|
|
@ -66,7 +66,7 @@ tracks/0/keys = {
|
|||
tracks/1/type = "value"
|
||||
tracks/1/imported = false
|
||||
tracks/1/enabled = true
|
||||
tracks/1/path = NodePath("BodySprite:frame")
|
||||
tracks/1/path = NodePath("Body:frame")
|
||||
tracks/1/interp = 1
|
||||
tracks/1/loop_wrap = true
|
||||
tracks/1/keys = {
|
||||
|
|
@ -82,7 +82,7 @@ step = 0.1
|
|||
tracks/0/type = "value"
|
||||
tracks/0/imported = false
|
||||
tracks/0/enabled = true
|
||||
tracks/0/path = NodePath("BodySprite:animation")
|
||||
tracks/0/path = NodePath("Body:animation")
|
||||
tracks/0/interp = 1
|
||||
tracks/0/loop_wrap = true
|
||||
tracks/0/keys = {
|
||||
|
|
@ -94,7 +94,7 @@ tracks/0/keys = {
|
|||
tracks/1/type = "value"
|
||||
tracks/1/imported = false
|
||||
tracks/1/enabled = true
|
||||
tracks/1/path = NodePath("BodySprite:frame")
|
||||
tracks/1/path = NodePath("Body:frame")
|
||||
tracks/1/interp = 1
|
||||
tracks/1/loop_wrap = true
|
||||
tracks/1/keys = {
|
||||
|
|
@ -106,11 +106,12 @@ tracks/1/keys = {
|
|||
|
||||
[sub_resource type="Animation" id="Animation_3ly8m"]
|
||||
resource_name = "stab"
|
||||
length = 0.9
|
||||
step = 0.1
|
||||
tracks/0/type = "value"
|
||||
tracks/0/imported = false
|
||||
tracks/0/enabled = true
|
||||
tracks/0/path = NodePath("BodySprite:animation")
|
||||
tracks/0/path = NodePath("Body:animation")
|
||||
tracks/0/interp = 1
|
||||
tracks/0/loop_wrap = true
|
||||
tracks/0/keys = {
|
||||
|
|
@ -122,14 +123,28 @@ tracks/0/keys = {
|
|||
tracks/1/type = "value"
|
||||
tracks/1/imported = false
|
||||
tracks/1/enabled = true
|
||||
tracks/1/path = NodePath("BodySprite:frame")
|
||||
tracks/1/path = NodePath("Body:frame")
|
||||
tracks/1/interp = 1
|
||||
tracks/1/loop_wrap = true
|
||||
tracks/1/keys = {
|
||||
"times": PackedFloat32Array(0),
|
||||
"transitions": PackedFloat32Array(1),
|
||||
"times": PackedFloat32Array(0, 0.1, 0.5),
|
||||
"transitions": PackedFloat32Array(1, 1, 1),
|
||||
"update": 1,
|
||||
"values": [0]
|
||||
"values": [0, 1, 2]
|
||||
}
|
||||
tracks/2/type = "method"
|
||||
tracks/2/imported = false
|
||||
tracks/2/enabled = true
|
||||
tracks/2/path = NodePath(".")
|
||||
tracks/2/interp = 1
|
||||
tracks/2/loop_wrap = true
|
||||
tracks/2/keys = {
|
||||
"times": PackedFloat32Array(0.9),
|
||||
"transitions": PackedFloat32Array(1),
|
||||
"values": [{
|
||||
"args": [],
|
||||
"method": &"OnAttackFinished"
|
||||
}]
|
||||
}
|
||||
|
||||
[sub_resource type="AnimationLibrary" id="AnimationLibrary_1i6dk"]
|
||||
|
|
@ -145,11 +160,11 @@ collision_layer = 4
|
|||
script = ExtResource("1_lyo8d")
|
||||
metadata/_edit_group_ = true
|
||||
|
||||
[node name="BodySprite" type="AnimatedSprite2D" parent="." unique_id=581143480]
|
||||
[node name="Body" type="AnimatedSprite2D" parent="." unique_id=581143480]
|
||||
unique_name_in_owner = true
|
||||
position = Vector2(2, -4)
|
||||
sprite_frames = ExtResource("2_ba6cv")
|
||||
animation = &"idle"
|
||||
offset = Vector2(9, -4)
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=1744954023]
|
||||
position = Vector2(0, 1)
|
||||
|
|
@ -189,6 +204,7 @@ value = 50.0
|
|||
show_percentage = false
|
||||
|
||||
[node name="AnimationPlayer" type="AnimationPlayer" parent="." unique_id=695394949]
|
||||
unique_name_in_owner = true
|
||||
libraries/ = SubResource("AnimationLibrary_1i6dk")
|
||||
|
||||
[connection signal="body_entered" from="Detection" to="." method="OnDetectionBodyEntered"]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue