22#include "Kismet/KismetSystemLibrary.h"
23#include "Engine/GameViewportClient.h"
24#include "GameFramework/Actor.h"
25#include "Engine/Engine.h"
26#include "UObject/Class.h"
27#include "Engine/World.h"
32static const FString
WALKER_ASSET_PATH =
"/Game/Agrarsense/Data/Walkers/WalkerActorAssetMap";
33static const FString
PROP_ASSET_PATH =
"/Game/Agrarsense/Data/Props/PropActorAssetMap";
52 FString ActorName, FString ActorID,
53 bool RandomZRotation,
bool SnapToGround)
63 TSubclassOf<AActor> ActorClass = ActorAssetData ? ActorAssetData->
ActorAsset.
Actor :
nullptr;
65 AActor* SpawnedActor =
TrySpawnActor(ActorClass,
Transform, ActorName, ActorID, SnapToGround, RandomZRotation);
71 FTransform SpawnTransform,
const FString& ActorName,
72 const FString& ActorID,
bool SnapAboveGround,
73 const float AboveOffset,
const bool DestroyOverlappingActors)
83 TSubclassOf<AActor> ActorClass = ActorAssetData ? ActorAssetData->
ActorAsset.
Actor :
nullptr;
85 ESpawnActorCollisionHandlingMethod SpawnCollisionHandling = ESpawnActorCollisionHandlingMethod::Undefined;
89 SpawnCollisionHandling = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
98 AActor* SpawnedActor =
TrySpawnActor(ActorClass, SpawnTransform, ActorName, ID,
false,
false, SpawnCollisionHandling);
106 SpawnTransform = SpawnedActor->GetTransform();
114 Vehicle->TeleportTo(SpawnTransform.GetLocation(), SpawnTransform.GetRotation().Rotator());
116 if (DestroyOverlappingActors)
126 FString ActorName, FString ActorID,
127 bool RandomZRotation,
bool SnapToGround)
137 TSubclassOf<AActor> ActorClass = ActorAssetData ? ActorAssetData->
ActorAsset.
Actor :
nullptr;
139 AActor* SpawnedActor =
TrySpawnActor(ActorClass,
Transform, ActorName, ActorID, SnapToGround, RandomZRotation);
145 InstancedActorData.
Actor = InstancedActor;
161 const FString& ActorID,
bool StartAutomatically)
171 TSubclassOf<AActor> ActorClass = ActorAssetData ? ActorAssetData->
ActorAsset.
Actor :
nullptr;
173 FTransform StartTransform;
174 auto& Transforms = Parameters.
Points;
176 if (!Transforms.IsEmpty())
180 SimulatorLog::Log(
"Failed to spawn FollowPath Walker due to insufficient path points.");
183 StartTransform = Transforms[0];
186 FString ID = ActorID;
192 AActor* SpawnedActor =
TrySpawnActor(ActorClass, StartTransform, ActorName, ID,
false,
false);
198 Walker->SetWalkerParameters(Parameters);
199 Walker->SetIgnoreInput(!StartAutomatically);
206 FString ActorName, FString ActorID,
207 bool SnapToGround,
bool RandomZRotation, ESpawnActorCollisionHandlingMethod CollisionMethod)
209 if (!IsInGameThread())
212 UE_LOG(LogTemp, Warning, TEXT(
"AssetLibrary.cpp: Attempting to spawn actor from a background thread. This is not allowed."));
220 UE_LOG(LogTemp, Warning, TEXT(
"AssetLibrary.cpp: ActorClass is nullptr! Cannot spawn actor."));
225 UWorld* World =
nullptr;
226 if (GEngine && GEngine->GameViewport)
228 World = GEngine->GameViewport->GetWorld();
237 AActor* SpawnedActor = World->SpawnActorDeferred<AActor>(ActorClass,
Transform,
nullptr,
nullptr, CollisionMethod);
244 Transform = SpawnedActor->GetTransform();
249 float RotationAngle = FMath::FRandRange(0.0f, 360.0f);
250 Transform.SetRotation(FQuat::MakeFromEuler(FVector(0.0f, 0.0f, RotationAngle)));
254 if (SpawnedActor && SpawnedActor->GetClass()->ImplementsInterface(UActorInformation::StaticClass()))
256 IActorInformation::Execute_SetActorIDAndName(SpawnedActor, ActorName, ActorID);
268 UWorld* World =
nullptr;
269 if (GEngine && GEngine->GameViewport)
271 World = GEngine->GameViewport->GetWorld();
274 if (!World || !SpawnedActor)
281 SpawnedActor->GetActorBounds(
true, Origin, BoxExtent);
283 float SphereRadius = BoxExtent.GetMax();
285 TArray<AActor*> OverlappingActors;
286 UKismetSystemLibrary::SphereOverlapActors(World, SpawnedActor->GetTransform().GetLocation(), SphereRadius, {},
nullptr, {}, OverlappingActors);
288 for (AActor* OverlappingActor : OverlappingActors)
290 if (OverlappingActor && OverlappingActor != SpawnedActor)
292 if (OverlappingActor->IsA(AWalker::StaticClass()) ||
293 OverlappingActor->IsA(AInstancedActor::StaticClass()) ||
294 OverlappingActor->IsA(AVehicle::StaticClass()))
296 OverlappingActor->Destroy();
306 return !VehicleData.
Vehicle.IsValid();
326 int32 RandomIndex = FMath::RandRange(0,
FoliageKeys.Num() - 1);
330 return RandomFoliageType;
337 return !ActorData.
Actor.IsValid();
340 TArray<AInstancedActor*> ValidActors;
345 if (ActorData.
Actor.IsValid())
347 ValidActors.Add(ActorData.
Actor.Get());
368 int32 RandomIndex = FMath::RandRange(0,
PropKeys.Num() - 1);
369 RandomPropType =
PropKeys[RandomIndex];
372 return RandomPropType;
379 return !ActorData.
Actor.IsValid();
382 TArray<AInstancedActor*> ValidActors;
387 if (ActorData.
Actor.IsValid())
389 ValidActors.Add(ActorData.
Actor.Get());
405 SpanwedWalkers.RemoveAll([](
const TWeakObjectPtr<AWalker>& ActorData)
407 return !ActorData.IsValid();
410 TArray<AWalker*> ValidWalkers;
415 AWalker* WalkerPtr = ActorData.Get();
418 ValidWalkers.Add(WalkerPtr);
429 if (WalkersActors.IsEmpty())
442 WalkersActors.Empty();
444 FString Message = FString::Printf(TEXT(
"Destroyed all %d Walkers."), WalkersActors.Num());
454 VehicleDataAsset = Cast<UVehicleActorAssetMapDataAsset>(VehicleActorAssetMap.TryLoad());
473 FoliageTypeMap = UEnumUtilities::CreateEnumStringMap<EFoliageTypes>(
"/Script/Agrarsense.EFoliageTypes");
493 PropTypeMap = UEnumUtilities::CreateEnumStringMap<EPropTypes>(
"/Script/Agrarsense.EPropTypes");
static const FString PROP_ASSET_PATH
static const FString VEHICLE_ASSET_PATH
static const FString FOLIAGE_ASSET_PATH
static const FString WALKER_ASSET_PATH
void SetTypeAndModel(const FString &Type, const FString &Model)
static void Log(const FString &Message, bool LogToTextFile=true, bool LogToROS=true)
static TWeakObjectPtr< UFoliageActorAssetMapDataAsset > FoliageActorAssetMapDataAsset
static UWalkerActorAssetMapDataAsset * GetWalkerActorAssetMapDataAsset()
static TArray< AInstancedActor * > GetAllAddedPropActors()
static void SetupWalkerDataAsset()
static TArray< FInstancedActorData > SpawnedPropActors
static TMap< EFoliageTypes, FString > FoliageTypeMap
static AVehicle * SpawnVehicle(EVehicleTypes VehicleType, FTransform SpawnTransform, const FString &ActorName, const FString &ActorID, bool SnapAboveGround=false, float AboveOffset=150.0f, bool DestroyOverlappingActors=false)
static void DestroyAllWalkers()
static TWeakObjectPtr< UVehicleActorAssetMapDataAsset > VehicleDataAsset
static void SetupVehicleDataAsset()
static EFoliageTypes GetRandomFoliageType()
static TArray< TWeakObjectPtr< AWalker > > SpanwedWalkers
static void DestroyOverlappingActorsSphere(AActor *SpawnedActor)
static TArray< FVehicleData > GetSpawnedVehicles()
static TArray< EPropTypes > PropKeys
static TArray< EFoliageTypes > FoliageKeys
static TMap< EPropTypes, FString > PropTypeMap
static TArray< AInstancedActor * > GetAllAddedFoliageActors()
static TArray< AWalker * > GetAllWalkers()
static void SetupPropDataAsset()
static TWeakObjectPtr< UPropActorAssetMapDataAsset > PropActorAssetMapDataAsset
static AActor * SpawnProp(EPropTypes PropType, FTransform Transform, FString ActorName="", FString ActorID="", bool RandomZRotation=false, bool SnapToGround=false)
static void SetupFoliageDataAsset()
static TWeakObjectPtr< UWalkerActorAssetMapDataAsset > WalkerActorAssetMapDataAsset
static AActor * TrySpawnActor(TSubclassOf< AActor > ActorClass, FTransform &Transform, FString ActorName, FString ActorID, bool SnapToGround, bool RandomZRotation, ESpawnActorCollisionHandlingMethod CollisionMethod=ESpawnActorCollisionHandlingMethod::Undefined)
static AWalker * SpawnWalker(FWalkerParameters Parameters, const FString &ActorName="", const FString &ActorID="", bool StartAutomatically=true)
static EPropTypes GetRandomPropType()
static TArray< FVehicleData > SpawnedVehicles
static UPropActorAssetMapDataAsset * GetPropActorAssetMapDataAsset()
static UFoliageActorAssetMapDataAsset * GetFoliageActorAssetMapDataAsset()
static AActor * SpawnFoliage(EFoliageTypes FoliageType, FTransform Transform, FString ActorName="", FString ActorID="", bool RandomZRotation=true, bool SnapToGround=true)
static TArray< FInstancedActorData > AddedFoliageActors
static FString ConvertVehicleTypeToString(EVehicleTypes VehicleType)
static FString ConvertWalkerTypeToString(EWalkerType WalkerType)
static bool SnapActorAboveGround(AActor *Actor, float AboveOffset=50.0f)
static bool SnapActorToGround(AActor *Actor, float StartZOffset=600.0f, float EndZOffset=600.0f)
TSubclassOf< AActor > Actor
TWeakObjectPtr< AInstancedActor > Actor
EVehicleTypes VehicleType
TWeakObjectPtr< AVehicle > Vehicle
EWalkerAction WalkerAction
TArray< FTransform > Points