// Project Sword & Gun Copyright © 2021 Kevin Poretti


#include "Components/SNGHealthComponent.h"

// Sets default values for this component's properties
USNGHealthComponent::USNGHealthComponent()
{
	// Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
	// off to improve performance if you don't need them.
	PrimaryComponentTick.bCanEverTick = false;

	DefaultHealth = 100.0f;
	// ...
}

float USNGHealthComponent::GetHealth() const
{
	return Health;
}

void USNGHealthComponent::AddHealth(float Delta)
{
	Health = FMath::Clamp(Health + Delta, 0.0f, DefaultHealth);

	UE_LOG(LogTemp, Log, TEXT("Health changed %s"), *FString::SanitizeFloat(Health));
	
	OnHealthChanged.Broadcast(this, Health, Delta);
}


void USNGHealthComponent::SubstractHealth(float Delta)
{
	Health = FMath::Clamp(Health - Delta, 0.0f, DefaultHealth);

	UE_LOG(LogTemp, Log, TEXT("Health changed %s"), *FString::SanitizeFloat(Health));
	
	OnHealthChanged.Broadcast(this, Health, Delta);
}

// Called when the game starts
void USNGHealthComponent::BeginPlay()
{
	Super::BeginPlay();
	
	Health = DefaultHealth;
}

