Add Healthbar to player
This commit is contained in:
parent
b0c46c615b
commit
c3bbb1ac6e
BIN
Content/Player/BP_PlayerCharacter.uasset (Stored with Git LFS)
BIN
Content/Player/BP_PlayerCharacter.uasset (Stored with Git LFS)
Binary file not shown.
Binary file not shown.
|
@ -35,6 +35,13 @@ APlayerCharacter::APlayerCharacter()
|
|||
|
||||
//Create 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()
|
||||
|
|
|
@ -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<UUserWidget> PlayerHUD = nullptr;
|
||||
|
||||
UPROPERTY(EditAnywhere)
|
||||
UWidgetComponent* HealthBarWidgetComponent;
|
||||
|
||||
private:
|
||||
FTimerHandle GarlicTimerHandle;
|
||||
|
||||
|
|
|
@ -19,7 +19,6 @@ class VAMPIRES_API AVampireAIController : public AAIController
|
|||
{
|
||||
GENERATED_BODY()
|
||||
|
||||
private:
|
||||
UBlackboardComponent* Blackboard;
|
||||
|
||||
UBehaviorTreeComponent* BehaviorTree;
|
||||
|
|
|
@ -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);
|
||||
}
|
|
@ -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();
|
||||
};
|
Loading…
Reference in New Issue