Skip to content

Instantly share code, notes, and snippets.

Created April 20, 2017 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/047746bba00c331966d06041b86d08c6 to your computer and use it in GitHub Desktop.
Save anonymous/047746bba00c331966d06041b86d08c6 to your computer and use it in GitHub Desktop.
// Fill out your copyright notice in the Description page of Project Settings.
#include "TD.h"
#include "EnemyAI.h"
#include "Waypoint.h"
#include "EnemyAIController.h"
// Sets default values
AEnemyAI::AEnemyAI()
{
// 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;
}
// Called when the game starts or when spawned
void AEnemyAI::BeginPlay()
{
Super::BeginPlay();
AIController = Cast<AEnemyAIController>(GetController());
UGameplayStatics::GetAllActorsOfClass(GetWorld(), AWaypoint::StaticClass(), Waypoints);
MoveToWaypoints();
}
// Called every frame
void AEnemyAI::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
CurrentVelocity = GetCharacterMovement()->Velocity;
}
// Called to bind functionality to input
void AEnemyAI::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
}
void AEnemyAI::MoveToWaypoints()
{
if (CurrentWaypoint > 1)
{
GetCharacterMovement()->Velocity = CurrentVelocity;
}
if (AIController)
{
if (CurrentWaypoint <= Waypoints.Num())
{
for (AActor* Waypoint : Waypoints)
{
AWaypoint* WaypointItr = Cast<AWaypoint>(Waypoint);
if (Waypoint)
{
if (WaypointItr->GetWaypointNumber() == CurrentWaypoint)
{
AIController->MoveToActor(Waypoint, 5.f, false);
break;
}
}
}
}
}
CurrentWaypoint++;
}
#include "TD.h"
#include "EnemyAI.h"
#include "EnemyAIController.h"
void AEnemyAIController::OnMoveCompleted(FAIRequestID RequestID, const FPathFollowingResult& Result)
{
AEnemyAI* EnemyAI = Cast<AEnemyAI>(GetControlledPawn());
if (EnemyAI)
{
EnemyAI->MoveToWaypoints();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment