LEAP Documentation 40220
Documentation for the LEAP project
AmbianceComponent.h
Go to the documentation of this file.
1// Copyright Blue Isle Studios Inc 2018. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
6#include "Components/ActorComponent.h"
7#include "AmbianceComponent.generated.h"
8
9class APawn;
10class USoundBase;
11
12UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
13class PROJECTX_API UAmbianceComponent : public UActorComponent
14{
15 GENERATED_BODY()
16
17public:
18 /* Distance form the player in which to play the one shot sounds. */
19 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sounds")
20 FVector2D RandomSoundPlayDistance = FVector2D(1000.0f, 10000.0f);
21 /* Delay between oneshots. This is per Oneshot layer. */
22 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sounds")
23 FVector2D RandomSoundPlayDelay = FVector2D(1.0f, 3.0f);
24 /* Looping sound to glue the ambiance together. Typically wind. */
25 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sounds")
26 USoundBase* AmbianceLooping;
27 /* These sounds will play sporadically around the player. More of them will give you a layering effect. These sounds play randomly around the player in a circle. */
28 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sounds")
29 TArray<USoundBase*> AmbianceOneShots;
30 /* If transitioning between two volumes that use the same looping sound. The system will do nothing. This makes transitions sound seamless*/
31 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sounds", meta = (EditCondition="!bOverrideDefaultAmbiance"))
32 bool bIgnoreIfSameLoopingSound = false;
33 /* Overrides the default ambiance if the player enters this volume and will persist until another override volume has been entered.
34 * Leaving any other ambiance volumes will return the ambiance to this one. Used to create regional ambiance that still has caves/buildings/other changes within it. */
35 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Sounds", meta = (EditCondition = "!bIgnoreIfSameLoopingSound"))
36 bool bOverrideDefaultAmbiance = false;
37
38 UPROPERTY()
39 class UAudioComponent* AmbianceLoopingComponent;
40
41 bool bIsPlaying;
42
43 UAmbianceComponent();
44
45 virtual void BeginPlay() override;
46 virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
47
48 UAmbianceComponent* GetDefaultAmbiance(const APawn* Pawn);
49 bool StopAllOtherAmbiance();
50 bool StopAmbiance(UAmbianceComponent* Ambiance = nullptr);
51 void PlayAmbiance(APawn* Listener, bool bStopOtherAmbiance = false);
52 void PlayRandomSound(USoundBase* SoundToPlay);
53 void OverrideDefaultAmbiance(APawn* Pawn);
54
55 UFUNCTION()
56 void OnAmbianceRegionEnter(AActor* OverlappedActor, AActor* OtherActor);
57 UFUNCTION()
58 void OnAmbianceRegionExit(AActor* OverlappedActor, AActor* OtherActor);
59
60 FVector GetRandomSoundLocation() const;
61 FORCEINLINE float GetRandomSoundDelay() const { return FMath::FRandRange(RandomSoundPlayDelay.X, RandomSoundPlayDelay.Y); }
62
63private:
64 static uint8 VolumeSemaphore;
65 static TWeakObjectPtr<UAmbianceComponent> DefaultAmbiance;
66
67 UPROPERTY()
68 APawn* AmbianceCenter;
69};