Add Healthbar to player

This commit is contained in:
baz 2024-08-28 02:38:31 +01:00
parent b0c46c615b
commit c3bbb1ac6e
7 changed files with 67 additions and 3 deletions

BIN
Content/Player/BP_PlayerCharacter.uasset (Stored with Git LFS)

Binary file not shown.

BIN
Content/Widgets/Player/BP_PlayerHealthbarWidget.uasset (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -35,6 +35,13 @@ APlayerCharacter::APlayerCharacter()
//Create Weapon Inventory Component //Create Weapon Inventory Component
WeaponInventoryComponent = CreateDefaultSubobject<UWeaponInventoryComponent>(TEXT("Weapon Inventory Component")); WeaponInventoryComponent = CreateDefaultSubobject<UWeaponInventoryComponent>(TEXT("Weapon Inventory Component"));
// Create HealthBar Widget Component
HealthBarWidgetComponent = CreateDefaultSubobject<UWidgetComponent>(TEXT("Healthbar"));
HealthBarWidgetComponent->SetupAttachment(RootComponent);
HealthBarWidgetComponent->SetRelativeLocation(FVector(0,0,90));
HealthBarWidgetComponent->SetTwoSided(true);
HealthBarWidgetComponent->SetBackgroundColor(FLinearColor(1,1,1,0));
} }
void APlayerCharacter::BeginPlay() void APlayerCharacter::BeginPlay()

View File

@ -10,6 +10,7 @@
#include "Camera/CameraComponent.h" #include "Camera/CameraComponent.h"
#include "GameFramework/SpringArmComponent.h" #include "GameFramework/SpringArmComponent.h"
#include "Blueprint/UserWidget.h" #include "Blueprint/UserWidget.h"
#include "Components/WidgetComponent.h"
#include "PlayerCharacter.generated.h" #include "PlayerCharacter.generated.h"
class UInputMappingContext; class UInputMappingContext;
@ -50,6 +51,9 @@ public:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
TSubclassOf<UUserWidget> PlayerHUD = nullptr; TSubclassOf<UUserWidget> PlayerHUD = nullptr;
UPROPERTY(EditAnywhere)
UWidgetComponent* HealthBarWidgetComponent;
private: private:
FTimerHandle GarlicTimerHandle; FTimerHandle GarlicTimerHandle;

View File

@ -19,7 +19,6 @@ class VAMPIRES_API AVampireAIController : public AAIController
{ {
GENERATED_BODY() GENERATED_BODY()
private:
UBlackboardComponent* Blackboard; UBlackboardComponent* Blackboard;
UBehaviorTreeComponent* BehaviorTree; UBehaviorTreeComponent* BehaviorTree;

View File

@ -0,0 +1,23 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "HealthbarWidget.h"
#include "Kismet/GameplayStatics.h"
#include "vampires/PlayerCharacter.h"
void UHealthbarWidget::NativeConstruct()
{
Super::NativeConstruct();
APlayerCharacter* player = Cast<APlayerCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
UHealthComponent* healthComponent = player->GetHealthComponent();
healthComponent->OnDamaged.BindUFunction(this, "UpdateHealthBar");
UpdateHealthBar();
}
void UHealthbarWidget::UpdateHealthBar()
{
APlayerCharacter* player = Cast<APlayerCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
float percent = player->GetHealthComponent()->GetCurrentHealth() / player->GetHealthComponent()->GetMaxHealth();
HealthBar->SetPercent(percent);
}

View File

@ -0,0 +1,28 @@
// 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 "vampires/VampireCharacter.h"
#include "HealthbarWidget.generated.h"
/**
*
*/
UCLASS()
class VAMPIRES_API UHealthbarWidget : public UUserWidget
{
GENERATED_BODY()
public:
UPROPERTY(BlueprintReadWrite, meta = (BindWidget))
UProgressBar* HealthBar;
virtual void NativeConstruct() override;
private:
UFUNCTION()
void UpdateHealthBar();
};