5
5
#include " DrawDebugHelpers.h"
6
6
#include " FPSGameMode.h"
7
7
#include " Net/UnrealNetwork.h"
8
+ #include " AI/Navigation/NavigationSystem.h"
8
9
9
10
10
11
// Sets default values
@@ -27,6 +28,11 @@ void AFPSAIGuard::BeginPlay()
27
28
Super::BeginPlay ();
28
29
29
30
OriginalRotation = GetActorRotation ();
31
+
32
+ if (bPatrol)
33
+ {
34
+ MoveToNextPatrolPoint ();
35
+ }
30
36
}
31
37
32
38
void AFPSAIGuard::OnPawnSeen (APawn* SeenPawn)
@@ -45,6 +51,13 @@ void AFPSAIGuard::OnPawnSeen(APawn* SeenPawn)
45
51
}
46
52
47
53
SetGuardState (EAIState::Alerted);
54
+
55
+ // Stop Movement if Patrolling
56
+ AController* Controller = GetController ();
57
+ if (Controller)
58
+ {
59
+ Controller->StopMovement ();
60
+ }
48
61
}
49
62
50
63
@@ -57,7 +70,6 @@ void AFPSAIGuard::OnNoiseHeard(APawn* NoiseInstigator, const FVector& Location,
57
70
58
71
DrawDebugSphere (GetWorld (), Location, 32 .0f , 12 , FColor::Green, false , 10 .0f );
59
72
60
-
61
73
FVector Direction = Location - GetActorLocation ();
62
74
Direction.Normalize ();
63
75
@@ -71,6 +83,13 @@ void AFPSAIGuard::OnNoiseHeard(APawn* NoiseInstigator, const FVector& Location,
71
83
GetWorldTimerManager ().SetTimer (TimerHandle_ResetOrientation, this , &AFPSAIGuard::ResetOrientation, 3 .0f );
72
84
73
85
SetGuardState (EAIState::Suspicious);
86
+
87
+ // Stop Movement if Patrolling
88
+ AController* Controller = GetController ();
89
+ if (Controller)
90
+ {
91
+ Controller->StopMovement ();
92
+ }
74
93
}
75
94
76
95
@@ -84,6 +103,12 @@ void AFPSAIGuard::ResetOrientation()
84
103
SetActorRotation (OriginalRotation);
85
104
86
105
SetGuardState (EAIState::Idle);
106
+
107
+ // Stopped investigating...if we are a patrolling pawn, pick a new patrol point to move to
108
+ if (bPatrol)
109
+ {
110
+ MoveToNextPatrolPoint ();
111
+ }
87
112
}
88
113
89
114
@@ -110,8 +135,36 @@ void AFPSAIGuard::Tick(float DeltaTime)
110
135
{
111
136
Super::Tick (DeltaTime);
112
137
138
+ // Patrol Goal Checks
139
+ if (CurrentPatrolPoint)
140
+ {
141
+ FVector Delta = GetActorLocation () - CurrentPatrolPoint->GetActorLocation ();
142
+ float DistanceToGoal = Delta.Size ();
143
+
144
+ // Check if we are within 50 units of our goal, if so - pick a new patrol point
145
+ if (DistanceToGoal < 50 )
146
+ {
147
+ MoveToNextPatrolPoint ();
148
+ }
149
+ }
150
+ }
151
+
152
+ void AFPSAIGuard::MoveToNextPatrolPoint ()
153
+ {
154
+ // Assign next patrol point.
155
+ if (CurrentPatrolPoint == nullptr || CurrentPatrolPoint == SecondPatrolPoint)
156
+ {
157
+ CurrentPatrolPoint = FirstPatrolPoint;
158
+ }
159
+ else
160
+ {
161
+ CurrentPatrolPoint = SecondPatrolPoint;
162
+ }
163
+
164
+ UNavigationSystem::SimpleMoveToActor (GetController (), CurrentPatrolPoint);
113
165
}
114
166
167
+
115
168
void AFPSAIGuard::GetLifetimeReplicatedProps (TArray< FLifetimeProperty > & OutLifetimeProps) const
116
169
{
117
170
Super::GetLifetimeReplicatedProps (OutLifetimeProps);
0 commit comments