// Fill out your copyright notice in the Description page of Project Settings.
#include "BaseAudioTrigger.h"
#include "Components/AudioComponent.h"
#include "Components/SphereComponent.h"
#include "ConstructorHelpers.h"
#include "Kismet/GameplayStatics.h"
#include "Sound/SoundCue.h"
// Sets default values
ABaseAudioTrigger::ABaseAudioTrigger()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = false;
/*Sphere Trigger*/
SphereTrigger = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere Trigger"));
if( SphereTrigger )
{
SphereTrigger->InitSphereRadius(50.0f);
SphereTrigger->SetCollisionProfileName(FName("STCol"));
SphereTrigger->OnComponentBeginOverlap.AddDynamic(this, &ABaseAudioTrigger::OnOverlapBegin);
SphereTrigger->SetMobility(EComponentMobility::Static);
RootComponent = SphereTrigger;
}
/*Audio*/
//The cue to play
CollectedCue = ConstructorHelpers::FObjectFinder<USoundCue>
(TEXT("SoundCue'/Game/Sounds/Test/Rock_Cue.Rock_Cue'")).Object;
//The container of the cue's logic
CollectableAudioComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("CollectableAudioComp"));
if( CollectableAudioComponent )
{
CollectableAudioComponent->bAutoActivate = false;
CollectableAudioComponent->SetupAttachment(RootComponent);
CollectableAudioComponent->OnAudioFinished.AddDynamic(this, &ABaseAudioTrigger::OnAudioFinished);
}
}
void ABaseAudioTrigger::PostInitializeComponents()
{
Super::PostInitializeComponents();
CollectableAudioComponent->SetSound(CollectedCue);
}
void ABaseAudioTrigger::OnOverlapBegin(UPrimitiveComponent * OverlappedComp, AActor * OtherActor, UPrimitiveComponent * OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
if( ( OtherActor != nullptr ) && ( OtherActor != this ) && ( OtherComp != nullptr ) )
{
if( !bFinished )
{
bFinished = true;
CollectableAudioComponent->Play();
UGameplayStatics::SpawnEmitterAttached(
ParticleEffect,
RootComponent
);
}
}
}
void ABaseAudioTrigger::OnAudioFinished()
{
Destroy();
}