diff --git a/Content/Blueprints/BP_Brain.uasset b/Content/Blueprints/BP_Brain.uasset new file mode 100644 index 0000000..276565a --- /dev/null +++ b/Content/Blueprints/BP_Brain.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:661b82fe19a217752ac727b025e88d1cfd0361664d130d1a2b74e6e0897319e1 +size 33690 diff --git a/Content/Blueprints/BP_EndlessZombieGameMode.uasset b/Content/Blueprints/BP_EndlessZombieGameMode.uasset index 9aa832f..b87ef4d 100644 --- a/Content/Blueprints/BP_EndlessZombieGameMode.uasset +++ b/Content/Blueprints/BP_EndlessZombieGameMode.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:d3dd1c59c26b87fd1cdc3103d29ce7833d5cbd841a008137c0c956a8af5251f9 -size 21542 +oid sha256:2d1667025d5c63688a775b95adddeaa4bc6a0d6e4b289eea31a0f1dfe3f07741 +size 20912 diff --git a/Content/HUD/WB_ZombiePlayerHUD.uasset b/Content/HUD/WB_ZombiePlayerHUD.uasset index ecb7a22..9a061f0 100644 --- a/Content/HUD/WB_ZombiePlayerHUD.uasset +++ b/Content/HUD/WB_ZombiePlayerHUD.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:7e8104ca75816a6d60ace1e793d5e2822ce9ada1bd7ea52e10c3564325438f9f -size 70602 +oid sha256:d0cca0987c932d092808324fc5f262177a0d94de9327f7f61cc9fe13ea1f782c +size 73711 diff --git a/Content/StaticMeshes/Brain/MI_Brain.uasset b/Content/StaticMeshes/Brain/MI_Brain.uasset new file mode 100644 index 0000000..74311a7 --- /dev/null +++ b/Content/StaticMeshes/Brain/MI_Brain.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b1e6e19c21ec51d15cdea079e2b63319df8fe82bfa7edf879354b49d6440130b +size 7264 diff --git a/Content/StaticMeshes/Brain/M_Brain.uasset b/Content/StaticMeshes/Brain/M_Brain.uasset new file mode 100644 index 0000000..da69c91 --- /dev/null +++ b/Content/StaticMeshes/Brain/M_Brain.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c15011a206f138d1f8f4a91b21ddfb1a352763e3b58e9dc2576c77e693ca7c23 +size 8058 diff --git a/Content/StaticMeshes/Brain/SM_Brain.uasset b/Content/StaticMeshes/Brain/SM_Brain.uasset new file mode 100644 index 0000000..d867bfa --- /dev/null +++ b/Content/StaticMeshes/Brain/SM_Brain.uasset @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0a2d1635317832ab19c1ccbd24f2d03f6d329a870eb1ae7a7917ee50030c0787 +size 48358 diff --git a/Source/EndlessZombie/Brain.cpp b/Source/EndlessZombie/Brain.cpp new file mode 100644 index 0000000..63df515 --- /dev/null +++ b/Source/EndlessZombie/Brain.cpp @@ -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 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(UGameplayStatics::GetGameMode(GetWorld())); + if (CurrentGameMode) + CurrentGameMode->UpdateScore(iPunctuation); +} diff --git a/Source/EndlessZombie/Brain.h b/Source/EndlessZombie/Brain.h new file mode 100644 index 0000000..2a7af0d --- /dev/null +++ b/Source/EndlessZombie/Brain.h @@ -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; +}; diff --git a/Source/EndlessZombie/EndlessZombieGameMode.cpp b/Source/EndlessZombie/EndlessZombieGameMode.cpp index 5fc98f7..780446b 100644 --- a/Source/EndlessZombie/EndlessZombieGameMode.cpp +++ b/Source/EndlessZombie/EndlessZombieGameMode.cpp @@ -1,6 +1,8 @@ // Copyright Epic Games, Inc. All Rights Reserved. #include "EndlessZombieGameMode.h" +#include "ZombiePlayerHUD.h" +#include "Blueprint/UserWidget.h" #include "EndlessZombieCharacter.h" #include "Kismet/GameplayStatics.h" #include "UObject/ConstructorHelpers.h" @@ -13,6 +15,34 @@ AEndlessZombieGameMode::AEndlessZombieGameMode() { DefaultPawnClass = PlayerPawnBPClass.Class; } + + // HUD + ZombiePlayerHUDClass = nullptr; + ZombieHUD = nullptr; +} + +void AEndlessZombieGameMode::BeginPlay() +{ + Super::BeginPlay(); + + if (ZombiePlayerHUDClass) + { + APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0); + ZombieHUD = CreateWidget(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() @@ -20,3 +50,16 @@ void AEndlessZombieGameMode::RestartLevel() FName LevelName = FName(GetWorld()->GetCurrentLevel()->GetName()); 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); +} diff --git a/Source/EndlessZombie/EndlessZombieGameMode.h b/Source/EndlessZombie/EndlessZombieGameMode.h index 18e28cf..c734f19 100644 --- a/Source/EndlessZombie/EndlessZombieGameMode.h +++ b/Source/EndlessZombie/EndlessZombieGameMode.h @@ -14,10 +14,25 @@ class AEndlessZombieGameMode : public AGameModeBase public: AEndlessZombieGameMode(); +protected: + virtual void BeginPlay() override; + virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override; + +public: void RestartLevel(); + UPROPERTY(EditAnywhere, Category = "HUD") + TSubclassOf ZombiePlayerHUDClass; + UPROPERTY() + class UZombiePlayerHUD* ZombieHUD; + 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); }; diff --git a/Source/EndlessZombie/PickupItem.cpp b/Source/EndlessZombie/PickupItem.cpp new file mode 100644 index 0000000..7d21ff4 --- /dev/null +++ b/Source/EndlessZombie/PickupItem.cpp @@ -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(TEXT("StaticMesh")); + RootComponent = StaticMesh; + + CollisionTrigger = CreateDefaultSubobject(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); + +} + diff --git a/Source/EndlessZombie/PickupItem.h b/Source/EndlessZombie/PickupItem.h new file mode 100644 index 0000000..82f9cc3 --- /dev/null +++ b/Source/EndlessZombie/PickupItem.h @@ -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; +}; diff --git a/Source/EndlessZombie/ZombieCharacter.cpp b/Source/EndlessZombie/ZombieCharacter.cpp index 99895e2..c39894f 100644 --- a/Source/EndlessZombie/ZombieCharacter.cpp +++ b/Source/EndlessZombie/ZombieCharacter.cpp @@ -54,10 +54,6 @@ 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 @@ -71,12 +67,6 @@ void AZombieCharacter::BeginPlay() { Subsystem->AddMappingContext(DefaultMappingContext, 0); } - if (ZombiePlayerHUDClass) - { - ZombieHUD = CreateWidget(PlayerController, ZombiePlayerHUDClass); - if (ZombieHUD) - ZombieHUD->AddToPlayerScreen(); - } } 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 void AZombieCharacter::Tick(float DeltaTime) { @@ -179,11 +158,6 @@ void AZombieCharacter::RestartLevel() } } -//void AZombieCharacter::Crouch(const FInputActionValue& Value) -//{ -// -//} - void AZombieCharacter::Die() { GetMesh()->SetCollisionProfileName(TEXT("Ragdoll")); @@ -211,15 +185,13 @@ void AZombieCharacter::ObstacleCollision() AEndlessZombieGameMode* CurrentGameMode = Cast(UGameplayStatics::GetGameMode(GetWorld())); if (CurrentGameMode) { - CurrentGameMode->iPlayerLife--; - if (CurrentGameMode->iPlayerLife > 0) + CurrentGameMode->UpdateLive(); + if (CurrentGameMode->iPlayerLive > 0) { PlayFlashEffect(); - ZombieHUD->ModifyLiveCounter(); } else { - ZombieHUD->ModifyLiveCounter(); Die(); } } diff --git a/Source/EndlessZombie/ZombieCharacter.h b/Source/EndlessZombie/ZombieCharacter.h index ea72fa9..857ebb4 100644 --- a/Source/EndlessZombie/ZombieCharacter.h +++ b/Source/EndlessZombie/ZombieCharacter.h @@ -48,8 +48,6 @@ 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; @@ -95,11 +93,6 @@ public: float fTimelineValue; FTimeline FlashTimeline; - UPROPERTY(EditAnywhere, Category = "HUD") - TSubclassOf ZombiePlayerHUDClass; - UPROPERTY() - class UZombiePlayerHUD* ZombieHUD; - private: void MoveForwardConstant(float DeltaTime); diff --git a/Source/EndlessZombie/ZombiePlayerHUD.cpp b/Source/EndlessZombie/ZombiePlayerHUD.cpp index 64bfa8e..bf73c06 100644 --- a/Source/EndlessZombie/ZombiePlayerHUD.cpp +++ b/Source/EndlessZombie/ZombiePlayerHUD.cpp @@ -34,13 +34,18 @@ void UZombiePlayerHUD::NativeConstruct() } CurrentGameMode = Cast(UGameplayStatics::GetGameMode(GetWorld())); + + if (TextScore) + { + TextScore->SetText(FText::AsNumber(CurrentGameMode->iScore)); + } } -void UZombiePlayerHUD::ModifyLiveCounter() +void UZombiePlayerHUD::UpdateLiveCounter() { if (CurrentGameMode) { - switch (CurrentGameMode->iPlayerLife) + switch (CurrentGameMode->iPlayerLive) { case 2: EmptyLive(LiveImg03); @@ -55,7 +60,15 @@ void UZombiePlayerHUD::ModifyLiveCounter() break; } } - + +} + +void UZombiePlayerHUD::UpdateScoreCounter(int iScore) +{ + if (TextScore) + { + TextScore->SetText(FText::AsNumber(CurrentGameMode->iScore)); + } } void UZombiePlayerHUD::EmptyLive(UImage* LiveImg) diff --git a/Source/EndlessZombie/ZombiePlayerHUD.h b/Source/EndlessZombie/ZombiePlayerHUD.h index 85631b1..48fedbb 100644 --- a/Source/EndlessZombie/ZombiePlayerHUD.h +++ b/Source/EndlessZombie/ZombiePlayerHUD.h @@ -33,10 +33,13 @@ public: UTexture2D* LiveGreenTexture; UPROPERTY(EditAnywhere) UTexture2D* LiveGreyTexture; + UPROPERTY(BlueprintReadWrite, meta = (BindWidget)) + UTextBlock* TextScore; AEndlessZombieGameMode* CurrentGameMode; - void ModifyLiveCounter(); + void UpdateLiveCounter(); + void UpdateScoreCounter(int iScore); private: void EmptyLive(UImage* LiveImg);