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