HAxis sos
This commit is contained in:
127
Source/UnrealProject/Sound/MusicPlayer.cpp
Normal file
127
Source/UnrealProject/Sound/MusicPlayer.cpp
Normal file
@@ -0,0 +1,127 @@
|
||||
// Project Lab - NHTV Igad
|
||||
|
||||
#include "UnrealProject.h"
|
||||
#include "DefaultGameInstance.h"
|
||||
#include "Prefs.h"
|
||||
#include "MusicPlayer.h"
|
||||
|
||||
|
||||
AMusicPlayer::AMusicPlayer()
|
||||
{
|
||||
PrimaryActorTick.bCanEverTick = true;
|
||||
|
||||
RootComponent = audioComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("Audio"));
|
||||
|
||||
bReplicates = true;
|
||||
|
||||
m_setInCombat = false;
|
||||
m_isInCombat = false;
|
||||
}
|
||||
|
||||
void AMusicPlayer::BeginPlay()
|
||||
{
|
||||
Super::BeginPlay();
|
||||
|
||||
float volume = Cast<UDefaultGameInstance>(GetGameInstance())->GetPrefs()->musicVolume;
|
||||
audioComponent->SetVolumeMultiplier(volume);
|
||||
|
||||
if (generalMusic.Num() > 0)
|
||||
{
|
||||
m_currentTrack = FMath::Rand() % generalMusic.Num();
|
||||
|
||||
audioComponent->SetSound(generalMusic[m_currentTrack]);
|
||||
audioComponent->Play();
|
||||
}
|
||||
}
|
||||
|
||||
void AMusicPlayer::Tick( float DeltaTime )
|
||||
{
|
||||
Super::Tick( DeltaTime );
|
||||
|
||||
// Change the music combat state
|
||||
if (m_setInCombat != m_isInCombat)
|
||||
{
|
||||
// Fade out the volume
|
||||
m_changeTime += DeltaTime * 1.5f;
|
||||
if (m_changeTime >= 1)
|
||||
{
|
||||
// Set next state
|
||||
m_isInCombat = m_setInCombat;
|
||||
audioComponent->Stop();
|
||||
m_changeTime = 0;
|
||||
}
|
||||
}
|
||||
|
||||
float volume = Cast<UDefaultGameInstance>(GetGameInstance())->GetPrefs()->musicVolume;
|
||||
audioComponent->SetVolumeMultiplier(volume * (1.0f - m_changeTime));
|
||||
|
||||
if (!audioComponent->IsPlaying())
|
||||
m_SelectNextTrack();
|
||||
}
|
||||
|
||||
void AMusicPlayer::m_SelectNextTrack()
|
||||
{
|
||||
// Regenerate the array of music (in case any tracks are NULL)
|
||||
TArray<USoundBase*>& sampleFrom = (m_isInCombat ? combatMusic : generalMusic);
|
||||
if (sampleFrom.Num() == 0)
|
||||
return;
|
||||
TArray<USoundBase*> availableTracks;
|
||||
for (int32 i = 0; i < sampleFrom.Num(); i++)
|
||||
{
|
||||
if (sampleFrom[i])
|
||||
availableTracks.Add(sampleFrom[i]);
|
||||
}
|
||||
if (availableTracks.Num() == 0)
|
||||
return;
|
||||
|
||||
// Only select a random playlist if there are plenty of tracks
|
||||
const int32 trackCount = availableTracks.Num();
|
||||
if (trackCount > 5)
|
||||
{
|
||||
// Add the current track to the recently played list
|
||||
m_lastTracks.Add(m_currentTrack);
|
||||
while (m_lastTracks.Num() > 3)
|
||||
m_lastTracks.RemoveAt(0);
|
||||
|
||||
// Select a random number
|
||||
m_currentTrack = FMath::Rand() % availableTracks.Num();
|
||||
while (true)
|
||||
{
|
||||
// Check in the array of recently played to see if we already played this
|
||||
bool found = false;
|
||||
for (int32 i = 0; i < m_lastTracks.Num(); i++)
|
||||
{
|
||||
if (m_currentTrack == m_lastTracks[i])
|
||||
{
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found)
|
||||
break;
|
||||
|
||||
// Pick the next track and see if we havent already played that
|
||||
m_currentTrack = (m_currentTrack + 1) % availableTracks.Num();
|
||||
}
|
||||
}
|
||||
else
|
||||
m_currentTrack = (m_currentTrack + 1) % availableTracks.Num();
|
||||
|
||||
// Set the new track
|
||||
audioComponent->SetSound(availableTracks[m_currentTrack]);
|
||||
audioComponent->Play();
|
||||
}
|
||||
|
||||
void AMusicPlayer::SetInCombat(bool inCombat)
|
||||
{
|
||||
check(Role == ROLE_Authority);
|
||||
m_setInCombat = inCombat;
|
||||
m_changeTime = 0;
|
||||
}
|
||||
|
||||
void AMusicPlayer::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
|
||||
{
|
||||
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
|
||||
|
||||
DOREPLIFETIME(AMusicPlayer, m_setInCombat);
|
||||
}
|
||||
41
Source/UnrealProject/Sound/MusicPlayer.h
Normal file
41
Source/UnrealProject/Sound/MusicPlayer.h
Normal file
@@ -0,0 +1,41 @@
|
||||
// Project Lab - NHTV Igad
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "GameFramework/Actor.h"
|
||||
#include "MusicPlayer.generated.h"
|
||||
|
||||
UCLASS()
|
||||
class UNREALPROJECT_API AMusicPlayer : public AActor
|
||||
{
|
||||
GENERATED_BODY()
|
||||
|
||||
public:
|
||||
AMusicPlayer();
|
||||
|
||||
virtual void BeginPlay() override;
|
||||
|
||||
virtual void Tick( float DeltaSeconds ) override;
|
||||
|
||||
UPROPERTY(EditAnywhere)
|
||||
TArray<class USoundBase*> generalMusic;
|
||||
UPROPERTY(EditAnywhere)
|
||||
TArray<class USoundBase*> combatMusic;
|
||||
|
||||
void SetInCombat(bool inCombat);
|
||||
|
||||
UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
|
||||
class UAudioComponent* audioComponent;
|
||||
|
||||
private:
|
||||
int32 m_currentTrack;
|
||||
TArray<int32> m_lastTracks;
|
||||
|
||||
UPROPERTY(Replicated)
|
||||
bool m_setInCombat;
|
||||
|
||||
bool m_isInCombat;
|
||||
float m_changeTime;
|
||||
|
||||
void m_SelectNextTrack();
|
||||
};
|
||||
59
Source/UnrealProject/Sound/SoundEffect.cpp
Normal file
59
Source/UnrealProject/Sound/SoundEffect.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
// 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);
|
||||
}
|
||||
27
Source/UnrealProject/Sound/SoundEffect.h
Normal file
27
Source/UnrealProject/Sound/SoundEffect.h
Normal file
@@ -0,0 +1,27 @@
|
||||
// Project Lab - NHTV Igad
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "GameFramework/Actor.h"
|
||||
#include "SoundEffect.generated.h"
|
||||
|
||||
UCLASS()
|
||||
class UNREALPROJECT_API ASoundEffect : public AActor
|
||||
{
|
||||
GENERATED_BODY()
|
||||
public:
|
||||
ASoundEffect();
|
||||
|
||||
virtual void BeginPlay() override;
|
||||
virtual void Tick( float DeltaSeconds ) override;
|
||||
|
||||
void Init(USoundBase* sound);
|
||||
|
||||
UFUNCTION()
|
||||
void OnAudioFinished();
|
||||
|
||||
private:
|
||||
UPROPERTY(Replicated)
|
||||
class UAudioComponent* m_audio;
|
||||
|
||||
};
|
||||
50
Source/UnrealProject/Sound/SoundFunctionLibrary.cpp
Normal file
50
Source/UnrealProject/Sound/SoundFunctionLibrary.cpp
Normal file
@@ -0,0 +1,50 @@
|
||||
// Project Lab - NHTV Igad
|
||||
|
||||
#include "UnrealProject.h"
|
||||
#include "SoundEffect.h"
|
||||
#include "DefaultGameInstance.h"
|
||||
#include "Prefs.h"
|
||||
#include "SoundFunctionLibrary.h"
|
||||
|
||||
ASoundEffect* USoundFunctionLibrary::CreateSoundEffect(USoundBase* sndClass, AActor* spawner)
|
||||
{
|
||||
if (!sndClass)
|
||||
{
|
||||
JERROR("Sound class was not assigned when spawning Sound Effect");
|
||||
return nullptr;
|
||||
}
|
||||
if (!spawner)
|
||||
{
|
||||
JERROR("Spawner was not assigned when spawning Sound Effect");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
UWorld* const world = spawner->GetWorld();
|
||||
|
||||
FTransform transform;
|
||||
transform.SetIdentity();
|
||||
transform.TransformPosition(spawner->GetActorLocation());
|
||||
ASoundEffect* sound = world->SpawnActorDeferred<ASoundEffect>(ASoundEffect::StaticClass(), transform);
|
||||
if (sound)
|
||||
{
|
||||
sound->Init(sndClass);
|
||||
UGameplayStatics::FinishSpawningActor(sound, transform);
|
||||
}
|
||||
else
|
||||
{
|
||||
GWERROR(L"Failed to CreateSoundEffect()");
|
||||
}
|
||||
return sound;
|
||||
}
|
||||
|
||||
|
||||
float USoundFunctionLibrary::GetSFXVolume(UDefaultGameInstance* gameInstance)
|
||||
{
|
||||
if (!gameInstance || !gameInstance->IsA(UDefaultGameInstance::StaticClass())) { JERROR("GameInstance was not assigned getting Sound Volume"); return 0; }
|
||||
return Cast<UDefaultGameInstance>(gameInstance)->GetPrefs()->sfxVolume;
|
||||
}
|
||||
float USoundFunctionLibrary::GetMusicVolume(UDefaultGameInstance* gameInstance)
|
||||
{
|
||||
if (!gameInstance || !gameInstance->IsA(UDefaultGameInstance::StaticClass())) { JERROR("GameInstance was not assigned getting Music Volume"); return 0; }
|
||||
return Cast<UDefaultGameInstance>(gameInstance)->GetPrefs()->musicVolume;
|
||||
}
|
||||
25
Source/UnrealProject/Sound/SoundFunctionLibrary.h
Normal file
25
Source/UnrealProject/Sound/SoundFunctionLibrary.h
Normal file
@@ -0,0 +1,25 @@
|
||||
// Project Lab - NHTV Igad
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Kismet/BlueprintFunctionLibrary.h"
|
||||
#include "SoundFunctionLibrary.generated.h"
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
UCLASS()
|
||||
class UNREALPROJECT_API USoundFunctionLibrary : public UBlueprintFunctionLibrary
|
||||
{
|
||||
GENERATED_BODY()
|
||||
|
||||
|
||||
public:
|
||||
UFUNCTION(BlueprintCallable, Category = "Sound")
|
||||
static class ASoundEffect* CreateSoundEffect(class USoundBase* sndClass, class AActor* spawner);
|
||||
|
||||
UFUNCTION(BlueprintCallable, Category = "Sound")
|
||||
static float GetSFXVolume(class UDefaultGameInstance* gameInstance);
|
||||
UFUNCTION(BlueprintCallable, Category = "Sound")
|
||||
static float GetMusicVolume(class UDefaultGameInstance* gameInstance);
|
||||
};
|
||||
Reference in New Issue
Block a user