Add basic ai functionality

This commit is contained in:
baz 2024-06-14 23:19:05 +01:00
parent 3bd49b0b90
commit 141ac232f0
6 changed files with 71 additions and 1 deletions

View File

@ -5,6 +5,8 @@
AEnemyCharacter::AEnemyCharacter(const FObjectInitializer& ObjectInitializer)
{
GetHealthComponent()->OnDamaged.BindUFunction(this, "OnDamaged");
GetHealthComponent()->OnDeath.BindUFunction(this, "OnDeath");
}
void AEnemyCharacter::BeginPlay()
@ -16,3 +18,16 @@ void AEnemyCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
UBehaviorTree* AEnemyCharacter::GetBehaviorTree()
{
return BehaviorTree;
}
void AEnemyCharacter::OnDamaged()
{
}
void AEnemyCharacter::OnDeath()
{
}

View File

@ -18,7 +18,7 @@ class VAMPIRES_API AEnemyCharacter : public AVampireCharacter
private:
UPROPERTY(EditDefaultsOnly, Meta = (AllowPrivateAccess = "true"))
UBehaviorTree* BehaviourTree;
UBehaviorTree* BehaviorTree;
public:
AEnemyCharacter(const FObjectInitializer& ObjectInitializer);
@ -28,4 +28,12 @@ protected:
public:
virtual void Tick(float DeltaTime) override;
UBehaviorTree* GetBehaviorTree();
UFUNCTION()
virtual void OnDamaged();
UFUNCTION()
virtual void OnDeath();
};

View File

@ -3,8 +3,15 @@
#include "VampireAIController.h"
#include "BehaviorTree/BlackboardComponent.h"
#include "Components/CapsuleComponent.h"
#include "GameFramework/PawnMovementComponent.h"
#include "Kismet/GameplayStatics.h"
AVampireAIController::AVampireAIController(const FObjectInitializer& object_initializer)
{
Blackboard = CreateDefaultSubobject<UBlackboardComponent>(TEXT("Blackboard"));
BehaviorTree = CreateDefaultSubobject<UBehaviorTreeComponent>(TEXT("Behavior Tree"));
}
void AVampireAIController::BeginPlay()
@ -20,12 +27,42 @@ void AVampireAIController::Tick(float DeltaTime)
void AVampireAIController::OnPossess(APawn* InPawn)
{
Super::OnPossess(InPawn);
EnemyCharacter = Cast<AEnemyCharacter>(InPawn);
check(EnemyCharacter);
EnemyCharacter->GetHealthComponent()->OnDamaged.BindUFunction(this, "OnDamaged");
EnemyCharacter->GetHealthComponent()->OnDeath.BindUFunction(this, "OnDeath");
if (UBehaviorTree* bt = EnemyCharacter->GetBehaviorTree())
{
Blackboard->InitializeBlackboard(*bt->BlackboardAsset);
BehaviorTree->StartTree(*bt);
Blackboard->SetValueAsObject("SelfActor", EnemyCharacter);
Blackboard->SetValueAsObject("Player", UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
}
}
void AVampireAIController::OnDamaged(FDamageInfo info)
{
if (EnemyCharacter->GetHealthComponent()->GetCurrentHealth() > 0.0f)
{
// TODO: Something
}
}
void AVampireAIController::OnDeath(FDamageInfo info)
{
// TODO: Do stuff here
EnemyCharacter->DetachFromControllerPendingDestroy();
EnemyCharacter->GetCapsuleComponent()->SetCollisionEnabled(ECollisionEnabled::NoCollision);
EnemyCharacter->GetCapsuleComponent()->SetCollisionResponseToAllChannels(ECR_Ignore);
if (UPawnMovementComponent* characterMovementComponent = EnemyCharacter->GetMovementComponent())
{
characterMovementComponent->StopMovementImmediately();
characterMovementComponent->StopActiveMovement();
characterMovementComponent->SetComponentTickEnabled(false);
}
EnemyCharacter->SetLifeSpan(0.1f);
}

View File

@ -4,6 +4,7 @@
#include "CoreMinimal.h"
#include "AIController.h"
#include "EnemyCharacter.h"
#include "HealthComponent.h"
#include "PlayerCharacter.h"
#include "BehaviorTree/BehaviorTreeComponent.h"
@ -24,6 +25,8 @@ private:
APlayerCharacter* PlayerCharacter;
AEnemyCharacter* EnemyCharacter;
public:
AVampireAIController(const FObjectInitializer& object_initializer);

View File

@ -34,3 +34,8 @@ void AVampireCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputCo
}
UHealthComponent* AVampireCharacter::GetHealthComponent()
{
return HealthComponent;
}

View File

@ -32,4 +32,6 @@ public:
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
UHealthComponent* GetHealthComponent();
};