Score Counter

Added the functionality to catch brains and update the score in the HUD.
Also, moved the creation of the HUD from the ZombiePlayerCharater to the GameMode
This commit is contained in:
JKuijperM 2023-05-02 11:20:30 +02:00
parent aa724c3e56
commit 800b10d65f
16 changed files with 224 additions and 46 deletions

View File

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

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:d3dd1c59c26b87fd1cdc3103d29ce7833d5cbd841a008137c0c956a8af5251f9 oid sha256:2d1667025d5c63688a775b95adddeaa4bc6a0d6e4b289eea31a0f1dfe3f07741
size 21542 size 20912

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:7e8104ca75816a6d60ace1e793d5e2822ce9ada1bd7ea52e10c3564325438f9f oid sha256:d0cca0987c932d092808324fc5f262177a0d94de9327f7f61cc9fe13ea1f782c
size 70602 size 73711

View File

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

View File

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

View File

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

View File

@ -0,0 +1,28 @@
// Copyright Jorge Kuijper. All Rights Reserved.
#include "Brain.h"
#include "EndlessZombieGameMode.h"
#include "Kismet/GameplayStatics.h"
#include "Components/BoxComponent.h"
ABrain::ABrain()
{
// Set the brain mesh
ConstructorHelpers::FObjectFinder<UStaticMesh> MeshObj(TEXT("/Game/StaticMeshes/Brain/SM_Brain.SM_Brain"));
if (MeshObj.Succeeded())
StaticMesh->SetStaticMesh(MeshObj.Object);
CollisionTrigger->SetRelativeLocation(FVector(0.f, -3.f, 2.f));
CollisionTrigger->SetRelativeScale3D(FVector(0.5f, 0.61f, 0.52f));
iPunctuation = 11;
}
void ABrain::OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
Super::OnBeginOverlap(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult);
AEndlessZombieGameMode* CurrentGameMode = Cast<AEndlessZombieGameMode>(UGameplayStatics::GetGameMode(GetWorld()));
if (CurrentGameMode)
CurrentGameMode->UpdateScore(iPunctuation);
}

View File

@ -0,0 +1,22 @@
// Copyright Jorge Kuijper. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "PickupItem.h"
#include "Brain.generated.h"
/**
*
*/
UCLASS()
class ENDLESSZOMBIE_API ABrain : public APickupItem
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ABrain();
virtual void OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) override;
};

View File

@ -1,6 +1,8 @@
// Copyright Epic Games, Inc. All Rights Reserved. // Copyright Epic Games, Inc. All Rights Reserved.
#include "EndlessZombieGameMode.h" #include "EndlessZombieGameMode.h"
#include "ZombiePlayerHUD.h"
#include "Blueprint/UserWidget.h"
#include "EndlessZombieCharacter.h" #include "EndlessZombieCharacter.h"
#include "Kismet/GameplayStatics.h" #include "Kismet/GameplayStatics.h"
#include "UObject/ConstructorHelpers.h" #include "UObject/ConstructorHelpers.h"
@ -13,6 +15,34 @@ AEndlessZombieGameMode::AEndlessZombieGameMode()
{ {
DefaultPawnClass = PlayerPawnBPClass.Class; DefaultPawnClass = PlayerPawnBPClass.Class;
} }
// HUD
ZombiePlayerHUDClass = nullptr;
ZombieHUD = nullptr;
}
void AEndlessZombieGameMode::BeginPlay()
{
Super::BeginPlay();
if (ZombiePlayerHUDClass)
{
APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0);
ZombieHUD = CreateWidget<UZombiePlayerHUD>(PlayerController, ZombiePlayerHUDClass);
if (ZombieHUD)
ZombieHUD->AddToPlayerScreen();
}
}
void AEndlessZombieGameMode::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
if (ZombieHUD)
{
ZombieHUD->RemoveFromParent();
ZombieHUD = nullptr;
}
Super::EndPlay(EndPlayReason);
} }
void AEndlessZombieGameMode::RestartLevel() void AEndlessZombieGameMode::RestartLevel()
@ -20,3 +50,16 @@ void AEndlessZombieGameMode::RestartLevel()
FName LevelName = FName(GetWorld()->GetCurrentLevel()->GetName()); FName LevelName = FName(GetWorld()->GetCurrentLevel()->GetName());
UGameplayStatics::OpenLevel(GetWorld(), LevelName); UGameplayStatics::OpenLevel(GetWorld(), LevelName);
} }
void AEndlessZombieGameMode::UpdateLive()
{
iPlayerLive--;
ZombieHUD->UpdateLiveCounter();
}
void AEndlessZombieGameMode::UpdateScore(int iNewScore)
{
iScore += iNewScore;
UE_LOG(LogTemp, Warning, TEXT("[AEndlessZombieGameMode::UpdateScore] %d"), iScore);
ZombieHUD->UpdateScoreCounter(iScore);
}

View File

@ -14,10 +14,25 @@ class AEndlessZombieGameMode : public AGameModeBase
public: public:
AEndlessZombieGameMode(); AEndlessZombieGameMode();
protected:
virtual void BeginPlay() override;
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
public:
void RestartLevel(); void RestartLevel();
UPROPERTY(EditAnywhere, Category = "HUD")
TSubclassOf<class UZombiePlayerHUD> ZombiePlayerHUDClass;
UPROPERTY()
class UZombiePlayerHUD* ZombieHUD;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Life") UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Life")
int iPlayerLife = 3; int iPlayerLive = 3;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Score")
int iScore = 0;
void UpdateLive();
void UpdateScore(int iNewScore);
}; };

