feat: add Hpbar, add RoundProgress

Hpbar  - not working
This commit is contained in:
BambiTheLama 2025-04-23 23:03:06 +02:00
parent 672b19326b
commit 54396bc42c
12 changed files with 122 additions and 1 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -9,7 +9,10 @@
"Type": "Runtime",
"LoadingPhase": "Default",
"AdditionalDependencies": [
"Engine"
"Engine",
"UMG",
"Slate",
"SlateCore"
]
}
],

View File

@ -2,6 +2,8 @@
#include "Characters/ExoPlayerCharacter.h"
#include "Blueprint/UserWidget.h"
#include "Characters/Components/ShootingComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Items/AmmoBoxBase.h"
@ -32,6 +34,20 @@ void AExoPlayerCharacter::BeginPlay()
Super::BeginPlay();
GetCapsuleComponent()->OnComponentBeginOverlap.AddDynamic(this, &AExoPlayerCharacter::OnActorBeginOverlap);
if (PlayerHudClass)
{
GEngine->AddOnScreenDebugMessage(-1,5.0f,FColor::Green,TEXT("Jest klasa hudu playera"));
PlayerHud = CreateWidget<UUserWidget>(GetWorld(),PlayerHudClass);
if (PlayerHud)
{
PlayerHud->AddToViewport();
PlayerHud->AddToPlayerScreen();
GEngine->AddOnScreenDebugMessage(-1,5.0f,FColor::Green,TEXT("Dodaje to Viewportu"));
}
}
}
void AExoPlayerCharacter::OnActorBeginOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,

View File

@ -0,0 +1,14 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "Widget/UWBP_HpBar.h"
#include "Components/ProgressBar.h"
#include "Widget/UWBP_RoundProgressBar.h"
void UWBP_HpBar::SetHp(float hp, float hpMax)
{
if (!HPBar)
return;
HPBar->SetPercent(hp/hpMax);
}

View File

@ -0,0 +1,35 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "Widget/UWBP_RoundProgressBar.h"
#include "Components/Image.h"
#include "Styling/SlateBrush.h"
void UUWBP_RoundProgressBar::BeginDestroy()
{
Super::BeginDestroy();
}
void UUWBP_RoundProgressBar::SetPercent(float percent)
{
if (!DynamicMaterial)
{
if (!ImageBar)
return;
FSlateBrush brush= ImageBar->Brush;
UMaterialInterface* material=Cast<UMaterialInterface>(brush.GetResourceObject());
if (!material)
return;
DynamicMaterial = UMaterialInstanceDynamic::Create(material, this);
if (DynamicMaterial)
{
FSlateBrush newBrush= brush;
newBrush.SetResourceObject(DynamicMaterial);
ImageBar->Brush = newBrush;
}
}
if (DynamicMaterial)
DynamicMaterial->SetScalarParameterValue("Percent",percent);
}

View File

@ -54,6 +54,9 @@ public:
UPROPERTY(EditAnywhere, Category = "Health")
float CurrentHealth = 50.0f;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "UI")
TSubclassOf<UUserWidget> PlayerHudClass;
UFUNCTION()
void OnActorBeginOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult
@ -66,4 +69,6 @@ protected:
virtual void BeginPlay() override;
float MaxHealth = 100.0f;
UUserWidget* PlayerHud;
};

View File

@ -0,0 +1,22 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "UWBP_HpBar.generated.h"
/**
*
*/
UCLASS()
class UWBP_HpBar : public UUserWidget
{
GENERATED_BODY()
public:
void SetHp(float hp,float hpMax);
UPROPERTY(EditAnywhere, meta = (BindWidget))
class UUWBP_RoundProgressBar* HPBar;
};

View File

@ -0,0 +1,26 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "UWBP_RoundProgressBar.generated.h"
/**
*
*/
UCLASS()
class EXO_API UUWBP_RoundProgressBar : public UUserWidget
{
GENERATED_BODY()
UMaterialInstanceDynamic* DynamicMaterial = nullptr;
public:
virtual void BeginDestroy() override;
UFUNCTION(BlueprintCallable)
void SetPercent(float percent);
UPROPERTY(EditAnywhere, meta = (BindWidget))
class UImage* ImageBar;
};