Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Copyright 2014-2015 Psylence Games. All Rights Reserved.
- #include "AdNoctvm.h"
- #include "AdNoctvmBTTask_Attack.h"
- #include "AdNoctvmAIController.h"
- #include "AdNoctvmPlayerCharacter.h"
- #include "AdNoctvmBaseEnemy.h"
- UAdNoctvmBTTask_Attack::UAdNoctvmBTTask_Attack(const FObjectInitializer& ObjectInitializer)
- : Super(ObjectInitializer)
- {
- NodeName = "Attack";
- AttackRadius = 50.f;
- TimerDelegate = FTimerDelegate::CreateUObject(this, &UAdNoctvmBTTask_Attack::OnAnimationTimerDone);
- }
- EBTNodeResult::Type UAdNoctvmBTTask_Attack::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
- {
- MyOwnerComp = &OwnerComp;
- AAdNoctvmAIController* MyAI = Cast<AAdNoctvmAIController>(OwnerComp.GetAIOwner());
- if (MyAI == NULL)
- {
- return EBTNodeResult::Failed;
- }
- UBlackboardComponent* MyBB = OwnerComp.GetBlackboardComponent();
- if (MyBB == NULL)
- {
- return EBTNodeResult::Failed;
- }
- TimerHandle.Invalidate();
- AAdNoctvmPlayerCharacter* PChar = Cast<AAdNoctvmPlayerCharacter>(MyAI->GetPlayerChar());
- if (PChar != NULL)
- {
- FVector PlayerLoc = PChar->GetActorLocation();
- FVector EnemyLoc = MyAI->GetPawn()->GetActorLocation();
- float Distance = FVector::Dist(PlayerLoc, EnemyLoc);
- EPathFollowingRequestResult::Type Result = EPathFollowingRequestResult::Failed;
- if (Distance > AttackRadius)
- {
- Result = MyAI->MoveToLocation(PlayerLoc, AttackRadius, true, true, false);
- if (Result == EPathFollowingRequestResult::Failed)
- {
- UE_LOG(AdNoctvmLog, Error, TEXT("MoveToActor failed"));
- return EBTNodeResult::Failed;
- }
- else if (Result == EPathFollowingRequestResult::RequestSuccessful)
- {
- const FAIRequestID RequestID = MyAI->GetCurrentMoveRequestID();
- WaitForMessage(OwnerComp, UBrainComponent::AIMessage_MoveFinished, RequestID);
- WaitForMessage(OwnerComp, UBrainComponent::AIMessage_RepathFailed);
- return EBTNodeResult::InProgress;
- }
- }
- else if (Distance <= AttackRadius)
- {
- if (MyAI->GetFocusActor() != PChar)
- {
- MyAI->SetFocus(PChar, EAIFocusPriority::Gameplay);
- }
- Enemy = Cast<AAdNoctvmBaseEnemy>(MyAI->GetPawn());
- if (Enemy != NULL)
- {
- if(Enemy->AttackAnim != NULL)
- {
- UAnimInstance* AnimInstance = Enemy->GetMesh()->GetAnimInstance();
- if(AnimInstance != NULL)
- {
- if (MyAI->GetMoveStatus() == EPathFollowingStatus::Moving)
- {
- MyAI->GetPathFollowingComponent()->AbortMove(TEXT("BehaviorTree abort"), MyAI->GetCurrentMoveRequestID());
- }
- Enemy->bIsAttacking = true;
- AnimInstance->Montage_Play(Enemy->AttackAnim, 1.f);
- const float FinishDelay = Enemy->AttackAnim->GetMaxCurrentTime();
- UE_LOG(AdNoctvmLog, Warning, TEXT("attacking"));
- if (FinishDelay > 0)
- {
- MyAI->GetWorld()->GetTimerManager().SetTimer(TimerHandle, TimerDelegate, FinishDelay, false);
- return EBTNodeResult::InProgress;
- }
- }
- }
- }
- }
- }
- return EBTNodeResult::Failed;
- }
- EBTNodeResult::Type UAdNoctvmBTTask_Attack::AbortTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
- {
- AAIController* MyController = NULL;
- if (&OwnerComp != NULL)
- {
- MyController = Cast<AAIController>(OwnerComp.GetAIOwner());
- }
- if (MyController && MyController->GetPathFollowingComponent())
- {
- MyController->GetPathFollowingComponent()->AbortMove(TEXT("BehaviorTree abort"), MyController->GetCurrentMoveRequestID());
- if (TimerHandle.IsValid())
- {
- MyController->GetWorld()->GetTimerManager().ClearTimer(TimerHandle);
- }
- }
- TimerHandle.Invalidate();
- return Super::AbortTask(OwnerComp, NodeMemory);
- }
- void UAdNoctvmBTTask_Attack::OnAnimationTimerDone()
- {
- if (MyOwnerComp && Enemy)
- {
- UE_LOG(AdNoctvmLog, Warning, TEXT("finishing attack task"));
- Enemy->bIsAttacking = false;
- FinishLatentTask(*MyOwnerComp, EBTNodeResult::Succeeded);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment