65 lines
1.2 KiB
C++
65 lines
1.2 KiB
C++
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
#include "Player/LMHealthComponent.h"
|
|
|
|
#include "Player/LMHitBox.h"
|
|
|
|
|
|
ULMHealthComponent::ULMHealthComponent()
|
|
{
|
|
PrimaryComponentTick.bCanEverTick = true;
|
|
}
|
|
|
|
void ULMHealthComponent::BeginPlay()
|
|
{
|
|
Super::BeginPlay();
|
|
|
|
if (bMaxHealthOnStart)
|
|
Health = MaxHealth;
|
|
|
|
RegisterHitBoxes();
|
|
}
|
|
|
|
void ULMHealthComponent::RegisterHitBoxes()
|
|
{
|
|
GetOwner()->GetComponents<ULMHitBox>(HitBoxes);
|
|
for (const auto HitBox : HitBoxes)
|
|
{
|
|
HitBox->OnHitBoxHit.AddUniqueDynamic(this, &ULMHealthComponent::HitBoxHit);
|
|
HitBox->SetHealthComponent(this);
|
|
}
|
|
}
|
|
|
|
void ULMHealthComponent::TakeDamage_Implementation(const float Damage)
|
|
{
|
|
if (Damage <= 0.0f)
|
|
return;
|
|
|
|
OnDamageReceived(Damage);
|
|
OnHealthChanged.Broadcast(Health, Damage);
|
|
|
|
if (Health <= 0.0f)
|
|
Kill();
|
|
}
|
|
|
|
void ULMHealthComponent::OnDamageReceived_Implementation(const float Damage)
|
|
{
|
|
Health = FMath::Clamp(Health - Damage, 0.0f, MaxHealth);
|
|
}
|
|
|
|
|
|
void ULMHealthComponent::Kill()
|
|
{
|
|
OnKill();
|
|
OnDeath.Broadcast();
|
|
}
|
|
|
|
void ULMHealthComponent::HitBoxHit(ULMHitBox* HitBox, const float Damage, const FVector& Origin, const FHitResult& HitInfo)
|
|
{
|
|
TakeDamage(Damage);
|
|
OnHit.Broadcast(Damage, Origin, HitInfo);
|
|
}
|
|
|
|
|