61 lines
997 B
C#
61 lines
997 B
C#
using System.Threading.Tasks;
|
|
using Godot;
|
|
|
|
namespace NodeWar.scripts.components;
|
|
|
|
public partial class HealthComponent : ProgressBar
|
|
{
|
|
[Export]
|
|
public Sprite2D Gfx { get; set; }
|
|
|
|
[Export]
|
|
public float Health { get; set; } = 100;
|
|
|
|
[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)
|
|
{
|
|
Value = Health;
|
|
}
|
|
}
|