AI Guard Patrol Challenge: C++/Blueprints

AI Guard Patrol Challenge: C++/Blueprints

AI Guard Patrol Challenge: C++/Blueprints

AI Guard Patrol Challenge: C++/Blueprints es el tercer reto propuesto por el curso Unreal Engine 4 Mastery: Create Multiplayer Games with C++ de Tom Looman certificado por Epic Games disponible en la plataforma Udemy.

A lo largo del curso se desarrollarán dos juegos con base en C++ y extendiendo la funcionalidad con Blueprints. Los dos juegos son multijugador y se profundizará también en AI y para reforzar los diferentes temas se proponen distintos retos.

En esta ocasión hay que prototipar en BP la patrulla del PNJ a lo largo del nivel para posteriormente implementarlo en C++. He planteado una función recursiva para patrullar entre puntos.

Cosas a destacar .h

class UPawnSensingComponent;

UENUM(BlueprintType)
enum class EAIState : uint8
{
    Idle,
    Suspicious,
    Alerted
};
UPROPERTY(VisibleAnywhere, Category = "Components")
UPawnSensingComponent* PawnSensingComp;

UFUNCTION()
void OnPawnSeen(APawn* SeenPawn);

UFUNCTION()
void OnNoiseHeard(APawn* NoiseInstigator, const FVector& Location, float Volumne);

FRotator OriginalRotaiton;

UFUNCTION()
void ResetOrientation();

FTimerHandle TimeHandle_ResetOrientation;

EAIState GuardState;
void SetGuardState(EAIState NewState);

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

// Guards patrol
UPROPERTY(EditDefaultsOnly, Category = "TargetClass")
TSubclassOf TargetClass;
TArray<AActor*> PatrolPoints;

// Recursive iteration
int ActualIteration;
FTimerHandle TimeHandle_DoIteration;
UFUNCTION()
void ExecuteIteration();

bool bGameEnded;

Cosas a destacar .cpp

void AAIGuard::BeginPlay()
{
    Super::BeginPlay();

    OriginalRotaiton = GetActorRotation();

    if (TargetClass)
    {
        UGameplayStatics::GetAllActorsOfClass(this, TargetClass, PatrolPoints);
        GetWorldTimerManager().SetTimer(TimeHandle_DoIteration, this, &AAIGuard::ExecuteIteration, 3.0f);
    }
    
}

void AAIGuard::OnPawnSeen(APawn * SeenPawn)
{
    if (SeenPawn == nullptr)
    {
        return;
    }

    DrawDebugSphere(GetWorld(), SeenPawn->GetActorLocation(), 32.0f, 12, FColor::Red, false, 10.0f);

    AFPSGameMode* GM = Cast(GetWorld()->GetAuthGameMode());
    if (GM)
    {
        GM->CompleteMission(SeenPawn, false);
    }

    SetGuardState(EAIState::Alerted);
    bGameEnded = true;
}

void AAIGuard::ExecuteIteration()
{
    if (bGameEnded)
    {
        UE_LOG(LogTemp, Warning, TEXT("Last Iteration"));
        return;
    }

    UE_LOG(LogTemp, Warning, TEXT("Iteration"));

    if (PatrolPoints.Num() > 0)
    {

        UAIBlueprintHelperLibrary::SimpleMoveToLocation(GetController(), PatrolPoints[ActualIteration]->GetActorLocation());

        if (PatrolPoints.Num() == (ActualIteration + 1))
        {
            ActualIteration = 0;

        }
        else
        {
            ActualIteration += 1;
        }

        GetWorldTimerManager().ClearTimer(TimeHandle_DoIteration);
        GetWorldTimerManager().SetTimer(TimeHandle_DoIteration, this, &AAIGuard::ExecuteIteration, 3.0f);
    }
}

A continuación os dejaré los enlaces al código, demo y al video en youtube para que podáis ver más. De momento me está gustando el curso pero al final compartiré una valoración general por si a alguien le pueda interesar que sepa más o menos que esperar del mismo.

Repositorio: AI Guard Patrol Challenge: C++/Blueprints [project]

Demo: AI Guard Patrol Challenge: C++/Blueprints [demo]

Curso: Unreal Engine 4 Mastery: Create Multiplayer Games with C++