60 lines
1.2 KiB
C++
60 lines
1.2 KiB
C++
// 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);
|
|
}
|