19 - Toggling the Spawn Volumes

给SpawnVolume添加 生成电池控制函数

Paste_Image.png

通过该函数 添加 删除电池生成定时器定时器


19 - Toggling the Spawn Volumes_第1张图片
Paste_Image.png

最终SpawnVolume.h
// Fill out your copyright notice in the Description page of Project Settings.

pragma once

include "GameFramework/Actor.h"

include "SpawnVolume.generated.h"

UCLASS()
class BATTERYCOLLECTOR_API ASpawnVolume : public AActor
{
GENERATED_BODY()

public:
// Sets default values for this actor's properties
ASpawnVolume();

protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;

public:
// Called every frame
virtual void Tick(float DeltaTime) override;

FORCEINLINE UBoxComponent* GetWhereToSpawn()const { return WhereToSpawn; }
UFUNCTION(BlueprintPure, Category = "Spawning")
FVector GetRandomPointInVolume();

UFUNCTION(BlueprintCallable, Category = "Spawning")
void SetSpawningPointInVolume(bool bShouldSpawn);

protected:
UPROPERTY(EditAnyWhere, Category = "Spawning")
TSubclassOf WhatToSpawn;
FTimerHandle SpawnTimer;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spawning")
float SpawnDelayRangeLow;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spawning")
float SpawnDelayRangeHigh;

private:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Spawning", meta = (AllowPrivateAccess = "true"))
class UBoxComponent* WhereToSpawn;

void SpawnPickup();

float SpawnDelay;

};

最终SpawnVolume.cpp
// Fill out your copyright notice in the Description page of Project Settings.

include "BatteryCollector.h"

include "SpawnVolume.h"

include "Kismet/KismetMathLibrary.h"

include "PickUp.h"

// Sets default values
ASpawnVolume::ASpawnVolume()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = false;
WhereToSpawn = CreateDefaultSubobject(TEXT("WhereToSpawn"));
RootComponent = WhereToSpawn;
SpawnDelayRangeLow = 1.0f;
SpawnDelayRangeHigh = 4.5f;
}

// Called when the game starts or when spawned
void ASpawnVolume::BeginPlay()
{
Super::BeginPlay();

}

// Called every frame
void ASpawnVolume::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);

}

FVector ASpawnVolume::GetRandomPointInVolume()
{
FVector SpawnOrigin = WhereToSpawn->Bounds.Origin;
FVector SpawnExtent = WhereToSpawn->Bounds.BoxExtent;
return UKismetMathLibrary::RandomPointInBoundingBox(SpawnOrigin, SpawnExtent);
}

void ASpawnVolume::SpawnPickup()
{
if (WhatToSpawn != NULL)
{
UWorld* const World = GetWorld();
if (World)
{
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.Instigator = Instigator;

        FVector SpawnLocation = GetRandomPointInVolume();
        FRotator SpawnRotation;
        SpawnRotation.Yaw = FMath::FRand()*360.0f;
        SpawnRotation.Pitch = FMath::FRand()*360.0f;
        SpawnRotation.Roll = FMath::FRand()*360.0f;

        APickUp* const SpawnedPickup = World->SpawnActor(WhatToSpawn, SpawnLocation, SpawnRotation, SpawnParams);
        SetSpawningPointInVolume(true);
    }
}

}

void ASpawnVolume::SetSpawningPointInVolume(bool bShouldSpawn)
{
if (bShouldSpawn)
{
SpawnDelay = FMath::FRandRange(SpawnDelayRangeLow, SpawnDelayRangeHigh);
GetWorldTimerManager().SetTimer(SpawnTimer, this, &ASpawnVolume::SpawnPickup, SpawnDelay, false);
}
else
{
GetWorldTimerManager().ClearTimer(SpawnTimer);
}
}

编辑BatteryCollectorGameMode 添加变量 保存场景中的SpawnVolume
.h
TArray SpawnVolumActors;
.cpp

19 - Toggling the Spawn Volumes_第2张图片
Paste_Image.png

你可能感兴趣的:(19 - Toggling the Spawn Volumes)