OLS/Source/ols/Public/AbilitySystem/Attributes/OLSHealthAttributeSet.h

97 lines
3.8 KiB
C++

// © 2024 Long Ly. All rights reserved. Any unauthorized use, reproduction, or distribution of this trademark is strictly prohibited and may result in legal action.
#pragma once
#include "CoreMinimal.h"
#include "AbilitySystemComponent.h"
#include "NativeGameplayTags.h"
#include "OLSAttributeSetBase.h"
#include "OLSHealthAttributeSet.generated.h"
OLS_API UE_DECLARE_GAMEPLAY_TAG_EXTERN(TAG_Gameplay_Damage);
OLS_API UE_DECLARE_GAMEPLAY_TAG_EXTERN(TAG_Gameplay_DamageImmunity);
OLS_API UE_DECLARE_GAMEPLAY_TAG_EXTERN(TAG_Gameplay_DamageSelfDestruct);
OLS_API UE_DECLARE_GAMEPLAY_TAG_EXTERN(TAG_Gameplay_FellOutOfWorld);
OLS_API UE_DECLARE_GAMEPLAY_TAG_EXTERN(TAG_OLS_Damage_Message);
/**
* UOLSHealthAttributeSet
*
* Class that defines attributes that are necessary for taking damage.
* Attribute examples include: health, shields, and resistances.
*/
UCLASS(BlueprintType)
class OLS_API UOLSHealthAttributeSet : public UOLSAttributeSetBase
{
GENERATED_BODY()
public:
UOLSHealthAttributeSet();
//~ Begin UObject interface.
virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;
//~ End UObject interface.
ATTRIBUTE_ACCESSORS(ThisClass, Health);
ATTRIBUTE_ACCESSORS(ThisClass, MaxHealth);
ATTRIBUTE_ACCESSORS(ThisClass, Healing);
ATTRIBUTE_ACCESSORS(ThisClass, Damage);
// Delegate when health changes due to damage/healing, some information may be missing on the client
mutable FOLSAttributeEventNativeDelegate OnHealthChanged;
// Delegate when max health changes
mutable FOLSAttributeEventNativeDelegate OnMaxHealthChanged;
// Delegate to broadcast when the health attribute reaches zero
mutable FOLSAttributeEventNativeDelegate OnOutOfHealth;
protected:
UFUNCTION()
void OnRep_Health(const FGameplayAttributeData& oldValue);
UFUNCTION()
void OnRep_MaxHealth(const FGameplayAttributeData& OldValue);
virtual bool PreGameplayEffectExecute(FGameplayEffectModCallbackData& data) override;
virtual void PostGameplayEffectExecute(const FGameplayEffectModCallbackData& Data) override;
virtual void PreAttributeBaseChange(const FGameplayAttribute& attribute, float& newValue) const override;
virtual void PreAttributeChange(const FGameplayAttribute& attribute, float& newValue) override;
virtual void PostAttributeChange(const FGameplayAttribute& attribute, float oldValue, float newValue) override;
void ClampAttribute(const FGameplayAttribute& attribute, float& outNewValue) const;
private:
// The current health attribute. The health will be capped by the max health attribute. Health is hidden from modifiers so only executions can modify it.
UPROPERTY(BlueprintReadOnly, ReplicatedUsing = OnRep_Health, Category = "OLS|Health", Meta = (HideFromModifiers, AllowPrivateAccess = true))
FGameplayAttributeData Health;
// The current max health attribute. Max health is an attribute since gameplay effects can modify it.
UPROPERTY(BlueprintReadOnly, ReplicatedUsing = OnRep_MaxHealth, Category = "OLS|Health", Meta = (AllowPrivateAccess = true))
FGameplayAttributeData MaxHealth;
// Used to track when the health reaches 0.
uint8 bIsOutOfHealth : 1 = false;
// Store the health before any changes
float MaxHealthBeforeAttributeChange = 0.f;
float HealthBeforeAttributeChange = 0.f
;
// -------------------------------------------------------------------
// Meta Attribute (please keep attributes that aren't 'stateful' below
// -------------------------------------------------------------------
// Incoming healing. This is mapped directly to +Health
UPROPERTY(BlueprintReadOnly, Category="Lyra|Health", Meta=(AllowPrivateAccess=true))
FGameplayAttributeData Healing;
// Incoming damage. This is mapped directly to -Health
UPROPERTY(BlueprintReadOnly, Category="Lyra|Health", Meta=(HideFromModifiers, AllowPrivateAccess=true))
FGameplayAttributeData Damage;
};