[박치영]

- AI Patrol System 구현
main
PCYPC\pcy35 2023-08-28 21:58:13 +09:00
parent 9ef28a708f
commit c42582a55c
19 changed files with 197 additions and 1 deletions

View File

@ -134,4 +134,6 @@ ManualIPAddress=
+PropertyRedirects=(OldName="/Script/D1.CombatCharacter.ToogleLockOnAction",NewName="/Script/D1.CombatCharacter.ToggleLockOnAction")
+ClassRedirects=(OldName="/Script/D1.UI_LockOnComponent",NewName="/Script/D1.LockOnWidgetComponent")
+PropertyRedirects=(OldName="/Script/D1.MasterAI.TargetingWidget",NewName="/Script/D1.MasterAI.TargetingWidgetComponent")
+PropertyRedirects=(OldName="/Script/D1.HumanoidEnemy.HealthBar",NewName="/Script/D1.HumanoidEnemy.HealthBarComponent")
+PropertyRedirects=(OldName="/Script/D1.HumanoidEnemy.HealthBar",NewName="/Script/D1.HumanoidEnemy.HealthBarComponent")
+PropertyRedirects=(OldName="/Script/D1.T_FindNextPatrolPoint.TargetLocation",NewName="/Script/D1.T_FindNextPatrolPoint.BlackboardKey_TargetLocation")
+PropertyRedirects=(OldName="/Script/D1.T_FindNextPatrolPoint.PatrolIndex",NewName="/Script/D1.T_FindNextPatrolPoint.BlackboardKey_PatrolIndex")

View File

@ -9,6 +9,7 @@
#include "Components/StatsComponent.h"
#include "Components/TargetingComponent.h"
#include "Definitions/GameEnums.h"
#include "Engine/TargetPoint.h"
#include "Interface/TargetingInterface.h"
#include "MasterAI.generated.h"
@ -99,6 +100,7 @@ protected: //Check Func
public: //Getter
FORCEINLINE class UBehaviorTree* GetBeHaviorTree() {return BehaviorTree;}
FORCEINLINE const TArray<ATargetPoint*>& GetPatrolPoints() { return PatrolPoints; }
private:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Initialization", meta=(AllowPrivateAccess="true"))
@ -137,6 +139,9 @@ private:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Montage|Actions", meta = (AllowPrivateAccess = "true"))
TObjectPtr<UAnimMontage> ExitCombat;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Patrol", meta = (AllowPrivateAccess = "true"))
TArray<TObjectPtr<ATargetPoint>> PatrolPoints;
private:
FTimerHandle StaminaTimerHandle;

View File

@ -0,0 +1,96 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "AI/T_FindNextPatrolPoint.h"
#include "AIController.h"
#include "MasterAI.h"
#include "NavigationSystem.h"
#include "BehaviorTree/BlackboardComponent.h"
UT_FindNextPatrolPoint::UT_FindNextPatrolPoint()
{
bShouldReverse = false;
bShouldLoop = true;
}
EBTNodeResult::Type UT_FindNextPatrolPoint::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
EBTNodeResult::Type result = Super::ExecuteTask(OwnerComp, NodeMemory);
if(result == EBTNodeResult::Failed)
return EBTNodeResult::Failed;
APawn* ControllingPawn = OwnerComp.GetAIOwner()->GetPawn();
if(ControllingPawn == nullptr)
return EBTNodeResult::Failed;
AMasterAI* masterAI = Cast<AMasterAI>(ControllingPawn);
if(masterAI == nullptr)
return EBTNodeResult::Failed;
ControlledMasterAI = masterAI;
BlackboardComponent = OwnerComp.GetBlackboardComponent();
if(SetPathLocation())
{
if(bShouldReverse)
DecrementPathIndex();
else
IncrementPathIndex();
}
else
return EBTNodeResult::Failed;
return EBTNodeResult::Succeeded;
}
bool UT_FindNextPatrolPoint::SetPathLocation()
{
if(!IsValid(ControlledMasterAI))
return false;
int PatrolIndex = BlackboardComponent->GetValueAsInt(BlackboardKey_PatrolIndex.SelectedKeyName);
if(!ControlledMasterAI->GetPatrolPoints().IsValidIndex(PatrolIndex))
return false;
UNavigationSystemV1* navSystem = UNavigationSystemV1::GetNavigationSystem(GetWorld());
if(navSystem == nullptr)
return false;
FVector targetLocation = ControlledMasterAI->GetPatrolPoints()[PatrolIndex]->GetActorLocation();
FNavLocation outLocation;
navSystem->ProjectPointToNavigation(targetLocation, outLocation);
BlackboardComponent->SetValueAsVector(BlackboardKey_TargetLocation.SelectedKeyName, outLocation.Location);
return true;
}
void UT_FindNextPatrolPoint::IncrementPathIndex()
{
if(!IsValid(ControlledMasterAI))
return;
int PatrolIndex = BlackboardComponent->GetValueAsInt(BlackboardKey_PatrolIndex.SelectedKeyName) + 1;
if(ControlledMasterAI->GetPatrolPoints().Num() > PatrolIndex)
BlackboardComponent->SetValueAsInt(BlackboardKey_PatrolIndex.SelectedKeyName, PatrolIndex);
else
{
if(bShouldLoop)
{
bShouldReverse = true;
DecrementPathIndex();
}
}
}
void UT_FindNextPatrolPoint::DecrementPathIndex()
{
if(!IsValid(ControlledMasterAI))
return;
int PatrolIndex = BlackboardComponent->GetValueAsInt(BlackboardKey_PatrolIndex.SelectedKeyName) - 1;
if(PatrolIndex >= 0)
BlackboardComponent->SetValueAsInt(BlackboardKey_PatrolIndex.SelectedKeyName, PatrolIndex);
else
{
bShouldReverse = false;
IncrementPathIndex();
}
}

