Add Companion Character for Generic Base

This commit is contained in:
Philip W 2023-10-04 02:25:27 +01:00
parent e13e6d1bac
commit 44e25b8472
14 changed files with 225 additions and 16 deletions

BIN
EndlessVendetta/Content/AI/EQSB_Enemy.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
EndlessVendetta/Content/AI/EQSB_Player.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
EndlessVendetta/Content/AI/EQS_Test.uasset (Stored with Git LFS) Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,85 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "AI_CompanionController.h"
#include "AI_EnemyController.h"
#include "CompanionCharacter.h"
#include "EndlessVendetta/EndlessVendettaCharacter.h"
#include "Perception/AIPerceptionComponent.h"
#include "Perception/AISenseConfig_Sight.h"
AAI_CompanionController::AAI_CompanionController(FObjectInitializer const& ObjectInitializer)
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
AAIController::SetGenericTeamId(FGenericTeamId(0));
SetupPerceptionSystem();
}
// Called when the game starts or when spawned
void AAI_CompanionController::BeginPlay()
{
Super::BeginPlay();
}
void AAI_CompanionController::OnPossess(APawn* InPawn)
{
Super::OnPossess(InPawn);
if (const ACompanionCharacter* CompanionCharacter = Cast<ACompanionCharacter>(InPawn))
{
if (UBehaviorTree* const BehaviorTree = CompanionCharacter->GetBehaviorTree())
{
UBlackboardComponent* TempBlackboardPtr;
UseBlackboard(BehaviorTree->BlackboardAsset, TempBlackboardPtr);
Blackboard = TempBlackboardPtr;
RunBehaviorTree(BehaviorTree);
}
}
}
// Called every frame
void AAI_CompanionController::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AAI_CompanionController::SetupPerceptionSystem()
{
SightConfig = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("Sight Config"));
if (IsValid(SightConfig))
{
SetPerceptionComponent(*CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("Perception Component")));
SightConfig->SightRadius = 2000.0f;
SightConfig->LoseSightRadius = 2100.0f;
SightConfig->PeripheralVisionAngleDegrees = 70.0f;
SightConfig->SetMaxAge(20.0f);
SightConfig->AutoSuccessRangeFromLastSeenLocation = 520.0f;
SightConfig->DetectionByAffiliation.bDetectEnemies = true;
SightConfig->DetectionByAffiliation.bDetectFriendlies = true;
SightConfig->DetectionByAffiliation.bDetectNeutrals = true;
GetPerceptionComponent()->SetDominantSense(*SightConfig->GetSenseImplementation());
GetPerceptionComponent()->OnTargetPerceptionUpdated.AddDynamic(this, &AAI_CompanionController::OnTargetPerceptionUpdated);
GetPerceptionComponent()->ConfigureSense(*SightConfig);
}
}
void AAI_CompanionController::OnTargetPerceptionUpdated(AActor* Actor, FAIStimulus const Stimulus)
{
if (AEndlessVendettaCharacter* const PlayerCharacter = Cast<AEndlessVendettaCharacter>(Actor))
{
// if (Stimulus.WasSuccessfullySensed())
// {
// GetBlackboardComponent()->SetValueAsObject("TargetPlayer", Actor);
// GetBlackboardComponent()->SetValueAsVector("TargetLocation", Stimulus.StimulusLocation);
// GetBlackboardComponent()->SetValueAsBool("CanSeePlayer", true);
// }
// else
// {
// GetBlackboardComponent()->ClearValue("TargetActor");
// GetBlackboardComponent()->ClearValue("TargetLocation");
// GetBlackboardComponent()->SetValueAsBool("CanSeePlayer", false);
// }
}
}

View File

@ -0,0 +1,36 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "AIController.h"
#include "AI_CompanionController.generated.h"
struct FAIStimulus;
UCLASS()
class ENDLESSVENDETTA_API AAI_CompanionController : public AAIController
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
explicit AAI_CompanionController(FObjectInitializer const& ObjectInitializer);
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
virtual void OnPossess(APawn* InPawn) override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
private:
class UAISenseConfig_Sight* SightConfig;
void SetupPerceptionSystem();
UFUNCTION()
void OnTargetPerceptionUpdated(AActor* Actor, FAIStimulus const Stimulus);
};

View File

@ -15,7 +15,7 @@ AAI_EnemyController::AAI_EnemyController(FObjectInitializer const& ObjectInitial
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
SetGenericTeamId(FGenericTeamId(1));
AAIController::SetGenericTeamId(FGenericTeamId(1));
SetupPerceptionSystem();
}

View File

@ -0,0 +1,50 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "CompanionCharacter.h"
#include "Perception/AIPerceptionStimuliSourceComponent.h"
#include "Perception/AISense_Sight.h"
// Sets default values
ACompanionCharacter::ACompanionCharacter()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void ACompanionCharacter::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ACompanionCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void ACompanionCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
}
UBehaviorTree* ACompanionCharacter::GetBehaviorTree() const
{
return BehaviorTree;
}
void ACompanionCharacter::SetupStimuliSourceComponent()
{
StimuliSourceComponent = CreateDefaultSubobject<UAIPerceptionStimuliSourceComponent>(TEXT("Stimuli Source Component"));
if (IsValid(StimuliSourceComponent))
{
StimuliSourceComponent->RegisterForSense(TSubclassOf<UAISense_Sight>());
StimuliSourceComponent->RegisterWithPerceptionSystem();
}
}

View File

@ -0,0 +1,38 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "BehaviorTree/BehaviorTree.h"
#include "GameFramework/Character.h"
#include "CompanionCharacter.generated.h"
UCLASS()
class ENDLESSVENDETTA_API ACompanionCharacter : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
ACompanionCharacter();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AI")
UBehaviorTree* BehaviorTree;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
UFUNCTION(BlueprintCallable, Category = "AI")
UBehaviorTree* GetBehaviorTree() const;
private:
class UAIPerceptionStimuliSourceComponent* StimuliSourceComponent;
void SetupStimuliSourceComponent();
};