Created the life counter

Added the life counter to the HUD of the game.
This commit is contained in:
JKuijperM 2023-04-07 16:38:54 +02:00
parent 5a05008779
commit bf291cdda5
11 changed files with 174 additions and 19 deletions

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:77b8a3d480b401da5564414c3a68d87e0ce6ea52c820d275b3b10131a96c4d22
size 28984
oid sha256:30dd531abe7ed074897e22da99df2fb0581802f31d12447faf9677628d9ee445
size 28698

View File

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:2f596ecd07c4b5bb9548d807ebe9496c20c533fd41f1371a465e5f500440d49d
size 44005

View File

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:39bd91a3aee79e4a34d10401d00ae0b18fe224ef22ecc225e3178ab5da6c1dec
size 10209

View File

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:b0098b96eebb2261cc036f37ca4f493f879a70ca033069f2830c0ce668e1b439
size 9855

View File

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

View File

@ -8,6 +8,14 @@ public class EndlessZombie : ModuleRules
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay", "EnhancedInput" });
PublicDependencyModuleNames.AddRange(new string[] {
"Core",
"CoreUObject",
"Engine",
"InputCore",
"HeadMountedDisplay",
"EnhancedInput",
"UMG"
});
}
}

View File

@ -15,6 +15,9 @@ public:
AEndlessZombieGameMode();
void RestartLevel();
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Life")
int iPlayerLife = 3;
};

View File

@ -1,6 +1,8 @@
// Copyright Jorge Kuijper. All Rights Reserved.
#include "ZombieCharacter.h"
#include "ZombiePlayerHUD.h"
#include "Blueprint/UserWidget.h"
#include "EndlessZombieGameMode.h"
#include "Camera/CameraComponent.h"
#include "EnhancedInputComponent.h"
@ -52,6 +54,10 @@ AZombieCharacter::AZombieCharacter()
FollowCamera->SetWorldRotation(FQuat(FRotator(-15.f, 0.f, 0.f)));
bReadyState = true;
// HUD
ZombiePlayerHUDClass = nullptr;
ZombieHUD = nullptr;
}
// Called when the game starts or when spawned
@ -65,6 +71,12 @@ void AZombieCharacter::BeginPlay()
{
Subsystem->AddMappingContext(DefaultMappingContext, 0);
}
if (ZombiePlayerHUDClass)
{
ZombieHUD = CreateWidget<UZombiePlayerHUD>(PlayerController, ZombiePlayerHUDClass);
if (ZombieHUD)
ZombieHUD->AddToPlayerScreen();
}
}
DynamicFlashMaterial = GetMesh()->CreateDynamicMaterialInstance(0);
@ -81,6 +93,17 @@ void AZombieCharacter::BeginPlay()
}
}
void AZombieCharacter::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
if (ZombieHUD)
{
ZombieHUD->RemoveFromParent();
ZombieHUD = nullptr;
}
Super::EndPlay(EndPlayReason);
}
// Called every frame
void AZombieCharacter::Tick(float DeltaTime)
{
@ -185,16 +208,22 @@ void AZombieCharacter::Die()
void AZombieCharacter::ObstacleCollision()
{
iPlayerLife--;
if (iPlayerLife > 0)
AEndlessZombieGameMode* CurrentGameMode = Cast<AEndlessZombieGameMode>(UGameplayStatics::GetGameMode(GetWorld()));
if (CurrentGameMode)
{
CurrentGameMode->iPlayerLife--;
if (CurrentGameMode->iPlayerLife > 0)
{
PlayFlashEffect();
ZombieHUD->ModifyLifeCounter();
}
else
{
ZombieHUD->ModifyLifeCounter();
Die();
}
}
}
void AZombieCharacter::PlayFlashEffect(float fMultiplier, FLinearColor Color, float fPlayRate)
{

View File

@ -48,6 +48,8 @@ protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
// APawn interface
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
@ -69,9 +71,7 @@ public:
UPROPERTY(EditAnywhere, Category = "Movement")
float fBaseSpeed = 5.f;
UPROPERTY(EditAnywhere, Category = "Movement")
float fAcceleration = .001f;
UPROPERTY(EditAnywhere, Category = "Life")
int iPlayerLife = 3;
float fAcceleration = .01f;
bool bCanTurn = false;
FRotator rDesireRotation = FRotator(0.f, 0.f, 0.f);
@ -95,6 +95,11 @@ public:
float fTimelineValue;
FTimeline FlashTimeline;
UPROPERTY(EditAnywhere, Category = "HUD")
TSubclassOf<class UZombiePlayerHUD> ZombiePlayerHUDClass;
UPROPERTY()
class UZombiePlayerHUD* ZombieHUD;
private:
void MoveForwardConstant(float DeltaTime);

View File

@ -0,0 +1,57 @@
// Copyright Jorge Kuijper. All Rights Reserved.
#include "ZombiePlayerHUD.h"
#include "Components/Image.h"
#include "Components/TextBlock.h"
#include "EndlessZombieGameMode.h"
#include "Kismet/GameplayStatics.h"
void UZombiePlayerHUD::NativeConstruct()
{
Super::NativeConstruct();
// ItemTitle can be nullptr if we haven't created it in the
// Blueprint subclass
if (TextLife)
{
TextLife->SetText(FText::FromString(TEXT("Lifes: ")));
}
if (LifeImg01)
{
LifeImg01->SetBrushFromTexture(LifeGreenTexture);
}
CurrentGameMode = Cast<AEndlessZombieGameMode>(UGameplayStatics::GetGameMode(GetWorld()));
}
void UZombiePlayerHUD::ModifyLifeCounter()
{
if (CurrentGameMode)
{
switch (CurrentGameMode->iPlayerLife)
{
case 2:
EmptyLife(LifeImg03);
break;
case 1:
EmptyLife(LifeImg02);
break;
case 0:
EmptyLife(LifeImg01);
break;
default:
break;
}
}
}
void UZombiePlayerHUD::EmptyLife(UImage* LifeImg)
{
if ((LifeImg) && (LifeGreyTexture))
{
LifeImg->SetBrushFromTexture(LifeGreyTexture);
}
}

View File

@ -0,0 +1,43 @@
// Copyright Jorge Kuijper. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "ZombiePlayerHUD.generated.h"
class UTextBlock;
class UImage;
class AEndlessZombieGameMode;
/**
*
*/
UCLASS()
class ENDLESSZOMBIE_API UZombiePlayerHUD : public UUserWidget
{
GENERATED_BODY()
protected:
virtual void NativeConstruct() override;
public:
UPROPERTY(BlueprintReadWrite, meta = (BindWidget))
UTextBlock* TextLife;
UPROPERTY(BlueprintReadWrite, meta = (BindWidget))
UImage* LifeImg01;
UPROPERTY(BlueprintReadWrite, meta = (BindWidget))
UImage* LifeImg02;
UPROPERTY(BlueprintReadWrite, meta = (BindWidget))
UImage* LifeImg03;
UPROPERTY(EditAnywhere)
UTexture2D* LifeGreenTexture;
UPROPERTY(EditAnywhere)
UTexture2D* LifeGreyTexture;
AEndlessZombieGameMode* CurrentGameMode;
void ModifyLifeCounter();
private:
void EmptyLife(UImage* LifeImg);
};