You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.7 KiB
65 lines
1.7 KiB
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
#include "MHealthComponent.h"
|
|
#include "MGameMode.h"
|
|
|
|
UMHealthComponent::UMHealthComponent()
|
|
{
|
|
DefaultHealth = 100.f;
|
|
bIsDead = false;
|
|
bIsInvincible = false;
|
|
}
|
|
|
|
|
|
void UMHealthComponent::BeginPlay()
|
|
{
|
|
Super::BeginPlay();
|
|
|
|
AActor* ComponentOwner = GetOwner();
|
|
if(ComponentOwner)
|
|
ComponentOwner->OnTakeAnyDamage.AddDynamic(this, &UMHealthComponent::HandleTakeAnyDamage);
|
|
|
|
Health = DefaultHealth;
|
|
}
|
|
|
|
|
|
void UMHealthComponent::HandleTakeAnyDamage(AActor* DamagedActor, float Damage, const class UDamageType* DamageType, class AController* InstigatedBy, AActor* DamageCauser)
|
|
{
|
|
if(bIsDead)
|
|
return;
|
|
|
|
if(!bIsInvincible)
|
|
Health = FMath::Clamp(Health - Damage, 0.f, DefaultHealth);
|
|
bIsDead = Health <= 0.f;
|
|
OnHealthChanged.Broadcast(this, Health, -(Health - Damage), DamageType, InstigatedBy, DamageCauser);
|
|
|
|
if(bIsDead)
|
|
{
|
|
AMGameMode* GM = Cast<AMGameMode>(GetWorld()->GetAuthGameMode());
|
|
if(GM)
|
|
{
|
|
GM->OnActorKilled.Broadcast(DamagedActor, DamageCauser, InstigatedBy);
|
|
}
|
|
|
|
OnActorDeath.Broadcast(this, DamagedActor, DamageCauser);
|
|
}
|
|
}
|
|
|
|
float UMHealthComponent::GetHealthRemappedInRange(float Min, float Max)
|
|
{
|
|
const FVector2D InputRange(0.f, DefaultHealth);
|
|
const FVector2D OutputRange(Min, Max);
|
|
return FMath::GetMappedRangeValueClamped(InputRange, OutputRange, Health);
|
|
}
|
|
|
|
|
|
void UMHealthComponent::Heal(float HealAmount)
|
|
{
|
|
if(HealAmount <= 0.f || bIsDead || Health <= 0.f)
|
|
return;
|
|
|
|
Health = FMath::Clamp(Health + HealAmount, 0.f, DefaultHealth);
|
|
OnHealthChanged.Broadcast(this, Health, HealAmount, nullptr, nullptr, nullptr);
|
|
}
|
|
|
|
|