// Fill out your copyright notice in the Description page of Project Settings.


#include "BTTask_RaiseWeapon.h"
#include "EndlessVendetta/AI/AI_EnemyController.h"
#include "EndlessVendetta/AI/EnemyCharacter.h"

UBTTask_RaiseWeapon::UBTTask_RaiseWeapon()
{
	NodeName = "Raise Weapon";
}

EBTNodeResult::Type UBTTask_RaiseWeapon::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
	if (const AAI_EnemyController* const AIEnemyController = Cast<AAI_EnemyController>(OwnerComp.GetAIOwner()))
	{
		if (AEnemyCharacter* const EnemyCharacter = Cast<AEnemyCharacter>(AIEnemyController->GetPawn()))
		{
			if (EnemyCharacter->WeaponRaised)
			{
				FinishLatentTask(OwnerComp, EBTNodeResult::Succeeded);
				return EBTNodeResult::Succeeded;
			}

			EnemyCharacter->EquipWeapon();
			EnemyCharacter->WeaponRaised = true;

			FinishLatentTask(OwnerComp, EBTNodeResult::Succeeded);
			return EBTNodeResult::Succeeded;
		}
	}

	return EBTNodeResult::Failed;
}
