|
|
|
|
| #include "LyraHealthSet.h"
|
| #include "AbilitySystem/Attributes/LyraAttributeSet.h"
|
| #include "LyraGameplayTags.h"
|
| #include "Net/UnrealNetwork.h"
|
| #include "AbilitySystem/LyraAbilitySystemComponent.h"
|
| #include "Engine/World.h"
|
| #include "GameplayEffectExtension.h"
|
| #include "Messages/LyraVerbMessage.h"
|
| #include "GameFramework/GameplayMessageSubsystem.h"
|
|
|
| #include UE_INLINE_GENERATED_CPP_BY_NAME(LyraHealthSet)
|
|
|
| UE_DEFINE_GAMEPLAY_TAG(TAG_Gameplay_Damage, "Gameplay.Damage");
|
| UE_DEFINE_GAMEPLAY_TAG(TAG_Gameplay_DamageImmunity, "Gameplay.DamageImmunity");
|
| UE_DEFINE_GAMEPLAY_TAG(TAG_Gameplay_DamageSelfDestruct, "Gameplay.Damage.SelfDestruct");
|
| UE_DEFINE_GAMEPLAY_TAG(TAG_Gameplay_FellOutOfWorld, "Gameplay.Damage.FellOutOfWorld");
|
| UE_DEFINE_GAMEPLAY_TAG(TAG_Lyra_Damage_Message, "Lyra.Damage.Message");
|
|
|
| ULyraHealthSet::ULyraHealthSet()
|
| : Health(100.0f)
|
| , MaxHealth(100.0f)
|
| {
|
| bOutOfHealth = false;
|
| MaxHealthBeforeAttributeChange = 0.0f;
|
| HealthBeforeAttributeChange = 0.0f;
|
| }
|
|
|
| void ULyraHealthSet::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
|
| {
|
| Super::GetLifetimeReplicatedProps(OutLifetimeProps);
|
|
|
| DOREPLIFETIME_CONDITION_NOTIFY(ULyraHealthSet, Health, COND_None, REPNOTIFY_Always);
|
| DOREPLIFETIME_CONDITION_NOTIFY(ULyraHealthSet, MaxHealth, COND_None, REPNOTIFY_Always);
|
| }
|
|
|
| void ULyraHealthSet::OnRep_Health(const FGameplayAttributeData& OldValue)
|
| {
|
| GAMEPLAYATTRIBUTE_REPNOTIFY(ULyraHealthSet, Health, OldValue);
|
|
|
|
|
|
|
|
|
|
|
| const float CurrentHealth = GetHealth();
|
| const float EstimatedMagnitude = CurrentHealth - OldValue.GetCurrentValue();
|
|
|
| OnHealthChanged.Broadcast(nullptr, nullptr, nullptr, EstimatedMagnitude, OldValue.GetCurrentValue(), CurrentHealth);
|
|
|
| if (!bOutOfHealth && CurrentHealth <= 0.0f)
|
| {
|
| OnOutOfHealth.Broadcast(nullptr, nullptr, nullptr, EstimatedMagnitude, OldValue.GetCurrentValue(), CurrentHealth);
|
| }
|
|
|
| bOutOfHealth = (CurrentHealth <= 0.0f);
|
| }
|
|
|
| void ULyraHealthSet::OnRep_MaxHealth(const FGameplayAttributeData& OldValue)
|
| {
|
| GAMEPLAYATTRIBUTE_REPNOTIFY(ULyraHealthSet, MaxHealth, OldValue);
|
|
|
|
|
|
|
| OnMaxHealthChanged.Broadcast(nullptr, nullptr, nullptr, GetMaxHealth() - OldValue.GetCurrentValue(), OldValue.GetCurrentValue(), GetMaxHealth());
|
| }
|
|
|
| bool ULyraHealthSet::PreGameplayEffectExecute(FGameplayEffectModCallbackData &Data)
|
| {
|
| if (!Super::PreGameplayEffectExecute(Data))
|
| {
|
| return false;
|
| }
|
|
|
|
|
| if (Data.EvaluatedData.Attribute == GetDamageAttribute())
|
| {
|
| if (Data.EvaluatedData.Magnitude > 0.0f)
|
| {
|
| const bool bIsDamageFromSelfDestruct = Data.EffectSpec.GetDynamicAssetTags().HasTagExact(TAG_Gameplay_DamageSelfDestruct);
|
|
|
| if (Data.Target.HasMatchingGameplayTag(TAG_Gameplay_DamageImmunity) && !bIsDamageFromSelfDestruct)
|
| {
|
|
|
| Data.EvaluatedData.Magnitude = 0.0f;
|
| return false;
|
| }
|
|
|
| #if !UE_BUILD_SHIPPING
|
|
|
| if (Data.Target.HasMatchingGameplayTag(LyraGameplayTags::Cheat_GodMode) && !bIsDamageFromSelfDestruct)
|
| {
|
|
|
| Data.EvaluatedData.Magnitude = 0.0f;
|
| return false;
|
| }
|
| #endif
|
| }
|
| }
|
|
|
|
|
| HealthBeforeAttributeChange = GetHealth();
|
| MaxHealthBeforeAttributeChange = GetMaxHealth();
|
|
|
| return true;
|
| }
|
|
|
| void ULyraHealthSet::PostGameplayEffectExecute(const FGameplayEffectModCallbackData& Data)
|
| {
|
| Super::PostGameplayEffectExecute(Data);
|
|
|
| const bool bIsDamageFromSelfDestruct = Data.EffectSpec.GetDynamicAssetTags().HasTagExact(TAG_Gameplay_DamageSelfDestruct);
|
| float MinimumHealth = 0.0f;
|
|
|
| #if !UE_BUILD_SHIPPING
|
|
|
| if (!bIsDamageFromSelfDestruct &&
|
| (Data.Target.HasMatchingGameplayTag(LyraGameplayTags::Cheat_GodMode) || Data.Target.HasMatchingGameplayTag(LyraGameplayTags::Cheat_UnlimitedHealth) ))
|
| {
|
| MinimumHealth = 1.0f;
|
| }
|
| #endif
|
|
|
| const FGameplayEffectContextHandle& EffectContext = Data.EffectSpec.GetEffectContext();
|
| AActor* Instigator = EffectContext.GetOriginalInstigator();
|
| AActor* Causer = EffectContext.GetEffectCauser();
|
|
|
| if (Data.EvaluatedData.Attribute == GetDamageAttribute())
|
| {
|
|
|
| if (Data.EvaluatedData.Magnitude > 0.0f)
|
| {
|
| FLyraVerbMessage Message;
|
| Message.Verb = TAG_Lyra_Damage_Message;
|
| Message.Instigator = Data.EffectSpec.GetEffectContext().GetEffectCauser();
|
| Message.InstigatorTags = *Data.EffectSpec.CapturedSourceTags.GetAggregatedTags();
|
| Message.Target = GetOwningActor();
|
| Message.TargetTags = *Data.EffectSpec.CapturedTargetTags.GetAggregatedTags();
|
|
|
|
|
| Message.Magnitude = Data.EvaluatedData.Magnitude;
|
|
|
| UGameplayMessageSubsystem& MessageSystem = UGameplayMessageSubsystem::Get(GetWorld());
|
| MessageSystem.BroadcastMessage(Message.Verb, Message);
|
| }
|
|
|
|
|
| SetHealth(FMath::Clamp(GetHealth() - GetDamage(), MinimumHealth, GetMaxHealth()));
|
| SetDamage(0.0f);
|
| }
|
| else if (Data.EvaluatedData.Attribute == GetHealingAttribute())
|
| {
|
|
|
| SetHealth(FMath::Clamp(GetHealth() + GetHealing(), MinimumHealth, GetMaxHealth()));
|
| SetHealing(0.0f);
|
| }
|
| else if (Data.EvaluatedData.Attribute == GetHealthAttribute())
|
| {
|
|
|
| SetHealth(FMath::Clamp(GetHealth(), MinimumHealth, GetMaxHealth()));
|
| }
|
| else if (Data.EvaluatedData.Attribute == GetMaxHealthAttribute())
|
| {
|
|
|
|
|
|
|
| OnMaxHealthChanged.Broadcast(Instigator, Causer, &Data.EffectSpec, Data.EvaluatedData.Magnitude, MaxHealthBeforeAttributeChange, GetMaxHealth());
|
| }
|
|
|
|
|
| if (GetHealth() != HealthBeforeAttributeChange)
|
| {
|
| OnHealthChanged.Broadcast(Instigator, Causer, &Data.EffectSpec, Data.EvaluatedData.Magnitude, HealthBeforeAttributeChange, GetHealth());
|
| }
|
|
|
| if ((GetHealth() <= 0.0f) && !bOutOfHealth)
|
| {
|
| OnOutOfHealth.Broadcast(Instigator, Causer, &Data.EffectSpec, Data.EvaluatedData.Magnitude, HealthBeforeAttributeChange, GetHealth());
|
| }
|
|
|
|
|
| bOutOfHealth = (GetHealth() <= 0.0f);
|
| }
|
|
|
| void ULyraHealthSet::PreAttributeBaseChange(const FGameplayAttribute& Attribute, float& NewValue) const
|
| {
|
| Super::PreAttributeBaseChange(Attribute, NewValue);
|
|
|
| ClampAttribute(Attribute, NewValue);
|
| }
|
|
|
| void ULyraHealthSet::PreAttributeChange(const FGameplayAttribute& Attribute, float& NewValue)
|
| {
|
| Super::PreAttributeChange(Attribute, NewValue);
|
|
|
| ClampAttribute(Attribute, NewValue);
|
| }
|
|
|
| void ULyraHealthSet::PostAttributeChange(const FGameplayAttribute& Attribute, float OldValue, float NewValue)
|
| {
|
| Super::PostAttributeChange(Attribute, OldValue, NewValue);
|
|
|
| if (Attribute == GetMaxHealthAttribute())
|
| {
|
|
|
| if (GetHealth() > NewValue)
|
| {
|
| ULyraAbilitySystemComponent* LyraASC = GetLyraAbilitySystemComponent();
|
| check(LyraASC);
|
|
|
| LyraASC->ApplyModToAttribute(GetHealthAttribute(), EGameplayModOp::Override, NewValue);
|
| }
|
| }
|
|
|
| if (bOutOfHealth && (GetHealth() > 0.0f))
|
| {
|
| bOutOfHealth = false;
|
| }
|
| }
|
|
|
| void ULyraHealthSet::ClampAttribute(const FGameplayAttribute& Attribute, float& NewValue) const
|
| {
|
| if (Attribute == GetHealthAttribute())
|
| {
|
|
|
| NewValue = FMath::Clamp(NewValue, 0.0f, GetMaxHealth());
|
| }
|
| else if (Attribute == GetMaxHealthAttribute())
|
| {
|
|
|
| NewValue = FMath::Max(NewValue, 1.0f);
|
| }
|
| }
|
|
|
|
|