This repository has been archived on 2025-04-18. You can view files and clone it, but cannot push or open issues or pull requests.
LegumeMix/Source/LegumeMix/Private/Player/LMHealthComponent.cpp
TjgL db589dc845 Transformed OnHit to delegate
Signed-off-by: TjgL <lithmoneo@gmail.com>
2025-03-14 15:11:00 +01:00

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);
}