From 26e44a4b4e7b72486baeca413c317602ba34ca18 Mon Sep 17 00:00:00 2001 From: Kingsmedia Date: Sat, 7 May 2022 12:03:33 +0200 Subject: [PATCH] 96 - Update Health in the HUD --- Source/Blaster/Character/BlasterCharacter.cpp | 17 +++++++---- Source/Blaster/Character/BlasterCharacter.h | 2 ++ .../BlasterPlayerController.cpp | 30 +++++++++++++++++++ .../BlasterPlayerController.h | 12 ++++++++ 4 files changed, 56 insertions(+), 5 deletions(-) diff --git a/Source/Blaster/Character/BlasterCharacter.cpp b/Source/Blaster/Character/BlasterCharacter.cpp index 2f4d4a3..982b74d 100644 --- a/Source/Blaster/Character/BlasterCharacter.cpp +++ b/Source/Blaster/Character/BlasterCharacter.cpp @@ -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(Controller); + if (BlasterPlayerController) + { + BlasterPlayerController->SetHUDHealth(Health, MaxHealth); + } +} + void ABlasterCharacter::GetLifetimeReplicatedProps(TArray& 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); diff --git a/Source/Blaster/Character/BlasterCharacter.h b/Source/Blaster/Character/BlasterCharacter.h index b1ef625..0e552a8 100644 --- a/Source/Blaster/Character/BlasterCharacter.h +++ b/Source/Blaster/Character/BlasterCharacter.h @@ -103,6 +103,8 @@ private: UFUNCTION() void OnRep_Health(); + + class ABlasterPlayerController* BlasterPlayerController; public: void SetOverlappingWeapon(AWeapon* Weapon); diff --git a/Source/Blaster/PlayerController/BlasterPlayerController.cpp b/Source/Blaster/PlayerController/BlasterPlayerController.cpp index 1e0f308..9a5384e 100644 --- a/Source/Blaster/PlayerController/BlasterPlayerController.cpp +++ b/Source/Blaster/PlayerController/BlasterPlayerController.cpp @@ -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(GetHUD()); +} + +void ABlasterPlayerController::SetHUDHealth(float Health, float MaxHealth) +{ + BlasterHUD = BlasterHUD == nullptr ? Cast(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)); + } +} diff --git a/Source/Blaster/PlayerController/BlasterPlayerController.h b/Source/Blaster/PlayerController/BlasterPlayerController.h index 1398b46..ea36af0 100644 --- a/Source/Blaster/PlayerController/BlasterPlayerController.h +++ b/Source/Blaster/PlayerController/BlasterPlayerController.h @@ -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; };