Move updates from Bindings to Events

This commit is contained in:
baz 2024-11-17 20:02:59 +00:00
parent 4386f0e383
commit 366dbbfda8
14 changed files with 101 additions and 34 deletions

BIN
Content/Widgets/HUD/BP_HUDWidget.uasset (Stored with Git LFS)

Binary file not shown.

View File

@ -18,8 +18,8 @@ AEnemyCharacter::AEnemyCharacter(const FObjectInitializer& ObjectInitializer)
void AEnemyCharacter::BeginPlay() void AEnemyCharacter::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
GetHealthComponent()->OnDamaged.BindUFunction(this, "OnDamaged"); GetHealthComponent()->OnDamaged.AddDynamic(this, &AEnemyCharacter::OnDamaged);
GetHealthComponent()->OnDeath.BindUFunction(this, "OnDeath"); GetHealthComponent()->OnDeath.AddDynamic(this, &AEnemyCharacter::OnDeath);
ObjectPoolComponent->OnRetrieve.BindUFunction(this, "ResetHealth"); ObjectPoolComponent->OnRetrieve.BindUFunction(this, "ResetHealth");
} }
@ -34,11 +34,11 @@ UBehaviorTree* AEnemyCharacter::GetBehaviorTree()
return BehaviorTree; return BehaviorTree;
} }
void AEnemyCharacter::OnDamaged() void AEnemyCharacter::OnDamaged(FDamageInfo damageInfo)
{ {
} }
void AEnemyCharacter::OnDeath() void AEnemyCharacter::OnDeath(FDamageInfo damageInfo)
{ {
FActorSpawnParameters actorSpawnParameters; FActorSpawnParameters actorSpawnParameters;
actorSpawnParameters.Owner = this; actorSpawnParameters.Owner = this;
@ -47,10 +47,6 @@ void AEnemyCharacter::OnDeath()
GetWorld()->SpawnActor<AEXPPickup>(EXPPickupTemplate, GetActorLocation(), FRotator::ZeroRotator, GetWorld()->SpawnActor<AEXPPickup>(EXPPickupTemplate, GetActorLocation(), FRotator::ZeroRotator,
actorSpawnParameters); actorSpawnParameters);
AVampireGameMode* gamemode = Cast<AVampireGameMode>(UGameplayStatics::GetGameMode(GetWorld()));
gamemode->IncrementEnemyDeathCount();
gamemode->GetEnemyObjectPoolManager()->ReturnObject(this);
} }
void AEnemyCharacter::ResetHealth() void AEnemyCharacter::ResetHealth()

View File

@ -3,6 +3,7 @@
#pragma once #pragma once
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "HealthComponent.h"
#include "VampireCharacter.h" #include "VampireCharacter.h"
#include "EnemyCharacter.generated.h" #include "EnemyCharacter.generated.h"
@ -39,10 +40,10 @@ public:
UBehaviorTree* GetBehaviorTree(); UBehaviorTree* GetBehaviorTree();
UFUNCTION() UFUNCTION()
virtual void OnDamaged(); virtual void OnDamaged(FDamageInfo damageInfo);
UFUNCTION() UFUNCTION()
virtual void OnDeath(); virtual void OnDeath(FDamageInfo damageInfo);
private: private:
UFUNCTION() UFUNCTION()

View File

@ -23,12 +23,12 @@ void UHealthComponent::TakeDamage(AActor* damagedActor, float damage, const UDam
CurrentHealth -= damage; CurrentHealth -= damage;
OnDamaged.ExecuteIfBound({damagedActor, damage, damageType, instigatedBy, damageCauser}); OnDamaged.Broadcast({damagedActor, damage, damageType, instigatedBy, damageCauser});
if (CurrentHealth <= 0.0f) if (CurrentHealth <= 0.0f)
{ {
IsDead = true; IsDead = true;
OnDeath.ExecuteIfBound({damagedActor, damage, damageType, instigatedBy, damageCauser}); OnDeath.Broadcast({damagedActor, damage, damageType, instigatedBy, damageCauser});
} }
} }

View File

