From 8e59d1a8b757182dafecd7b9c02c1a0bc946eda5 Mon Sep 17 00:00:00 2001 From: Louis Hobbs Date: Mon, 6 Feb 2023 01:07:01 +0000 Subject: [PATCH] Add basic Player HUD --- Content/Player/DefaultGamemode.uasset | 4 ++-- Content/Player/PlayerCharacter.uasset | 4 ++-- Content/UI/PlayerHUD.uasset | 3 +++ Content/UI/WidgetHUD.uasset | 3 +++ Source/Nakatomi/Nakatomi.Build.cs | 2 +- Source/Nakatomi/PlayerCharacter.cpp | 29 +++++++++++++++++++++++++++ Source/Nakatomi/PlayerCharacter.h | 12 +++++++++++ 7 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 Content/UI/PlayerHUD.uasset create mode 100644 Content/UI/WidgetHUD.uasset diff --git a/Content/Player/DefaultGamemode.uasset b/Content/Player/DefaultGamemode.uasset index 603b28a..b3e9f87 100644 --- a/Content/Player/DefaultGamemode.uasset +++ b/Content/Player/DefaultGamemode.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:39be4b5146602dd27098c9a4de8d1381b808a0af82945cf0b7dcd064cf83b02c -size 19680 +oid sha256:6bcbeecf5ff3f378a35de9d728a38cc38466000be8726cc93dd607aec3493589 +size 19921 diff --git a/Content/Player/PlayerCharacter.uasset b/Content/Player/PlayerCharacter.uasset index 6c22674..dcba240 100644 --- a/Content/Player/PlayerCharacter.uasset +++ b/Content/Player/PlayerCharacter.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:7fa7a42459254da49fb79e0870082557af4cb332e1683cc3c858441b7e19eb61 -size 38074 +oid sha256:0977fa5f83da69719d01c9921cdc2f453ddbeeb09c32146fa3645aac08c58c4f +size 36527 diff --git a/Content/UI/PlayerHUD.uasset b/Content/UI/PlayerHUD.uasset new file mode 100644 index 0000000..eeec161 --- /dev/null +++ b/Content/UI/PlayerHUD.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e3aed9eaa098b4d6fa0a1a10ba2515ca40f432ffcd36a872c4a99d08c87edf58 +size 21080 diff --git a/Content/UI/WidgetHUD.uasset b/Content/UI/WidgetHUD.uasset new file mode 100644 index 0000000..38e7c8f --- /dev/null +++ b/Content/UI/WidgetHUD.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d9f9fa7c114d0a2a43d994d503b61a2c79246bea1a6ef4bfe70d40b0c1ee2e6c +size 64644 diff --git a/Source/Nakatomi/Nakatomi.Build.cs b/Source/Nakatomi/Nakatomi.Build.cs index a75dab6..06ba12a 100644 --- a/Source/Nakatomi/Nakatomi.Build.cs +++ b/Source/Nakatomi/Nakatomi.Build.cs @@ -8,7 +8,7 @@ public class Nakatomi : ModuleRules { PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs; - PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "EnhancedInput", "FieldSystemEngine", "GeometryCollectionEngine" }); + PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "EnhancedInput", "FieldSystemEngine", "GeometryCollectionEngine", "UMG" }); PrivateDependencyModuleNames.AddRange(new string[] { }); diff --git a/Source/Nakatomi/PlayerCharacter.cpp b/Source/Nakatomi/PlayerCharacter.cpp index c9015b5..51b87da 100644 --- a/Source/Nakatomi/PlayerCharacter.cpp +++ b/Source/Nakatomi/PlayerCharacter.cpp @@ -63,6 +63,15 @@ void APlayerCharacter::BeginPlay() this->Tags.Add(FName("Player")); } + if (PlayerHUD) + { + currentPlayerHUD = UUserWidget::CreateWidgetInstance(*GetWorld(), PlayerHUD, FName("PLayer HUD")); + + if (currentPlayerHUD) + { + currentPlayerHUD->AddToViewport(); + } + } } // Called every frame @@ -469,3 +478,23 @@ void APlayerCharacter::ClearAllTimers() GetWorldTimerManager().ClearTimer(FireTimerHandle); GetWorldTimerManager().ClearTimer(CooldownTimerHandle); } + +int APlayerCharacter::GetCurrentAmmoCount() +{ + if (CurrentWeapon == nullptr) + { + return -1; + } + + return CurrentWeapon->GetAmmoCount(); +} + +float APlayerCharacter::GetCurrentHealthCount() +{ + if (!GetHealthComponent()) + { + return -1; + } + + return GetHealthComponent()->GetCurrentHealth(); +} diff --git a/Source/Nakatomi/PlayerCharacter.h b/Source/Nakatomi/PlayerCharacter.h index fa3d7da..f500a0a 100644 --- a/Source/Nakatomi/PlayerCharacter.h +++ b/Source/Nakatomi/PlayerCharacter.h @@ -11,6 +11,7 @@ #include "Weapon.h" #include "Engine/EngineTypes.h" #include "Engine/DamageEvents.h" +#include "Blueprint/UserWidget.h" #include "PlayerCharacter.generated.h" class UInputAction; @@ -59,6 +60,9 @@ public: UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) UInputAction* WeaponSwitchingAction; + UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) + TSubclassOf PlayerHUD; + protected: UPROPERTY(EditDefaultsOnly, BlueprintReadWrite) @@ -85,6 +89,8 @@ private: bool IsFiring = false; + class UUserWidget* currentPlayerHUD; + public: // Sets default values for this character's properties APlayerCharacter(); @@ -147,4 +153,10 @@ public: void WeaponCooldownHandler(); void ClearAllTimers(); + + UFUNCTION(BlueprintCallable) + int GetCurrentAmmoCount(); + + UFUNCTION(BlueprintCallable) + float GetCurrentHealthCount(); };