From d93127a74e41d941b27cd7f45734c1fe15673382 Mon Sep 17 00:00:00 2001 From: baz Date: Wed, 6 Aug 2025 21:35:25 +0100 Subject: [PATCH] Extra checks in SetCurrentHealth --- Source/vampires/HealthComponent.cpp | 24 ++++++++++++++++++++---- Source/vampires/HealthComponent.h | 1 + 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/Source/vampires/HealthComponent.cpp b/Source/vampires/HealthComponent.cpp index 69585a0..c634f78 100644 --- a/Source/vampires/HealthComponent.cpp +++ b/Source/vampires/HealthComponent.cpp @@ -59,12 +59,26 @@ float UHealthComponent::GetCurrentHealth() void UHealthComponent::SetCurrentHealth(float Value) { - CurrentHealth = Value; - if (CurrentHealth > MaxHealth) + if (Value > MaxHealth) { CurrentHealth = MaxHealth; } + else if (Value <= 0.0f) + { + IsDead = true; + OnDeath.Broadcast({GetOwner(), CurrentHealth - Value, nullptr, nullptr, nullptr}); + CurrentHealth = 0.0f; + } + else + { + if (Value < CurrentHealth) + { + OnDamaged.Broadcast({GetOwner(), CurrentHealth - Value, nullptr, nullptr, nullptr}); + } + + CurrentHealth = Value; + } } void UHealthComponent::ResetHealth() @@ -75,8 +89,10 @@ void UHealthComponent::ResetHealth() void UHealthComponent::RecoverHealth(float value) { - // TODO: We might want to add some extra checking here - IncrementHealth(value); + if (value > 0) + { + IncrementHealth(value); + } } bool UHealthComponent::GetIsDead() diff --git a/Source/vampires/HealthComponent.h b/Source/vampires/HealthComponent.h index a2b4f95..462b268 100644 --- a/Source/vampires/HealthComponent.h +++ b/Source/vampires/HealthComponent.h @@ -47,6 +47,7 @@ protected: UPROPERTY(VisibleAnywhere) float CurrentHealth; +private: bool IsDead = false; bool CanDamage = true;