Add Healthbar to EnemyCharacter

This commit is contained in:
baz 2024-03-08 02:33:40 +00:00
parent 7396d50db9
commit e30e644b58
4 changed files with 77 additions and 0 deletions

View File

@ -8,6 +8,7 @@
#include "BehaviorTree/BehaviorTree.h" #include "BehaviorTree/BehaviorTree.h"
#include "BehaviorTree/BlackboardComponent.h" #include "BehaviorTree/BlackboardComponent.h"
#include "BehaviorTree/BlackboardData.h" #include "BehaviorTree/BlackboardData.h"
#include "UI/EnemyHealthbarUserWidget.h"
#define COLLISION_WEAPON ECC_GameTraceChannel1 #define COLLISION_WEAPON ECC_GameTraceChannel1
@ -21,6 +22,12 @@ AEnemyCharacter::AEnemyCharacter(const FObjectInitializer& ObjectInitializer) :
GetHealthComponent()->OnDeath.BindUFunction(this, "OnDeath"); GetHealthComponent()->OnDeath.BindUFunction(this, "OnDeath");
GetHealthComponent()->SetMaxHealth(100.0f); GetHealthComponent()->SetMaxHealth(100.0f);
HealthbarWidgetComponent = CreateDefaultSubobject<UWidgetComponent>(TEXT("Healthbar"));
HealthbarWidgetComponent->SetupAttachment(RootComponent);
HealthbarWidgetComponent->SetRelativeLocation(FVector(0,0,90));
HealthbarWidgetComponent->SetTwoSided(true);
HealthbarWidgetComponent->SetBackgroundColor(FLinearColor(1,1,1,0));
this->Tags.Add(FName("Enemy")); this->Tags.Add(FName("Enemy"));
} }
@ -63,6 +70,12 @@ void AEnemyCharacter::BeginPlay()
AEnemyAIController* controller = Cast<AEnemyAIController>(GetController()); AEnemyAIController* controller = Cast<AEnemyAIController>(GetController());
controller->GetBlackboardComponent()->SetValueAsFloat("CurrentHealth", GetHealthComponent()->GetCurrentHealth()); controller->GetBlackboardComponent()->SetValueAsFloat("CurrentHealth", GetHealthComponent()->GetCurrentHealth());
GetHealthComponent()->OnDamaged.BindUFunction(this, "OnDamaged"); GetHealthComponent()->OnDamaged.BindUFunction(this, "OnDamaged");
if (HealthbarWidgetComponent->GetWidget())
{
UEnemyHealthbarUserWidget* healthbar = Cast<UEnemyHealthbarUserWidget>(HealthbarWidgetComponent->GetWidget());
healthbar->BindOwner(this);
}
} }
void AEnemyCharacter::PlayOnFireAnimations() void AEnemyCharacter::PlayOnFireAnimations()

View File

@ -7,6 +7,7 @@
#include "PatrolRoute.h" #include "PatrolRoute.h"
#include "BehaviorTree/BehaviorTreeComponent.h" #include "BehaviorTree/BehaviorTreeComponent.h"
#include "RandomWeaponParameters.h" #include "RandomWeaponParameters.h"
#include "Components/WidgetComponent.h"
#include "EnemyCharacter.generated.h" #include "EnemyCharacter.generated.h"
@ -28,6 +29,9 @@ public:
UPROPERTY(EditAnywhere) UPROPERTY(EditAnywhere)
float DefendRadius = 500.0f; float DefendRadius = 500.0f;
UPROPERTY(EditAnywhere)
UWidgetComponent* HealthbarWidgetComponent;
private: private:
UPROPERTY(EditDefaultsOnly, Meta = (AllowPrivateAccess = "true")) UPROPERTY(EditDefaultsOnly, Meta = (AllowPrivateAccess = "true"))
UBehaviorTree* BehaviourTree; UBehaviorTree* BehaviourTree;

View File

@ -0,0 +1,26 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "../UI/EnemyHealthbarUserWidget.h"
#include "Nakatomi/EnemyCharacter.h"
void UEnemyHealthbarUserWidget::BindOwner(AEnemyCharacter* NewOwner)
{
Owner = NewOwner;
if (Owner)
{
auto healthComponent = Owner->GetHealthComponent();
healthComponent->OnDamaged.BindUFunction(this, "UpdateHealthbar");
}
}
void UEnemyHealthbarUserWidget::UpdateHealthbar()
{
if (Owner)
{
float percent = Owner->GetHealthComponent()->GetCurrentHealth() / Owner->GetHealthComponent()->GetMaxHealth();
Healthbar->SetPercent(percent);
}
}

View File

@ -0,0 +1,34 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "Components/ProgressBar.h"
#include "Nakatomi/EnemyCharacter.h"
#include "EnemyHealthbarUserWidget.generated.h"
/**
*
*/
UCLASS()
class NAKATOMI_API UEnemyHealthbarUserWidget : public UUserWidget
{
GENERATED_BODY()
public:
UPROPERTY(BlueprintReadWrite, meta = (BindWidget))
UProgressBar* Healthbar;
AEnemyCharacter* Owner;
public:
void BindOwner(AEnemyCharacter* NewOwner);
private:
UFUNCTION()
void UpdateHealthbar();
};