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);
79 Data.
Actor = InstancedActor;
87 FTransform SpawnTransform,
const FString& ActorName,
88 const FString& ActorID,
bool SnapAboveGround,
89 const float AboveOffset,
const bool DestroyOverlappingActors)
99 TSubclassOf<AActor> ActorClass = ActorAssetData ? ActorAssetData->
ActorAsset.
Actor :
nullptr;
101 ESpawnActorCollisionHandlingMethod SpawnCollisionHandling = ESpawnActorCollisionHandlingMethod::Undefined;
105 SpawnCollisionHandling = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
108 FString ID = ActorID;
114 AActor* SpawnedActor =
TrySpawnActor(ActorClass, SpawnTransform, ActorName, ID,
false,
false, SpawnCollisionHandling);
122 SpawnTransform = SpawnedActor->GetTransform();
130 Vehicle->TeleportTo(SpawnTransform.GetLocation(), SpawnTransform.GetRotation().Rotator());
132 if (DestroyOverlappingActors)
148 FString ActorName, FString ActorID,
149 bool RandomZRotation,
bool SnapToGround)
159 TSubclassOf<AActor> ActorClass = ActorAssetData ? ActorAssetData->
ActorAsset.
Actor :
nullptr;
161 AActor* SpawnedActor =
TrySpawnActor(ActorClass,
Transform, ActorName, ActorID, SnapToGround, RandomZRotation);
167 InstancedActorData.
Actor = InstancedActor;
180 Data.
Actor = InstancedActor;
188 const FString& ActorID,
bool StartAutomatically)
198 TSubclassOf<AActor> ActorClass = ActorAssetData ? ActorAssetData->
ActorAsset.
Actor :
nullptr;
200 FTransform StartTransform;
201 auto& Transforms = Parameters.
Points;
203 if (!Transforms.IsEmpty())
207 SimulatorLog::Log(
"Failed to spawn FollowPath Walker due to insufficient path points.");
210 StartTransform = Transforms[0];
213 FString ID = ActorID;
219 AActor* SpawnedActor =
TrySpawnActor(ActorClass, StartTransform, ActorName, ID,
false,
false);
225 Walker->SetWalkerParameters(Parameters);
226 Walker->SetIgnoreInput(!StartAutomatically);
236 FString ActorName, FString ActorID,
237 bool SnapToGround,
bool RandomZRotation, ESpawnActorCollisionHandlingMethod CollisionMethod)
239 if (!IsInGameThread())
242 UE_LOG(LogTemp, Warning, TEXT(
"AssetLibrary.cpp: Attempting to spawn actor from a background thread. This is not allowed."));
250 UE_LOG(LogTemp, Warning, TEXT(
"AssetLibrary.cpp: ActorClass is nullptr! Cannot spawn actor."));
255 UWorld* World =
nullptr;
256 if (GEngine && GEngine->GameViewport)
258 World = GEngine->GameViewport->GetWorld();
267 AActor* SpawnedActor = World->SpawnActorDeferred<AActor>(ActorClass,
Transform,
nullptr,
nullptr, CollisionMethod);
274 Transform = SpawnedActor->GetTransform();
279 float RotationAngle = FMath::FRandRange(0.0f, 360.0f);
280 Transform.SetRotation(FQuat::MakeFromEuler(FVector(0.0f, 0.0f, RotationAngle)));
284 if (SpawnedActor && SpawnedActor->GetClass()->ImplementsInterface(UActorInformation::StaticClass()))
286 IActorInformation::Execute_SetActorIDAndName(SpawnedActor, ActorName, ActorID);
298 UWorld* World =
nullptr;
299 if (GEngine && GEngine->GameViewport)
301 World = GEngine->GameViewport->GetWorld();
304 if (!World || !SpawnedActor)
311 SpawnedActor->GetActorBounds(
true, Origin, BoxExtent);
313 float SphereRadius = BoxExtent.GetMax();
315 TArray<AActor*> OverlappingActors;
316 UKismetSystemLibrary::SphereOverlapActors(World, SpawnedActor->GetTransform().GetLocation(), SphereRadius, {},
nullptr, {}, OverlappingActors);
318 for (AActor* OverlappingActor : OverlappingActors)
320 if (OverlappingActor && OverlappingActor != SpawnedActor)
322 if (OverlappingActor->IsA(AWalker::StaticClass()) ||
323 OverlappingActor->IsA(AInstancedActor::StaticClass()) ||
324 OverlappingActor->IsA(AVehicle::StaticClass()))
326 OverlappingActor->Destroy();
336 return !VehicleData.
Vehicle.IsValid();
356 int32 RandomIndex = FMath::RandRange(0,
FoliageKeys.Num() - 1);
360 return RandomFoliageType;
367 return !ActorData.
Actor.IsValid();
370 TArray<AInstancedActor*> ValidActors;
375 if (ActorData.
Actor.IsValid())
377 ValidActors.Add(ActorData.
Actor.Get());
398 int32 RandomIndex = FMath::RandRange(0,
PropKeys.Num() - 1);
399 RandomPropType =
PropKeys[RandomIndex];
402 return RandomPropType;
409 return !ActorData.
Actor.IsValid();
412 TArray<AInstancedActor*> ValidActors;
417 if (ActorData.
Actor.IsValid())
419 ValidActors.Add(ActorData.
Actor.Get());
435 SpawnedWalkers.RemoveAll([](
const TWeakObjectPtr<AWalker>& ActorData)
437 return !ActorData.IsValid();
440 TArray<AWalker*> ValidWalkers;
445 AWalker* WalkerPtr = ActorData.Get();
448 ValidWalkers.Add(WalkerPtr);
459 if (WalkersActors.IsEmpty())
472 WalkersActors.Empty();
474 FString Message = FString::Printf(TEXT(
"Destroyed all %d Walkers."), WalkersActors.Num());
484 VehicleDataAsset = Cast<UVehicleActorAssetMapDataAsset>(VehicleActorAssetMap.TryLoad());
503 FoliageTypeMap = UEnumUtilities::CreateEnumStringMap<EFoliageTypes>(
"/Script/Agrarsense.EFoliageTypes");
523 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 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 TArray< TWeakObjectPtr< AWalker > > SpawnedWalkers
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