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()
{
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<AEXPPickup>(EXPPickupTemplate, GetActorLocation(), FRotator::ZeroRotator,
actorSpawnParameters);
AVampireGameMode* gamemode = Cast<AVampireGameMode>(UGameplayStatics::GetGameMode(GetWorld()));
gamemode->IncrementEnemyDeathCount();
gamemode->GetEnemyObjectPoolManager()->ReturnObject(this);
}
void AEnemyCharacter::ResetHealth()

View File

@ -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()

View File

@ -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});
}
}

View File

@ -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

View File

@ -51,8 +51,8 @@ void AVampireAIController::OnPossess(APawn* InPawn)
EnemyCharacter = Cast<AEnemyCharacter>(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);

View File

@ -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);
}
}
}

View File

@ -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<AEnemyCharacter> EnemyTemplate;
private:
APlayerCharacter* PlayerCharacter;
FOnEnemyDeathCountIncrementDelegate OnEnemyDeathCountIncrementDelegate;
AVampirePlayerController* PlayerController;
private:
TObjectPtr<APlayerCharacter> PlayerCharacter;
TObjectPtr<AVampirePlayerController> PlayerController;
FTimerHandle SpawnEnemyTimerDelegate;
int EnemyDeathCount = 0;
AObjectPoolManager* EnemyObjectPoolManager = nullptr;
TObjectPtr<AObjectPoolManager> 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();

View File

@ -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<UEXPComponent>())
{
expComponent->OnEXPGained.AddUniqueDynamic(this, &AVampirePlayerController::UpdatePlayerEXPHUD);
expComponent->OnEXPLevelUp.AddUniqueDynamic(this, &AVampirePlayerController::UpdatePlayerLevelHUD);
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)
@ -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);
}
}

View File

@ -29,4 +29,10 @@ protected:
UFUNCTION()
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 "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)));
}

View File

@ -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);
};

View File

@ -14,11 +14,11 @@ void UHealthbarWidget::NativeConstruct()
Super::NativeConstruct();
APlayerCharacter* player = Cast<APlayerCharacter>(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<APlayerCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
float percent = player->GetHealthComponent()->GetCurrentHealth() / player->GetHealthComponent()->GetMaxHealth();

View File

@ -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);
};