void UHealthComponent::BeginPlay()
{
Super::BeginPlay();
GameModeRef = Cast<ATankGameModeBase>(UGameplayStatics::GetGameMode(GetWorld()));
Owner = GetOwner();
if(Owner)
{
Owner->OnTakeAnyDamage.AddDynamic(this, &UHealthComponent::TakeDamage);
}
else
{
UE_LOG(LogTemp, Warning, TEXT("Health component has no reference to the Owner"));
}
}
void UHealthComponent::TakeDamage(AActor* DamagedActor, float Damage, const class UDamageType* DamageType, class AController* InstigatedBy, AActor* DamageActor)
{
if(Damage == 0 || Health == 0)
{
return;
}
Health = FMath::Clamp(Health - Damage, 0.0f, DefaultHealth);
if(Health <= 0)
{
if(GameModeRef)
{
GameModeRef->ActorDied(Owner);
}
else
{
UE_LOG(LogTemp, Warning, TEXT("Health component has no reference to the GameMode"));
}
}
}