202 lines
5.7 KiB
C#
202 lines
5.7 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using Bt = Dancer.Behavior;
|
|
|
|
namespace Dancer;
|
|
|
|
[Meta(typeof(IAutoNode))]
|
|
public sealed partial class Birb : CharacterBody2D, IAttackable,
|
|
IProvide<IHitboxOwner>, IHitboxOwner
|
|
{
|
|
public override void _Notification(int what) => this.Notify(what);
|
|
|
|
[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 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 Bt.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;
|
|
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.PlayAnimation("prepare_attack"),
|
|
new Bt.Delay(0.4) { RandomVariation = 0.2 },
|
|
]),
|
|
]),
|
|
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;
|
|
var right = Target!.GlobalPosition + Vector2.Right * AttackDistance;
|
|
var targetPosition =
|
|
GlobalPosition.DistanceSquaredTo(left)
|
|
< GlobalPosition.DistanceSquaredTo(right)
|
|
? left
|
|
: right;
|
|
return WalkTo(targetPosition)
|
|
? Bt.Result.Success
|
|
: Bt.Result.Running;
|
|
}),
|
|
]),
|
|
]),
|
|
]),
|
|
new Bt.MemoSequence([
|
|
new Bt.Check(() => _state is State.Idle),
|
|
new Bt.PlayAnimation("idle"),
|
|
new Bt.DoNothing(),
|
|
]),
|
|
])
|
|
)
|
|
{
|
|
AnimationPlayer = AnimationPlayer,
|
|
};
|
|
|
|
public void OnReady()
|
|
{
|
|
this.Provide();
|
|
SetPhysicsProcess(true);
|
|
SetProcess(true);
|
|
}
|
|
|
|
public void OnPhysicsProcess(double delta)
|
|
{
|
|
Tree.Process(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;
|
|
HealthBar.Visible = _hp < MaxHp;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="position"></param>
|
|
/// <returns>If the goal will be reached this tick or not.</returns>
|
|
public bool WalkTo(Vector2 position)
|
|
{
|
|
_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() => _motionGoal = new MotionGoal.Stop();
|
|
|
|
public void FaceToward(Vector2 pos)
|
|
{
|
|
Body.Scale = new(pos.X > GlobalPosition.X ? 1 : -1, 1);
|
|
}
|
|
|
|
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)
|
|
{
|
|
_hp -= attack.Damage;
|
|
Velocity += attack.Impulse / Weight;
|
|
if (_hp <= 0.01) QueueFree();
|
|
}
|
|
|
|
IHitboxOwner IProvide<IHitboxOwner>.Value() => this;
|
|
|
|
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;
|
|
attackable.GetAttacked(new()
|
|
{
|
|
Attacker = this,
|
|
Damage = 1f,
|
|
});
|
|
}
|
|
}
|