// Copyright Zelle Games #include "BTTask_Attack.h" #include "AIController.h" #include "PlayMontageCallbackProxy.h" #include "Unit.h" #include "BehaviorTree/BlackboardComponent.h" EBTNodeResult::Type UBTTask_Attack::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) { OwnerComponent = &OwnerComp; const AUnit* Target = Cast(OwnerComp.GetBlackboardComponent()->GetValueAsObject(BlackboardKey.SelectedKeyName)); AUnit* ThisUnit = Cast(OwnerComp.GetAIOwner()->GetPawn()); if(!Target || !ThisUnit || Target->IsDead()) { return EBTNodeResult::Failed; } if(FVector::Distance(ThisUnit->GetActorLocation(), Target->GetActorLocation()) >= ThisUnit->Range + 500) { return EBTNodeResult::Failed; } if(ThisUnit->CooldownTimer < ThisUnit->Cooldown) { return EBTNodeResult::Failed; } MontageCallbackProxy = UPlayMontageCallbackProxy::CreateProxyObjectForPlayMontage( ThisUnit->GetMesh(), ThisUnit->MeleeMontage[FMath::RandRange(0, ThisUnit->MeleeMontage.Num() -1)]); if(ThisUnit->AttachedWeaponMesh && ThisUnit->AttachedWeaponMontage) { UPlayMontageCallbackProxy::CreateProxyObjectForPlayMontage( ThisUnit->AttachedWeaponMesh, ThisUnit->AttachedWeaponMontage); } ThisUnit->CooldownTimer = 0; return EBTNodeResult::Succeeded; }