Newer
Older
SkyFrontier-Project-IADE-UE4-3D / Source / SkyFrontier / Private / HealthSystem.cpp
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #include "HealthSystem.h"
  3. UHealthSystem::UHealthSystem()
  4. {
  5. PrimaryComponentTick.bCanEverTick = false;
  6. PrimaryComponentTick.bStartWithTickEnabled = false;
  7. }
  8. void UHealthSystem::BeginPlay()
  9. {
  10. Super::BeginPlay();
  11. Health = MaxHealth;
  12. Shield = 0;
  13. }
  14. float UHealthSystem::GetHealth() const
  15. {
  16. return Health;
  17. }
  18. float UHealthSystem::GetMaxHealth() const
  19. {
  20. return MaxHealth;
  21. }
  22. float UHealthSystem::GetHealthAsPercentage() const
  23. {
  24. return Health / MaxHealth;
  25. }
  26. float UHealthSystem::GetShield() const
  27. {
  28. return Shield;
  29. }
  30. void UHealthSystem::ModifyHealth(const float Amount)
  31. {
  32. if(Amount == 0)
  33. return;
  34. Health += Amount;
  35. Amount > 0 ? OnDamageHealedEvent.Broadcast(Amount) : OnDamageTakenEvent.Broadcast(Amount);
  36. }
  37. void UHealthSystem::TakeDamage(const float Amount)
  38. {
  39. if(Amount > 0)
  40. {
  41. Health -= Amount;
  42. OnDamageTakenEvent.Broadcast(Amount);
  43. }
  44. }
  45. void UHealthSystem::RecoverHealth(const float Amount)
  46. {
  47. if(Amount > 0)
  48. {
  49. Health += Amount;
  50. if (Health >= MaxHealth)
  51. {
  52. Health = MaxHealth;
  53. }
  54. OnDamageHealedEvent.Broadcast(Amount);
  55. }
  56. }
  57. void UHealthSystem::ReceiveShield(const float Amount)
  58. {
  59. if(Amount >= 0)
  60. {
  61. Shield += Amount;
  62. OnShieldReceiveEvent.Broadcast(Amount);
  63. }
  64. }
  65. void UHealthSystem::RemoveShield(const float Amount)
  66. {
  67. if(Amount >= 0)
  68. {
  69. Shield -= Amount;
  70. if (Shield < 0)
  71. {
  72. Shield = 0;
  73. }
  74. OnShieldReceiveEvent.Broadcast(Amount);
  75. }
  76. }