View File

@ -0,0 +1,37 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "BehaviorTree/BTTaskNode.h"
#include "T_FindNextPatrolPoint.generated.h"
/**
*
*/
UCLASS()
class D1_API UT_FindNextPatrolPoint : public UBTTaskNode
{
GENERATED_BODY()
public:
UT_FindNextPatrolPoint();
virtual EBTNodeResult::Type ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) override;
public:
bool SetPathLocation();
void IncrementPathIndex();
void DecrementPathIndex();
private:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="BlackBoard", meta=(AllowPrivateAccess="true"))
FBlackboardKeySelector BlackboardKey_TargetLocation;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="BlackBoard", meta=(AllowPrivateAccess="true"))
FBlackboardKeySelector BlackboardKey_PatrolIndex;
private:
TObjectPtr<class AMasterAI> ControlledMasterAI;
TObjectPtr<class UBlackboardComponent> BlackboardComponent;
bool bShouldReverse;
bool bShouldLoop;
};

View File

@ -0,0 +1,32 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "AI/T_SetMovementSpeed.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "AIController.h"
UT_SetMovementSpeed::UT_SetMovementSpeed()
{
MovementSpeed = 400.f;
}
EBTNodeResult::Type UT_SetMovementSpeed::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
EBTNodeResult::Type Result = Super::ExecuteTask(OwnerComp, NodeMemory);
if(Result == EBTNodeResult::Failed)
return EBTNodeResult::Failed;
APawn* ControllingPawn = OwnerComp.GetAIOwner()->GetPawn();
if(ControllingPawn == nullptr)
return EBTNodeResult::Failed;
UCharacterMovementComponent* MovementComponent = ControllingPawn->GetComponentByClass<UCharacterMovementComponent>();
if(MovementComponent == nullptr)
return EBTNodeResult::Failed;
MovementComponent->MaxWalkSpeed = MovementSpeed;
return EBTNodeResult::Succeeded;
}

View File

@ -0,0 +1,23 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "BehaviorTree/BTTaskNode.h"
#include "T_SetMovementSpeed.generated.h"
/**
*
*/
UCLASS()
class D1_API UT_SetMovementSpeed : public UBTTaskNode
{
GENERATED_BODY()
public:
UT_SetMovementSpeed();
virtual EBTNodeResult::Type ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) override;
private:
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta=(AllowPrivateAccess="true"))
float MovementSpeed;
};

View File

@ -11,6 +11,7 @@ public class D1 : ModuleRules
PublicDependencyModuleNames.AddRange(new string[] {
"Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay",
"EnhancedInput", "AnimGraphRuntime", "GameplayTags", "Niagara",
"AIModule", "GameplayTasks", "NavigationSystem"
});
PublicIncludePaths.AddRange(new string[] { "D1" });