From 366dbbfda897ac75cccded5599b7a6e21bd7075b Mon Sep 17 00:00:00 2001 From: baz Date: Sun, 17 Nov 2024 20:02:59 +0000 Subject: [PATCH] Move updates from Bindings to Events --- Content/Widgets/HUD/BP_HUDWidget.uasset | 4 +-- Source/vampires/EnemyCharacter.cpp | 12 +++------ Source/vampires/EnemyCharacter.h | 5 ++-- Source/vampires/HealthComponent.cpp | 4 +-- Source/vampires/HealthComponent.h | 4 +-- Source/vampires/VampireAIController.cpp | 6 ++--- Source/vampires/VampireGameMode.cpp | 20 ++++++++++++++- Source/vampires/VampireGameMode.h | 20 +++++++++------ Source/vampires/VampirePlayerController.cpp | 27 +++++++++++++++++++++ Source/vampires/VampirePlayerController.h | 6 +++++ Source/vampires/Widgets/HUDWidget.cpp | 10 ++++++-- Source/vampires/Widgets/HUDWidget.h | 8 +++++- Source/vampires/Widgets/HealthbarWidget.cpp | 6 ++--- Source/vampires/Widgets/HealthbarWidget.h | 3 ++- 14 files changed, 101 insertions(+), 34 deletions(-) diff --git a/Content/Widgets/HUD/BP_HUDWidget.uasset b/Content/Widgets/HUD/BP_HUDWidget.uasset index 1c881b1..3715442 100644 --- a/Content/Widgets/HUD/BP_HUDWidget.uasset +++ b/Content/Widgets/HUD/BP_HUDWidget.uasset @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:26c446db9c13f15d492cff4807e3319e2a07686b73310833dd28b3b8f5e75579 -size 154070 +oid sha256:fb07fd9c1fb2ebf0469bafa5bd0d1d8258b2b6f3ea85e18f9bb576d93190b300 +size 111070 diff --git a/Source/vampires/EnemyCharacter.cpp b/Source/vampires/EnemyCharacter.cpp index bbf0981..84b0902 100644 --- a/Source/vampires/EnemyCharacter.cpp +++ b/Source/vampires/EnemyCharacter.cpp @@ -18,8 +18,8 @@ AEnemyCharacter::AEnemyCharacter(const FObjectInitializer& ObjectInitializer) void AEnemyCharacter::BeginPlay() { Super::BeginPlay(); - GetHealthComponent()->OnDamaged.BindUFunction(this, "OnDamaged"); - GetHealthComponent()->OnDeath.BindUFunction(this, "OnDeath"); + GetHealthComponent()->OnDamaged.AddDynamic(this, &AEnemyCharacter::OnDamaged); + GetHealthComponent()->OnDeath.AddDynamic(this, &AEnemyCharacter::OnDeath); ObjectPoolComponent->OnRetrieve.BindUFunction(this, "ResetHealth"); } @@ -34,11 +34,11 @@ UBehaviorTree* AEnemyCharacter::GetBehaviorTree() return BehaviorTree; } -void AEnemyCharacter::OnDamaged() +void AEnemyCharacter::OnDamaged(FDamageInfo damageInfo) { } -void AEnemyCharacter::OnDeath() +void AEnemyCharacter::OnDeath(FDamageInfo damageInfo) { FActorSpawnParameters actorSpawnParameters; actorSpawnParameters.Owner = this; @@ -47,10 +47,6 @@ void AEnemyCharacter::OnDeath() GetWorld()->SpawnActor(EXPPickupTemplate, GetActorLocation(), FRotator::ZeroRotator, actorSpawnParameters); - - AVampireGameMode* gamemode = Cast(UGameplayStatics::GetGameMode(GetWorld())); - gamemode->IncrementEnemyDeathCount(); - gamemode->GetEnemyObjectPoolManager()->ReturnObject(this); } void AEnemyCharacter::ResetHealth() diff --git a/Source/vampires/EnemyCharacter.h b/Source/vampires/EnemyCharacter.h index 55b72ab..c01c0ef 100644 --- a/Source/vampires/EnemyCharacter.h +++ b/Source/vampires/EnemyCharacter.h @@ -3,6 +3,7 @@ #pragma once #include "CoreMinimal.h" +#include "HealthComponent.h" #include "VampireCharacter.h" #include "EnemyCharacter.generated.h" @@ -39,10 +40,10 @@ public: UBehaviorTree* GetBehaviorTree(); UFUNCTION() - virtual void OnDamaged(); + virtual void OnDamaged(FDamageInfo damageInfo); UFUNCTION() - virtual void OnDeath(); + virtual void OnDeath(FDamageInfo damageInfo); private: UFUNCTION() diff --git a/Source/vampires/HealthComponent.cpp b/Source/vampires/HealthComponent.cpp index e916f81..116c8e2 100644 --- a/Source/vampires/HealthComponent.cpp +++ b/Source/vampires/HealthComponent.cpp @@ -23,12 +23,12 @@ void UHealthComponent::TakeDamage(AActor* damagedActor, float damage, const UDam CurrentHealth -= damage; - OnDamaged.ExecuteIfBound({damagedActor, damage, damageType, instigatedBy, damageCauser}); + OnDamaged.Broadcast({damagedActor, damage, damageType, instigatedBy, damageCauser}); if (CurrentHealth <= 0.0f) { IsDead = true; - OnDeath.ExecuteIfBound({damagedActor, damage, damageType, instigatedBy, damageCauser}); + OnDeath.Broadcast({damagedActor, damage, damageType, instigatedBy, damageCauser}); } } diff --git a/Source/vampires/HealthComponent.h b/Source/vampires/HealthComponent.h index ada866d..abd0329 100644 --- a/Source/vampires/HealthComponent.h +++ b/Source/vampires/HealthComponent.h @@ -27,8 +27,8 @@ struct FDamageInfo AActor* DamageCauser; }; -DECLARE_DELEGATE_OneParam(FOnDamageDelegate, FDamageInfo) -DECLARE_DELEGATE_OneParam(FOnDeathDelegate, FDamageInfo) +DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnDamageDelegate, FDamageInfo, damageInfo); +DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnDeathDelegate, FDamageInfo, damageInfo); UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent)) class VAMPIRES_API UHealthComponent : public UActorComponent diff --git a/Source/vampires/VampireAIController.cpp b/Source/vampires/VampireAIController.cpp index 6e9ef60..baf4afe 100644 --- a/Source/vampires/VampireAIController.cpp +++ b/Source/vampires/VampireAIController.cpp @@ -51,8 +51,8 @@ void AVampireAIController::OnPossess(APawn* InPawn) EnemyCharacter = Cast(InPawn); check(EnemyCharacter); EnemyCharacter->bUseControllerRotationYaw = false; - EnemyCharacter->GetHealthComponent()->OnDamaged.BindUFunction(this, "OnDamaged"); - EnemyCharacter->GetHealthComponent()->OnDeath.BindUFunction(this, "OnDeath"); + EnemyCharacter->GetHealthComponent()->OnDamaged.AddDynamic(this, &AVampireAIController::OnDamaged); + EnemyCharacter->GetHealthComponent()->OnDeath.AddDynamic(this, &AVampireAIController::OnDeath); if (UBehaviorTree* bt = EnemyCharacter->GetBehaviorTree()) { @@ -73,7 +73,7 @@ void AVampireAIController::OnDamaged(FDamageInfo info) void AVampireAIController::OnDeath(FDamageInfo info) { // TODO: Do stuff here - EnemyCharacter->OnDeath(); + EnemyCharacter->OnDeath(info); /*EnemyCharacter->DetachFromControllerPendingDestroy(); EnemyCharacter->GetCapsuleComponent()->SetCollisionEnabled(ECollisionEnabled::NoCollision); EnemyCharacter->GetCapsuleComponent()->SetCollisionResponseToAllChannels(ECR_Ignore); diff --git a/Source/vampires/VampireGameMode.cpp b/Source/vampires/VampireGameMode.cpp index 443c660..3e28a09 100644 --- a/Source/vampires/VampireGameMode.cpp +++ b/Source/vampires/VampireGameMode.cpp @@ -4,6 +4,7 @@ #include "VampireGameMode.h" #include "EnemyCharacter.h" +#include "HealthComponent.h" #include "ObjectPoolManager.h" #include "PlayerCharacter.h" #include "VampirePlayerController.h" @@ -25,6 +26,13 @@ int AVampireGameMode::GetEnemyDeathCount() return EnemyDeathCount; } +void AVampireGameMode::HandleOnEnemyDeath(FDamageInfo damageInfo) +{ + IncrementEnemyDeathCount(); + EnemyObjectPoolManager->ReturnObject(damageInfo.DamagedActor); + OnEnemyDeathCountIncrementDelegate.Broadcast(EnemyDeathCount); +} + void AVampireGameMode::SpawnEnemy() { FVector TopLeft, TopLeftDir; @@ -78,7 +86,17 @@ void AVampireGameMode::SpawnEnemy() Direction.Normalize(); Direction *= CapsuleRadius; Actor->SetActorLocation(SpawnLocation + Direction); - Actor->SpawnDefaultController(); + + if (!Actor->Controller) + { + Actor->SpawnDefaultController(); + } + + if (!Actor->GetHealthComponent()->OnDeath.IsAlreadyBound(this, &AVampireGameMode::HandleOnEnemyDeath)) + { + Actor->GetHealthComponent()->OnDeath.AddDynamic(this, &AVampireGameMode::HandleOnEnemyDeath); + } + } } diff --git a/Source/vampires/VampireGameMode.h b/Source/vampires/VampireGameMode.h index a1a3289..c2aab5b 100644 --- a/Source/vampires/VampireGameMode.h +++ b/Source/vampires/VampireGameMode.h @@ -3,6 +3,7 @@ #pragma once #include "CoreMinimal.h" +#include "HealthComponent.h" #include "GameFramework/GameMode.h" #include "VampireGameMode.generated.h" @@ -10,9 +11,9 @@ class AObjectPoolManager; class AVampirePlayerController; class APlayerCharacter; class AEnemyCharacter; -/** - * - */ + +DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnEnemyDeathCountIncrementDelegate, int, level); + UCLASS() class VAMPIRES_API AVampireGameMode : public AGameMode { @@ -22,16 +23,18 @@ public: UPROPERTY(EditDefaultsOnly) TSubclassOf EnemyTemplate; -private: - APlayerCharacter* PlayerCharacter; + FOnEnemyDeathCountIncrementDelegate OnEnemyDeathCountIncrementDelegate; - AVampirePlayerController* PlayerController; +private: + TObjectPtr PlayerCharacter; + + TObjectPtr PlayerController; FTimerHandle SpawnEnemyTimerDelegate; int EnemyDeathCount = 0; - AObjectPoolManager* EnemyObjectPoolManager = nullptr; + TObjectPtr EnemyObjectPoolManager = nullptr; protected: virtual void BeginPlay() override; @@ -40,6 +43,9 @@ public: UFUNCTION(BlueprintCallable, BlueprintPure) int GetEnemyDeathCount(); + UFUNCTION() + void HandleOnEnemyDeath(FDamageInfo damageInfo); + UFUNCTION() void IncrementEnemyDeathCount(); diff --git a/Source/vampires/VampirePlayerController.cpp b/Source/vampires/VampirePlayerController.cpp index be47cc3..a500880 100644 --- a/Source/vampires/VampirePlayerController.cpp +++ b/Source/vampires/VampirePlayerController.cpp @@ -5,7 +5,9 @@ #include "EXPComponent.h" #include "HealthComponent.h" +#include "VampireGameMode.h" #include "Blueprint/UserWidget.h" +#include "Kismet/GameplayStatics.h" #include "Widgets/HUDWidget.h" void AVampirePlayerController::OnPossess(APawn* aPawn) @@ -19,7 +21,16 @@ void AVampirePlayerController::OnPossess(APawn* aPawn) if (UEXPComponent* expComponent = aPawn->GetComponentByClass()) { expComponent->OnEXPGained.AddUniqueDynamic(this, &AVampirePlayerController::UpdatePlayerEXPHUD); + expComponent->OnEXPLevelUp.AddUniqueDynamic(this, &AVampirePlayerController::UpdatePlayerLevelHUD); UpdatePlayerEXPHUD(expComponent->GetCurrentEXP(), expComponent->GetCurrentLevelPercent()); + UpdatePlayerLevelHUD(expComponent->GetCurrentLevel()); + } + + AVampireGameMode* gamemode = Cast(UGameplayStatics::GetGameMode(GetWorld())); + if (gamemode) + { + gamemode->OnEnemyDeathCountIncrementDelegate.AddDynamic(this, &AVampirePlayerController::UpdateKillCountHUD); + UpdateKillCountHUD(gamemode->GetEnemyDeathCount()); } if (currentPlayerHUD) @@ -36,3 +47,19 @@ void AVampirePlayerController::UpdatePlayerEXPHUD(int exp, float currentLevelPer currentPlayerHUD->UpdateEXPBar(currentLevelPercent); } } + +void AVampirePlayerController::UpdatePlayerLevelHUD(int level) +{ + if (currentPlayerHUD) + { + currentPlayerHUD->UpdateLevelBlock(level); + } +} + +void AVampirePlayerController::UpdateKillCountHUD(int killCount) +{ + if (currentPlayerHUD) + { + currentPlayerHUD->UpdateKillBlock(killCount); + } +} diff --git a/Source/vampires/VampirePlayerController.h b/Source/vampires/VampirePlayerController.h index 41a0614..bafc124 100644 --- a/Source/vampires/VampirePlayerController.h +++ b/Source/vampires/VampirePlayerController.h @@ -29,4 +29,10 @@ protected: UFUNCTION() void UpdatePlayerEXPHUD(int exp, float currentLevelPercent); + + UFUNCTION() + void UpdatePlayerLevelHUD(int level); + + UFUNCTION() + void UpdateKillCountHUD(int killCount); }; diff --git a/Source/vampires/Widgets/HUDWidget.cpp b/Source/vampires/Widgets/HUDWidget.cpp index 25dd251..85a4516 100644 --- a/Source/vampires/Widgets/HUDWidget.cpp +++ b/Source/vampires/Widgets/HUDWidget.cpp @@ -2,8 +2,8 @@ #include "HUDWidget.h" - #include "Components/ProgressBar.h" +#include "Components/TextBlock.h" void UHUDWidget::Init() { @@ -14,10 +14,16 @@ void UHUDWidget::UpdateEXPBar(float currentLevelPercent) EXPbar->SetPercent(currentLevelPercent); } -void UHUDWidget::UpdateLevelBlock() +void UHUDWidget::UpdateLevelBlock(int level) { + LevelBlock->SetText(FText::FromString("LV" + FString::FromInt(level))); } void UHUDWidget::UpdateTimerBlock() { } + +void UHUDWidget::UpdateKillBlock(int killCount) +{ + KillBLock->SetText(FText::FromString("Kills: " + FString::FromInt(killCount))); +} diff --git a/Source/vampires/Widgets/HUDWidget.h b/Source/vampires/Widgets/HUDWidget.h index cc515c2..290f9f2 100644 --- a/Source/vampires/Widgets/HUDWidget.h +++ b/Source/vampires/Widgets/HUDWidget.h @@ -26,6 +26,9 @@ public: UPROPERTY(BlueprintReadWrite, meta = (BindWidget)) UTextBlock* TimerBLock; + + UPROPERTY(BlueprintReadWrite, meta = (BindWidget)) + UTextBlock* KillBLock; void Init(); @@ -33,9 +36,12 @@ public: void UpdateEXPBar(float currentLevelPercent); UFUNCTION() - void UpdateLevelBlock(); + void UpdateLevelBlock(int level); UFUNCTION() void UpdateTimerBlock(); + + UFUNCTION() + void UpdateKillBlock(int killCount); }; diff --git a/Source/vampires/Widgets/HealthbarWidget.cpp b/Source/vampires/Widgets/HealthbarWidget.cpp index 59e6210..6bb13a5 100644 --- a/Source/vampires/Widgets/HealthbarWidget.cpp +++ b/Source/vampires/Widgets/HealthbarWidget.cpp @@ -14,11 +14,11 @@ void UHealthbarWidget::NativeConstruct() Super::NativeConstruct(); APlayerCharacter* player = Cast(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0)); UHealthComponent* healthComponent = player->GetHealthComponent(); - healthComponent->OnDamaged.BindUFunction(this, "UpdateHealthBar"); - UpdateHealthBar(); + healthComponent->OnDamaged.AddDynamic(this, &UHealthbarWidget::UpdateHealthBar); + UpdateHealthBar({}); } -void UHealthbarWidget::UpdateHealthBar() +void UHealthbarWidget::UpdateHealthBar(FDamageInfo damageInfo) { APlayerCharacter* player = Cast(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0)); float percent = player->GetHealthComponent()->GetCurrentHealth() / player->GetHealthComponent()->GetMaxHealth(); diff --git a/Source/vampires/Widgets/HealthbarWidget.h b/Source/vampires/Widgets/HealthbarWidget.h index a74edf5..1a4a779 100644 --- a/Source/vampires/Widgets/HealthbarWidget.h +++ b/Source/vampires/Widgets/HealthbarWidget.h @@ -4,6 +4,7 @@ #include "CoreMinimal.h" #include "Blueprint/UserWidget.h" +#include "vampires/HealthComponent.h" #include "HealthbarWidget.generated.h" class UProgressBar; @@ -23,5 +24,5 @@ public: private: UFUNCTION() - void UpdateHealthBar(); + void UpdateHealthBar(FDamageInfo damageInfo); };