-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLyraHeroComponent.cpp
More file actions
580 lines (488 loc) · 19 KB
/
LyraHeroComponent.cpp
File metadata and controls
580 lines (488 loc) · 19 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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
// Copyright Epic Games, Inc. All Rights Reserved.
#include "LyraHeroComponent.h"
#include "Components/GameFrameworkComponentDelegates.h"
#include "Logging/MessageLog.h"
#include "Input/LyraMappableConfigPair.h"
#include "LyraLogChannels.h"
#include "EnhancedInputSubsystems.h"
#include "Player/LyraPlayerController.h"
#include "Player/LyraPlayerState.h"
#include "Player/LyraLocalPlayer.h"
#include "Character/LyraPawnExtensionComponent.h"
#include "Character/LyraPawnData.h"
#include "Character/LyraCharacter.h"
#include "AbilitySystem/LyraAbilitySystemComponent.h"
#include "Input/LyraInputConfig.h"
#include "Input/LyraInputComponent.h"
#include "Camera/LyraCameraComponent.h"
#include "LyraGameplayTags.h"
#include "Components/GameFrameworkComponentManager.h"
#include "PlayerMappableInputConfig.h"
#include "Camera/LyraCameraMode.h"
#include UE_INLINE_GENERATED_CPP_BY_NAME(LyraHeroComponent)
#if WITH_EDITOR
#include "Misc/UObjectToken.h"
#endif // WITH_EDITOR
namespace LyraHero
{
static const float LookYawRate = 300.0f;
static const float LookPitchRate = 165.0f;
};
const FName ULyraHeroComponent::NAME_BindInputsNow("BindInputsNow");
const FName ULyraHeroComponent::NAME_ActorFeatureName("Hero");
ULyraHeroComponent::ULyraHeroComponent(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
AbilityCameraMode = nullptr;
bReadyToBindInputs = false;
// @TangoAlphaDev
SecondaryAbilityCameraMode = nullptr;
// @TangoAlphaDev
}
void ULyraHeroComponent::OnRegister()
{
Super::OnRegister();
if (!GetPawn<APawn>())
{
UE_LOG(LogLyra, Error, TEXT("[ULyraHeroComponent::OnRegister] This component has been added to a blueprint whose base class is not a Pawn. To use this component, it MUST be placed on a Pawn Blueprint."));
#if WITH_EDITOR
if (GIsEditor)
{
static const FText Message = NSLOCTEXT("LyraHeroComponent", "NotOnPawnError", "has been added to a blueprint whose base class is not a Pawn. To use this component, it MUST be placed on a Pawn Blueprint. This will cause a crash if you PIE!");
static const FName HeroMessageLogName = TEXT("LyraHeroComponent");
FMessageLog(HeroMessageLogName).Error()
->AddToken(FUObjectToken::Create(this, FText::FromString(GetNameSafe(this))))
->AddToken(FTextToken::Create(Message));
FMessageLog(HeroMessageLogName).Open();
}
#endif
}
else
{
// Register with the init state system early, this will only work if this is a game world
RegisterInitStateFeature();
}
}
bool ULyraHeroComponent::CanChangeInitState(UGameFrameworkComponentManager* Manager, FGameplayTag CurrentState, FGameplayTag DesiredState) const
{
check(Manager);
APawn* Pawn = GetPawn<APawn>();
if (!CurrentState.IsValid() && DesiredState == LyraGameplayTags::InitState_Spawned)
{
// As long as we have a real pawn, let us transition
if (Pawn)
{
return true;
}
}
else if (CurrentState == LyraGameplayTags::InitState_Spawned && DesiredState == LyraGameplayTags::InitState_DataAvailable)
{
// The player state is required.
if (!GetPlayerState<ALyraPlayerState>())
{
return false;
}
// If we're authority or autonomous, we need to wait for a controller with registered ownership of the player state.
if (Pawn->GetLocalRole() != ROLE_SimulatedProxy)
{
AController* Controller = GetController<AController>();
const bool bHasControllerPairedWithPS = (Controller != nullptr) && \
(Controller->PlayerState != nullptr) && \
(Controller->PlayerState->GetOwner() == Controller);
if (!bHasControllerPairedWithPS)
{
return false;
}
}
const bool bIsLocallyControlled = Pawn->IsLocallyControlled();
const bool bIsBot = Pawn->IsBotControlled();
if (bIsLocallyControlled && !bIsBot)
{
ALyraPlayerController* LyraPC = GetController<ALyraPlayerController>();
// The input component and local player is required when locally controlled.
if (!Pawn->InputComponent || !LyraPC || !LyraPC->GetLocalPlayer())
{
return false;
}
}
return true;
}
else if (CurrentState == LyraGameplayTags::InitState_DataAvailable && DesiredState == LyraGameplayTags::InitState_DataInitialized)
{
// Wait for player state and extension component
ALyraPlayerState* LyraPS = GetPlayerState<ALyraPlayerState>();
return LyraPS && Manager->HasFeatureReachedInitState(Pawn, ULyraPawnExtensionComponent::NAME_ActorFeatureName, LyraGameplayTags::InitState_DataInitialized);
}
else if (CurrentState == LyraGameplayTags::InitState_DataInitialized && DesiredState == LyraGameplayTags::InitState_GameplayReady)
{
// TODO add ability initialization checks?
return true;
}
return false;
}
void ULyraHeroComponent::HandleChangeInitState(UGameFrameworkComponentManager* Manager, FGameplayTag CurrentState, FGameplayTag DesiredState)
{
if (CurrentState == LyraGameplayTags::InitState_DataAvailable && DesiredState == LyraGameplayTags::InitState_DataInitialized)
{
APawn* Pawn = GetPawn<APawn>();
ALyraPlayerState* LyraPS = GetPlayerState<ALyraPlayerState>();
if (!ensure(Pawn && LyraPS))
{
return;
}
const bool bIsLocallyControlled = Pawn->IsLocallyControlled();
const ULyraPawnData* PawnData = nullptr;
if (ULyraPawnExtensionComponent* PawnExtComp = ULyraPawnExtensionComponent::FindPawnExtensionComponent(Pawn))
{
PawnData = PawnExtComp->GetPawnData<ULyraPawnData>();
// The player state holds the persistent data for this player (state that persists across deaths and multiple pawns).
// The ability system component and attribute sets live on the player state.
PawnExtComp->InitializeAbilitySystem(LyraPS->GetLyraAbilitySystemComponent(), LyraPS);
}
if (ALyraPlayerController* LyraPC = GetController<ALyraPlayerController>())
{
if (Pawn->InputComponent != nullptr)
{
InitializePlayerInput(Pawn->InputComponent);
}
}
if (bIsLocallyControlled && PawnData)
{
if (ULyraCameraComponent* CameraComponent = ULyraCameraComponent::FindCameraComponent(Pawn))
{
CameraComponent->DetermineCameraModeDelegate.BindUObject(this, &ThisClass::DetermineCameraMode);
}
}
}
}
void ULyraHeroComponent::OnActorInitStateChanged(const FActorInitStateChangedParams& Params)
{
if (Params.FeatureName == ULyraPawnExtensionComponent::NAME_ActorFeatureName)
{
if (Params.FeatureState == LyraGameplayTags::InitState_DataInitialized)
{
// If the extension component says all all other components are initialized, try to progress to next state
CheckDefaultInitialization();
}
}
}
void ULyraHeroComponent::CheckDefaultInitialization()
{
static const TArray<FGameplayTag> StateChain = { LyraGameplayTags::InitState_Spawned, LyraGameplayTags::InitState_DataAvailable, LyraGameplayTags::InitState_DataInitialized, LyraGameplayTags::InitState_GameplayReady };
// This will try to progress from spawned (which is only set in BeginPlay) through the data initialization stages until it gets to gameplay ready
ContinueInitStateChain(StateChain);
}
void ULyraHeroComponent::BeginPlay()
{
Super::BeginPlay();
// Listen for when the pawn extension component changes init state
BindOnActorInitStateChanged(ULyraPawnExtensionComponent::NAME_ActorFeatureName, FGameplayTag(), false);
// Notifies that we are done spawning, then try the rest of initialization
ensure(TryToChangeInitState(LyraGameplayTags::InitState_Spawned));
CheckDefaultInitialization();
}
void ULyraHeroComponent::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
UnregisterInitStateFeature();
Super::EndPlay(EndPlayReason);
}
void ULyraHeroComponent::InitializePlayerInput(UInputComponent* PlayerInputComponent)
{
check(PlayerInputComponent);
const APawn* Pawn = GetPawn<APawn>();
if (!Pawn)
{
return;
}
const APlayerController* PC = GetController<APlayerController>();
check(PC);
const ULyraLocalPlayer* LP = Cast<ULyraLocalPlayer>(PC->GetLocalPlayer());
check(LP);
UEnhancedInputLocalPlayerSubsystem* Subsystem = LP->GetSubsystem<UEnhancedInputLocalPlayerSubsystem>();
check(Subsystem);
Subsystem->ClearAllMappings();
if (const ULyraPawnExtensionComponent* PawnExtComp = ULyraPawnExtensionComponent::FindPawnExtensionComponent(Pawn))
{
if (const ULyraPawnData* PawnData = PawnExtComp->GetPawnData<ULyraPawnData>())
{
if (const ULyraInputConfig* InputConfig = PawnData->InputConfig)
{
// Register any default input configs with the settings so that they will be applied to the player during AddInputMappings
for (const FMappableConfigPair& Pair : DefaultInputConfigs)
{
if (Pair.bShouldActivateAutomatically && Pair.CanBeActivated())
{
FModifyContextOptions Options = {};
Options.bIgnoreAllPressedKeysUntilRelease = false;
// Actually add the config to the local player
Subsystem->AddPlayerMappableConfig(Pair.Config.LoadSynchronous(), Options);
}
}
// The Lyra Input Component has some additional functions to map Gameplay Tags to an Input Action.
// If you want this functionality but still want to change your input component class, make it a subclass
// of the ULyraInputComponent or modify this component accordingly.
ULyraInputComponent* LyraIC = Cast<ULyraInputComponent>(PlayerInputComponent);
if (ensureMsgf(LyraIC, TEXT("Unexpected Input Component class! The Gameplay Abilities will not be bound to their inputs. Change the input component to ULyraInputComponent or a subclass of it.")))
{
// Add the key mappings that may have been set by the player
LyraIC->AddInputMappings(InputConfig, Subsystem);
// This is where we actually bind and input action to a gameplay tag, which means that Gameplay Ability Blueprints will
// be triggered directly by these input actions Triggered events.
TArray<uint32> BindHandles;
LyraIC->BindAbilityActions(InputConfig, this, &ThisClass::Input_AbilityInputTagPressed, &ThisClass::Input_AbilityInputTagReleased, /*out*/ BindHandles);
LyraIC->BindNativeAction(InputConfig, LyraGameplayTags::InputTag_Move, ETriggerEvent::Triggered, this, &ThisClass::Input_Move, /*bLogIfNotFound=*/ false);
LyraIC->BindNativeAction(InputConfig, LyraGameplayTags::InputTag_Look_Mouse, ETriggerEvent::Triggered, this, &ThisClass::Input_LookMouse, /*bLogIfNotFound=*/ false);
LyraIC->BindNativeAction(InputConfig, LyraGameplayTags::InputTag_Look_Stick, ETriggerEvent::Triggered, this, &ThisClass::Input_LookStick, /*bLogIfNotFound=*/ false);
LyraIC->BindNativeAction(InputConfig, LyraGameplayTags::InputTag_Crouch, ETriggerEvent::Triggered, this, &ThisClass::Input_Crouch, /*bLogIfNotFound=*/ false);
LyraIC->BindNativeAction(InputConfig, LyraGameplayTags::InputTag_AutoRun, ETriggerEvent::Triggered, this, &ThisClass::Input_AutoRun, /*bLogIfNotFound=*/ false);
}
}
}
}
if (ensure(!bReadyToBindInputs))
{
bReadyToBindInputs = true;
}
UGameFrameworkComponentManager::SendGameFrameworkComponentExtensionEvent(const_cast<APlayerController*>(PC), NAME_BindInputsNow);
UGameFrameworkComponentManager::SendGameFrameworkComponentExtensionEvent(const_cast<APawn*>(Pawn), NAME_BindInputsNow);
}
void ULyraHeroComponent::AddAdditionalInputConfig(const ULyraInputConfig* InputConfig)
{
TArray<uint32> BindHandles;
const APawn* Pawn = GetPawn<APawn>();
if (!Pawn)
{
return;
}
const APlayerController* PC = GetController<APlayerController>();
check(PC);
const ULocalPlayer* LP = PC->GetLocalPlayer();
check(LP);
UEnhancedInputLocalPlayerSubsystem* Subsystem = LP->GetSubsystem<UEnhancedInputLocalPlayerSubsystem>();
check(Subsystem);
if (const ULyraPawnExtensionComponent* PawnExtComp = ULyraPawnExtensionComponent::FindPawnExtensionComponent(Pawn))
{
ULyraInputComponent* LyraIC = Pawn->FindComponentByClass<ULyraInputComponent>();
if (ensureMsgf(LyraIC, TEXT("Unexpected Input Component class! The Gameplay Abilities will not be bound to their inputs. Change the input component to ULyraInputComponent or a subclass of it.")))
{
LyraIC->BindAbilityActions(InputConfig, this, &ThisClass::Input_AbilityInputTagPressed, &ThisClass::Input_AbilityInputTagReleased, /*out*/ BindHandles);
}
}
}
void ULyraHeroComponent::RemoveAdditionalInputConfig(const ULyraInputConfig* InputConfig)
{
//@TODO: Implement me!
}
bool ULyraHeroComponent::IsReadyToBindInputs() const
{
return bReadyToBindInputs;
}
// @TangoAlphaDev
void ULyraHeroComponent::ResetPlayerInputAfterPossess()
{
const APawn* Pawn = GetPawn<APawn>();
ALyraPlayerState* LyraPS = GetPlayerState<ALyraPlayerState>();
if (!ensure(Pawn && LyraPS))
{
return;
}
if (ULyraPawnExtensionComponent* PawnExtComp = ULyraPawnExtensionComponent::FindPawnExtensionComponent(Pawn))
{
// set loose gameplay tag so our character knows they were just repossessed
LyraPS->GetLyraAbilitySystemComponent()->SetLooseGameplayTagCount(LyraGameplayTags::Status_Repossessed, 1);
// The player state holds the persistent data for this player (state that persists across deaths and multiple pawns).
// The ability system component and attribute sets live on the player state.
PawnExtComp->InitializeAbilitySystem(LyraPS->GetLyraAbilitySystemComponent(), LyraPS);
LyraPS->GetLyraAbilitySystemComponent()->RemoveLooseGameplayTag(LyraGameplayTags::Status_Repossessed, 1);
}
if (GetController<ALyraPlayerController>())
{
if (Pawn->InputComponent != nullptr)
{
bReadyToBindInputs = false;
InitializePlayerInput(Pawn->InputComponent);
}
}
}
// @TangoAlphaDev
void ULyraHeroComponent::Input_AbilityInputTagPressed(FGameplayTag InputTag)
{
if (const APawn* Pawn = GetPawn<APawn>())
{
if (const ULyraPawnExtensionComponent* PawnExtComp = ULyraPawnExtensionComponent::FindPawnExtensionComponent(Pawn))
{
if (ULyraAbilitySystemComponent* LyraASC = PawnExtComp->GetLyraAbilitySystemComponent())
{
LyraASC->AbilityInputTagPressed(InputTag);
}
}
}
}
void ULyraHeroComponent::Input_AbilityInputTagReleased(FGameplayTag InputTag)
{
const APawn* Pawn = GetPawn<APawn>();
if (!Pawn)
{
return;
}
if (const ULyraPawnExtensionComponent* PawnExtComp = ULyraPawnExtensionComponent::FindPawnExtensionComponent(Pawn))
{
if (ULyraAbilitySystemComponent* LyraASC = PawnExtComp->GetLyraAbilitySystemComponent())
{
LyraASC->AbilityInputTagReleased(InputTag);
}
}
}
void ULyraHeroComponent::Input_Move(const FInputActionValue& InputActionValue)
{
APawn* Pawn = GetPawn<APawn>();
AController* Controller = Pawn ? Pawn->GetController() : nullptr;
// If the player has attempted to move again then cancel auto running
if (ALyraPlayerController* LyraController = Cast<ALyraPlayerController>(Controller))
{
LyraController->SetIsAutoRunning(false);
}
if (Controller)
{
const FVector2D Value = InputActionValue.Get<FVector2D>();
const FRotator MovementRotation(0.0f, Controller->GetControlRotation().Yaw, 0.0f);
if (Value.X != 0.0f)
{
const FVector MovementDirection = MovementRotation.RotateVector(FVector::RightVector);
Pawn->AddMovementInput(MovementDirection, Value.X);
}
if (Value.Y != 0.0f)
{
const FVector MovementDirection = MovementRotation.RotateVector(FVector::ForwardVector);
Pawn->AddMovementInput(MovementDirection, Value.Y);
}
}
}
void ULyraHeroComponent::Input_LookMouse(const FInputActionValue& InputActionValue)
{
APawn* Pawn = GetPawn<APawn>();
if (!Pawn)
{
return;
}
const FVector2D Value = InputActionValue.Get<FVector2D>();
if (Value.X != 0.0f)
{
Pawn->AddControllerYawInput(Value.X);
}
if (Value.Y != 0.0f)
{
Pawn->AddControllerPitchInput(Value.Y);
}
}
void ULyraHeroComponent::Input_LookStick(const FInputActionValue& InputActionValue)
{
APawn* Pawn = GetPawn<APawn>();
if (!Pawn)
{
return;
}
const FVector2D Value = InputActionValue.Get<FVector2D>();
const UWorld* World = GetWorld();
check(World);
if (Value.X != 0.0f)
{
Pawn->AddControllerYawInput(Value.X * LyraHero::LookYawRate * World->GetDeltaSeconds());
}
if (Value.Y != 0.0f)
{
Pawn->AddControllerPitchInput(Value.Y * LyraHero::LookPitchRate * World->GetDeltaSeconds());
}
}
void ULyraHeroComponent::Input_Crouch(const FInputActionValue& InputActionValue)
{
if (ALyraCharacter* Character = GetPawn<ALyraCharacter>())
{
Character->ToggleCrouch();
}
}
void ULyraHeroComponent::Input_AutoRun(const FInputActionValue& InputActionValue)
{
if (APawn* Pawn = GetPawn<APawn>())
{
if (ALyraPlayerController* Controller = Cast<ALyraPlayerController>(Pawn->GetController()))
{
// Toggle auto running
Controller->SetIsAutoRunning(!Controller->GetIsAutoRunning());
}
}
}
TSubclassOf<ULyraCameraMode> ULyraHeroComponent::DetermineCameraMode() const
{
// @TangoAlphaDev
if (SecondaryAbilityCameraMode)
{
return SecondaryAbilityCameraMode;
}
// @TangoAlphaDev
if (AbilityCameraMode)
{
return AbilityCameraMode;
}
const APawn* Pawn = GetPawn<APawn>();
if (!Pawn)
{
return nullptr;
}
if (ULyraPawnExtensionComponent* PawnExtComp = ULyraPawnExtensionComponent::FindPawnExtensionComponent(Pawn))
{
if (const ULyraPawnData* PawnData = PawnExtComp->GetPawnData<ULyraPawnData>())
{
return PawnData->DefaultCameraMode;
}
}
return nullptr;
}
void ULyraHeroComponent::SetAbilityCameraMode(TSubclassOf<ULyraCameraMode> CameraMode, const FGameplayAbilitySpecHandle& OwningSpecHandle)
{
if (CameraMode)
{
AbilityCameraMode = CameraMode;
AbilityCameraModeOwningSpecHandle = OwningSpecHandle;
}
}
void ULyraHeroComponent::ClearAbilityCameraMode(const FGameplayAbilitySpecHandle& OwningSpecHandle)
{
// @TangoAlphaDev
AbilityCameraMode = nullptr;
AbilityCameraModeOwningSpecHandle = FGameplayAbilitySpecHandle();
/*
if (AbilityCameraModeOwningSpecHandle == OwningSpecHandle)
{
AbilityCameraMode = nullptr;
AbilityCameraModeOwningSpecHandle = FGameplayAbilitySpecHandle();
}
*/
// @TangoAlphaDev
}
// @TangoAlphaDev
void ULyraHeroComponent::SetSecondaryAbilityCameraMode(TSubclassOf<ULyraCameraMode> CameraMode, const FGameplayAbilitySpecHandle& OwningSpecHandle)
{
if (CameraMode)
{
// Store the current AbilityCameraMode before changing it
StoredAbilityCameraMode = AbilityCameraMode;
// Set the new secondary ability camera mode
SetAbilityCameraMode(CameraMode, OwningSpecHandle);
SecondaryAbilityCameraMode = CameraMode;
SecondaryAbilityCameraModeOwningSpecHandle = OwningSpecHandle;
}
}
void ULyraHeroComponent::ClearSecondaryAbilityCameraMode(const FGameplayAbilitySpecHandle& OwningSpecHandle)
{
if (SecondaryAbilityCameraModeOwningSpecHandle == OwningSpecHandle)
{
SecondaryAbilityCameraMode = nullptr;
SecondaryAbilityCameraModeOwningSpecHandle = FGameplayAbilitySpecHandle();
// Restore the original AbilityCameraMode
SetAbilityCameraMode(StoredAbilityCameraMode, AbilityCameraModeOwningSpecHandle);
// Clear the stored mode
StoredAbilityCameraMode = nullptr;
}
}
// @TangoAlphaDev