-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathTile.cpp
More file actions
148 lines (127 loc) · 4.26 KB
/
Tile.cpp
File metadata and controls
148 lines (127 loc) · 4.26 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Fill out your copyright notice in the Description page of Project Settings.
#include "S05_TestingGrounds.h"
#include "Tile.h"
#include "DrawDebugHelpers.h"
#include "ActorPool.h"
// Sets default values
ATile::ATile()
{
// 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;
NavigationBoundsOffset = FVector(2000, 0, 0);
MinExtent = FVector(0, -2000, 0);
MaxExtent = FVector(4000, 2000, 0);
}
void ATile::SetPool(UActorPool* InPool)
{
UE_LOG(LogTemp, Warning, TEXT("[%s] Setting Pool %s"), *(this->GetName()), *(InPool->GetName()));
Pool = InPool;
PositionNavMeshBoundsVolume();
}
void ATile::PositionNavMeshBoundsVolume()
{
NavMeshBoundsVolume = Pool->Checkout();
if (NavMeshBoundsVolume == nullptr)
{
UE_LOG(LogTemp, Warning, TEXT("[%s] Not enough actors in pool."), *GetName());
return;
}
UE_LOG(LogTemp, Warning, TEXT("[%s] Checked out: {%s}"), *GetName(), *NavMeshBoundsVolume->GetName());
NavMeshBoundsVolume->SetActorLocation(GetActorLocation() + NavigationBoundsOffset);
FNavigationSystem::Build(*GetWorld());
}
void ATile::PlaceActors(TSubclassOf<AActor> ToSpawn, int MinSpawn, int MaxSpawn, float Radius, float MinScale, float MaxScale)
{
TArray<FSpawnPosition> SpawnPositions = RandomSpawnPositions(MinSpawn, MaxSpawn, Radius, MinScale, MaxScale);
for (FSpawnPosition SpawnPosition : SpawnPositions)
{
PlaceActor(ToSpawn, SpawnPosition);
}
}
void ATile::PlaceAIPawns(TSubclassOf<APawn> ToSpawn, int MinSpawn, int MaxSpawn, float Radius)
{
TArray<FSpawnPosition> SpawnPositions = RandomSpawnPositions(MinSpawn, MaxSpawn, Radius, 1, 1);
for (FSpawnPosition SpawnPosition : SpawnPositions)
{
PlaceAIPawn(ToSpawn, SpawnPosition);
}
}
TArray<FSpawnPosition> ATile::RandomSpawnPositions(int MinSpawn, int MaxSpawn, float Radius, float MinScale, float MaxScale)
{
TArray<FSpawnPosition> SpawnPositions;
int NumberToSpawn = FMath::RandRange(MinSpawn, MaxSpawn);
for (size_t i = 0; i < NumberToSpawn; i++)
{
FSpawnPosition SpawnPosition;
SpawnPosition.Scale = FMath::RandRange(MinScale, MaxScale);
bool found = FindEmptyLocation(SpawnPosition.Location, Radius * SpawnPosition.Scale);
if (found)
{
SpawnPosition.Rotation = FMath::RandRange(-180.f, 180.f);
SpawnPositions.Add(SpawnPosition);
}
}
return SpawnPositions;
}
bool ATile::FindEmptyLocation(FVector& OutLocation, float Radius) {
FBox Bounds(MinExtent, MaxExtent);
const int MAX_ATTEMPTS = 100;
for (size_t i = 0; i < MAX_ATTEMPTS; i++)
{
FVector CandidatePoint = FMath::RandPointInBox(Bounds);
if (CanSpawnAtLocation(CandidatePoint, Radius)) {
OutLocation = CandidatePoint;
return true;
}
}
return false;
}
void ATile::PlaceActor(TSubclassOf<AActor> ToSpawn, FSpawnPosition SpawnPosition) {
AActor* Spawned = GetWorld()->SpawnActor<AActor>(ToSpawn);
Spawned->SetActorRelativeLocation(SpawnPosition.Location);
Spawned->AttachToActor(this, FAttachmentTransformRules(EAttachmentRule::KeepRelative, false));
Spawned->SetActorRotation(FRotator(0, SpawnPosition.Rotation, 0));
Spawned->SetActorScale3D(FVector(SpawnPosition.Scale));
}
void ATile::PlaceAIPawn(TSubclassOf<APawn> ToSpawn, FSpawnPosition SpawnPosition)
{
FRotator Rotation = FRotator(0, SpawnPosition.Rotation, 0);
APawn* Spawned = GetWorld()->SpawnActor<APawn>(ToSpawn, SpawnPosition.Location, Rotation);
if (Spawned == nullptr)
{
return;
}
Spawned->AttachToActor(this, FAttachmentTransformRules(EAttachmentRule::KeepRelative, false));
Spawned->SpawnDefaultController();
Spawned->Tags.Add(FName("Enemy"));
}
// Called when the game starts or when spawned
void ATile::BeginPlay()
{
Super::BeginPlay();
}
void ATile::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
if (Pool != nullptr && NavMeshBoundsVolume != nullptr) {
Pool->Return(NavMeshBoundsVolume);
}
}
// Called every frame
void ATile::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
bool ATile::CanSpawnAtLocation(FVector Location, float Radius)
{
FHitResult HitResult;
FVector GlobalLocation = ActorToWorld().TransformPosition(Location);
bool HasHit = GetWorld()->SweepSingleByChannel(
HitResult,
GlobalLocation,
GlobalLocation,
FQuat::Identity,
ECollisionChannel::ECC_GameTraceChannel2,
FCollisionShape::MakeSphere(Radius)
);
return !HasHit;
}