Compare commits

...

2 Commits

Author SHA1 Message Date
baz ba0243bd65 Create Healthbar UserWidget 2024-03-08 02:34:54 +00:00
baz e30e644b58 Add Healthbar to EnemyCharacter 2024-03-08 02:33:40 +00:00
9 changed files with 90 additions and 4 deletions

Binary file not shown.

BIN
Content/Enemy/Worker/C_Worker.uasset (Stored with Git LFS)

Binary file not shown.

BIN
Content/UI/EnemyHealthbar/EnemyHealthbar.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/UI/EnemyHealthbar/M_WhitePixel.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/UI/EnemyHealthbar/T_WhitePixel.uasset (Stored with Git LFS) Normal file

Binary file not shown.

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
@ -20,6 +21,12 @@ AEnemyCharacter::AEnemyCharacter(const FObjectInitializer& ObjectInitializer) :
GetHealthComponent()->OnDamaged.BindUFunction(this, "OnDamaged"); GetHealthComponent()->OnDamaged.BindUFunction(this, "OnDamaged");
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"
@ -27,6 +28,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"))

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();
};