-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLyraHealthComponent.cpp
More file actions
475 lines (379 loc) · 15.3 KB
/
LyraHealthComponent.cpp
File metadata and controls
475 lines (379 loc) · 15.3 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
// 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 "LyraCharacter.h" // @Tango
#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.
HealthSet->OnHealthChanged.AddUObject(this, &ThisClass::HandleHealthChanged);
HealthSet->OnMaxHealthChanged.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);
}
void ULyraHealthComponent::UninitializeFromAbilitySystem()
{
ClearGameplayTags();
if (HealthSet)
{
HealthSet->OnHealthChanged.RemoveAll(this);
HealthSet->OnMaxHealthChanged.RemoveAll(this);
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;
}
void ULyraHealthComponent::HandleHealthChanged(AActor* DamageInstigator, AActor* DamageCauser, const FGameplayEffectSpec* DamageEffectSpec, float DamageMagnitude, float OldValue, float NewValue)
{
OnHealthChanged.Broadcast(this, OldValue, NewValue, DamageInstigator);
}
void ULyraHealthComponent::HandleMaxHealthChanged(AActor* DamageInstigator, AActor* DamageCauser, const FGameplayEffectSpec* DamageEffectSpec, float DamageMagnitude, float OldValue, float NewValue)
{
OnMaxHealthChanged.Broadcast(this, OldValue, NewValue, DamageInstigator);
}
void ULyraHealthComponent::HandleOutOfHealth(AActor* DamageInstigator, AActor* DamageCauser, const FGameplayEffectSpec* DamageEffectSpec, float DamageMagnitude, float OldValue, float NewValue)
{
#if WITH_SERVER_CODE
if (AbilitySystemComponent && DamageEffectSpec)
{
// @Tango
// Check head shot
const FGameplayEffectContextHandle& ContextHandle = DamageEffectSpec->GetEffectContext();
if (const FHitResult* Hit = ContextHandle.GetHitResult())
{
const FName HitBone = Hit->BoneName;
if (HitBone != FName("head"))
{
// Get owning LyraCharacter to check downed state
ALyraCharacter* LyraChar = Cast<ALyraCharacter>(GetOwner());
if (LyraChar)
{
if (!LyraChar->isWasDowned)
{
if (!LyraChar->isDowned)
{
LyraChar->isDowned = true;
// Prevent actual death by restoring minimal health
if (AbilitySystemComponent && HealthSet)
{
AbilitySystemComponent->SetNumericAttributeBase(
ULyraHealthSet::GetHealthAttribute(), 1.f);
}
SaveLethalSpec(DamageEffectSpec, DamageInstigator, DamageCauser);
BleedOut(LyraChar);
return;
}
}
}
}
}
// 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
// Fall damage function
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);
}
}
// Lethal damage spec snapshot function
void ULyraHealthComponent::SaveLethalSpec(const FGameplayEffectSpec* Spec, AActor* DamageInstigator, AActor* DamageCauser)
{
if (!Spec) return;
// Clear variable
LethalSnapshot = FCarbonLethalSnapshot();
// Save variables
LethalSnapshot.Instigator = DamageInstigator;
LethalSnapshot.Causer = DamageCauser;
LethalSnapshot.DamageDef = Spec->Def;
LethalSnapshot.SourceTags = *Spec->CapturedSourceTags.GetAggregatedTags();
const FGameplayEffectContextHandle& Ctx = Spec->GetEffectContext();
LethalSnapshot.SourceObject = Ctx.GetSourceObject();
UE_LOG(LogLyra, Log, TEXT("LyraHealthComponent: Spec Instigator %s used %s with %s."), *GetNameSafe(LethalSnapshot.Instigator.Get()), *GetNameSafe(LethalSnapshot.DamageDef.Get()), *LethalSnapshot.SourceTags.ToStringSimple());
};
// Start timer to bleed out
void ULyraHealthComponent::BleedOut(ALyraCharacter* LyraChar)
{
UE_LOG(LogLyra, Log, TEXT("LyraHealthComponent: Bleed out started."));
if (!GetWorld()) return;
GetWorld()->GetTimerManager().SetTimer(
DeathDelayTimerHandle,
this, &ULyraHealthComponent::BleedOutDeath,
LyraChar->Grit,
false);
UE_LOG(LogLyra, Log, TEXT("LyraHealthComponent: Bleed out ended after %f."), LyraChar->Grit);
}
// Bleed out death
void ULyraHealthComponent::BleedOutDeath()
{
#if WITH_SERVER_CODE
// If not healed
if (GetHealth() == 1.f)
{
// Rebuild context with from cached variables
if (AbilitySystemComponent)
{
const TSubclassOf<UGameplayEffect> DamageGE = ULyraAssetManager::GetSubclass(ULyraGameData::Get().DamageGameplayEffect_SetByCaller);
if (DamageGE)
{
FGameplayEffectContextHandle Ctx = AbilitySystemComponent->MakeEffectContext();
if (AActor* Inst = LethalSnapshot.Instigator.Get())
{
Ctx.AddInstigator(Inst, LethalSnapshot.Causer.Get());
}
if (UObject* SrcObj = LethalSnapshot.SourceObject.Get())
{
Ctx.AddSourceObject(SrcObj);
}
FGameplayEffectSpecHandle SpecHandle = AbilitySystemComponent->MakeOutgoingSpec(DamageGE, 1.f, Ctx);
if (FGameplayEffectSpec* Spec = SpecHandle.Data.Get())
{
// Apply type tag for elimination feed
for (const FGameplayTag& T : LethalSnapshot.SourceTags)
{
Spec->AddDynamicAssetTag(T);
}
Spec->SetSetByCallerMagnitude(LyraGameplayTags::SetByCaller_Damage, 1.f);
AbilitySystemComponent->ApplyGameplayEffectSpecToSelf(*Spec);
return;
}
}
}
// Fallback to default death
DamageSelfDestruct(false);
return;
}
// If healed cancel and exit
GetWorld()->GetTimerManager().ClearTimer(DeathDelayTimerHandle);
#endif
}