|
|
|
|
| #include "LyraHeroComponent.h"
|
| #include "Components/GameFrameworkComponentDelegates.h"
|
| #include "Logging/MessageLog.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 "UserSettings/EnhancedInputUserSettings.h"
|
| #include "InputMappingContext.h"
|
|
|
| #include UE_INLINE_GENERATED_CPP_BY_NAME(LyraHeroComponent)
|
|
|
| #if WITH_EDITOR
|
| #include "Misc/UObjectToken.h"
|
| #endif
|
|
|
| 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;
|
| }
|
|
|
| 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
|
| {
|
|
|
| RegisterInitStateFeature();
|
| }
|
| }
|
|
|
| bool ULyraHeroComponent::CanChangeInitState(UGameFrameworkComponentManager* Manager, FGameplayTag CurrentState, FGameplayTag DesiredState) const
|
| {
|
| check(Manager);
|
|
|
| APawn* Pawn = GetPawn<APawn>();
|
|
|
| if (!CurrentState.IsValid() && DesiredState == LyraGameplayTags::InitState_Spawned)
|
| {
|
|
|
| if (Pawn)
|
| {
|
| return true;
|
| }
|
| }
|
| else if (CurrentState == LyraGameplayTags::InitState_Spawned && DesiredState == LyraGameplayTags::InitState_DataAvailable)
|
| {
|
|
|
| if (!GetPlayerState<ALyraPlayerState>())
|
| {
|
| return false;
|
| }
|
|
|
|
|
| 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>();
|
|
|
|
|
| if (!Pawn->InputComponent || !LyraPC || !LyraPC->GetLocalPlayer())
|
| {
|
| return false;
|
| }
|
| }
|
|
|
| return true;
|
| }
|
| else if (CurrentState == LyraGameplayTags::InitState_DataAvailable && DesiredState == LyraGameplayTags::InitState_DataInitialized)
|
| {
|
|
|
| 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)
|
| {
|
|
|
| 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 ULyraPawnData* PawnData = nullptr;
|
|
|
| if (ULyraPawnExtensionComponent* PawnExtComp = ULyraPawnExtensionComponent::FindPawnExtensionComponent(Pawn))
|
| {
|
| PawnData = PawnExtComp->GetPawnData<ULyraPawnData>();
|
|
|
|
|
|
|
| PawnExtComp->InitializeAbilitySystem(LyraPS->GetLyraAbilitySystemComponent(), LyraPS);
|
| }
|
|
|
| if (ALyraPlayerController* LyraPC = GetController<ALyraPlayerController>())
|
| {
|
| if (Pawn->InputComponent != nullptr)
|
| {
|
| InitializePlayerInput(Pawn->InputComponent);
|
| }
|
| }
|
|
|
|
|
| if (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)
|
| {
|
|
|
| CheckDefaultInitialization();
|
| }
|
| }
|
| }
|
|
|
| void ULyraHeroComponent::CheckDefaultInitialization()
|
| {
|
| static const TArray<FGameplayTag> StateChain = { LyraGameplayTags::InitState_Spawned, LyraGameplayTags::InitState_DataAvailable, LyraGameplayTags::InitState_DataInitialized, LyraGameplayTags::InitState_GameplayReady };
|
|
|
|
|
| ContinueInitStateChain(StateChain);
|
| }
|
|
|
| void ULyraHeroComponent::BeginPlay()
|
| {
|
| Super::BeginPlay();
|
|
|
|
|
| BindOnActorInitStateChanged(ULyraPawnExtensionComponent::NAME_ActorFeatureName, FGameplayTag(), false);
|
|
|
|
|
| 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)
|
| {
|
| for (const FInputMappingContextAndPriority& Mapping : DefaultInputMappings)
|
| {
|
| if (UInputMappingContext* IMC = Mapping.InputMapping.LoadSynchronous())
|
| {
|
| if (Mapping.bRegisterWithSettings)
|
| {
|
| if (UEnhancedInputUserSettings* Settings = Subsystem->GetUserSettings())
|
| {
|
| Settings->RegisterInputMappingContext(IMC);
|
| }
|
|
|
| FModifyContextOptions Options = {};
|
| Options.bIgnoreAllPressedKeysUntilRelease = false;
|
|
|
| Subsystem->AddMappingContext(IMC, Mapping.Priority, Options);
|
| }
|
| }
|
| }
|
|
|
|
|
|
|
|
|
| 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.")))
|
| {
|
|
|
| LyraIC->AddInputMappings(InputConfig, Subsystem);
|
|
|
|
|
|
|
| TArray<uint32> BindHandles;
|
| LyraIC->BindAbilityActions(InputConfig, this, &ThisClass::Input_AbilityInputTagPressed, &ThisClass::Input_AbilityInputTagReleased, BindHandles);
|
|
|
| LyraIC->BindNativeAction(InputConfig, LyraGameplayTags::InputTag_Move, ETriggerEvent::Triggered, this, &ThisClass::Input_Move, false);
|
| LyraIC->BindNativeAction(InputConfig, LyraGameplayTags::InputTag_Look_Mouse, ETriggerEvent::Triggered, this, &ThisClass::Input_LookMouse, false);
|
| LyraIC->BindNativeAction(InputConfig, LyraGameplayTags::InputTag_Look_Stick, ETriggerEvent::Triggered, this, &ThisClass::Input_LookStick, false);
|
| LyraIC->BindNativeAction(InputConfig, LyraGameplayTags::InputTag_Crouch, ETriggerEvent::Triggered, this, &ThisClass::Input_Crouch, false);
|
| LyraIC->BindNativeAction(InputConfig, LyraGameplayTags::InputTag_AutoRun, ETriggerEvent::Triggered, this, &ThisClass::Input_AutoRun, 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, BindHandles);
|
| }
|
| }
|
| }
|
|
|
| void ULyraHeroComponent::RemoveAdditionalInputConfig(const ULyraInputConfig* InputConfig)
|
| {
|
|
|
| }
|
|
|
| bool ULyraHeroComponent::IsReadyToBindInputs() const
|
| {
|
| return bReadyToBindInputs;
|
| }
|
|
|
| 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 (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()))
|
| {
|
|
|
| Controller->SetIsAutoRunning(!Controller->GetIsAutoRunning());
|
| }
|
| }
|
| }
|
|
|
| TSubclassOf<ULyraCameraMode> ULyraHeroComponent::DetermineCameraMode() const
|
| {
|
| 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)
|
| {
|
| if (AbilityCameraModeOwningSpecHandle == OwningSpecHandle)
|
| {
|
| AbilityCameraMode = nullptr;
|
| AbilityCameraModeOwningSpecHandle = FGameplayAbilitySpecHandle();
|
| }
|
| }
|
|
|
|
|