blaster/Source/Blaster/Pickups/PickupSpawnPoint.cpp

60 lines
1.2 KiB
C++
Raw Normal View History

2022-05-23 21:45:28 +00:00
// Fill out your copyright notice in the Description page of Project Settings.
#include "PickupSpawnPoint.h"
#include "Pickup.h"
#include "Kismet/GameplayStatics.h"
APickupSpawnPoint::APickupSpawnPoint()
{
PrimaryActorTick.bCanEverTick = true;
bReplicates = true;
}
void APickupSpawnPoint::BeginPlay()
{
Super::BeginPlay();
StartSpawnPickupTimer(nullptr);
}
void APickupSpawnPoint::SpawnPickup()
{
if (const int32 NumPickupClasses = PickupClasses.Num(); NumPickupClasses > 0)
{
const int32 Selection = FMath::RandRange(0, NumPickupClasses - 1);
SpawnedPickup = GetWorld()->SpawnActor<APickup>(
PickupClasses[Selection],
GetActorTransform()
);
if (HasAuthority() && SpawnedPickup)
{
SpawnedPickup->OnDestroyed.AddDynamic(this, &APickupSpawnPoint::StartSpawnPickupTimer);
}
}
}
void APickupSpawnPoint::StartSpawnPickupTimer(AActor* Destroyed)
{
const float SpawnTime = FMath::RandRange(SpawnPickupTimeMin, SpawnPickupTimeMax);
GetWorldTimerManager().SetTimer(
SpawnPickupTimer,
this,
&APickupSpawnPoint::SpawnPickupTimerFinished,
SpawnTime
);
}
void APickupSpawnPoint::SpawnPickupTimerFinished()
{
if (HasAuthority())
{
SpawnPickup();
}
}
void APickupSpawnPoint::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}