// Fill out your copyright notice in the Description page of Project Settings. #include "EnemySpawner.h" // Sets default values AEnemySpawner::AEnemySpawner() { // 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; level_location = TUniquePtr(new LevelLocation()); spawn_delay_s = 5.0f; max_spawn_count = 5; } // Called when the game starts or when spawned void AEnemySpawner::BeginPlay() { Super::BeginPlay(); /* * Initialize random stream. */ random.Initialize(FMath::Rand()); } // Called every frame void AEnemySpawner::Tick(float delta_s) { Super::Tick(delta_s); if (!level_location->is_initialized()) { level_location->initialize(GetWorld(), marker_class); } spawn_wait_s += delta_s; if (next_spawn_threshold_s == 0) { next_spawn_threshold_s = spawn_delay_s + random.GetFraction(); } else if (spawn_wait_s > next_spawn_threshold_s) { const int spawn_count = random.RandRange(0, max_spawn_count); for (int i = 0; i < spawn_count; i++) { const FVector location = level_location->get_random_mark(); FActorSpawnParameters spawnParameters; spawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn; spawnParameters.Owner = this; AAIController* controller = GetWorld()->SpawnActor( controller_class, location, FRotator(0,0,0), spawnParameters ); ABasePawn* enemy = GetWorld()->SpawnActor( pawn_class, location, FRotator(0,0,0), spawnParameters ); controller->Possess(enemy); } spawn_wait_s = 0; next_spawn_threshold_s = spawn_delay_s + random.GetFraction(); } }