Add core game components including ConfigFileHandler, GameManager, SaveSystem, and UIManager
This commit is contained in:
65
scripts/components/HealthComponent.cs
Normal file
65
scripts/components/HealthComponent.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using System.Threading.Tasks;
|
||||
using Godot;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.components;
|
||||
|
||||
public partial class HealthComponent : Node
|
||||
{
|
||||
[Export] public float Health { get; set; } = 1.0f;
|
||||
[Export] public float MaxHealth { get; set; } = 1.0f;
|
||||
[Export] public AudioStreamPlayer2D HurtSfx { get; set; }
|
||||
[Export] public AudioStreamPlayer2D HealSfx { get; set; }
|
||||
|
||||
[Signal] public delegate void HealthChangedEventHandler(float delta, float totalHealth);
|
||||
[Signal] public delegate void DeathEventHandler();
|
||||
|
||||
public void SetHealth(float newValue)
|
||||
{
|
||||
_ = ApplyHealthChange(newValue);
|
||||
}
|
||||
|
||||
public void IncreaseHealth(float delta)
|
||||
{
|
||||
_ = ApplyHealthChange(Health + delta);
|
||||
}
|
||||
|
||||
public void DecreaseHealth(float delta)
|
||||
{
|
||||
_ = ApplyHealthChange(Health - delta);
|
||||
}
|
||||
|
||||
public float GetDelta(float newValue) => newValue - Health;
|
||||
|
||||
private async Task ApplyHealthChange(float newHealth, bool playSfx = true)
|
||||
{
|
||||
newHealth = Mathf.Clamp(newHealth, 0.0f, MaxHealth);
|
||||
var delta = newHealth - Health;
|
||||
|
||||
if (delta == 0.0f)
|
||||
return;
|
||||
|
||||
if (playSfx)
|
||||
{
|
||||
if (delta > 0f && HealSfx != null)
|
||||
{
|
||||
HealSfx.Play();
|
||||
}
|
||||
else if (delta < 0f && HurtSfx != null)
|
||||
{
|
||||
HurtSfx.Play();
|
||||
await HurtSfx.ToSignal(HurtSfx, AudioStreamPlayer2D.SignalName.Finished);
|
||||
}
|
||||
}
|
||||
|
||||
Health = newHealth;
|
||||
|
||||
if (Health <= 0f)
|
||||
{
|
||||
EmitSignalDeath();
|
||||
}
|
||||
else
|
||||
{
|
||||
EmitSignalHealthChanged(delta, Health);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user