File size: 5,163 Bytes
7fd553e | 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 | // Copyright Epic Games, Inc. All Rights Reserved.
#include "LyraGameplayAbility_Interact.h"
#include "AbilitySystemComponent.h"
#include "Interaction/IInteractableTarget.h"
#include "Interaction/InteractionStatics.h"
#include "Interaction/Tasks/AbilityTask_GrantNearbyInteraction.h"
#include "NativeGameplayTags.h"
#include "Player/LyraPlayerController.h"
#include "UI/IndicatorSystem/IndicatorDescriptor.h"
#include "UI/IndicatorSystem/LyraIndicatorManagerComponent.h"
#include UE_INLINE_GENERATED_CPP_BY_NAME(LyraGameplayAbility_Interact)
class UUserWidget;
UE_DEFINE_GAMEPLAY_TAG_STATIC(TAG_Ability_Interaction_Activate, "Ability.Interaction.Activate");
UE_DEFINE_GAMEPLAY_TAG(TAG_INTERACTION_DURATION_MESSAGE, "Ability.Interaction.Duration.Message");
ULyraGameplayAbility_Interact::ULyraGameplayAbility_Interact(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
ActivationPolicy = ELyraAbilityActivationPolicy::OnSpawn;
InstancingPolicy = EGameplayAbilityInstancingPolicy::InstancedPerActor;
NetExecutionPolicy = EGameplayAbilityNetExecutionPolicy::LocalPredicted;
}
void ULyraGameplayAbility_Interact::ActivateAbility(const FGameplayAbilitySpecHandle Handle, const FGameplayAbilityActorInfo* ActorInfo, const FGameplayAbilityActivationInfo ActivationInfo, const FGameplayEventData* TriggerEventData)
{
Super::ActivateAbility(Handle, ActorInfo, ActivationInfo, TriggerEventData);
UAbilitySystemComponent* AbilitySystem = GetAbilitySystemComponentFromActorInfo();
if (AbilitySystem && AbilitySystem->GetOwnerRole() == ROLE_Authority)
{
UAbilityTask_GrantNearbyInteraction* Task = UAbilityTask_GrantNearbyInteraction::GrantAbilitiesForNearbyInteractors(this, InteractionScanRange, InteractionScanRate);
Task->ReadyForActivation();
}
}
void ULyraGameplayAbility_Interact::UpdateInteractions(const TArray<FInteractionOption>& InteractiveOptions)
{
if (ALyraPlayerController* PC = GetLyraPlayerControllerFromActorInfo())
{
if (ULyraIndicatorManagerComponent* IndicatorManager = ULyraIndicatorManagerComponent::GetComponent(PC))
{
for (UIndicatorDescriptor* Indicator : Indicators)
{
IndicatorManager->RemoveIndicator(Indicator);
}
Indicators.Reset();
for (const FInteractionOption& InteractionOption : InteractiveOptions)
{
AActor* InteractableTargetActor = UInteractionStatics::GetActorFromInteractableTarget(InteractionOption.InteractableTarget);
TSoftClassPtr<UUserWidget> InteractionWidgetClass =
InteractionOption.InteractionWidgetClass.IsNull() ? DefaultInteractionWidgetClass : InteractionOption.InteractionWidgetClass;
UIndicatorDescriptor* Indicator = NewObject<UIndicatorDescriptor>();
Indicator->SetDataObject(InteractableTargetActor);
Indicator->SetSceneComponent(InteractableTargetActor->GetRootComponent());
Indicator->SetIndicatorClass(InteractionWidgetClass);
IndicatorManager->AddIndicator(Indicator);
Indicators.Add(Indicator);
}
}
else
{
//TODO This should probably be a noisy warning. Why are we updating interactions on a PC that can never do anything with them?
}
}
CurrentOptions = InteractiveOptions;
}
void ULyraGameplayAbility_Interact::TriggerInteraction()
{
if (CurrentOptions.Num() == 0)
{
return;
}
UAbilitySystemComponent* AbilitySystem = GetAbilitySystemComponentFromActorInfo();
if (AbilitySystem)
{
const FInteractionOption& InteractionOption = CurrentOptions[0];
AActor* Instigator = GetAvatarActorFromActorInfo();
AActor* InteractableTargetActor = UInteractionStatics::GetActorFromInteractableTarget(InteractionOption.InteractableTarget);
// Allow the target to customize the event data we're about to pass in, in case the ability needs custom data
// that only the actor knows.
FGameplayEventData Payload;
Payload.EventTag = TAG_Ability_Interaction_Activate;
Payload.Instigator = Instigator;
Payload.Target = InteractableTargetActor;
// If needed we allow the interactable target to manipulate the event data so that for example, a button on the wall
// may want to specify a door actor to execute the ability on, so it might choose to override Target to be the
// door actor.
InteractionOption.InteractableTarget->CustomizeInteractionEventData(TAG_Ability_Interaction_Activate, Payload);
// Grab the target actor off the payload we're going to use it as the 'avatar' for the interaction, and the
// source InteractableTarget actor as the owner actor.
AActor* TargetActor = const_cast<AActor*>(ToRawPtr(Payload.Target));
// The actor info needed for the interaction.
FGameplayAbilityActorInfo ActorInfo;
ActorInfo.InitFromActor(InteractableTargetActor, TargetActor, InteractionOption.TargetAbilitySystem);
// Trigger the ability using event tag.
const bool bSuccess = InteractionOption.TargetAbilitySystem->TriggerAbilityFromGameplayEvent(
InteractionOption.TargetInteractionAbilityHandle,
&ActorInfo,
TAG_Ability_Interaction_Activate,
&Payload,
*InteractionOption.TargetAbilitySystem
);
}
}
|