Skip to content

Commit

Permalink
Fallback to default entry if physical surface type is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
dyanikoglu committed Mar 23, 2021
1 parent e928afe commit 9024de2
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,112 +18,136 @@

void UALSAnimNotifyFootstep::Notify(USkeletalMeshComponent* MeshComp, UAnimSequenceBase* Animation)
{
if (!MeshComp || !MeshComp->GetAnimInstance())
{
return;
}

if (HitDataTable)
{
UWorld* World = MeshComp->GetWorld();
AActor* MeshOwner = MeshComp->GetOwner();

const FVector FootLocation = MeshComp->GetSocketLocation(FootSocketName);
const FRotator FootRotation = MeshComp->GetSocketRotation(FootSocketName);
const FVector TraceEnd = FootLocation - MeshOwner->GetActorUpVector() * TraceLength;

FHitResult Hit;
TArray<AActor*> ActorsToIgnore;
ActorsToIgnore.Add(MeshOwner);
ActorsToIgnore.Append(MeshOwner->Children);
if (UKismetSystemLibrary::LineTraceSingle(World, FootLocation, TraceEnd, TraceChannel, true, ActorsToIgnore, DrawDebugType, Hit, true))
{
if (!Hit.PhysMaterial.Get())
{
return;
}

const EPhysicalSurface SurfaceType = Hit.PhysMaterial.Get()->SurfaceType;

TArray<FALSHitFX*> HitFXRows;
HitDataTable->GetAllRows<FALSHitFX>(FString(), HitFXRows);

FALSHitFX* HitFX = nullptr;
if (auto Result = HitFXRows.FindByPredicate([&](const FALSHitFX* Value){return SurfaceType == Value->SurfaceType;}))
{
HitFX = *Result;
}
else
{
return;
}

if (bSpawnSound && HitFX->Sound.LoadSynchronous())
{
UAudioComponent* SpawnedSound = nullptr;

const float MaskCurveValue = MeshComp->GetAnimInstance()->GetCurveValue(FName(TEXT("Mask_FootstepSound")));
const float FinalVolMult = bOverrideMaskCurve ? VolumeMultiplier : VolumeMultiplier * (1.0f - MaskCurveValue);

switch (HitFX->SoundSpawnType)
{
case EALSSpawnType::Location:
SpawnedSound = UGameplayStatics::SpawnSoundAtLocation(World, HitFX->Sound.Get(), Hit.Location + HitFX->SoundLocationOffset,
HitFX->SoundRotationOffset, FinalVolMult, PitchMultiplier);
break;

case EALSSpawnType::Attached:
SpawnedSound = UGameplayStatics::SpawnSoundAttached(HitFX->Sound.Get(), MeshComp, FootSocketName, HitFX->SoundLocationOffset,
HitFX->SoundRotationOffset, HitFX->SoundAttachmentType, true, FinalVolMult, PitchMultiplier);

break;
}
if (SpawnedSound)
{
SpawnedSound->SetIntParameter(SoundParameterName, static_cast<int32>(FootstepType));
}
}

if (bSpawnNiagara && HitFX->NiagaraSystem.LoadSynchronous())
{
UNiagaraComponent* SpawnedParticle = nullptr;
const FVector Location = Hit.Location + MeshOwner->GetTransform().TransformVector(HitFX->DecalLocationOffset);

switch (HitFX->NiagaraSpawnType)
{
case EALSSpawnType::Location:
SpawnedParticle = UNiagaraFunctionLibrary::SpawnSystemAtLocation(World, HitFX->NiagaraSystem.Get(), Location, FootRotation + HitFX->NiagaraRotationOffset);
break;

case EALSSpawnType::Attached:
SpawnedParticle = UNiagaraFunctionLibrary::SpawnSystemAttached(HitFX->NiagaraSystem.Get(), MeshComp, FootSocketName, HitFX->NiagaraLocationOffset,
HitFX->NiagaraRotationOffset, HitFX->NiagaraAttachmentType, true);
break;
}
}

if (bSpawnDecal && HitFX->DecalMaterial.LoadSynchronous())
{
const FVector Location = Hit.Location + MeshOwner->GetTransform().TransformVector(HitFX->DecalLocationOffset);

const FVector DecalSize = FVector(bMirrorDecalX ? -HitFX->DecalSize.X : HitFX->DecalSize.X, bMirrorDecalY ? -HitFX->DecalSize.Y : HitFX->DecalSize.Y,
bMirrorDecalZ ? -HitFX->DecalSize.Z : HitFX->DecalSize.Z);

UDecalComponent* SpawnedDecal = nullptr;
switch (HitFX->DecalSpawnType)
{
case EALSSpawnType::Location:
SpawnedDecal = UGameplayStatics::SpawnDecalAtLocation(World, HitFX->DecalMaterial.Get(), DecalSize, Location, FootRotation + HitFX->DecalRotationOffset, HitFX->DecalLifeSpan);
break;

case EALSSpawnType::Attached:
SpawnedDecal = UGameplayStatics::SpawnDecalAttached(HitFX->DecalMaterial.Get(), DecalSize, Hit.Component.Get(), NAME_None, Location,
FootRotation + HitFX->DecalRotationOffset, HitFX->DecalAttachmentType, HitFX->DecalLifeSpan);
break;
}
}
}
}
if (!MeshComp || !MeshComp->GetAnimInstance())
{
return;
}

if (HitDataTable)
{
UWorld* World = MeshComp->GetWorld();
AActor* MeshOwner = MeshComp->GetOwner();

const FVector FootLocation = MeshComp->GetSocketLocation(FootSocketName);
const FRotator FootRotation = MeshComp->GetSocketRotation(FootSocketName);
const FVector TraceEnd = FootLocation - MeshOwner->GetActorUpVector() * TraceLength;

FHitResult Hit;
TArray<AActor*> ActorsToIgnore;
ActorsToIgnore.Add(MeshOwner);
ActorsToIgnore.Append(MeshOwner->Children);
if (UKismetSystemLibrary::LineTraceSingle(World, FootLocation, TraceEnd, TraceChannel, true, ActorsToIgnore,
DrawDebugType, Hit, true))
{
if (!Hit.PhysMaterial.Get())
{
return;
}

const EPhysicalSurface SurfaceType = Hit.PhysMaterial.Get()->SurfaceType;

TArray<FALSHitFX*> HitFXRows;
HitDataTable->GetAllRows<FALSHitFX>(FString(), HitFXRows);

FALSHitFX* HitFX = nullptr;
if (auto FoundResult = HitFXRows.FindByPredicate([&](const FALSHitFX* Value)
{
return SurfaceType == Value->SurfaceType;
}))
{
HitFX = *FoundResult;
}
else if (auto DefaultResult = HitFXRows.FindByPredicate([&](const FALSHitFX* Value)
{
return EPhysicalSurface::SurfaceType_Default == Value->SurfaceType;
}))
{
HitFX = *DefaultResult;
}

if (bSpawnSound && HitFX->Sound.LoadSynchronous())
{
UAudioComponent* SpawnedSound = nullptr;

const float MaskCurveValue = MeshComp->GetAnimInstance()->GetCurveValue(
FName(TEXT("Mask_FootstepSound")));
const float FinalVolMult = bOverrideMaskCurve
? VolumeMultiplier
: VolumeMultiplier * (1.0f - MaskCurveValue);

switch (HitFX->SoundSpawnType)
{
case EALSSpawnType::Location:
SpawnedSound = UGameplayStatics::SpawnSoundAtLocation(
World, HitFX->Sound.Get(), Hit.Location + HitFX->SoundLocationOffset,
HitFX->SoundRotationOffset, FinalVolMult, PitchMultiplier);
break;

case EALSSpawnType::Attached:
SpawnedSound = UGameplayStatics::SpawnSoundAttached(HitFX->Sound.Get(), MeshComp, FootSocketName,
HitFX->SoundLocationOffset,
HitFX->SoundRotationOffset,
HitFX->SoundAttachmentType, true, FinalVolMult,
PitchMultiplier);

break;
}
if (SpawnedSound)
{
SpawnedSound->SetIntParameter(SoundParameterName, static_cast<int32>(FootstepType));
}
}

if (bSpawnNiagara && HitFX->NiagaraSystem.LoadSynchronous())
{
UNiagaraComponent* SpawnedParticle = nullptr;
const FVector Location = Hit.Location + MeshOwner->GetTransform().TransformVector(
HitFX->DecalLocationOffset);

switch (HitFX->NiagaraSpawnType)
{
case EALSSpawnType::Location:
SpawnedParticle = UNiagaraFunctionLibrary::SpawnSystemAtLocation(
World, HitFX->NiagaraSystem.Get(), Location, FootRotation + HitFX->NiagaraRotationOffset);
break;

case EALSSpawnType::Attached:
SpawnedParticle = UNiagaraFunctionLibrary::SpawnSystemAttached(
HitFX->NiagaraSystem.Get(), MeshComp, FootSocketName, HitFX->NiagaraLocationOffset,
HitFX->NiagaraRotationOffset, HitFX->NiagaraAttachmentType, true);
break;
}
}

if (bSpawnDecal && HitFX->DecalMaterial.LoadSynchronous())
{
const FVector Location = Hit.Location + MeshOwner->GetTransform().TransformVector(
HitFX->DecalLocationOffset);

const FVector DecalSize = FVector(bMirrorDecalX ? -HitFX->DecalSize.X : HitFX->DecalSize.X,
bMirrorDecalY ? -HitFX->DecalSize.Y : HitFX->DecalSize.Y,
bMirrorDecalZ ? -HitFX->DecalSize.Z : HitFX->DecalSize.Z);

UDecalComponent* SpawnedDecal = nullptr;
switch (HitFX->DecalSpawnType)
{
case EALSSpawnType::Location:
SpawnedDecal = UGameplayStatics::SpawnDecalAtLocation(
World, HitFX->DecalMaterial.Get(), DecalSize, Location,
FootRotation + HitFX->DecalRotationOffset, HitFX->DecalLifeSpan);
break;

case EALSSpawnType::Attached:
SpawnedDecal = UGameplayStatics::SpawnDecalAttached(HitFX->DecalMaterial.Get(), DecalSize,
Hit.Component.Get(), NAME_None, Location,
FootRotation + HitFX->DecalRotationOffset,
HitFX->DecalAttachmentType,
HitFX->DecalLifeSpan);
break;
}
}
}
}
}

FString UALSAnimNotifyFootstep::GetNotifyName_Implementation() const
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ class ALSV4_CPP_API UALSAnimNotifyFootstep : public UAnimNotify

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Trace")
float TraceLength = 50.0f;

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Decal")
bool bSpawnDecal = false;
bool bSpawnDecal = false;

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Decal")
bool bMirrorDecalX = false;

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Decal")
bool bMirrorDecalY = false;

Expand All @@ -78,5 +78,4 @@ class ALSV4_CPP_API UALSAnimNotifyFootstep : public UAnimNotify

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Niagara")
bool bSpawnNiagara = false;

};

0 comments on commit 9024de2

Please sign in to comment.