View File

@ -0,0 +1,40 @@
// Copyright Jorge Kuijper. All Rights Reserved.
#include "PickupItem.h"
#include "Components/BoxComponent.h"
// Sets default values
APickupItem::APickupItem()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMesh"));
RootComponent = StaticMesh;
CollisionTrigger = CreateDefaultSubobject<UBoxComponent>(TEXT("CollisionTrigger"));
CollisionTrigger->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);
}
// Called when the game starts or when spawned
void APickupItem::BeginPlay()
{
Super::BeginPlay();
CollisionTrigger->OnComponentBeginOverlap.AddDynamic(this, &APickupItem::OnBeginOverlap);
}
void APickupItem::OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
this->Destroy();
}
// Called every frame
void APickupItem::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}

View File

@ -0,0 +1,37 @@
// Copyright Jorge Kuijper. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "PickupItem.generated.h"
class UBoxComponent;
UCLASS()
class ENDLESSZOMBIE_API APickupItem : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
APickupItem();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
UFUNCTION()
virtual void OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UPROPERTY(BlueprintReadWrite, EditAnywhere)
UStaticMeshComponent* StaticMesh;
UPROPERTY(BlueprintReadWrite, EditAnywhere)
UBoxComponent* CollisionTrigger;
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category="Score")
int iPunctuation = 10;
};

View File

@ -54,10 +54,6 @@ 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
@ -71,12 +67,6 @@ 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);
@ -93,17 +83,6 @@ 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)
{ {
@ -179,11 +158,6 @@ void AZombieCharacter::RestartLevel()
} }
} }
//void AZombieCharacter::Crouch(const FInputActionValue& Value)
//{
//
//}
void AZombieCharacter::Die() void AZombieCharacter::Die()
{ {
GetMesh()->SetCollisionProfileName(TEXT("Ragdoll")); GetMesh()->SetCollisionProfileName(TEXT("Ragdoll"));
@ -211,15 +185,13 @@ void AZombieCharacter::ObstacleCollision()
AEndlessZombieGameMode* CurrentGameMode = Cast<AEndlessZombieGameMode>(UGameplayStatics::GetGameMode(GetWorld())); AEndlessZombieGameMode* CurrentGameMode = Cast<AEndlessZombieGameMode>(UGameplayStatics::GetGameMode(GetWorld()));
if (CurrentGameMode) if (CurrentGameMode)
{ {
CurrentGameMode->iPlayerLife--; CurrentGameMode->UpdateLive();
if (CurrentGameMode->iPlayerLife > 0) if (CurrentGameMode->iPlayerLive > 0)
{ {
PlayFlashEffect(); PlayFlashEffect();
ZombieHUD->ModifyLiveCounter();
} }
else else
{ {
ZombieHUD->ModifyLiveCounter();
Die(); Die();
} }
} }

View File

@ -48,8 +48,6 @@ 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;
@ -95,11 +93,6 @@ 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

@ -34,13 +34,18 @@ void UZombiePlayerHUD::NativeConstruct()
} }
CurrentGameMode = Cast<AEndlessZombieGameMode>(UGameplayStatics::GetGameMode(GetWorld())); CurrentGameMode = Cast<AEndlessZombieGameMode>(UGameplayStatics::GetGameMode(GetWorld()));
if (TextScore)
{
TextScore->SetText(FText::AsNumber(CurrentGameMode->iScore));
}
} }
void UZombiePlayerHUD::ModifyLiveCounter() void UZombiePlayerHUD::UpdateLiveCounter()
{ {
if (CurrentGameMode) if (CurrentGameMode)
{ {
switch (CurrentGameMode->iPlayerLife) switch (CurrentGameMode->iPlayerLive)
{ {
case 2: case 2:
EmptyLive(LiveImg03); EmptyLive(LiveImg03);
@ -58,6 +63,14 @@ void UZombiePlayerHUD::ModifyLiveCounter()
} }
void UZombiePlayerHUD::UpdateScoreCounter(int iScore)
{
if (TextScore)
{
TextScore->SetText(FText::AsNumber(CurrentGameMode->iScore));
}
}
void UZombiePlayerHUD::EmptyLive(UImage* LiveImg) void UZombiePlayerHUD::EmptyLive(UImage* LiveImg)
{ {
if ((LiveImg) && (LiveGreyTexture)) if ((LiveImg) && (LiveGreyTexture))

View File

@ -33,10 +33,13 @@ public:
UTexture2D* LiveGreenTexture; UTexture2D* LiveGreenTexture;
UPROPERTY(EditAnywhere) UPROPERTY(EditAnywhere)
UTexture2D* LiveGreyTexture; UTexture2D* LiveGreyTexture;
UPROPERTY(BlueprintReadWrite, meta = (BindWidget))
UTextBlock* TextScore;
AEndlessZombieGameMode* CurrentGameMode; AEndlessZombieGameMode* CurrentGameMode;
void ModifyLiveCounter(); void UpdateLiveCounter();
void UpdateScoreCounter(int iScore);
private: private:
void EmptyLive(UImage* LiveImg); void EmptyLive(UImage* LiveImg);