haxis/Source/UnrealProject/Sound/SoundEffect.cpp

59 lines
1.3 KiB
C++

// Project Lab - NHTV Igad
#include "UnrealProject.h"
#include "SoundEffect.h"
#include "Prefs.h"
#include "DefaultGameInstance.h"
// Sets default values
ASoundEffect::ASoundEffect()
{
PrimaryActorTick.bCanEverTick = true;
bReplicates = true;
RootComponent = m_audio = CreateDefaultSubobject<UAudioComponent>(TEXT("Audio"));
}
void ASoundEffect::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME_CONDITION(ASoundEffect, m_audio, COND_InitialOnly);
}
// Called when the game starts or when spawned
void ASoundEffect::BeginPlay()
{
Super::BeginPlay();
m_audio->OnAudioFinished.AddDynamic(this, &ASoundEffect::OnAudioFinished);
if (!m_audio->Sound)
Destroy();
// Initial volume
float volume = Cast<UDefaultGameInstance>(GetGameInstance())->GetPrefs()->sfxVolume;
m_audio->SetVolumeMultiplier(volume);
m_audio->Play();
}
// Called every frame
void ASoundEffect::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
// Update volume settings
float volume = Cast<UDefaultGameInstance>(GetGameInstance())->GetPrefs()->sfxVolume;
m_audio->SetVolumeMultiplier(volume);
}
void ASoundEffect::OnAudioFinished()
{
Destroy();
}
void ASoundEffect::Init(USoundBase* sound)
{
m_audio->SetSound(sound);
}