#include "TPSHealthComponent.h" #include "GameFramework/Actor.h" // Sets default values for this component's properties UTPSHealthComponent::UTPSHealthComponent() { DefaultHealth = 100; } // Called when the game starts void UTPSHealthComponent::BeginPlay() { Super::BeginPlay(); AActor* MyOwner = GetOwner(); if (MyOwner) { MyOwner->OnTakeAnyDamage.AddDynamic(this, &UTPSHealthComponent::HandleTakeAnyDamage); } Health = DefaultHealth; } void UTPSHealthComponent::HandleTakeAnyDamage(AActor* DamagedActor, float Damage, const class UDamageType* DamageType, class AController* InstigatedBy, AActor* DamageCauser) { if (Damage <= 0.0f) {return;} Health = FMath::Clamp(Health - Damage, 0.0f, DefaultHealth); UE_LOG(LogTemp, Log, TEXT("Health Changed: %s"), *FString::SanitizeFloat(Health)); OnHealthChanged.Broadcast(this, Health, Damage, DamageType, InstigatedBy, DamageCauser); } float UTPSHealthComponent::GetHealth() const { return Health; } void UTPSHealthComponent::Heal(float HealAmount) { if (HealAmount <= 0.0f || Health <= 0.0f) { return; } Health = FMath::Clamp(Health + HealAmount, 0.0f, DefaultHealth); UE_LOG(LogTemp, Log, TEXT("Health is now: %s (+%s)"), *FString::SanitizeFloat(Health), *FString::SanitizeFloat(HealAmount)); OnHealthChanged.Broadcast(this, Health, -HealAmount, nullptr, nullptr, nullptr); }