-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpawnVolume.cpp
More file actions
105 lines (69 loc) · 2.53 KB
/
SpawnVolume.cpp
File metadata and controls
105 lines (69 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// 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;
// Create the box component to represent spawn volume
whereToSpawn = CreateDefaultSubobject<UBoxComponent>(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);
}
float ASpawnVolume::GetNewSpawnDelay(){
return FMath::FRandRange(spawnDelayRangeLow, spawnDelayRangeHigh);
}
void ASpawnVolume::SetActive(bool newSpawnState){
spawnActive = newSpawnState;
if(spawnActive){
spawnDelay = GetNewSpawnDelay();
GetWorldTimerManager().SetTimer(spawnTimer, this, &ASpawnVolume::SpawnPickup, spawnDelay, false);
} else {
GetWorldTimerManager().ClearTimer(spawnTimer);
}
}
bool ASpawnVolume::IsActive(){
return spawnActive;
}
void ASpawnVolume::SpawnPickup(){
// Check if we have set something to spawn
if(whatToSpawn != NULL){
// Check for a valid world
UWorld* const world = GetWorld();
if(world){
// Set the spawn parameters
FActorSpawnParameters spawnParams;
spawnParams.Owner = this;
spawnParams.Instigator = Instigator;
// Give some values for where and how the pickup will spawn
FVector spawnLocation = GetRandomPointInVolume();
FRotator spawnRotation;
spawnRotation.Yaw = FMath::FRand() * 360.0f;
spawnRotation.Pitch = FMath::FRand() * 360.0f;
spawnRotation.Roll = FMath::FRand() * 360.0f;
// Spawn the pickup
APickup* const spawnedPickup = world->SpawnActor<APickup>(whatToSpawn, spawnLocation, spawnRotation, spawnParams);
// Reset the timer
spawnDelay = GetNewSpawnDelay();
GetWorldTimerManager().SetTimer(spawnTimer, this, &ASpawnVolume::SpawnPickup, spawnDelay, false);
}
}
}