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>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue