Advertisement
Guest User

Untitled

a guest
May 7th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1.  
  2. #include "TPSHealthComponent.h"
  3. #include "GameFramework/Actor.h"
  4.  
  5.  
  6. // Sets default values for this component's properties
  7. UTPSHealthComponent::UTPSHealthComponent()
  8. {
  9.     DefaultHealth = 100;
  10. }
  11.  
  12.  
  13. // Called when the game starts
  14. void UTPSHealthComponent::BeginPlay()
  15. {
  16.     Super::BeginPlay();
  17.  
  18.     AActor* MyOwner = GetOwner();
  19.     if (MyOwner)
  20.     {  
  21.         MyOwner->OnTakeAnyDamage.AddDynamic(this, &UTPSHealthComponent::HandleTakeAnyDamage);
  22.     }
  23.  
  24.     Health = DefaultHealth;
  25. }
  26.  
  27. void UTPSHealthComponent::HandleTakeAnyDamage(AActor* DamagedActor, float Damage, const class UDamageType* DamageType, class AController* InstigatedBy, AActor* DamageCauser)
  28. {
  29.     if (Damage <= 0.0f) {return;}
  30.  
  31.     Health = FMath::Clamp(Health - Damage, 0.0f, DefaultHealth);
  32.     UE_LOG(LogTemp, Log, TEXT("Health Changed: %s"), *FString::SanitizeFloat(Health));
  33.     OnHealthChanged.Broadcast(this, Health, Damage, DamageType, InstigatedBy, DamageCauser);
  34. }
  35.  
  36. float UTPSHealthComponent::GetHealth() const
  37. {
  38.     return Health;
  39. }
  40.  
  41. void UTPSHealthComponent::Heal(float HealAmount)
  42. {
  43.     if (HealAmount <= 0.0f || Health <= 0.0f)
  44.     {
  45.         return;
  46.     }
  47.  
  48.     Health = FMath::Clamp(Health + HealAmount, 0.0f, DefaultHealth);
  49.  
  50.     UE_LOG(LogTemp, Log, TEXT("Health is now: %s (+%s)"), *FString::SanitizeFloat(Health), *FString::SanitizeFloat(HealAmount));
  51.  
  52.     OnHealthChanged.Broadcast(this, Health, -HealAmount, nullptr, nullptr, nullptr);
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement