From 64a21df05927c5480b312e77860880584e0271cd Mon Sep 17 00:00:00 2001 From: baz Date: Fri, 4 Apr 2025 22:08:11 +0100 Subject: [PATCH] Switch to Timeline for game Timer --- Content/Gamemode/BP_DefaultGamemode.uasset | 4 +-- Source/vampires/Interfaces/Playerable.cpp | 7 ++++++ Source/vampires/Interfaces/Playerable.h | 28 +++++++++++++++++++++ Source/vampires/VampireGameMode.cpp | 19 ++++++++++++-- Source/vampires/VampireGameMode.h | 8 ++++++ Source/vampires/VampirePlayerController.cpp | 14 ++++++++--- Source/vampires/VampirePlayerController.h | 9 ++++--- Source/vampires/Widgets/HUDWidget.cpp | 12 ++++----- Source/vampires/Widgets/HUDWidget.h | 2 +- 9 files changed, 85 insertions(+), 18 deletions(-) create mode 100644 Source/vampires/Interfaces/Playerable.cpp create mode 100644 Source/vampires/Interfaces/Playerable.h diff --git a/Content/Gamemode/BP_DefaultGamemode.uasset b/Content/Gamemode/BP_DefaultGamemode.uasset index 61822c0..c40855a 100644 --- a/Content/Gamemode/BP_DefaultGamemode.uasset +++ b/Content/Gamemode/BP_DefaultGamemode.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:687c80b754c3098ddedbf2fec50a312a28c7f3b1813ba52f5ba7a2773e330da4 -size 21959 +oid sha256:dcd3da4cffc3cc99be9dd5e9630ea63788acaa8d09a18318dfd048696a14bcae +size 45397 diff --git a/Source/vampires/Interfaces/Playerable.cpp b/Source/vampires/Interfaces/Playerable.cpp new file mode 100644 index 0000000..abbce2e --- /dev/null +++ b/Source/vampires/Interfaces/Playerable.cpp @@ -0,0 +1,7 @@ +// Louis Hobbs | 2024-2025 + + +#include "Playerable.h" + + +// Add default functionality here for any IPlayerable functions that are not pure virtual. diff --git a/Source/vampires/Interfaces/Playerable.h b/Source/vampires/Interfaces/Playerable.h new file mode 100644 index 0000000..ea3764d --- /dev/null +++ b/Source/vampires/Interfaces/Playerable.h @@ -0,0 +1,28 @@ +// Louis Hobbs | 2024-2025 + +#pragma once + +#include "CoreMinimal.h" +#include "UObject/Interface.h" +#include "Playerable.generated.h" + +// This class does not need to be modified. +UINTERFACE(Blueprintable) +class UPlayerable : public UInterface +{ + GENERATED_BODY() +}; + +/** + * + */ +class VAMPIRES_API IPlayerable +{ + GENERATED_BODY() + + // Add interface functions to this class. This is the class that will be inherited to implement this interface. +public: + + UFUNCTION(BlueprintCallable, BlueprintNativeEvent) + void UpdateTimerHUDElement(float deltaTime); +}; diff --git a/Source/vampires/VampireGameMode.cpp b/Source/vampires/VampireGameMode.cpp index bd04987..86cda28 100644 --- a/Source/vampires/VampireGameMode.cpp +++ b/Source/vampires/VampireGameMode.cpp @@ -76,9 +76,9 @@ void AVampireGameMode::SpawnEnemy() if (AActor* enemy = GetEnemyObjectPoolManager_Implementation()->GetObject()) { - if (UKismetSystemLibrary::DoesImplementInterface(enemy, UEnemyable::StaticClass()) && EnemyDataAssets.Num() > 0) + if (UKismetSystemLibrary::DoesImplementInterface(enemy, UEnemyable::StaticClass()) && SpawnableEnemyDataAssets.Num() > 0) { - IEnemyable::Execute_LoadDataFromDataAsset(enemy, EnemyDataAssets[FMath::RandRange(0, EnemyDataAssets.Num() - 1)]); + IEnemyable::Execute_LoadDataFromDataAsset(enemy, SpawnableEnemyDataAssets[FMath::RandRange(0, SpawnableEnemyDataAssets.Num() - 1)]); SpawnLocation.Z = PlayerCharacter->GetActorLocation().Z; FTransform Transform; @@ -139,6 +139,21 @@ AObjectPoolManager* AVampireGameMode::GetPickupObjectPoolManager_Implementation( return PickupObjectPoolManager; } +void AVampireGameMode::AddRandomEnemyTypeToPool() +{ + if (EnemyDataAssets.Num() > 0) + { + int32 rand = FMath::RandRange(0, EnemyDataAssets.Num() - 1); + SpawnableEnemyDataAssets.Add(EnemyDataAssets[rand]); + EnemyDataAssets.RemoveAt(rand); + } +} + +void AVampireGameMode::EndGame() +{ + UKismetSystemLibrary::QuitGame(GetWorld(), UGameplayStatics::GetPlayerController(GetWorld(), 0), EQuitPreference::Quit, true); +} + void AVampireGameMode::IncrementEnemyDeathCount() { EnemyDeathCount++; diff --git a/Source/vampires/VampireGameMode.h b/Source/vampires/VampireGameMode.h index 3ae1237..aab8eb2 100644 --- a/Source/vampires/VampireGameMode.h +++ b/Source/vampires/VampireGameMode.h @@ -52,6 +52,8 @@ private: TObjectPtr ProjectileObjectPoolManager = nullptr; TObjectPtr PickupObjectPoolManager = nullptr; + + TArray> SpawnableEnemyDataAssets; protected: virtual void BeginPlay() override; @@ -72,6 +74,12 @@ public: virtual AObjectPoolManager* GetPickupObjectPoolManager_Implementation() override; + UFUNCTION(BlueprintCallable) + void AddRandomEnemyTypeToPool(); + + UFUNCTION(BlueprintCallable) + void EndGame(); + protected: UFUNCTION() void SpawnEnemy(); diff --git a/Source/vampires/VampirePlayerController.cpp b/Source/vampires/VampirePlayerController.cpp index c477d94..f814d8b 100644 --- a/Source/vampires/VampirePlayerController.cpp +++ b/Source/vampires/VampirePlayerController.cpp @@ -52,8 +52,6 @@ void AVampirePlayerController::OnPossess(APawn* aPawn) gamemode->OnEnemyDeathCountIncrementDelegate.AddDynamic(this, &AVampirePlayerController::UpdateKillCountHUD); UpdateKillCountHUD(gamemode->GetEnemyDeathCount()); } - - GetWorld()->GetTimerManager().SetTimer(pawnLifeTimeHandle, this, &AVampirePlayerController::UpdateTimerHUD, 1.0f, true,0.f); if (currentPlayerHUD) { @@ -163,11 +161,11 @@ void AVampirePlayerController::UpdatePlayerLevelHUD(int level) } } -void AVampirePlayerController::UpdateTimerHUD() +void AVampirePlayerController::UpdateTimerHUD(float deltaTime) { if (currentPlayerHUD) { - currentPlayerHUD->UpdateTimerBlock(GetPawn()); + currentPlayerHUD->UpdateTimerBlock(deltaTime); } } @@ -186,3 +184,11 @@ void AVampirePlayerController::UpdateGoldCountHUD(int goldCount) currentPlayerHUD->UpdateGoldBlock(goldCount); } } + +void AVampirePlayerController::UpdateTimerHUDElement_Implementation(float deltaTime) +{ + if (currentPlayerHUD) + { + currentPlayerHUD->UpdateTimerBlock(deltaTime); + } +} diff --git a/Source/vampires/VampirePlayerController.h b/Source/vampires/VampirePlayerController.h index 95f0525..d192b20 100644 --- a/Source/vampires/VampirePlayerController.h +++ b/Source/vampires/VampirePlayerController.h @@ -4,6 +4,7 @@ #include "CoreMinimal.h" #include "GameFramework/PlayerController.h" +#include "Interfaces/Playerable.h" #include "VampirePlayerController.generated.h" class ULevelUpWidget; @@ -15,7 +16,7 @@ class UHUDWidget; * */ UCLASS(Abstract) -class VAMPIRES_API AVampirePlayerController : public APlayerController +class VAMPIRES_API AVampirePlayerController : public APlayerController, public IPlayerable { GENERATED_BODY() @@ -69,12 +70,14 @@ protected: UFUNCTION() void UpdatePlayerLevelHUD(int level); - UFUNCTION() - void UpdateTimerHUD(); + UFUNCTION(BlueprintCallable) + void UpdateTimerHUD(float deltaTime); UFUNCTION() void UpdateKillCountHUD(int killCount); UFUNCTION() void UpdateGoldCountHUD(int goldCount); + + virtual void UpdateTimerHUDElement_Implementation(float deltaTime) override; }; diff --git a/Source/vampires/Widgets/HUDWidget.cpp b/Source/vampires/Widgets/HUDWidget.cpp index 2ba0b4b..6cb3836 100644 --- a/Source/vampires/Widgets/HUDWidget.cpp +++ b/Source/vampires/Widgets/HUDWidget.cpp @@ -19,18 +19,18 @@ void UHUDWidget::UpdateLevelBlock(int level) LevelBlock->SetText(FText::FromString("LV" + FString::FromInt(level))); } -void UHUDWidget::UpdateTimerBlock(APawn* pawn) +void UHUDWidget::UpdateTimerBlock(float deltaTime) { - int timeSinceCreation = FMath::FloorToInt(pawn->GetGameTimeSinceCreation()); + int timeSinceStart = FMath::FloorToInt(deltaTime); - FString mins = FString::FromInt(timeSinceCreation / 60); - if (timeSinceCreation / 60 < 10) + FString mins = FString::FromInt(timeSinceStart / 60); + if (timeSinceStart / 60 < 10) { mins = "0" + mins; } - FString secs = FString::FromInt(timeSinceCreation % 60); - if (timeSinceCreation % 60 < 10) + FString secs = FString::FromInt(timeSinceStart % 60); + if (timeSinceStart % 60 < 10) { secs = "0" + secs; } diff --git a/Source/vampires/Widgets/HUDWidget.h b/Source/vampires/Widgets/HUDWidget.h index 8e14fc9..afe63f6 100644 --- a/Source/vampires/Widgets/HUDWidget.h +++ b/Source/vampires/Widgets/HUDWidget.h @@ -42,7 +42,7 @@ public: void UpdateLevelBlock(int level); UFUNCTION() - void UpdateTimerBlock(APawn* pawn); + void UpdateTimerBlock(float deltaTime); UFUNCTION() void UpdateKillBlock(int killCount);