-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNodeAI.cpp
94 lines (79 loc) · 2.14 KB
/
NodeAI.cpp
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
// Fill out your copyright notice in the Description page of Project Settings.
#include "NodeAI.h"
#include "Components/BillboardComponent.h"
#include "Kismet/KismetMathLibrary.h"
#if WITH_EDITOR
#include "DrawDebugHelpers.h"
#endif
// Sets default values
ANodeAI::ANodeAI()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
#if WITH_EDITOR
PrimaryActorTick.bCanEverTick = true;
PrimaryActorTick.bStartWithTickEnabled = true;
#else
PrimaryActorTick.bCanEverTick = false;
PrimaryActorTick.bStartWithTickEnabled = false;
#endif
//Disable damage
bCanBeDamaged = false;
//Initialise
SetRootComponent(CreateDefaultSubobject<UBillboardComponent>("Root"));
}
bool ANodeAI::ShouldTickIfViewportsOnly() const
{
#if WITH_EDITOR
//Draw arrows indicating next nodes
if (NextNodes.Num() > 0)
{
UWorld* const World = GetWorld();
if (World)
{
const FVector ActorPos = GetActorLocation();
for (AActor* const Actor : NextNodes)
{
if (Actor)
{
//DrawDebugLine(World, ActorPos, Actor->GetActorLocation(), FColor::Blue, false, -1.f, '\000', 4.f);
DrawDebugDirectionalArrow(World, ActorPos, Actor->GetActorLocation(), 1000.f, FColor::Blue, false, -1.f, '\000', 4.f);
}
}
}
}
return true;
#else
return false;
#endif
}
ANodeAI* ANodeAI::GetNextNode() const
{
if (NextNodes.Num() > 0)
{
//Get random node in array
const int32 Index = UKismetMathLibrary::RandomIntegerInRange(0, NextNodes.Num() - 1);
//Return it
if (NextNodes[Index])
{
return NextNodes[Index];
}
}
//Something went wrong
return nullptr;
}
// Called when the game starts or when spawned
void ANodeAI::BeginPlay()
{
Super::BeginPlay();
//Drops down to the floor at beginning
UWorld* const World = GetWorld();
if (World)
{
FHitResult HitResult{};
const bool Result = World->LineTraceSingleByChannel(HitResult, GetActorLocation(), GetActorLocation() + -FVector::UpVector * 9999.f, ECollisionChannel::ECC_Visibility);
if (Result)
{
SetActorLocation(HitResult.Location);
}
}
}