Guest User

Untitled

a guest
Mar 16th, 2015
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.89 KB | None | 0 0
  1. // Copyright 2014-2015 Psylence Games. All Rights Reserved.
  2.  
  3. #include "AdNoctvm.h"
  4. #include "AdNoctvmBTTask_Attack.h"
  5. #include "AdNoctvmAIController.h"
  6. #include "AdNoctvmPlayerCharacter.h"
  7. #include "AdNoctvmBaseEnemy.h"
  8.  
  9. UAdNoctvmBTTask_Attack::UAdNoctvmBTTask_Attack(const FObjectInitializer& ObjectInitializer)
  10.     : Super(ObjectInitializer)
  11. {
  12.     NodeName = "Attack";
  13.    
  14.     AttackRadius = 50.f;
  15.    
  16.     TimerDelegate = FTimerDelegate::CreateUObject(this, &UAdNoctvmBTTask_Attack::OnAnimationTimerDone);
  17. }
  18.  
  19. EBTNodeResult::Type UAdNoctvmBTTask_Attack::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
  20. {
  21.     MyOwnerComp = &OwnerComp;
  22.     AAdNoctvmAIController* MyAI = Cast<AAdNoctvmAIController>(OwnerComp.GetAIOwner());
  23.  
  24.     if (MyAI == NULL)
  25.     {
  26.         return EBTNodeResult::Failed;
  27.     }
  28.  
  29.     UBlackboardComponent* MyBB = OwnerComp.GetBlackboardComponent();
  30.    
  31.     if (MyBB == NULL)
  32.     {
  33.         return EBTNodeResult::Failed;
  34.     }
  35.    
  36.     TimerHandle.Invalidate();
  37.    
  38.     AAdNoctvmPlayerCharacter* PChar = Cast<AAdNoctvmPlayerCharacter>(MyAI->GetPlayerChar());
  39.    
  40.     if (PChar != NULL)
  41.     {
  42.         FVector PlayerLoc = PChar->GetActorLocation();
  43.         FVector EnemyLoc = MyAI->GetPawn()->GetActorLocation();
  44.         float Distance = FVector::Dist(PlayerLoc, EnemyLoc);
  45.         EPathFollowingRequestResult::Type Result = EPathFollowingRequestResult::Failed;
  46.        
  47.         if (Distance > AttackRadius)
  48.         {
  49.             Result = MyAI->MoveToLocation(PlayerLoc, AttackRadius, true, true, false);
  50.  
  51.             if (Result == EPathFollowingRequestResult::Failed)
  52.             {
  53.                 UE_LOG(AdNoctvmLog, Error, TEXT("MoveToActor failed"));
  54.                 return EBTNodeResult::Failed;
  55.             }
  56.             else if (Result == EPathFollowingRequestResult::RequestSuccessful)
  57.             {
  58.                 const FAIRequestID RequestID = MyAI->GetCurrentMoveRequestID();
  59.  
  60.                 WaitForMessage(OwnerComp, UBrainComponent::AIMessage_MoveFinished, RequestID);
  61.                 WaitForMessage(OwnerComp, UBrainComponent::AIMessage_RepathFailed);
  62.  
  63.                 return EBTNodeResult::InProgress;
  64.             }
  65.         }
  66.         else if (Distance <= AttackRadius)
  67.         {
  68.             if (MyAI->GetFocusActor() != PChar)
  69.             {
  70.                 MyAI->SetFocus(PChar, EAIFocusPriority::Gameplay);
  71.             }
  72.            
  73.             Enemy = Cast<AAdNoctvmBaseEnemy>(MyAI->GetPawn());
  74.            
  75.             if (Enemy != NULL)
  76.             {
  77.                 if(Enemy->AttackAnim != NULL)
  78.                 {
  79.                     UAnimInstance* AnimInstance = Enemy->GetMesh()->GetAnimInstance();
  80.                     if(AnimInstance != NULL)
  81.                     {
  82.                         if (MyAI->GetMoveStatus() == EPathFollowingStatus::Moving)
  83.                         {
  84.                             MyAI->GetPathFollowingComponent()->AbortMove(TEXT("BehaviorTree abort"), MyAI->GetCurrentMoveRequestID());
  85.                         }
  86.                        
  87.                         Enemy->bIsAttacking = true;
  88.                         AnimInstance->Montage_Play(Enemy->AttackAnim, 1.f);
  89.                         const float FinishDelay = Enemy->AttackAnim->GetMaxCurrentTime();
  90.                         UE_LOG(AdNoctvmLog, Warning, TEXT("attacking"));
  91.                        
  92.                         if (FinishDelay > 0)
  93.                         {
  94.                             MyAI->GetWorld()->GetTimerManager().SetTimer(TimerHandle, TimerDelegate, FinishDelay, false);
  95.                             return EBTNodeResult::InProgress;
  96.                         }
  97.                     }
  98.                 }
  99.             }
  100.         }
  101.     }
  102.  
  103.     return EBTNodeResult::Failed;
  104. }
  105.  
  106. EBTNodeResult::Type UAdNoctvmBTTask_Attack::AbortTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
  107. {
  108.     AAIController* MyController = NULL;
  109.    
  110.     if (&OwnerComp != NULL)
  111.     {
  112.         MyController = Cast<AAIController>(OwnerComp.GetAIOwner());
  113.     }
  114.  
  115.     if (MyController && MyController->GetPathFollowingComponent())
  116.     {
  117.         MyController->GetPathFollowingComponent()->AbortMove(TEXT("BehaviorTree abort"), MyController->GetCurrentMoveRequestID());
  118.        
  119.         if (TimerHandle.IsValid())
  120.         {
  121.             MyController->GetWorld()->GetTimerManager().ClearTimer(TimerHandle);
  122.         }
  123.     }
  124.    
  125.     TimerHandle.Invalidate();
  126.  
  127.     return Super::AbortTask(OwnerComp, NodeMemory);
  128. }
  129.  
  130. void UAdNoctvmBTTask_Attack::OnAnimationTimerDone()
  131. {
  132.     if (MyOwnerComp && Enemy)
  133.     {
  134.         UE_LOG(AdNoctvmLog, Warning, TEXT("finishing attack task"));
  135.         Enemy->bIsAttacking = false;
  136.         FinishLatentTask(*MyOwnerComp, EBTNodeResult::Succeeded);
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment