Skip to content

Commit

Permalink
Added AI Patrol Challenge code and level
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Looman committed Nov 28, 2017
1 parent 3009a3c commit 6918732
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
Binary file added Content/Challenges/AIPatrol/AIPatrolMap_P.umap
Binary file not shown.
55 changes: 54 additions & 1 deletion Source/FPSGame/Private/FPSAIGuard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "DrawDebugHelpers.h"
#include "FPSGameMode.h"
#include "Net/UnrealNetwork.h"
#include "AI/Navigation/NavigationSystem.h"


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

OriginalRotation = GetActorRotation();

if (bPatrol)
{
MoveToNextPatrolPoint();
}
}

void AFPSAIGuard::OnPawnSeen(APawn* SeenPawn)
Expand All @@ -45,6 +51,13 @@ void AFPSAIGuard::OnPawnSeen(APawn* SeenPawn)
}

SetGuardState(EAIState::Alerted);

// Stop Movement if Patrolling
AController* Controller = GetController();
if (Controller)
{
Controller->StopMovement();
}
}


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

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


FVector Direction = Location - GetActorLocation();
Direction.Normalize();

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

SetGuardState(EAIState::Suspicious);

// Stop Movement if Patrolling
AController* Controller = GetController();
if (Controller)
{
Controller->StopMovement();
}
}


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

SetGuardState(EAIState::Idle);

// Stopped investigating...if we are a patrolling pawn, pick a new patrol point to move to
if (bPatrol)
{
MoveToNextPatrolPoint();
}
}


Expand All @@ -110,8 +135,36 @@ void AFPSAIGuard::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);

// Patrol Goal Checks
if (CurrentPatrolPoint)
{
FVector Delta = GetActorLocation() - CurrentPatrolPoint->GetActorLocation();
float DistanceToGoal = Delta.Size();

// Check if we are within 50 units of our goal, if so - pick a new patrol point
if (DistanceToGoal < 50)
{
MoveToNextPatrolPoint();
}
}
}

void AFPSAIGuard::MoveToNextPatrolPoint()
{
// Assign next patrol point.
if (CurrentPatrolPoint == nullptr || CurrentPatrolPoint == SecondPatrolPoint)
{
CurrentPatrolPoint = FirstPatrolPoint;
}
else
{
CurrentPatrolPoint = SecondPatrolPoint;
}

UNavigationSystem::SimpleMoveToActor(GetController(), CurrentPatrolPoint);
}


void AFPSAIGuard::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
Expand Down
23 changes: 23 additions & 0 deletions Source/FPSGame/Public/FPSAIGuard.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,28 @@ class FPSGAME_API AFPSAIGuard : public ACharacter
public:
// Called every frame
virtual void Tick(float DeltaTime) override;


protected:

// CHALLENGE CODE

/* Let the guard go on patrol */
UPROPERTY(EditInstanceOnly, Category = "AI")
bool bPatrol;

/* First of two patrol points to patrol between */
UPROPERTY(EditInstanceOnly, Category = "AI", meta = (EditCondition="bPatrol"))
AActor* FirstPatrolPoint;

/* Second of two patrol points to patrol between */
UPROPERTY(EditInstanceOnly, Category = "AI", meta = (EditCondition = "bPatrol"))
AActor* SecondPatrolPoint;

// The current point the actor is either moving to or standing at
AActor* CurrentPatrolPoint;

void MoveToNextPatrolPoint();


};

0 comments on commit 6918732

Please sign in to comment.