Add Health Components

This commit is contained in:
Louis Hobbs 2022-12-13 04:10:20 +00:00
parent 22afeb23db
commit c47a072996
4 changed files with 217 additions and 0 deletions

View File

@ -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();
// ...
}

View File

@ -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;
};

View File

@ -0,0 +1,9 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "PlayerHealthComponent.h"
void UPlayerHealthComponent::BeginPlay()
{
Super::BeginPlay();
}

View File

@ -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;
};