using System; using System.Threading.Tasks; using Godot; namespace NodeWar.scripts.components; public partial class HealthComponent : ProgressBar { private float _health = 100; [Export] public Sprite2D Gfx { get; set; } [Export] public float Health { get => _health; set { if (value >= MaxHealth) { _health = MaxHealth; } _health = value; } } [Export] public float MaxHealth { get; set; } = 100; [Signal] public delegate float HealthChangedEventHandler(float oldHealth, float newHealth); public void TakeDamage(float damage) { Health -= damage; EmitSignal(SignalName.HealthChanged, Health + damage, Health); if (Health <= 0) { Die(); return; } if (Gfx != null) { var originalColor = Gfx.Modulate; Gfx.Modulate = new Color(10, 10, 10); GetTree().CreateTimer(0.1f).Timeout += () => { Gfx.Modulate = originalColor; }; } } public void Die() { GD.Print("I am dying!"); GetParent().QueueFree(); } public override void _Ready() { Value = Health; MaxValue = MaxHealth; } public override void _Process(double delta) { Visible = Math.Abs(Health - MaxHealth) > 0.001f; Value = Health; } }