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 version https://git-lfs.github.com/spec/v1
oid sha256:77b8a3d480b401da5564414c3a68d87e0ce6ea52c820d275b3b10131a96c4d22 oid sha256:30dd531abe7ed074897e22da99df2fb0581802f31d12447faf9677628d9ee445
size 28984 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", "Type": "Runtime",
"LoadingPhase": "Default", "LoadingPhase": "Default",
"AdditionalDependencies": [ "AdditionalDependencies": [
"Engine" "Engine",
"UMG"
] ]
} }
], ],

View File

@ -4,10 +4,18 @@ using UnrealBuildTool;
public class EndlessZombie : ModuleRules public class EndlessZombie : ModuleRules
{ {
public EndlessZombie(ReadOnlyTargetRules Target) : base(Target) public EndlessZombie(ReadOnlyTargetRules Target) : base(Target)
{ {
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs; 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(); AEndlessZombieGameMode();
void RestartLevel(); void RestartLevel();
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Life")
int iPlayerLife = 3;
}; };

View File

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

View File

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