Skip to content

Commit 6918732

Browse files
author
Tom Looman
committed
Added AI Patrol Challenge code and level
1 parent 3009a3c commit 6918732

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

Diff for: Content/Challenges/AIPatrol/AIPatrolMap_P.umap

9.23 MB
Binary file not shown.

Diff for: Source/FPSGame/Private/FPSAIGuard.cpp

+54-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "DrawDebugHelpers.h"
66
#include "FPSGameMode.h"
77
#include "Net/UnrealNetwork.h"
8+
#include "AI/Navigation/NavigationSystem.h"
89

910

1011
// Sets default values
@@ -27,6 +28,11 @@ void AFPSAIGuard::BeginPlay()
2728
Super::BeginPlay();
2829

2930
OriginalRotation = GetActorRotation();
31+
32+
if (bPatrol)
33+
{
34+
MoveToNextPatrolPoint();
35+
}
3036
}
3137

3238
void AFPSAIGuard::OnPawnSeen(APawn* SeenPawn)
@@ -45,6 +51,13 @@ void AFPSAIGuard::OnPawnSeen(APawn* SeenPawn)
4551
}
4652

4753
SetGuardState(EAIState::Alerted);
54+
55+
// Stop Movement if Patrolling
56+
AController* Controller = GetController();
57+
if (Controller)
58+
{
59+
Controller->StopMovement();
60+
}
4861
}
4962

5063

@@ -57,7 +70,6 @@ void AFPSAIGuard::OnNoiseHeard(APawn* NoiseInstigator, const FVector& Location,
5770

5871
DrawDebugSphere(GetWorld(), Location, 32.0f, 12, FColor::Green, false, 10.0f);
5972

60-
6173
FVector Direction = Location - GetActorLocation();
6274
Direction.Normalize();
6375

@@ -71,6 +83,13 @@ void AFPSAIGuard::OnNoiseHeard(APawn* NoiseInstigator, const FVector& Location,
7183
GetWorldTimerManager().SetTimer(TimerHandle_ResetOrientation, this, &AFPSAIGuard::ResetOrientation, 3.0f);
7284

7385
SetGuardState(EAIState::Suspicious);
86+
87+
// Stop Movement if Patrolling
88+
AController* Controller = GetController();
89+
if (Controller)
90+
{
91+
Controller->StopMovement();
92+
}
7493
}
7594

7695

@@ -84,6 +103,12 @@ void AFPSAIGuard::ResetOrientation()
84103
SetActorRotation(OriginalRotation);
85104

86105
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+
}
87112
}
88113

89114

@@ -110,8 +135,36 @@ void AFPSAIGuard::Tick(float DeltaTime)
110135
{
111136
Super::Tick(DeltaTime);
112137

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);
113165
}
114166

167+
115168
void AFPSAIGuard::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
116169
{
117170
Super::GetLifetimeReplicatedProps(OutLifetimeProps);

Diff for: Source/FPSGame/Public/FPSAIGuard.h

+23
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,28 @@ class FPSGAME_API AFPSAIGuard : public ACharacter
6363
public:
6464
// Called every frame
6565
virtual void Tick(float DeltaTime) override;
66+
67+
68+
protected:
69+
70+
// CHALLENGE CODE
71+
72+
/* Let the guard go on patrol */
73+
UPROPERTY(EditInstanceOnly, Category = "AI")
74+
bool bPatrol;
75+
76+
/* First of two patrol points to patrol between */
77+
UPROPERTY(EditInstanceOnly, Category = "AI", meta = (EditCondition="bPatrol"))
78+
AActor* FirstPatrolPoint;
79+
80+
/* Second of two patrol points to patrol between */
81+
UPROPERTY(EditInstanceOnly, Category = "AI", meta = (EditCondition = "bPatrol"))
82+
AActor* SecondPatrolPoint;
83+
84+
// The current point the actor is either moving to or standing at
85+
AActor* CurrentPatrolPoint;
86+
87+
void MoveToNextPatrolPoint();
88+
6689

6790
};

0 commit comments

Comments
 (0)