using Godot;    

namespace Newlon.Components.Zombies;

public partial class RuntimeZombieData : Entity
{
    [Export]
    private Armor _armor;
    [Signal] public delegate void HasBeenKilledEventHandler(RuntimeZombieData who);
    public bool AbleToEat = true;

    public override void _Ready()
    {
        base._Ready();


        LocalTimescale += (float)GD.RandRange(-0.05, 0.05);
    }

    public override void Heal(float amount, Node origin)
    {
        if (_armor != null)
        {
            HP += _armor.Heal(amount);
        }
        else
            HP += amount;
        EmitSignal(SignalName.OnHPChanged, amount, origin);

        if (HP > MaxHP)
        {
            HP = MaxHP;
        }
    }
    public override void TakeDamage(float amount, Node origin)
    {
        if (_armor != null)
        {
            HP -= _armor.RecieveDamage(amount);
        }
        else
            HP -= amount;
        EmitSignal(SignalName.OnHPChanged, -amount, origin);
        EmitSignal(SignalName.OnDamaged);

        if (HP <= 0)
        {
            KillByDamage();
        }
    }
    #region Death sequence
    private bool _killed = false;
    public override void KillByDamage()
    {
        if (_killed) return;
        _killed = true;
        AbleToEat = false;
        EmitSignal(SignalName.HasBeenKilled,this);
    }
    
    #endregion
}
