From c47a072996497d70d23682b3b21edb2d06949730 Mon Sep 17 00:00:00 2001 From: Louis Hobbs Date: Tue, 13 Dec 2022 04:10:20 +0000 Subject: [PATCH] Add Health Components --- Source/Nakatomi/HealthComponent.cpp | 107 ++++++++++++++++++++++ Source/Nakatomi/HealthComponent.h | 81 ++++++++++++++++ Source/Nakatomi/PlayerHealthComponent.cpp | 9 ++ Source/Nakatomi/PlayerHealthComponent.h | 20 ++++ 4 files changed, 217 insertions(+) create mode 100644 Source/Nakatomi/HealthComponent.cpp create mode 100644 Source/Nakatomi/HealthComponent.h create mode 100644 Source/Nakatomi/PlayerHealthComponent.cpp create mode 100644 Source/Nakatomi/PlayerHealthComponent.h diff --git a/Source/Nakatomi/HealthComponent.cpp b/Source/Nakatomi/HealthComponent.cpp new file mode 100644 index 0000000..0f6f24d --- /dev/null +++ b/Source/Nakatomi/HealthComponent.cpp @@ -0,0 +1,107 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "HealthComponent.h" + +// Sets default values for this component's properties +UHealthComponent::UHealthComponent() +{ + // Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features + // off to improve performance if you don't need them. + PrimaryComponentTick.bCanEverTick = false; + + // ... +} + +void UHealthComponent::TakeDamage() +{ +} + +void UHealthComponent::DecrementHealth(float value) +{ + value *= !CanDamage; + + CurrentHealth -= value; + + if (CurrentHealth <= 0.0f) + { + // TODO: Call some death logic here + OnDeath.ExecuteIfBound(); + } +} + +void UHealthComponent::IncrementHealth(float value) +{ + CurrentHealth += value; + + if (CurrentHealth > MaxHealth) + { + CurrentHealth = MaxHealth; + } +} + +float UHealthComponent::GetMaxHealth() +{ + return MaxHealth; +} + +void UHealthComponent::SetMaxHealth(float value) +{ + MaxHealth = value; +} + +float UHealthComponent::GetCurrentHealth() +{ + return CurrentHealth; +} + +void UHealthComponent::SetCurrentHealth(float value) +{ + // TODO: We might want to add some extra checking here + CurrentHealth = value; +} + +void UHealthComponent::ResetHealth() +{ + CurrentHealth = MaxHealth; +} + +void UHealthComponent::RecoverHealth(float value) +{ + // TODO: We might want to add some extra checking here + IncrementHealth(value); +} + +bool UHealthComponent::GetIsDead() +{ + return IsDead; +} + +void UHealthComponent::SetIsDead(bool isDead) +{ + IsDead = isDead; +} + +bool UHealthComponent::GetCanDamage() +{ + return CanDamage; +} + +void UHealthComponent::SetCanDamage(bool canDamage) +{ + CanDamage = canDamage; +} + + +// Called when the game starts +void UHealthComponent::BeginPlay() +{ + Super::BeginPlay(); + + // ... + +} + + + + diff --git a/Source/Nakatomi/HealthComponent.h b/Source/Nakatomi/HealthComponent.h new file mode 100644 index 0000000..ef0f6d9 --- /dev/null +++ b/Source/Nakatomi/HealthComponent.h @@ -0,0 +1,81 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "Components/ActorComponent.h" +#include "HealthComponent.generated.h" + +DECLARE_DELEGATE(FOnDeathDelegate) + +UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) ) +class NAKATOMI_API UHealthComponent : public UActorComponent +{ + GENERATED_BODY() + +public: + + FOnDeathDelegate OnDeath; + +private: + + UPROPERTY(EditDefaultsOnly) + float MaxHealth = 100.f; + + UPROPERTY(VisibleAnywhere) + float CurrentHealth; + + bool IsDead = false; + + bool CanDamage; + +public: + + // Sets default values for this component's properties + UHealthComponent(); + + UFUNCTION() + void TakeDamage(); + + UFUNCTION() + void DecrementHealth(float value); + + UFUNCTION() + void IncrementHealth(float value); + + UFUNCTION() + float GetMaxHealth(); + + UFUNCTION() + void SetMaxHealth(float value); + + UFUNCTION() + float GetCurrentHealth(); + + UFUNCTION() + void SetCurrentHealth(float value); + + UFUNCTION() + void ResetHealth(); + + UFUNCTION() + void RecoverHealth(float healing); + + UFUNCTION() + bool GetIsDead(); + + UFUNCTION() + void SetIsDead(bool isDead); + + UFUNCTION() + bool GetCanDamage(); + + UFUNCTION() + void SetCanDamage(bool canDamage); + +protected: + // Called when the game starts + virtual void BeginPlay() override; + + +}; diff --git a/Source/Nakatomi/PlayerHealthComponent.cpp b/Source/Nakatomi/PlayerHealthComponent.cpp new file mode 100644 index 0000000..f6ca724 --- /dev/null +++ b/Source/Nakatomi/PlayerHealthComponent.cpp @@ -0,0 +1,9 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "PlayerHealthComponent.h" + +void UPlayerHealthComponent::BeginPlay() +{ + Super::BeginPlay(); +} diff --git a/Source/Nakatomi/PlayerHealthComponent.h b/Source/Nakatomi/PlayerHealthComponent.h new file mode 100644 index 0000000..c4cf8d6 --- /dev/null +++ b/Source/Nakatomi/PlayerHealthComponent.h @@ -0,0 +1,20 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "HealthComponent.h" +#include "PlayerHealthComponent.generated.h" + +/** + * + */ +UCLASS() +class NAKATOMI_API UPlayerHealthComponent : public UHealthComponent +{ + GENERATED_BODY() + +protected: + virtual void BeginPlay() override; + +};