Skip to content

Commit

Permalink
Add 3D UI state above guard
Browse files Browse the repository at this point in the history
  • Loading branch information
Relrin committed Jan 4, 2018
1 parent a9aa41d commit 831cfe7
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 4 deletions.
Binary file modified Content/Blueprints/BP_Guard.uasset
Binary file not shown.
Binary file modified Content/Maps/FirstPersonExampleMap.umap
Binary file not shown.
Binary file modified Content/Maps/FirstPersonExampleMap_BuiltData.uasset
Binary file not shown.
Binary file added Content/UI/WBP_GuardState.uasset
Binary file not shown.
32 changes: 31 additions & 1 deletion Source/FPSGame/Private/FPSAIGuard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#include "FPSGameMode.h"

// Sets default values
AFPSAIGuard::AFPSAIGuard()
AFPSAIGuard::AFPSAIGuard():
CurrentState(EAIState::Idle)
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
Expand Down Expand Up @@ -32,10 +33,17 @@ void AFPSAIGuard::OnPawnSeen(APawn* Pawn)
{
GameMode->CompleteMission(Pawn, false);
}

SetCurrentState(EAIState::Alerted);
}

void AFPSAIGuard::OnPawnHeard(APawn* NoiseInstigator, const FVector& Location, float Volume)
{
if (CurrentState == EAIState::Alerted)
{
return;
}

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

Expand All @@ -46,11 +54,33 @@ void AFPSAIGuard::OnPawnHeard(APawn* NoiseInstigator, const FVector& Location, f

GetWorldTimerManager().ClearTimer(TimeHandle_ResetOrientation);
GetWorldTimerManager().SetTimer(TimeHandle_ResetOrientation, this, &AFPSAIGuard::ResetOrientation, 3.0f);

if (CurrentState != EAIState::Alerted)
{
SetCurrentState(EAIState::Suspicious);
}
}

void AFPSAIGuard::ResetOrientation()
{
if (CurrentState == EAIState::Alerted)
{
return;
}

SetActorRotation(OriginalRotation);
SetCurrentState(EAIState::Idle);
}

void AFPSAIGuard::SetCurrentState(EAIState NewState)
{
if (CurrentState == NewState)
{
return;
}

CurrentState = NewState;
OnStateChanged(NewState);
}

// Called every frame
Expand Down
20 changes: 17 additions & 3 deletions Source/FPSGame/Public/FPSAIGuard.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@

class UPawnSensingComponent;

UENUM(BlueprintType)
enum class EAIState: uint8
{
Idle,
Suspicious,
Alerted
};


UCLASS()
class FPSGAME_API AFPSAIGuard : public ACharacter
{
Expand All @@ -28,12 +37,17 @@ class FPSGAME_API AFPSAIGuard : public ACharacter
UFUNCTION()
void OnPawnHeard(APawn* NoiseInstigator, const FVector& Location, float Volume);

FRotator OriginalRotation;
FTimerHandle TimeHandle_ResetOrientation;

UFUNCTION()
void ResetOrientation();

UFUNCTION(BlueprintImplementableEvent, Category = "AI")
void OnStateChanged(EAIState NewState);

void SetCurrentState(EAIState NewState);

FRotator OriginalRotation;
FTimerHandle TimeHandle_ResetOrientation;
EAIState CurrentState;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
Expand Down

0 comments on commit 831cfe7

Please sign in to comment.