Implemented NPC Manager with NPC Stations

This commit is contained in:
Rafal Swierczek 2024-05-12 12:17:06 +01:00
parent 005361ff32
commit efc9a0315e
10 changed files with 158 additions and 6 deletions

View File

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

View File

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

View File

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

View File

@ -0,0 +1,56 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "NPC_Manager.h"
#include "Kismet/GameplayStatics.h"
// Sets default values
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()
{
for (ANPC_Station* NPC_Station : NPC_Stations)
{
FVector PlayersLoc = PlayersActor->GetActorLocation();
FVector StationLoc = NPC_Station->GetActorLocation();
float Distance = FVector::Distance(PlayersLoc, StationLoc);
Distance > NPC_StationRenderDistance ? NPC_Station->DisableStation() : NPC_Station->EnableStation();
}
}
// Called when the game starts or when spawned
void ANPC_Manager::BeginPlay()
{
Super::BeginPlay();
PlayersActor = UGameplayStatics::GetPlayerPawn(GetWorld(), 0);
if (!IsValid(PlayersActor))
{
SetActorTickEnabled(false);
return;
}
if (NPC_StationClasses.IsEmpty()) return;
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);
if (IsValid(NPC_Station)) NPC_Stations.Add(NPC_Station);
}
}
// Called every frame
void ANPC_Manager::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
UpdateNPC_Stations();
}

View File

@ -0,0 +1,45 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "NPC_Station.h"
#include "GameFramework/Actor.h"
#include "NPC_Manager.generated.h"
UCLASS()
class ENDLESSVENDETTA_API ANPC_Manager : public AActor
{
GENERATED_BODY()
AActor* PlayersActor;
// Used for Selecting Random Stations to Spawn
UPROPERTY(EditDefaultsOnly, Category = "NPC")
TArray<TSubclassOf<ANPC_Station>> NPC_StationClasses;
// How far can the station be from the player before it's diabled
UPROPERTY(EditDefaultsOnly, Category = "NPC")
float NPC_StationRenderDistance = 5000;
// references to All Spawned NPC Stations
TArray<ANPC_Station*> NPC_Stations;
// Dynamically Enables/Disables Stations based on distance to player
void UpdateNPC_Stations();
protected:
// Spawn points for NPC Stations
UPROPERTY(EditAnywhere, Category = "NPC", meta = (MakeEditWidget = "true"))
TArray<FVector> StationPoints;
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Sets default values for this actor's properties
ANPC_Manager();
// Called every frame
virtual void Tick(float DeltaTime) override;
};

View File

@ -0,0 +1,15 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "NPC_Station.h"
// Sets default values
ANPC_Station::ANPC_Station()
{
// 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;
}

View File

@ -0,0 +1,27 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "NPC_Station.generated.h"
UCLASS()
class ENDLESSVENDETTA_API ANPC_Station : public AActor
{
GENERATED_BODY()
protected:
public:
// Sets default values for this actor's properties
ANPC_Station();
UFUNCTION(BlueprintImplementableEvent)
void EnableStation();
UFUNCTION(BlueprintImplementableEvent)
void DisableStation();
};