Implemented NPC Station Functionality

This commit is contained in:
Rafal Swierczek 2024-05-12 14:52:35 +01:00
parent 61c564966c
commit e227edba8d
15 changed files with 124 additions and 29 deletions

Binary file not shown.

Binary file not shown.

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:86987c9bdd1a2707a515186793698bc07d938101b1de0b500286cc16629e9359
size 22534
oid sha256:ca12e2004403a8ce0dfec5274e3b0ac13896cd6ad60932db1a1601469ffea0f6
size 22656

View File

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:9ea4c7725b4114e66152f59eabb0ee14ad9924dfb754bc29c0d81ef8fe676a19
size 88907

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:b049e683fe37a8cc646bbb5d5181a5fac5bb1f4be1accf14e2cc42102b32ce58
size 33754

View File

@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:6181121e680650a713fd6faa5be4bde5b96ed85921fc5bf76c55f7fd7baa8071
size 35338

Binary file not shown.

View File

@ -0,0 +1,6 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "NPC_InfoStruct.h"

View File

@ -0,0 +1,22 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "NPC_InfoStruct.generated.h"
USTRUCT(BlueprintType)
struct FNPC_InfoStruct
{
GENERATED_BODY();
UPROPERTY(BlueprintReadWrite, VisibleAnywhere)
FVector NPC_Location;
UPROPERTY(BlueprintReadWrite, VisibleAnywhere)
TArray<USoundBase*> BarkLines;
UPROPERTY(BlueprintReadWrite, VisibleAnywhere)
UAudioComponent* VoiceComp;
};

View File

@ -10,7 +10,7 @@ ANPC_Manager::ANPC_Manager()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
void ANPC_Manager::UpdateNPC_Stations()
@ -40,11 +40,12 @@ void ANPC_Manager::BeginPlay()
FRotator DefaultRot = FRotator(0, 0, 0);
for (FVector StationPoint : StationPoints)
{
int RandInt = FMath::RandRange(0, NPC_StationClasses.Num() - 1);// make station points be relative to world
ANPC_Station* NPC_Station = GetWorld()->SpawnActor<ANPC_Station>(NPC_StationClasses[RandInt], StationPoint, DefaultRot);
int RandInt = FMath::RandRange(0, NPC_StationClasses.Num() - 1);
FVector StationPointWorldLocation = StationPoint + GetActorLocation();
ANPC_Station* NPC_Station = GetWorld()->SpawnActor<ANPC_Station>(NPC_StationClasses[RandInt], StationPointWorldLocation, DefaultRot);
if (IsValid(NPC_Station)) NPC_Stations.Add(NPC_Station);
}
SetActorTickInterval(1);
}
// Called every frame

View File

@ -3,6 +3,44 @@
#include "NPC_Station.h"
#include "Components/AudioComponent.h"
#include "Kismet/GameplayStatics.h"
void ANPC_Station::SetupNPCs(TArray<FNPC_InfoStruct> NPC_Info_Array)
{
NPCs = NPC_Info_Array;
}
void ANPC_Station::Interact()
{
if (NPCs.IsEmpty() || !IsValid(PlayersPawn)) return;
// Find closest NPC to the player
int ClosestIndex = 0;
float Distance = 999999999;
for (int i = 0; i < NPCs.Num(); i++)
{
float CheckDistance = FVector::Distance(PlayersPawn->GetActorLocation(), NPCs[i].NPC_Location);
if (CheckDistance <= Distance)
{
ClosestIndex = i;
Distance = CheckDistance;
}
}
// Get a random voice line and play if it's valid
if (NPCs[ClosestIndex].BarkLines.IsEmpty() || NPCs[ClosestIndex].VoiceComp == nullptr) return;
int RandomVoiceLineIndex = FMath::RandRange(0, NPCs[ClosestIndex].BarkLines.Num() - 1);
if (!IsValid(NPCs[ClosestIndex].BarkLines[RandomVoiceLineIndex])) return;
NPCs[ClosestIndex].VoiceComp->Sound = NPCs[ClosestIndex].BarkLines[RandomVoiceLineIndex];
NPCs[ClosestIndex].VoiceComp->Play();
}
void ANPC_Station::BeginPlay()
{
Super::BeginPlay();
PlayersPawn = UGameplayStatics::GetPlayerPawn(GetWorld(), 0);
}
// Sets default values
ANPC_Station::ANPC_Station()
{
@ -11,5 +49,21 @@ ANPC_Station::ANPC_Station()
}
void ANPC_Station::EnableStation()
{
if (bIsEnabled) return;
GetRootComponent()->SetVisibility(true, true);
GetRootComponent()->SetActive(true);
bIsEnabled = true;
}
void ANPC_Station::DisableStation()
{
if (!bIsEnabled) return;
GetRootComponent()->SetVisibility(false, true);
GetRootComponent()->SetActive(false);
bIsEnabled = false;
}

View File

@ -3,24 +3,39 @@
#pragma once
#include "CoreMinimal.h"
#include "NPC_InfoStruct.h"
#include "EndlessVendetta/InteractionInterface.h"
#include "GameFramework/Actor.h"
#include "NPC_Station.generated.h"
UCLASS()
class ENDLESSVENDETTA_API ANPC_Station : public AActor
class ENDLESSVENDETTA_API ANPC_Station : public AActor, public IInteractionInterface
{
GENERATED_BODY()
bool bIsEnabled = false;
// Contains infor about individual NPC locations and Voices
TArray<FNPC_InfoStruct> NPCs;
AActor* PlayersPawn;
protected:
UFUNCTION(BlueprintCallable)
void SetupNPCs(TArray<FNPC_InfoStruct> NPC_Info_Array);
void Interact() override;
void BeginPlay() override;
public:
// Sets default values for this actor's properties
ANPC_Station();
UFUNCTION(BlueprintImplementableEvent)
void EnableStation();
UFUNCTION(BlueprintImplementableEvent)
void DisableStation();