Implemented Voice Randomizer for Walking NPCs

This commit is contained in:
Rafal Swierczek 2024-05-13 18:18:30 +01:00
parent 0a74af3f44
commit bbdf749e11
10 changed files with 52 additions and 8 deletions

Binary file not shown.

Binary file not shown.

View File

@ -43,6 +43,9 @@ void ANPC_Manager::SpawnNPC_Walkers()
FRotator Rot = WalkersSpawnTransform.GetRotation().Rotator();
ANPC_WalkerClass* SpawnedWalker = GetWorld()->SpawnActor<ANPC_WalkerClass>(NPC_Walker, Loc, Rot);
NPC_Walkers.Add(SpawnedWalker);
if (VoiceLinesCollection.IsEmpty()) continue;
int i = FMath::RandRange(0, VoiceLinesCollection.Num() - 1);
SpawnedWalker->SetVoiceline(VoiceLinesCollection[i]);
}
}

View File

@ -8,6 +8,7 @@
#include "GameFramework/Actor.h"
#include "NPC_Manager.generated.h"
UCLASS()
class ENDLESSVENDETTA_API ANPC_Manager : public AActor
{
@ -48,6 +49,10 @@ protected:
// Spawn points for NPC Stations
UPROPERTY(EditAnywhere, Category = "NPC", meta = (MakeEditWidget = "true"))
TArray<FVector> StationPoints;
// VoiceLines used for randomly distributing amongst walking NPCs
UPROPERTY(EditDefaultsOnly, Category = "NPC")
TArray<FVoiceLines> VoiceLinesCollection;
// Called when the game starts or when spawned
virtual void BeginPlay() override;

View File

@ -3,6 +3,8 @@
#include "NPC_WalkerClass.h"
#include "Components/AudioComponent.h"
// Sets default values
ANPC_WalkerClass::ANPC_WalkerClass()
{
@ -33,5 +35,16 @@ void ANPC_WalkerClass::DisableNPC()
bIsEnabled = false;
}
void ANPC_WalkerClass::Interact()
{
if (VoiceLines.IsEmpty()) return;
UAudioComponent* Audio = Cast<UAudioComponent>(GetComponentByClass(UAudioComponent::StaticClass()));;
if (!IsValid(Audio)) return;
int i = FMath::RandRange(0, VoiceLines.Num() - 1);
if (!IsValid(VoiceLines[i])) return;
Audio->Sound = VoiceLines[i];
Audio->Play();
}

View File

@ -3,17 +3,20 @@
#pragma once
#include "CoreMinimal.h"
#include "VoicelinesStruct.h"
#include "EndlessVendetta/InteractionInterface.h"
#include "GameFramework/Character.h"
#include "NPC_WalkerClass.generated.h"
UCLASS()
class ENDLESSVENDETTA_API ANPC_WalkerClass : public ACharacter
class ENDLESSVENDETTA_API ANPC_WalkerClass : public ACharacter, public IInteractionInterface
{
GENERATED_BODY()
int WalkingSpotIndex = 0;
int Polarity = 1;
bool bIsEnabled = true;
TArray<USoundBase*> VoiceLines;
protected:
UPROPERTY(EditAnywhere, Category = "NPC", meta = (MakeEditWidget = "true"))
@ -33,4 +36,11 @@ public:
void DisableNPC();
void SetVoiceline(FVoiceLines VL)
{
VoiceLines = VL.Voices;
}
void Interact() override;
};

View File

@ -0,0 +1 @@
#include "VoicelinesStruct.h"

View File

@ -0,0 +1,12 @@
#pragma once
#include "VoicelinesStruct.generated.h"
USTRUCT(BlueprintType)
struct FVoiceLines
{
GENERATED_BODY();
UPROPERTY(EditAnywhere, Category = "NPC")
TArray<USoundBase*> Voices;
};