Spawn vehicles based on config file

This commit is contained in:
nsubiron 2017-06-02 21:24:43 +01:00
parent 2cd2c600eb
commit 029cfc9943
4 changed files with 47 additions and 33 deletions

View File

@ -11,18 +11,14 @@
// Sets default values
AVehicleSpawnerBase::AVehicleSpawnerBase(const FObjectInitializer& ObjectInitializer) :
Super(ObjectInitializer),
RandomStream(Seed)
{
PrimaryActorTick.bCanEverTick = true;
PrimaryActorTick.TickGroup = TG_PrePhysics;
}
RandomStream(Seed) {}
void AVehicleSpawnerBase::BeginPlay()
{
Super::BeginPlay();
NumberOfVehicles = FMath::Max(0, NumberOfVehicles);
// Allocate space for walkers.
Vehicles.Reserve(NumberOfVehicles);
@ -38,22 +34,26 @@ void AVehicleSpawnerBase::BeginPlay()
SpawnPoints.Add(*It);
}
UE_LOG(LogCarla, Log, TEXT("Found %d positions for spawning vehilces"), SpawnPoints.Num());
UE_LOG(LogCarla, Log, TEXT("Found %d positions for spawning vehicles"), SpawnPoints.Num());
if (SpawnPoints.Num() < 2) {
if (SpawnPoints.Num() < NumberOfVehicles) {
bSpawnVehicles = false;
UE_LOG(LogCarla, Error, TEXT("We don't have enough spawn points for vehicles!"));
}
while (bSpawnVehicles && (NumberOfVehicles > Vehicles.Num())) {
// Try to spawn one walker.
TryToSpawnRandomVehicle();
if (bSpawnVehicles) {
const int32 MaximumNumberOfAttempts = 4 * NumberOfVehicles;
int32 NumberOfAttempts = 0;
while ((NumberOfVehicles > Vehicles.Num()) && (NumberOfAttempts < MaximumNumberOfAttempts)) {
// Try to spawn one vehicle.
TryToSpawnRandomVehicle();
++NumberOfAttempts;
}
}
}
void AVehicleSpawnerBase::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (NumberOfVehicles > Vehicles.Num()) {
UE_LOG(LogCarla, Error, TEXT("Requested %d vehicles, but we were only able to spawn %d"), NumberOfVehicles, Vehicles.Num());
}
}
// =============================================================================

View File

@ -12,18 +12,17 @@ class APlayerStart;
UCLASS(Abstract)
class CARLA_API AVehicleSpawnerBase : public AActor
{
GENERATED_BODY()
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AVehicleSpawnerBase(const FObjectInitializer& ObjectInitializer);
// Sets default values for this actor's properties
AVehicleSpawnerBase(const FObjectInitializer& ObjectInitializer);
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called when the game starts or when spawned
virtual void BeginPlay() override;
UFUNCTION(BlueprintCallable)
const FRandomStream &GetRandomStream() const
@ -40,11 +39,7 @@ protected:
UFUNCTION(BlueprintImplementableEvent)
AAICarlaVehicleController* GetVehicleController(AWheeledVehicle* Vechicle);
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
void SetNumberOfVehicles(int32 Count);
@ -77,7 +72,7 @@ protected:
UPROPERTY()
FRandomStream RandomStream;
//UPROPERTY(Category = "Vechicle Spawner", VisibleAnywhere, AdvancedDisplay)
UPROPERTY(Category = "Vechicle Spawner", VisibleAnywhere, AdvancedDisplay)
TArray<APlayerStart *> SpawnPoints;
/*
UPROPERTY(Category = "Vechicle Spawner", BlueprintReadOnly, EditAnywhere, AdvancedDisplay)

View File

@ -59,6 +59,10 @@ void ACarlaGameModeBase::InitGame(
DynamicWeather = GetWorld()->SpawnActor<ADynamicWeather>(DynamicWeatherClass);
}
if (VehicleSpawnerClass != nullptr) {
VehicleSpawner = GetWorld()->SpawnActor<AVehicleSpawnerBase>(VehicleSpawnerClass);
}
if (WalkerSpawnerClass != nullptr) {
WalkerSpawner = GetWorld()->SpawnActor<AWalkerSpawnerBase>(WalkerSpawnerClass);
}
@ -109,6 +113,13 @@ void ACarlaGameModeBase::BeginPlay()
UE_LOG(LogCarla, Error, TEXT("Missing dynamic weather actor!"));
}
// Setup other vehicles.
if (VehicleSpawner != nullptr) {
VehicleSpawner->SetNumberOfVehicles(CarlaSettings.NumberOfVehicles);
} else {
UE_LOG(LogCarla, Error, TEXT("Missing vehicle spawner actor!"));
}
// Setup walkers.
if (WalkerSpawner != nullptr) {
WalkerSpawner->SetNumberOfWalkers(CarlaSettings.NumberOfPedestrians);

View File

@ -3,6 +3,7 @@
#pragma once
#include "GameFramework/GameModeBase.h"
#include "AI/VehicleSpawnerBase.h"
#include "AI/WalkerSpawnerBase.h"
#include "CarlaGameControllerBase.h"
#include "DynamicWeather.h"
@ -43,6 +44,10 @@ protected:
UPROPERTY(Category = "CARLA Classes", EditAnywhere, BlueprintReadOnly)
TSubclassOf<ADynamicWeather> DynamicWeatherClass;
/** The class of VehicleSpawner to spawn. */
UPROPERTY(Category = "CARLA Classes", EditAnywhere, BlueprintReadOnly)
TSubclassOf<AVehicleSpawnerBase> VehicleSpawnerClass;
/** The class of WalkerSpawner to spawn. */
UPROPERTY(Category = "CARLA Classes", EditAnywhere, BlueprintReadOnly)
TSubclassOf<AWalkerSpawnerBase> WalkerSpawnerClass;
@ -77,6 +82,9 @@ private:
UPROPERTY()
ADynamicWeather *DynamicWeather;
UPROPERTY()
AVehicleSpawnerBase *VehicleSpawner;
UPROPERTY()
AWalkerSpawnerBase *WalkerSpawner;
};