From c3bbb1ac6e8d54710ee98df5c579677a55136158 Mon Sep 17 00:00:00 2001 From: baz Date: Wed, 28 Aug 2024 02:38:31 +0100 Subject: [PATCH] Add Healthbar to player --- Content/Player/BP_PlayerCharacter.uasset | 4 +-- .../Player/BP_PlayerHealthbarWidget.uasset | 3 ++ Source/vampires/PlayerCharacter.cpp | 7 +++++ Source/vampires/PlayerCharacter.h | 4 +++ Source/vampires/VampireAIController.h | 1 - Source/vampires/Widgets/HealthbarWidget.cpp | 23 +++++++++++++++ Source/vampires/Widgets/HealthbarWidget.h | 28 +++++++++++++++++++ 7 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 Content/Widgets/Player/BP_PlayerHealthbarWidget.uasset create mode 100644 Source/vampires/Widgets/HealthbarWidget.cpp create mode 100644 Source/vampires/Widgets/HealthbarWidget.h diff --git a/Content/Player/BP_PlayerCharacter.uasset b/Content/Player/BP_PlayerCharacter.uasset index 786a5e9..02ee5f8 100644 --- a/Content/Player/BP_PlayerCharacter.uasset +++ b/Content/Player/BP_PlayerCharacter.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:bcce95e6c31b5dd49f80544da18520f420886f763d1ee74466c0eab9002a253f -size 51902 +oid sha256:852c60f9f125e964733cc9fe6234325e10ce7ea36fab369b504f6888bf0d4bb9 +size 52469 diff --git a/Content/Widgets/Player/BP_PlayerHealthbarWidget.uasset b/Content/Widgets/Player/BP_PlayerHealthbarWidget.uasset new file mode 100644 index 0000000..0aa9d3a --- /dev/null +++ b/Content/Widgets/Player/BP_PlayerHealthbarWidget.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:66b0ca4d61fcb7ee30b04983044c8a2bae62f2ad2ea71abc60e74947029778ef +size 26194 diff --git a/Source/vampires/PlayerCharacter.cpp b/Source/vampires/PlayerCharacter.cpp index 0fa9648..4cff263 100644 --- a/Source/vampires/PlayerCharacter.cpp +++ b/Source/vampires/PlayerCharacter.cpp @@ -35,6 +35,13 @@ APlayerCharacter::APlayerCharacter() //Create Weapon Inventory Component WeaponInventoryComponent = CreateDefaultSubobject(TEXT("Weapon Inventory Component")); + + // Create HealthBar Widget Component + HealthBarWidgetComponent = CreateDefaultSubobject(TEXT("Healthbar")); + HealthBarWidgetComponent->SetupAttachment(RootComponent); + HealthBarWidgetComponent->SetRelativeLocation(FVector(0,0,90)); + HealthBarWidgetComponent->SetTwoSided(true); + HealthBarWidgetComponent->SetBackgroundColor(FLinearColor(1,1,1,0)); } void APlayerCharacter::BeginPlay() diff --git a/Source/vampires/PlayerCharacter.h b/Source/vampires/PlayerCharacter.h index a9f41cd..c595568 100644 --- a/Source/vampires/PlayerCharacter.h +++ b/Source/vampires/PlayerCharacter.h @@ -10,6 +10,7 @@ #include "Camera/CameraComponent.h" #include "GameFramework/SpringArmComponent.h" #include "Blueprint/UserWidget.h" +#include "Components/WidgetComponent.h" #include "PlayerCharacter.generated.h" class UInputMappingContext; @@ -50,6 +51,9 @@ public: UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) TSubclassOf PlayerHUD = nullptr; + UPROPERTY(EditAnywhere) + UWidgetComponent* HealthBarWidgetComponent; + private: FTimerHandle GarlicTimerHandle; diff --git a/Source/vampires/VampireAIController.h b/Source/vampires/VampireAIController.h index c743376..dd34516 100644 --- a/Source/vampires/VampireAIController.h +++ b/Source/vampires/VampireAIController.h @@ -19,7 +19,6 @@ class VAMPIRES_API AVampireAIController : public AAIController { GENERATED_BODY() -private: UBlackboardComponent* Blackboard; UBehaviorTreeComponent* BehaviorTree; diff --git a/Source/vampires/Widgets/HealthbarWidget.cpp b/Source/vampires/Widgets/HealthbarWidget.cpp new file mode 100644 index 0000000..b066075 --- /dev/null +++ b/Source/vampires/Widgets/HealthbarWidget.cpp @@ -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(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0)); + UHealthComponent* healthComponent = player->GetHealthComponent(); + healthComponent->OnDamaged.BindUFunction(this, "UpdateHealthBar"); + UpdateHealthBar(); +} + +void UHealthbarWidget::UpdateHealthBar() +{ + APlayerCharacter* player = Cast(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0)); + float percent = player->GetHealthComponent()->GetCurrentHealth() / player->GetHealthComponent()->GetMaxHealth(); + HealthBar->SetPercent(percent); +} diff --git a/Source/vampires/Widgets/HealthbarWidget.h b/Source/vampires/Widgets/HealthbarWidget.h new file mode 100644 index 0000000..9ecba3e --- /dev/null +++ b/Source/vampires/Widgets/HealthbarWidget.h @@ -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(); +};