-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFPSExtraAttributesComponent.cpp
178 lines (142 loc) · 6.19 KB
/
FPSExtraAttributesComponent.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Fill out your copyright notice in the Description page of Project Settings.
#include "FPSExtraAttributesComponent.h"
#include "LyraLogChannels.h"
#include "AbilitySystem/LyraAbilitySystemComponent.h"
#include "FPSExtraAttributes.h"
#include "GameplayEffect.h"
#include "GameplayEffectExtension.h"
#include "Messages/LyraVerbMessageHelpers.h"
UFPSExtraAttributesComponent::UFPSExtraAttributesComponent(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
PrimaryComponentTick.bStartWithTickEnabled = false;
PrimaryComponentTick.bCanEverTick = false;
SetIsReplicatedByDefault(true);
AbilitySystemComponent = nullptr;
FPSExtraAttributes = nullptr;
}
void UFPSExtraAttributesComponent::InitializeWithAbilitySystem(ULyraAbilitySystemComponent* InASC)
{
AActor* Owner = GetOwner();
check(Owner);
if (AbilitySystemComponent)
{
UE_LOG(LogLyra, Error, TEXT("FPSExtraAttributesComponent: component for owner [%s] has already been initialized with an ability system."), *GetNameSafe(Owner));
return;
}
AbilitySystemComponent = InASC;
if (!AbilitySystemComponent)
{
UE_LOG(LogLyra, Error, TEXT("FPSExtraAttributesComponent: Cannot initialize AbilitySystemComponent component for owner [%s] with NULL ability system."), *GetNameSafe(Owner));
return;
}
FPSExtraAttributes = AbilitySystemComponent->GetSet<UFPSExtraAttributes>();
if (!FPSExtraAttributes)
{
UE_LOG(LogLyra, Error, TEXT("FPSExtraAttributesComponent: Cannot initialize FPSExtraAttributes component for owner [%s] with NULL set on the ability system."), *GetNameSafe(Owner));
return;
}
// Register to listen for attribute changes.
AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(UFPSExtraAttributes::GetArmorAttribute()).AddUObject(this, &ThisClass::HandleArmorChanged);
AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(UFPSExtraAttributes::GetMaxArmorAttribute()).AddUObject(this, &ThisClass::HandleMaxArmorChanged);
AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(UFPSExtraAttributes::GetStaminaAttribute()).AddUObject(this, &ThisClass::HandleStaminaChanged);
AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(UFPSExtraAttributes::GetMaxStaminaAttribute()).AddUObject(this, &ThisClass::HandleMaxStaminaChanged);
AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(UFPSExtraAttributes::GetManaAttribute()).AddUObject(this, &ThisClass::HandleManaChanged);
AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(UFPSExtraAttributes::GetMaxManaAttribute()).AddUObject(this, &ThisClass::HandleMaxManaChanged);
}
void UFPSExtraAttributesComponent::UninitializeFromAbilitySystem()
{
FPSExtraAttributes = nullptr;
AbilitySystemComponent = nullptr;
}
void UFPSExtraAttributesComponent::OnUnregister()
{
UninitializeFromAbilitySystem();
Super::OnUnregister();
}
float UFPSExtraAttributesComponent::GetArmor() const
{
return (FPSExtraAttributes ? FPSExtraAttributes->GetArmor() : 0.0f);
}
float UFPSExtraAttributesComponent::GetMaxArmor() const
{
return (FPSExtraAttributes ? FPSExtraAttributes->GetMaxArmor() : 0.0f);
}
float UFPSExtraAttributesComponent::GetArmorNormalized() const
{
if (FPSExtraAttributes)
{
const float Armor = FPSExtraAttributes->GetArmor();
const float MaxArmor = FPSExtraAttributes->GetMaxArmor();
return ((MaxArmor > 0.0f) ? (Armor / MaxArmor) : 0.0f);
}
return 0.0f;
}
float UFPSExtraAttributesComponent::GetStamina() const
{
return (FPSExtraAttributes ? FPSExtraAttributes->GetStamina() : 0.0f);
}
float UFPSExtraAttributesComponent::GetMaxStamina() const
{
return (FPSExtraAttributes ? FPSExtraAttributes->GetMaxStamina() : 0.0f);
}
float UFPSExtraAttributesComponent::GetStaminaNormalized() const
{
if (FPSExtraAttributes)
{
const float Stamina = FPSExtraAttributes->GetStamina();
const float MaxStamina = FPSExtraAttributes->GetMaxStamina();
return ((MaxStamina > 0.0f) ? (Stamina / MaxStamina) : 0.0f);
}
return 0.0f;
}
float UFPSExtraAttributesComponent::GetMana() const
{
return (FPSExtraAttributes ? FPSExtraAttributes->GetMana() : 0.0f);
}
float UFPSExtraAttributesComponent::GetMaxMana() const
{
return (FPSExtraAttributes ? FPSExtraAttributes->GetMaxMana() : 0.0f);
}
float UFPSExtraAttributesComponent::GetManaNormalized() const
{
if (FPSExtraAttributes)
{
const float Mana = FPSExtraAttributes->GetMana();
const float MaxMana = FPSExtraAttributes->GetMaxMana();
return ((MaxMana > 0.0f) ? (Mana / MaxMana) : 0.0f);
}
return 0.0f;
}
static AActor* GetInstigatorFromAttrChangeDataFPSExtraAttributes(const FOnAttributeChangeData& ChangeData)
{
if (ChangeData.GEModData != nullptr)
{
const FGameplayEffectContextHandle& EffectContext = ChangeData.GEModData->EffectSpec.GetEffectContext();
return EffectContext.GetOriginalInstigator();
}
return nullptr;
}
void UFPSExtraAttributesComponent::HandleArmorChanged(const FOnAttributeChangeData& ChangeData)
{
OnArmorChanged.Broadcast(this, ChangeData.OldValue, ChangeData.NewValue, GetInstigatorFromAttrChangeDataFPSExtraAttributes(ChangeData));
}
void UFPSExtraAttributesComponent::HandleMaxArmorChanged(const FOnAttributeChangeData& ChangeData)
{
OnMaxArmorChanged.Broadcast(this, ChangeData.OldValue, ChangeData.NewValue, GetInstigatorFromAttrChangeDataFPSExtraAttributes(ChangeData));
}
void UFPSExtraAttributesComponent::HandleStaminaChanged(const FOnAttributeChangeData &ChangeData)
{
OnStaminaChanged.Broadcast(this, ChangeData.OldValue, ChangeData.NewValue, GetInstigatorFromAttrChangeDataFPSExtraAttributes(ChangeData));
}
void UFPSExtraAttributesComponent::HandleMaxStaminaChanged(const FOnAttributeChangeData &ChangeData)
{
OnMaxStaminaChanged.Broadcast(this, ChangeData.OldValue, ChangeData.NewValue, GetInstigatorFromAttrChangeDataFPSExtraAttributes(ChangeData));
}
void UFPSExtraAttributesComponent::HandleManaChanged(const FOnAttributeChangeData &ChangeData)
{
OnManaChanged.Broadcast(this, ChangeData.OldValue, ChangeData.NewValue, GetInstigatorFromAttrChangeDataFPSExtraAttributes(ChangeData));
}
void UFPSExtraAttributesComponent::HandleMaxManaChanged(const FOnAttributeChangeData &ChangeData)
{
OnMaxManaChanged.Broadcast(this, ChangeData.OldValue, ChangeData.NewValue, GetInstigatorFromAttrChangeDataFPSExtraAttributes(ChangeData));
}