-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemySpawner.cpp
More file actions
66 lines (55 loc) · 1.74 KB
/
Copy pathEnemySpawner.cpp
File metadata and controls
66 lines (55 loc) · 1.74 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
// Fill out your copyright notice in the Description page of Project Settings.
#include "EnemySpawner.h"
#include "Components/BoxComponent.h"
#include "Kismet/KismetMathLibrary.h"
#include "Kismet/GameplayStatics.h"
#include "SpaceShip.h"
#include "Engine/World.h"
#include "Enemy.h"
#include "TimerManager.h"
// Sets default values
AEnemySpawner::AEnemySpawner()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
SpawnArea = CreateDefaultSubobject<UBoxComponent>(TEXT("SpawnArea"));
RootComponent = SpawnArea;
SpawnInterval = 2.0f;
MaxEnemyNum = 30;
CurrentEnemyCount = 0;
}
// Called when the game starts or when spawned
void AEnemySpawner::BeginPlay()
{
Super::BeginPlay();
SpaceShip = Cast<ASpaceShip>(UGameplayStatics::GetPlayerPawn(this, 0));
GetWorldTimerManager().SetTimer(TimerHandle_Spawn, this, &AEnemySpawner::SpawnEnemy, SpawnInterval, true, 0);
}
FVector AEnemySpawner::GetGenerateLocation()
{
float Distance = 0;
FVector Location;
while (Distance < MiniDistanceToPlayer) {
Location = UKismetMathLibrary::RandomPointInBoundingBox(SpawnArea->Bounds.Origin, SpawnArea->Bounds.BoxExtent);
Distance = (Location - SpaceShip->GetActorLocation()).Size();
}
return Location;
}
void AEnemySpawner::SpawnEnemy()
{
if (!SpaceShip->GetBDead() && CurrentEnemyCount < MaxEnemyNum) {
FActorSpawnParameters SpawnParameters;
GetWorld()->SpawnActor<AEnemy>(Enemy, GetGenerateLocation(), FRotator::ZeroRotator, SpawnParameters);
++CurrentEnemyCount;
}
}
// Called every frame
void AEnemySpawner::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AEnemySpawner::DecreaseEnemyCount()
{
if (CurrentEnemyCount > 0)
CurrentEnemyCount--;
}