Newer
Older
SkyFrontier-Project-IADE-UE4-3D / Source / SkyFrontier / Private / HealthSystem.cpp
@Nelson Luis Moreira da Costa Nelson Luis Moreira da Costa on 16 Jan 2023 1 KB Fixed issue that would soft crash the running application
  1. #include "HealthSystem.h"
  2. #include "Net/UnrealNetwork.h"
  3. #include "Engine/Engine.h"
  4. #include "Components/ActorComponent.h"
  5. UHealthSystem::UHealthSystem()
  6. {
  7. PrimaryComponentTick.bCanEverTick = false;
  8. PrimaryComponentTick.bStartWithTickEnabled = false;
  9. SetIsReplicatedByDefault(true);
  10. MaxHealth = 100;
  11. }
  12. void UHealthSystem::BeginPlay()
  13. {
  14. Super::BeginPlay();
  15. Health = MaxHealth;
  16. Shield = 0;
  17. }
  18. void UHealthSystem::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
  19. {
  20. Super::GetLifetimeReplicatedProps(OutLifetimeProps);
  21. DOREPLIFETIME(UHealthSystem, Health);
  22. }
  23. float UHealthSystem::GetHealth() const
  24. {
  25. return Health;
  26. }
  27. float UHealthSystem::GetMaxHealth() const
  28. {
  29. return MaxHealth;
  30. }
  31. float UHealthSystem::GetShield() const
  32. {
  33. return Shield;
  34. }
  35. void UHealthSystem::TakeDamage(const float Amount)
  36. {
  37. if (Amount > 0)
  38. {
  39. Health -= Amount;
  40. OnDamageTakenEvent.Broadcast(Amount);
  41. }
  42. }
  43. void UHealthSystem::RecoverHealth(const float Amount)
  44. {
  45. if (Amount > 0)
  46. {
  47. Health += Amount;
  48. if (Health >= MaxHealth)
  49. {
  50. Health = MaxHealth;
  51. }
  52. OnDamageHealedEvent.Broadcast(Amount);
  53. }
  54. }
  55. void UHealthSystem::ReceiveShield(const float Amount)
  56. {
  57. if (Amount >= 0)
  58. {
  59. Shield += Amount;
  60. OnShieldReceiveEvent.Broadcast(Amount);
  61. }
  62. }
  63. void UHealthSystem::RemoveShield(const float Amount)
  64. {
  65. if (Amount >= 0)
  66. {
  67. Shield -= Amount;
  68. if (Shield < 0)
  69. {
  70. Shield = 0;
  71. }
  72. OnShieldReceiveEvent.Broadcast(Amount);
  73. }
  74. }