Implemented Walker NPCs

This commit is contained in:
Rafal Swierczek 2024-05-13 11:16:01 +01:00
parent 9c12ecce85
commit 528e317961
7 changed files with 145 additions and 22 deletions

View File

@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/Environment/Highlighting/HighlightingSourceSnapshotLocation/@EntryValue">C:\Users\Rafal\AppData\Local\JetBrains\Rider2023.3\resharper-host\temp\Rider\vAny\CoverageData\_EndlessVendetta.-1253833435\Snapshot\snapshot.utdcvr</s:String></wpf:ResourceDictionary>

View File

@ -13,14 +13,58 @@ ANPC_Manager::ANPC_Manager()
}
void ANPC_Manager::SpawnNPC_Stations()
{
if (NPC_StationClasses.IsEmpty()) return;
FRotator DefaultRot = FRotator(0, 0, 0);
int StationClassIndex = 0;
for (FVector StationPoint : StationPoints)
{
FVector StationPointWorldLocation = StationPoint + GetActorLocation();
ANPC_Station* NPC_Station = GetWorld()->SpawnActor<ANPC_Station>(NPC_StationClasses[StationClassIndex], StationPointWorldLocation, DefaultRot);
if (IsValid(NPC_Station)) NPC_Stations.Add(NPC_Station);
StationClassIndex++;
if (StationClassIndex >= NPC_StationClasses.Num())
{
StationClassIndex = 0;
}
}
}
void ANPC_Manager::SpawnNPC_Walkers()
{
if (NPC_WalkerClasses.IsEmpty()) return;
for (TSubclassOf<ANPC_WalkerClass> NPC_Walker : NPC_WalkerClasses)
{
FTransform WalkersSpawnTransform = NPC_Walker->GetDefaultObject<ANPC_WalkerClass>()->SpawnTransform;
FVector Loc = WalkersSpawnTransform.GetLocation();
FRotator Rot = WalkersSpawnTransform.GetRotation().Rotator();
ANPC_WalkerClass* SpawnedWalker = GetWorld()->SpawnActor<ANPC_WalkerClass>(NPC_Walker, Loc, Rot);
NPC_Walkers.Add(SpawnedWalker);
}
}
void ANPC_Manager::UpdateNPC_Stations()
{
for (ANPC_Station* NPC_Station : NPC_Stations)
{
if (!IsValid(NPC_Station)) continue;
FVector PlayersLoc = PlayersActor->GetActorLocation();
FVector StationLoc = NPC_Station->GetActorLocation();
float Distance = FVector::Distance(PlayersLoc, StationLoc);
Distance > NPC_StationRenderDistance ? NPC_Station->DisableStation() : NPC_Station->EnableStation();
Distance > NPC_RenderDistance ? NPC_Station->DisableStation() : NPC_Station->EnableStation();
}
}
void ANPC_Manager::UpdateNPC_Walkers()
{
for (ANPC_WalkerClass* Walker : NPC_Walkers)
{
if (!IsValid(Walker)) continue;
float Distance = FVector::Distance(PlayersActor->GetActorLocation(), Walker->GetActorLocation());
Distance <= NPC_RenderDistance ? Walker->EnableNPC() : Walker->DisableNPC();
}
}
@ -35,22 +79,8 @@ void ANPC_Manager::BeginPlay()
SetActorTickEnabled(false);
return;
}
if (NPC_StationClasses.IsEmpty()) return;
FRotator DefaultRot = FRotator(0, 0, 0);
int StationClassIndex = 0;
for (FVector StationPoint : StationPoints)
{
FVector StationPointWorldLocation = StationPoint + GetActorLocation();
ANPC_Station* NPC_Station = GetWorld()->SpawnActor<ANPC_Station>(NPC_StationClasses[StationClassIndex], StationPointWorldLocation, DefaultRot);
if (IsValid(NPC_Station)) NPC_Stations.Add(NPC_Station);
StationClassIndex++;
if (StationClassIndex >= NPC_StationClasses.Num())
{
StationClassIndex = 0;
}
}
SpawnNPC_Stations();
SpawnNPC_Walkers();
SetActorTickInterval(1);
}
@ -59,5 +89,6 @@ void ANPC_Manager::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
UpdateNPC_Stations();
UpdateNPC_Walkers();
}

View File

@ -4,6 +4,7 @@
#include "CoreMinimal.h"
#include "NPC_Station.h"
#include "NPC_WalkerClass.h"
#include "GameFramework/Actor.h"
#include "NPC_Manager.generated.h"
@ -20,13 +21,29 @@ class ENDLESSVENDETTA_API ANPC_Manager : public AActor
// How far can the station be from the player before it's diabled
UPROPERTY(EditDefaultsOnly, Category = "NPC")
float NPC_StationRenderDistance = 5000;
float NPC_RenderDistance = 6000;
// references to All Spawned NPC Stations
TArray<ANPC_Station*> NPC_Stations;
// Used for Spawning walking NPCs
UPROPERTY(EditDefaultsOnly, Category = "NPC")
TArray<TSubclassOf<ANPC_WalkerClass>> NPC_WalkerClasses;
// Reference to Walking NPCs
TArray<ANPC_WalkerClass*> NPC_Walkers;
// Spawn NPC Stations
void SpawnNPC_Stations();
// Spawn NPC Walkers
void SpawnNPC_Walkers();
// Dynamically Enables/Disables Stations based on distance to player
void UpdateNPC_Stations();
// Dynamically Enables/Disables Walking NPCs based on distance to player
void UpdateNPC_Walkers();
protected:
// Spawn points for NPC Stations
UPROPERTY(EditAnywhere, Category = "NPC", meta = (MakeEditWidget = "true"))

View File

@ -0,0 +1,37 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "NPC_WalkerClass.h"
// Sets default values
ANPC_WalkerClass::ANPC_WalkerClass()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
FVector ANPC_WalkerClass::UpdateWalkingTarget()
{
int BoundsCheck = WalkingSpotIndex + Polarity;
if (BoundsCheck < 0 || BoundsCheck >= WalkingSpots.Num()) Polarity *= -1;
WalkingSpotIndex += Polarity;
return WalkingSpots[WalkingSpotIndex];
}
void ANPC_WalkerClass::EnableNPC()
{
if (bIsEnabled) return;
GetRootComponent()->SetVisibility(true, true);
bIsEnabled = true;
}
void ANPC_WalkerClass::DisableNPC()
{
if (!bIsEnabled) return;
GetRootComponent()->SetVisibility(false, true);
bIsEnabled = false;
}

View File

@ -0,0 +1,36 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "NPC_WalkerClass.generated.h"
UCLASS()
class ENDLESSVENDETTA_API ANPC_WalkerClass : public ACharacter
{
GENERATED_BODY()
int WalkingSpotIndex = 0;
int Polarity = 1;
bool bIsEnabled = true;
protected:
UPROPERTY(VisibleAnywhere, Category = "NPC", meta = (MakeEditWidget = "true"))
TArray<FVector> WalkingSpots;
public:
UPROPERTY(EditDefaultsOnly, Category = "NPC")
const FTransform SpawnTransform;
// Sets default values for this character's properties
ANPC_WalkerClass();
UFUNCTION(BlueprintCallable)
FVector UpdateWalkingTarget();
void EnableNPC();
void DisableNPC();
};