@ -27,8 +27,8 @@ struct FDamageInfo
AActor* DamageCauser; AActor* DamageCauser;
}; };
DECLARE_DELEGATE_OneParam(FOnDamageDelegate, FDamageInfo) DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnDamageDelegate, FDamageInfo, damageInfo);
DECLARE_DELEGATE_OneParam(FOnDeathDelegate, FDamageInfo) DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnDeathDelegate, FDamageInfo, damageInfo);
UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent)) UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class VAMPIRES_API UHealthComponent : public UActorComponent class VAMPIRES_API UHealthComponent : public UActorComponent

View File

@ -51,8 +51,8 @@ void AVampireAIController::OnPossess(APawn* InPawn)
EnemyCharacter = Cast<AEnemyCharacter>(InPawn); EnemyCharacter = Cast<AEnemyCharacter>(InPawn);
check(EnemyCharacter); check(EnemyCharacter);
EnemyCharacter->bUseControllerRotationYaw = false; EnemyCharacter->bUseControllerRotationYaw = false;
EnemyCharacter->GetHealthComponent()->OnDamaged.BindUFunction(this, "OnDamaged"); EnemyCharacter->GetHealthComponent()->OnDamaged.AddDynamic(this, &AVampireAIController::OnDamaged);
EnemyCharacter->GetHealthComponent()->OnDeath.BindUFunction(this, "OnDeath"); EnemyCharacter->GetHealthComponent()->OnDeath.AddDynamic(this, &AVampireAIController::OnDeath);
if (UBehaviorTree* bt = EnemyCharacter->GetBehaviorTree()) if (UBehaviorTree* bt = EnemyCharacter->GetBehaviorTree())
{ {
@ -73,7 +73,7 @@ void AVampireAIController::OnDamaged(FDamageInfo info)
void AVampireAIController::OnDeath(FDamageInfo info) void AVampireAIController::OnDeath(FDamageInfo info)
{ {
// TODO: Do stuff here // TODO: Do stuff here
EnemyCharacter->OnDeath(); EnemyCharacter->OnDeath(info);
/*EnemyCharacter->DetachFromControllerPendingDestroy(); /*EnemyCharacter->DetachFromControllerPendingDestroy();
EnemyCharacter->GetCapsuleComponent()->SetCollisionEnabled(ECollisionEnabled::NoCollision); EnemyCharacter->GetCapsuleComponent()->SetCollisionEnabled(ECollisionEnabled::NoCollision);
EnemyCharacter->GetCapsuleComponent()->SetCollisionResponseToAllChannels(ECR_Ignore); EnemyCharacter->GetCapsuleComponent()->SetCollisionResponseToAllChannels(ECR_Ignore);

View File

@ -4,6 +4,7 @@
#include "VampireGameMode.h" #include "VampireGameMode.h"
#include "EnemyCharacter.h" #include "EnemyCharacter.h"
#include "HealthComponent.h"
#include "ObjectPoolManager.h" #include "ObjectPoolManager.h"
#include "PlayerCharacter.h" #include "PlayerCharacter.h"
#include "VampirePlayerController.h" #include "VampirePlayerController.h"
@ -25,6 +26,13 @@ int AVampireGameMode::GetEnemyDeathCount()
return EnemyDeathCount; return EnemyDeathCount;
} }
void AVampireGameMode::HandleOnEnemyDeath(FDamageInfo damageInfo)
{
IncrementEnemyDeathCount();
EnemyObjectPoolManager->ReturnObject(damageInfo.DamagedActor);
OnEnemyDeathCountIncrementDelegate.Broadcast(EnemyDeathCount);
}
void AVampireGameMode::SpawnEnemy() void AVampireGameMode::SpawnEnemy()
{ {
FVector TopLeft, TopLeftDir; FVector TopLeft, TopLeftDir;
@ -78,7 +86,17 @@ void AVampireGameMode::SpawnEnemy()
Direction.Normalize(); Direction.Normalize();
Direction *= CapsuleRadius; Direction *= CapsuleRadius;
Actor->SetActorLocation(SpawnLocation + Direction); 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);
}
} }
} }

View File

@ -3,6 +3,7 @@
#pragma once #pragma once
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "HealthComponent.h"
#include "GameFramework/GameMode.h" #include "GameFramework/GameMode.h"
#include "VampireGameMode.generated.h" #include "VampireGameMode.generated.h"
@ -10,9 +11,9 @@ class AObjectPoolManager;
class AVampirePlayerController; class AVampirePlayerController;
class APlayerCharacter; class APlayerCharacter;
class AEnemyCharacter; class AEnemyCharacter;
/**
* DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnEnemyDeathCountIncrementDelegate, int, level);
*/
UCLASS() UCLASS()
class VAMPIRES_API AVampireGameMode : public AGameMode class VAMPIRES_API AVampireGameMode : public AGameMode
{ {
@ -22,16 +23,18 @@ public:
UPROPERTY(EditDefaultsOnly) UPROPERTY(EditDefaultsOnly)
TSubclassOf<AEnemyCharacter> EnemyTemplate; TSubclassOf<AEnemyCharacter> EnemyTemplate;
private: FOnEnemyDeathCountIncrementDelegate OnEnemyDeathCountIncrementDelegate;
APlayerCharacter* PlayerCharacter;
AVampirePlayerController* PlayerController; private:
TObjectPtr<APlayerCharacter> PlayerCharacter;
TObjectPtr<AVampirePlayerController> PlayerController;
FTimerHandle SpawnEnemyTimerDelegate; FTimerHandle SpawnEnemyTimerDelegate;
int EnemyDeathCount = 0; int EnemyDeathCount = 0;
AObjectPoolManager* EnemyObjectPoolManager = nullptr; TObjectPtr<AObjectPoolManager> EnemyObjectPoolManager = nullptr;
protected: protected:
virtual void BeginPlay() override; virtual void BeginPlay() override;
@ -40,6 +43,9 @@ public:
UFUNCTION(BlueprintCallable, BlueprintPure) UFUNCTION(BlueprintCallable, BlueprintPure)
int GetEnemyDeathCount(); int GetEnemyDeathCount();
UFUNCTION()
void HandleOnEnemyDeath(FDamageInfo damageInfo);
UFUNCTION() UFUNCTION()
void IncrementEnemyDeathCount(); void IncrementEnemyDeathCount();

View File

@ -5,7 +5,9 @@
#include "EXPComponent.h" #include "EXPComponent.h"
#include "HealthComponent.h" #include "HealthComponent.h"
#include "VampireGameMode.h"
#include "Blueprint/UserWidget.h" #include "Blueprint/UserWidget.h"
#include "Kismet/GameplayStatics.h"
#include "Widgets/HUDWidget.h" #include "Widgets/HUDWidget.h"
void AVampirePlayerController::OnPossess(APawn* aPawn) void AVampirePlayerController::OnPossess(APawn* aPawn)
@ -19,7 +21,16 @@ void AVampirePlayerController::OnPossess(APawn* aPawn)
if (UEXPComponent* expComponent = aPawn->GetComponentByClass<UEXPComponent>()) if (UEXPComponent* expComponent = aPawn->GetComponentByClass<UEXPComponent>())
{ {
expComponent->OnEXPGained.AddUniqueDynamic(this, &AVampirePlayerController::UpdatePlayerEXPHUD); expComponent->OnEXPGained.AddUniqueDynamic(this, &AVampirePlayerController::UpdatePlayerEXPHUD);
expComponent->OnEXPLevelUp.AddUniqueDynamic(this, &AVampirePlayerController::UpdatePlayerLevelHUD);
UpdatePlayerEXPHUD(expComponent->GetCurrentEXP(), expComponent->GetCurrentLevelPercent()); UpdatePlayerEXPHUD(expComponent->GetCurrentEXP(), expComponent->GetCurrentLevelPercent());
UpdatePlayerLevelHUD(expComponent->GetCurrentLevel());
}
AVampireGameMode* gamemode = Cast<AVampireGameMode>(UGameplayStatics::GetGameMode(GetWorld()));
if (gamemode)
{
gamemode->OnEnemyDeathCountIncrementDelegate.AddDynamic(this, &AVampirePlayerController::UpdateKillCountHUD);
UpdateKillCountHUD(gamemode->GetEnemyDeathCount());
} }
if (currentPlayerHUD) if (currentPlayerHUD)
@ -36,3 +47,19 @@ void AVampirePlayerController::UpdatePlayerEXPHUD(int exp, float currentLevelPer
currentPlayerHUD->UpdateEXPBar(currentLevelPercent); currentPlayerHUD->UpdateEXPBar(currentLevelPercent);
} }
} }
void AVampirePlayerController::UpdatePlayerLevelHUD(int level)
{
if (currentPlayerHUD)
{
currentPlayerHUD->UpdateLevelBlock(level);
}
}
void AVampirePlayerController::UpdateKillCountHUD(int killCount)
{
if (currentPlayerHUD)
{
currentPlayerHUD->UpdateKillBlock(killCount);
}
}

View File

@ -29,4 +29,10 @@ protected:
UFUNCTION() UFUNCTION()
void UpdatePlayerEXPHUD(int exp, float currentLevelPercent); void UpdatePlayerEXPHUD(int exp, float currentLevelPercent);
UFUNCTION()
void UpdatePlayerLevelHUD(int level);
UFUNCTION()
void UpdateKillCountHUD(int killCount);
}; };

View File

@ -2,8 +2,8 @@
#include "HUDWidget.h" #include "HUDWidget.h"
#include "Components/ProgressBar.h" #include "Components/ProgressBar.h"
#include "Components/TextBlock.h"
void UHUDWidget::Init() void UHUDWidget::Init()
{ {
@ -14,10 +14,16 @@ void UHUDWidget::UpdateEXPBar(float currentLevelPercent)
EXPbar->SetPercent(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::UpdateTimerBlock()
{ {
} }
void UHUDWidget::UpdateKillBlock(int killCount)
{
KillBLock->SetText(FText::FromString("Kills: " + FString::FromInt(killCount)));
}

View File

@ -26,6 +26,9 @@ public:
UPROPERTY(BlueprintReadWrite, meta = (BindWidget)) UPROPERTY(BlueprintReadWrite, meta = (BindWidget))
UTextBlock* TimerBLock; UTextBlock* TimerBLock;
UPROPERTY(BlueprintReadWrite, meta = (BindWidget))
UTextBlock* KillBLock;
void Init(); void Init();
@ -33,9 +36,12 @@ public:
void UpdateEXPBar(float currentLevelPercent); void UpdateEXPBar(float currentLevelPercent);
UFUNCTION() UFUNCTION()
void UpdateLevelBlock(); void UpdateLevelBlock(int level);
UFUNCTION() UFUNCTION()
void UpdateTimerBlock(); void UpdateTimerBlock();
UFUNCTION()
void UpdateKillBlock(int killCount);
}; };

View File

@ -14,11 +14,11 @@ void UHealthbarWidget::NativeConstruct()
Super::NativeConstruct(); Super::NativeConstruct();
APlayerCharacter* player = Cast<APlayerCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0)); APlayerCharacter* player = Cast<APlayerCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
UHealthComponent* healthComponent = player->GetHealthComponent(); UHealthComponent* healthComponent = player->GetHealthComponent();
healthComponent->OnDamaged.BindUFunction(this, "UpdateHealthBar"); healthComponent->OnDamaged.AddDynamic(this, &UHealthbarWidget::UpdateHealthBar);
UpdateHealthBar(); UpdateHealthBar({});
} }
void UHealthbarWidget::UpdateHealthBar() void UHealthbarWidget::UpdateHealthBar(FDamageInfo damageInfo)
{ {
APlayerCharacter* player = Cast<APlayerCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0)); APlayerCharacter* player = Cast<APlayerCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
float percent = player->GetHealthComponent()->GetCurrentHealth() / player->GetHealthComponent()->GetMaxHealth(); float percent = player->GetHealthComponent()->GetCurrentHealth() / player->GetHealthComponent()->GetMaxHealth();

View File

@ -4,6 +4,7 @@
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "Blueprint/UserWidget.h" #include "Blueprint/UserWidget.h"
#include "vampires/HealthComponent.h"
#include "HealthbarWidget.generated.h" #include "HealthbarWidget.generated.h"
class UProgressBar; class UProgressBar;
@ -23,5 +24,5 @@ public:
private: private:
UFUNCTION() UFUNCTION()
void UpdateHealthBar(); void UpdateHealthBar(FDamageInfo damageInfo);
}; };