From e55d62ff05e27371e407a60f1cee52ab3e31187b Mon Sep 17 00:00:00 2001 From: baz Date: Mon, 18 Nov 2024 02:23:30 +0000 Subject: [PATCH] Remove some casts --- Source/vampires/EXPPickup.cpp | 9 ++++---- Source/vampires/GoldPickup.cpp | 8 +++---- Source/vampires/Widgets/HealthbarWidget.cpp | 25 ++++++++++++++------- Source/vampires/Widgets/HealthbarWidget.h | 1 - 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/Source/vampires/EXPPickup.cpp b/Source/vampires/EXPPickup.cpp index f7605cb..a539680 100644 --- a/Source/vampires/EXPPickup.cpp +++ b/Source/vampires/EXPPickup.cpp @@ -2,9 +2,7 @@ #include "EXPPickup.h" - #include "EXPComponent.h" -#include "PlayerCharacter.h" void AEXPPickup::BeginPlay() { @@ -17,11 +15,12 @@ void AEXPPickup::Tick(float DeltaSeconds) } void AEXPPickup::OnInnerBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, - UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) + UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, + const FHitResult& SweepResult) { - if (APlayerCharacter* PlayerCharacter = Cast(OtherActor)) + if (UEXPComponent* expComponent = OtherActor->GetComponentByClass()) { - PlayerCharacter->GetEXPComponent()->IncrementEXP(EXP); + expComponent->IncrementEXP(EXP); Super::OnInnerBeginOverlap(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult); } } diff --git a/Source/vampires/GoldPickup.cpp b/Source/vampires/GoldPickup.cpp index c7fc9c7..12453e4 100644 --- a/Source/vampires/GoldPickup.cpp +++ b/Source/vampires/GoldPickup.cpp @@ -4,7 +4,6 @@ #include "GoldPickup.h" #include "GoldComponent.h" -#include "PlayerCharacter.h" class APlayerCharacter; @@ -19,11 +18,12 @@ void AGoldPickup::Tick(float DeltaSeconds) } void AGoldPickup::OnInnerBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, - UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) + UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, + const FHitResult& SweepResult) { - if (APlayerCharacter* PlayerCharacter = Cast(OtherActor)) + if (UGoldComponent* goldComponent = OtherActor->GetComponentByClass()) { - PlayerCharacter->GetGoldComponent()->IncrementGold(Gold); + goldComponent->IncrementGold(Gold); Super::OnInnerBeginOverlap(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult); } } diff --git a/Source/vampires/Widgets/HealthbarWidget.cpp b/Source/vampires/Widgets/HealthbarWidget.cpp index 6bb13a5..4100759 100644 --- a/Source/vampires/Widgets/HealthbarWidget.cpp +++ b/Source/vampires/Widgets/HealthbarWidget.cpp @@ -4,7 +4,6 @@ #include "HealthbarWidget.h" #include "Kismet/GameplayStatics.h" -#include "vampires/PlayerCharacter.h" #include "Components/ProgressBar.h" #include "vampires/HealthComponent.h" #include "vampires/VampireCharacter.h" @@ -12,15 +11,25 @@ void UHealthbarWidget::NativeConstruct() { Super::NativeConstruct(); - APlayerCharacter* player = Cast(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0)); - UHealthComponent* healthComponent = player->GetHealthComponent(); - healthComponent->OnDamaged.AddDynamic(this, &UHealthbarWidget::UpdateHealthBar); - UpdateHealthBar({}); + + if (ACharacter* character = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0)) + { + if (UHealthComponent* healthComponent = character->FindComponentByClass()) + { + healthComponent->OnDamaged.AddDynamic(this, &UHealthbarWidget::UpdateHealthBar); + UpdateHealthBar({}); + } + } } void UHealthbarWidget::UpdateHealthBar(FDamageInfo damageInfo) { - APlayerCharacter* player = Cast(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0)); - float percent = player->GetHealthComponent()->GetCurrentHealth() / player->GetHealthComponent()->GetMaxHealth(); - HealthBar->SetPercent(percent); + if (ACharacter* character = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0)) + { + if (UHealthComponent* healthComponent = character->FindComponentByClass()) + { + float percent = healthComponent->GetCurrentHealth() / healthComponent->GetMaxHealth(); + HealthBar->SetPercent(percent); + } + } } diff --git a/Source/vampires/Widgets/HealthbarWidget.h b/Source/vampires/Widgets/HealthbarWidget.h index 1a4a779..488f9c4 100644 --- a/Source/vampires/Widgets/HealthbarWidget.h +++ b/Source/vampires/Widgets/HealthbarWidget.h @@ -4,7 +4,6 @@ #include "CoreMinimal.h" #include "Blueprint/UserWidget.h" -#include "vampires/HealthComponent.h" #include "HealthbarWidget.generated.h" class UProgressBar;