96 - Update Health in the HUD

This commit is contained in:
Kingsmedia 2022-05-07 12:03:33 +02:00
parent ce6d62f323
commit 26e44a4b4e
4 changed files with 56 additions and 5 deletions

View File

@ -5,6 +5,7 @@
#include "Blaster/Blaster.h"
#include "Blaster/Components/CombatComponent.h"
#include "Blaster/PlayerController/BlasterPlayerController.h"
#include "Blaster/Weapon/Weapon.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
@ -48,6 +49,17 @@ ABlasterCharacter::ABlasterCharacter()
MinNetUpdateFrequency = 33.f;
}
void ABlasterCharacter::BeginPlay()
{
Super::BeginPlay();
BlasterPlayerController = Cast<ABlasterPlayerController>(Controller);
if (BlasterPlayerController)
{
BlasterPlayerController->SetHUDHealth(Health, MaxHealth);
}
}
void ABlasterCharacter::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
@ -100,11 +112,6 @@ void ABlasterCharacter::PlayHitReactMontage()
}
}
void ABlasterCharacter::BeginPlay()
{
Super::BeginPlay();
}
void ABlasterCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);

View File

@ -103,6 +103,8 @@ private:
UFUNCTION()
void OnRep_Health();
class ABlasterPlayerController* BlasterPlayerController;
public:
void SetOverlappingWeapon(AWeapon* Weapon);

View File

@ -3,3 +3,33 @@
#include "BlasterPlayerController.h"
#include "Blaster/HUD/BlasterHUD.h"
#include "Blaster/HUD/CharacterOverlay.h"
#include "Components/ProgressBar.h"
#include "Components/TextBlock.h"
void ABlasterPlayerController::BeginPlay()
{
Super::BeginPlay();
BlasterHUD = Cast<ABlasterHUD>(GetHUD());
}
void ABlasterPlayerController::SetHUDHealth(float Health, float MaxHealth)
{
BlasterHUD = BlasterHUD == nullptr ? Cast<ABlasterHUD>(GetHUD()) : BlasterHUD;
bool bHUDValid =
BlasterHUD &&
BlasterHUD->CharacterOverlay &&
BlasterHUD->CharacterOverlay->HealthBar &&
BlasterHUD->CharacterOverlay->HealthText;
if (bHUDValid)
{
const float HealthPercent = Health / MaxHealth;
BlasterHUD->CharacterOverlay->HealthBar->SetPercent(HealthPercent);
const FString HealthText = FString::Printf(TEXT("%d/%d"), FMath::CeilToInt(Health), FMath::CeilToInt(MaxHealth));
BlasterHUD->CharacterOverlay->HealthText->SetText(FText::FromString(HealthText));
}
}

View File

@ -13,5 +13,17 @@ UCLASS()
class BLASTER_API ABlasterPlayerController : public APlayerController
{
GENERATED_BODY()
public:
void SetHUDHealth(float Health, float MaxHealth);
protected:
virtual void BeginPlay() override;
private:
class ABlasterHUD* BlasterHUD;
};