presidents-brigade/Source/PresidentsBrigade/EnemySpawner.cpp
Sara Montecino 1a864c9a73 Add enemies
2022-11-11 16:44:16 -08:00

60 lines
1.4 KiB
C++
Executable File

// 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;
spawn_delay_s = 5.0f;
}
// 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);
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)
{
FActorSpawnParameters spawnParameters;
spawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
spawnParameters.Owner = this;
AAIController* controller = GetWorld()->SpawnActor<AAIController>(
controller_class,
GetActorLocation(),
FRotator(0,0,0),
spawnParameters
);
ABasePawn* enemy = GetWorld()->SpawnActor<ABasePawn>(
pawn_class,
GetActorLocation(),
FRotator(0,0,0),
spawnParameters
);
controller->Possess(enemy);
spawn_wait_s = 0;
next_spawn_threshold_s = spawn_delay_s + random.GetFraction();
}
}