RealtimeStyleTransferRuntime/Source/LyraGame/Feedback/ContextEffects/LyraContextEffectComponent.cpp

180 lines
5.6 KiB
C++
Raw Normal View History

2022-05-23 18:41:30 +00:00
// Copyright Epic Games, Inc. All Rights Reserved.
#include "LyraContextEffectComponent.h"
2022-09-13 07:18:28 +00:00
#include "Chaos/ChaosEngineInterface.h"
#include "Containers/EnumAsByte.h"
#include "Containers/Map.h"
#include "Engine/EngineBaseTypes.h"
#include "Engine/HitResult.h"
#include "Engine/World.h"
2022-05-23 18:41:30 +00:00
#include "LyraContextEffectsSubsystem.h"
#include "PhysicalMaterials/PhysicalMaterial.h"
2022-09-13 07:18:28 +00:00
#include "UObject/WeakObjectPtr.h"
#include "UObject/WeakObjectPtrTemplates.h"
class UAnimSequenceBase;
class USceneComponent;
2022-05-23 18:41:30 +00:00
// Sets default values for this component's properties
ULyraContextEffectComponent::ULyraContextEffectComponent()
{
// Disable component tick, enable Auto Activate
PrimaryComponentTick.bCanEverTick = false;
bAutoActivate = true;
// ...
}
// Called when the game starts
void ULyraContextEffectComponent::BeginPlay()
{
Super::BeginPlay();
// ...
CurrentContexts.AppendTags(DefaultEffectContexts);
CurrentContextEffectsLibraries = DefaultContextEffectsLibraries;
// On Begin Play, Load and Add Context Effects pairings
if (const UWorld* World = GetWorld())
{
if (ULyraContextEffectsSubsystem* LyraContextEffectsSubsystem = World->GetSubsystem<ULyraContextEffectsSubsystem>())
{
LyraContextEffectsSubsystem->LoadAndAddContextEffectsLibraries(GetOwner(), CurrentContextEffectsLibraries);
}
}
}
void ULyraContextEffectComponent::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
// On End PLay, remove unnecessary context effects pairings
if (const UWorld* World = GetWorld())
{
if (ULyraContextEffectsSubsystem* LyraContextEffectsSubsystem = World->GetSubsystem<ULyraContextEffectsSubsystem>())
{
LyraContextEffectsSubsystem->UnloadAndRemoveContextEffectsLibraries(GetOwner());
}
}
Super::EndPlay(EndPlayReason);
}
// Implementation of Interface's AnimMotionEffect function
void ULyraContextEffectComponent::AnimMotionEffect_Implementation(const FName Bone, const FGameplayTag MotionEffect, USceneComponent* StaticMeshComponent,
const FVector LocationOffset, const FRotator RotationOffset, const UAnimSequenceBase* AnimationSequence,
const bool bHitSuccess, const FHitResult HitResult, FGameplayTagContainer Contexts,
FVector VFXScale, float AudioVolume, float AudioPitch)
{
// Prep Components
TArray<UAudioComponent*> AudioComponentsToAdd;
TArray<UNiagaraComponent*> NiagaraComponentsToAdd;
FGameplayTagContainer TotalContexts;
// Aggregate contexts
TotalContexts.AppendTags(Contexts);
TotalContexts.AppendTags(CurrentContexts);
// Check if converting Physical Surface Type to Context
if (bConvertPhysicalSurfaceToContext)
{
// Get Phys Mat Type Pointer
TWeakObjectPtr<UPhysicalMaterial> PhysicalSurfaceTypePtr = HitResult.PhysMaterial;
// Check if pointer is okay
if (PhysicalSurfaceTypePtr.IsValid())
{
// Get the Surface Type Pointer
TEnumAsByte<EPhysicalSurface> PhysicalSurfaceType = PhysicalSurfaceTypePtr->SurfaceType;
// If Settings are valid
if (const ULyraContextEffectsSettings* LyraContextEffectsSettings = GetDefault<ULyraContextEffectsSettings>())
{
// Convert Surface Type to known
if (const FGameplayTag* SurfaceContextPtr = LyraContextEffectsSettings->SurfaceTypeToContextMap.Find(PhysicalSurfaceType))
{
FGameplayTag SurfaceContext = *SurfaceContextPtr;
TotalContexts.AddTag(SurfaceContext);
}
}
}
}
// Cycle through Active Audio Components and cache
for (UAudioComponent* ActiveAudioComponent : ActiveAudioComponents)
{
if (ActiveAudioComponent)
{
AudioComponentsToAdd.Add(ActiveAudioComponent);
}
}
// Cycle through Active Niagara Components and cache
for (UNiagaraComponent* ActiveNiagaraComponent : ActiveNiagaraComponents)
{
if (ActiveNiagaraComponent)
{
NiagaraComponentsToAdd.Add(ActiveNiagaraComponent);
}
}
// Get World
if (const UWorld* World = GetWorld())
{
// Get Subsystem
if (ULyraContextEffectsSubsystem* LyraContextEffectsSubsystem = World->GetSubsystem<ULyraContextEffectsSubsystem>())
{
// Set up Audio Components and Niagara
TArray<UAudioComponent*> AudioComponents;
TArray<UNiagaraComponent*> NiagaraComponents;
// Spawn effects
LyraContextEffectsSubsystem->SpawnContextEffects(GetOwner(), StaticMeshComponent, Bone,
LocationOffset, RotationOffset, MotionEffect, TotalContexts,
AudioComponents, NiagaraComponents, VFXScale, AudioVolume, AudioPitch);
// Append resultant effects
AudioComponentsToAdd.Append(AudioComponents);
NiagaraComponentsToAdd.Append(NiagaraComponents);
}
}
// Append Active Audio Components
ActiveAudioComponents.Empty();
ActiveAudioComponents.Append(AudioComponentsToAdd);
// Append Active
ActiveNiagaraComponents.Empty();
ActiveNiagaraComponents.Append(NiagaraComponentsToAdd);
}
void ULyraContextEffectComponent::UpdateEffectContexts(FGameplayTagContainer NewEffectContexts)
{
// Reset and update
CurrentContexts.Reset(NewEffectContexts.Num());
CurrentContexts.AppendTags(NewEffectContexts);
}
void ULyraContextEffectComponent::UpdateLibraries(
TSet<TSoftObjectPtr<ULyraContextEffectsLibrary>> NewContextEffectsLibraries)
{
// Clear out existing Effects
CurrentContextEffectsLibraries = NewContextEffectsLibraries;
// Get World
if (const UWorld* World = GetWorld())
{
// Get Subsystem
if (ULyraContextEffectsSubsystem* LyraContextEffectsSubsystem = World->GetSubsystem<ULyraContextEffectsSubsystem>())
{
// Load and Add Libraries to Subsystem
LyraContextEffectsSubsystem->LoadAndAddContextEffectsLibraries(GetOwner(), CurrentContextEffectsLibraries);
}
}
}