-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLyraHealthComponent.cpp
More file actions
354 lines (284 loc) · 11.7 KB
/
LyraHealthComponent.cpp
File metadata and controls
354 lines (284 loc) · 11.7 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// Copyright Epic Games, Inc. All Rights Reserved.
#include "Character/LyraHealthComponent.h"
#include "AbilitySystem/Attributes/LyraAttributeSet.h"
#include "LyraLogChannels.h"
#include "System/LyraAssetManager.h"
#include "System/LyraGameData.h"
#include "LyraGameplayTags.h"
#include "Net/UnrealNetwork.h"
#include "GameplayEffectExtension.h"
#include "AbilitySystem/LyraAbilitySystemComponent.h"
#include "AbilitySystem/Attributes/LyraHealthSet.h"
#include "Messages/LyraVerbMessage.h"
#include "Messages/LyraVerbMessageHelpers.h"
#include "GameFramework/GameplayMessageSubsystem.h"
#include "GameFramework/PlayerState.h"
#include "Engine/World.h"
#include UE_INLINE_GENERATED_CPP_BY_NAME(LyraHealthComponent)
UE_DEFINE_GAMEPLAY_TAG_STATIC(TAG_Lyra_Elimination_Message, "Lyra.Elimination.Message");
ULyraHealthComponent::ULyraHealthComponent(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
PrimaryComponentTick.bStartWithTickEnabled = false;
PrimaryComponentTick.bCanEverTick = false;
SetIsReplicatedByDefault(true);
AbilitySystemComponent = nullptr;
HealthSet = nullptr;
DeathState = ELyraDeathState::NotDead;
}
void ULyraHealthComponent::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(ULyraHealthComponent, DeathState);
}
void ULyraHealthComponent::OnUnregister()
{
UninitializeFromAbilitySystem();
Super::OnUnregister();
}
void ULyraHealthComponent::InitializeWithAbilitySystem(ULyraAbilitySystemComponent* InASC)
{
AActor* Owner = GetOwner();
check(Owner);
if (AbilitySystemComponent)
{
UE_LOG(LogLyra, Error, TEXT("LyraHealthComponent: Health component for owner [%s] has already been initialized with an ability system."), *GetNameSafe(Owner));
return;
}
AbilitySystemComponent = InASC;
if (!AbilitySystemComponent)
{
UE_LOG(LogLyra, Error, TEXT("LyraHealthComponent: Cannot initialize health component for owner [%s] with NULL ability system."), *GetNameSafe(Owner));
return;
}
HealthSet = AbilitySystemComponent->GetSet<ULyraHealthSet>();
if (!HealthSet)
{
UE_LOG(LogLyra, Error, TEXT("LyraHealthComponent: Cannot initialize health component for owner [%s] with NULL health set on the ability system."), *GetNameSafe(Owner));
return;
}
// Register to listen for attribute changes.
AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(ULyraHealthSet::GetHealthAttribute()).AddUObject(this, &ThisClass::HandleHealthChanged);
AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(ULyraHealthSet::GetMaxHealthAttribute()).AddUObject(this, &ThisClass::HandleMaxHealthChanged);
HealthSet->OnOutOfHealth.AddUObject(this, &ThisClass::HandleOutOfHealth);
// TEMP: Reset attributes to default values. Eventually this will be driven by a spread sheet.
AbilitySystemComponent->SetNumericAttributeBase(ULyraHealthSet::GetHealthAttribute(), HealthSet->GetMaxHealth());
ClearGameplayTags();
OnHealthChanged.Broadcast(this, HealthSet->GetHealth(), HealthSet->GetHealth(), nullptr);
OnMaxHealthChanged.Broadcast(this, HealthSet->GetHealth(), HealthSet->GetHealth(), nullptr);
//UGameFrameworkComponentManager::SendGameFrameworkComponentExtensionEvent(GetOwner(), UGameFrameworkComponentManager::NAME_HealthComponentReady);
}
void ULyraHealthComponent::UninitializeFromAbilitySystem()
{
ClearGameplayTags();
if (HealthSet)
{
HealthSet->OnOutOfHealth.RemoveAll(this);
}
HealthSet = nullptr;
AbilitySystemComponent = nullptr;
}
void ULyraHealthComponent::ClearGameplayTags()
{
if (AbilitySystemComponent)
{
AbilitySystemComponent->SetLooseGameplayTagCount(LyraGameplayTags::Status_Death_Dying, 0);
AbilitySystemComponent->SetLooseGameplayTagCount(LyraGameplayTags::Status_Death_Dead, 0);
}
}
float ULyraHealthComponent::GetHealth() const
{
return (HealthSet ? HealthSet->GetHealth() : 0.0f);
}
float ULyraHealthComponent::GetMaxHealth() const
{
return (HealthSet ? HealthSet->GetMaxHealth() : 0.0f);
}
float ULyraHealthComponent::GetHealthNormalized() const
{
if (HealthSet)
{
const float Health = HealthSet->GetHealth();
const float MaxHealth = HealthSet->GetMaxHealth();
return ((MaxHealth > 0.0f) ? (Health / MaxHealth) : 0.0f);
}
return 0.0f;
}
static AActor* GetInstigatorFromAttrChangeData(const FOnAttributeChangeData& ChangeData)
{
if (ChangeData.GEModData != nullptr)
{
const FGameplayEffectContextHandle& EffectContext = ChangeData.GEModData->EffectSpec.GetEffectContext();
return EffectContext.GetOriginalInstigator();
}
return nullptr;
}
void ULyraHealthComponent::HandleHealthChanged(const FOnAttributeChangeData& ChangeData)
{
OnHealthChanged.Broadcast(this, ChangeData.OldValue, ChangeData.NewValue, GetInstigatorFromAttrChangeData(ChangeData));
}
void ULyraHealthComponent::HandleMaxHealthChanged(const FOnAttributeChangeData& ChangeData)
{
OnMaxHealthChanged.Broadcast(this, ChangeData.OldValue, ChangeData.NewValue, GetInstigatorFromAttrChangeData(ChangeData));
}
void ULyraHealthComponent::HandleOutOfHealth(AActor* DamageInstigator, AActor* DamageCauser, const FGameplayEffectSpec& DamageEffectSpec, float DamageMagnitude)
{
#if WITH_SERVER_CODE
if (AbilitySystemComponent)
{
// Send the "GameplayEvent.Death" gameplay event through the owner's ability system. This can be used to trigger a death gameplay ability.
{
FGameplayEventData Payload;
Payload.EventTag = LyraGameplayTags::GameplayEvent_Death;
Payload.Instigator = DamageInstigator;
Payload.Target = AbilitySystemComponent->GetAvatarActor();
Payload.OptionalObject = DamageEffectSpec.Def;
Payload.ContextHandle = DamageEffectSpec.GetEffectContext();
Payload.InstigatorTags = *DamageEffectSpec.CapturedSourceTags.GetAggregatedTags();
Payload.TargetTags = *DamageEffectSpec.CapturedTargetTags.GetAggregatedTags();
Payload.EventMagnitude = DamageMagnitude;
FScopedPredictionWindow NewScopedWindow(AbilitySystemComponent, true);
AbilitySystemComponent->HandleGameplayEvent(Payload.EventTag, &Payload);
}
// Send a standardized verb message that other systems can observe
{
FLyraVerbMessage Message;
Message.Verb = TAG_Lyra_Elimination_Message;
Message.Instigator = DamageInstigator;
Message.InstigatorTags = *DamageEffectSpec.CapturedSourceTags.GetAggregatedTags();
Message.Target = ULyraVerbMessageHelpers::GetPlayerStateFromObject(AbilitySystemComponent->GetAvatarActor());
Message.TargetTags = *DamageEffectSpec.CapturedTargetTags.GetAggregatedTags();
//@TODO: Fill out context tags, and any non-ability-system source/instigator tags
//@TODO: Determine if it's an opposing team kill, self-own, team kill, etc...
UGameplayMessageSubsystem& MessageSystem = UGameplayMessageSubsystem::Get(GetWorld());
MessageSystem.BroadcastMessage(Message.Verb, Message);
}
//@TODO: assist messages (could compute from damage dealt elsewhere)?
}
#endif // #if WITH_SERVER_CODE
}
void ULyraHealthComponent::OnRep_DeathState(ELyraDeathState OldDeathState)
{
const ELyraDeathState NewDeathState = DeathState;
// Revert the death state for now since we rely on StartDeath and FinishDeath to change it.
DeathState = OldDeathState;
if (OldDeathState > NewDeathState)
{
// The server is trying to set us back but we've already predicted past the server state.
UE_LOG(LogLyra, Warning, TEXT("LyraHealthComponent: Predicted past server death state [%d] -> [%d] for owner [%s]."), (uint8)OldDeathState, (uint8)NewDeathState, *GetNameSafe(GetOwner()));
return;
}
if (OldDeathState == ELyraDeathState::NotDead)
{
if (NewDeathState == ELyraDeathState::DeathStarted)
{
StartDeath();
}
else if (NewDeathState == ELyraDeathState::DeathFinished)
{
StartDeath();
FinishDeath();
}
else
{
UE_LOG(LogLyra, Error, TEXT("LyraHealthComponent: Invalid death transition [%d] -> [%d] for owner [%s]."), (uint8)OldDeathState, (uint8)NewDeathState, *GetNameSafe(GetOwner()));
}
}
else if (OldDeathState == ELyraDeathState::DeathStarted)
{
if (NewDeathState == ELyraDeathState::DeathFinished)
{
FinishDeath();
}
else
{
UE_LOG(LogLyra, Error, TEXT("LyraHealthComponent: Invalid death transition [%d] -> [%d] for owner [%s]."), (uint8)OldDeathState, (uint8)NewDeathState, *GetNameSafe(GetOwner()));
}
}
ensureMsgf((DeathState == NewDeathState), TEXT("LyraHealthComponent: Death transition failed [%d] -> [%d] for owner [%s]."), (uint8)OldDeathState, (uint8)NewDeathState, *GetNameSafe(GetOwner()));
}
void ULyraHealthComponent::StartDeath()
{
if (DeathState != ELyraDeathState::NotDead)
{
return;
}
DeathState = ELyraDeathState::DeathStarted;
if (AbilitySystemComponent)
{
AbilitySystemComponent->SetLooseGameplayTagCount(LyraGameplayTags::Status_Death_Dying, 1);
}
AActor* Owner = GetOwner();
check(Owner);
OnDeathStarted.Broadcast(Owner);
Owner->ForceNetUpdate();
}
void ULyraHealthComponent::FinishDeath()
{
if (DeathState != ELyraDeathState::DeathStarted)
{
return;
}
DeathState = ELyraDeathState::DeathFinished;
if (AbilitySystemComponent)
{
AbilitySystemComponent->SetLooseGameplayTagCount(LyraGameplayTags::Status_Death_Dead, 1);
}
AActor* Owner = GetOwner();
check(Owner);
OnDeathFinished.Broadcast(Owner);
Owner->ForceNetUpdate();
}
void ULyraHealthComponent::DamageSelfDestruct(bool bFellOutOfWorld)
{
if ((DeathState == ELyraDeathState::NotDead) && AbilitySystemComponent)
{
const TSubclassOf<UGameplayEffect> DamageGE = ULyraAssetManager::GetSubclass(ULyraGameData::Get().DamageGameplayEffect_SetByCaller);
if (!DamageGE)
{
UE_LOG(LogLyra, Error, TEXT("LyraHealthComponent: DamageSelfDestruct failed for owner [%s]. Unable to find gameplay effect [%s]."), *GetNameSafe(GetOwner()), *ULyraGameData::Get().DamageGameplayEffect_SetByCaller.GetAssetName());
return;
}
FGameplayEffectSpecHandle SpecHandle = AbilitySystemComponent->MakeOutgoingSpec(DamageGE, 1.0f, AbilitySystemComponent->MakeEffectContext());
FGameplayEffectSpec* Spec = SpecHandle.Data.Get();
if (!Spec)
{
UE_LOG(LogLyra, Error, TEXT("LyraHealthComponent: DamageSelfDestruct failed for owner [%s]. Unable to make outgoing spec for [%s]."), *GetNameSafe(GetOwner()), *GetNameSafe(DamageGE));
return;
}
Spec->AddDynamicAssetTag(TAG_Gameplay_DamageSelfDestruct);
if (bFellOutOfWorld)
{
Spec->AddDynamicAssetTag(TAG_Gameplay_FellOutOfWorld);
}
const float DamageAmount = GetMaxHealth();
Spec->SetSetByCallerMagnitude(LyraGameplayTags::SetByCaller_Damage, DamageAmount);
AbilitySystemComponent->ApplyGameplayEffectSpecToSelf(*Spec);
}
}
// @TangoAlphaDev
void ULyraHealthComponent::ApplyFallDamage(float Velocity, int VelocityLimit, float FallDamageMultiplier)
{
if (Velocity > 0.0f && AbilitySystemComponent)
{
const TSubclassOf<UGameplayEffect> DamageGE = ULyraAssetManager::GetSubclass(ULyraGameData::Get().DamageGameplayEffect_SetByCaller);
if (!DamageGE)
{
UE_LOG(LogLyra, Error, TEXT("LyraHealthComponent: ApplyFallDamage failed. Unable to find gameplay effect for fall damage."));
return;
}
FGameplayEffectSpecHandle SpecHandle = AbilitySystemComponent->MakeOutgoingSpec(DamageGE, 1.0f, AbilitySystemComponent->MakeEffectContext());
FGameplayEffectSpec* Spec = SpecHandle.Data.Get();
if (!Spec)
{
UE_LOG(LogLyra, Error, TEXT("LyraHealthComponent: ApplyFallDamage failed. Unable to make outgoing spec for fall damage."));
return;
}
const float DamageAmount = (Velocity - VelocityLimit) * FallDamageMultiplier;
Spec->SetSetByCallerMagnitude(LyraGameplayTags::SetByCaller_Damage, DamageAmount);
AbilitySystemComponent->ApplyGameplayEffectSpecToSelf(*Spec);
// Print to log
UE_LOG(LogLyra, Log, TEXT("LyraHealthComponent: Applied fall damage of %f with velocity %f and FallDamageMultiplier of %f."), DamageAmount, Velocity, FallDamageMultiplier);
}
}
// @TangoAlphaDev