07 - Defining What to Spawn

修改SpawnVolume 添加SpawnPickup 函数 用来生成PickUp Actor在场景中

最终 SpawnVolume.h

#pragma once

#include "GameFramework/Actor.h"

#include "SpawnVolume.generated.h"

UCLASS()

class BATTERYCOLLECTOR_API ASpawnVolume : publicAActor

{

GENERATED_BODY()

public:

// Setsdefault values for this actor's properties

ASpawnVolume();

protected:

// Calledwhen the game starts or when spawned

virtual voidBeginPlay() override;

public:

// Calledevery frame

virtual voidTick(float DeltaTime) override;

FORCEINLINEUBoxComponent* GetWhereToSpawn()const { return WhereToSpawn; }

UFUNCTION(BlueprintPure,Category = "Spawning")

FVectorGetRandomPointInVolume();

protected:

UPROPERTY(EditAnyWhere,Category = "Spawning")

TSubclassOf WhatToSpawn; //TShubclassOf UClass* 的模板类

private:

UPROPERTY(VisibleAnywhere,BlueprintReadOnly, Category = "Spawning", meta = (AllowPrivateAccess= "true"))

classUBoxComponent* WhereToSpawn;

voidSpawnPickup();

};

最终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 tocall Tick() every frame.You can turnthis off to improve performance if you don't need it.

PrimaryActorTick.bCanEverTick=false;

WhereToSpawn= CreateDefaultSubobject(TEXT("WhereToSpawn"));

RootComponent= WhereToSpawn;

}

// Called when the game starts or when

spawned

voidASpawnVolume::BeginPlay()

{

Super::BeginPlay();

}

// Called every frame

voidASpawnVolume::Tick(floatDeltaTime)

{

Super::Tick(DeltaTime);

}

FVectorASpawnVolume::GetRandomPointInVolume()

{

FVectorSpawnOrigin =WhereToSpawn->Bounds.Origin;

FVectorSpawnExtent =WhereToSpawn->Bounds.BoxExtent;

returnUKismetMathLibrary::RandomPointInBoundingBox(SpawnOrigin, SpawnExtent);

}

voidASpawnVolume::SpawnPickup()

{

if(WhatToSpawn !=NULL)

{

UWorld*constWorld = GetWorld();

if(World)

{

FActorSpawnParametersSpawnParams;

SpawnParams.Owner=this;

SpawnParams.Instigator= Instigator;

FVectorSpawnLocation =GetRandomPointInVolume();

FRotatorSpawnRotation;

SpawnRotation.Yaw=FMath::FRand()*360.0f;

SpawnRotation.Pitch=FMath::FRand()*360.0f;

SpawnRotation.Roll=FMath::FRand()*360.0f;

APickUp*constSpawnedPickup =

World->SpawnActor(WhatToSpawn, SpawnLocation, SpawnRotation,SpawnParams);

}

}

}

你可能感兴趣的:(07 - Defining What to Spawn)