BaseAudioTrigger.cpp

From Kieran McEvaddy, 1 Month ago, written in C++, viewed 67 times. This paste is a reply to Re: BaseAudioTrigger.cpp from Kieran McEvaddy - view diff
URL http://codebin.org/view/96f724bd Embed
Download Paste or View Raw
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3. #include "BaseAudioTrigger.h"
  4. #include "Components/AudioComponent.h"
  5. #include "Components/SphereComponent.h"
  6. #include "ConstructorHelpers.h"
  7. #include "Kismet/GameplayStatics.h"
  8. #include "Sound/SoundCue.h"
  9.  
  10. // Sets default values
  11. ABaseAudioTrigger::ABaseAudioTrigger()
  12. {
  13.         // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  14.         PrimaryActorTick.bCanEverTick = false;
  15.  
  16.         /*Sphere Trigger*/
  17.         SphereTrigger = CreateDefaultSubobject<USphereComponent>(TEXT("Sphere Trigger"));
  18.         if( SphereTrigger )
  19.         {
  20.                 SphereTrigger->InitSphereRadius(50.0f);
  21.                 SphereTrigger->SetCollisionProfileName(FName("STCol"));
  22.                 SphereTrigger->OnComponentBeginOverlap.AddDynamic(this, &ABaseAudioTrigger::OnOverlapBegin);
  23.                 SphereTrigger->SetMobility(EComponentMobility::Static);
  24.  
  25.                 RootComponent = SphereTrigger;
  26.         }
  27.  
  28.         /*Audio*/
  29.         //The cue to play
  30.         CollectedCue = ConstructorHelpers::FObjectFinder<USoundCue>
  31.                 (TEXT("SoundCue'/Game/Sounds/Test/Rock_Cue.Rock_Cue'")).Object;
  32.  
  33.         //The container of the cue's logic
  34.         CollectableAudioComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("CollectableAudioComp"));
  35.         if( CollectableAudioComponent )
  36.         {
  37.                 CollectableAudioComponent->bAutoActivate = false;
  38.                 CollectableAudioComponent->SetupAttachment(RootComponent);
  39.                 CollectableAudioComponent->OnAudioFinished.AddDynamic(this, &ABaseAudioTrigger::OnAudioFinished);
  40.         }
  41.  
  42.  
  43. }
  44.  
  45. void ABaseAudioTrigger::PostInitializeComponents()
  46. {
  47.         Super::PostInitializeComponents();
  48.  
  49.         CollectableAudioComponent->SetSound(CollectedCue);
  50. }
  51.  
  52. void ABaseAudioTrigger::OnOverlapBegin(UPrimitiveComponent * OverlappedComp, AActor * OtherActor, UPrimitiveComponent * OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
  53. {
  54.         if( ( OtherActor != nullptr ) && ( OtherActor != this ) && ( OtherComp != nullptr ) )
  55.         {
  56.                 if( !bFinished )
  57.                 {
  58.                         bFinished = true;
  59.                         CollectableAudioComponent->Play();
  60.  
  61.                         UGameplayStatics::SpawnEmitterAttached(
  62.                                 ParticleEffect,
  63.                                 RootComponent
  64.                         );
  65.                 }
  66.         }
  67. }
  68.  
  69. void ABaseAudioTrigger::OnAudioFinished()
  70. {
  71.         Destroy();
  72. }
  73.  
  74.  

Reply to "BaseAudioTrigger.cpp"

Here you can reply to the paste above