added Health and Mana widgets and visibility based on player distance

This commit is contained in:
Caleb Buhungiro
2025-06-27 15:36:48 +08:00
parent c47347e635
commit 05e06a57be
8 changed files with 149 additions and 8 deletions

View File

@@ -3,10 +3,19 @@
#include "Crunch/Public/Character/CCharacter.h"
#include "Components/WidgetComponent.h"
#include "GAS/CAbilitySystemComponent.h"
#include "GAS/CAttributeSet.h"
#include "Kismet/GameplayStatics.h"
#include "Widgets/OverHeadStatsGauge.h"
void ACCharacter::BeginPlay()
{
Super::BeginPlay();
ConfigureOverHeadStatusWidget();
}
ACCharacter::ACCharacter()
{
PrimaryActorTick.bCanEverTick = true;
@@ -18,6 +27,8 @@ ACCharacter::ACCharacter()
* This is also valid
* CAttributeSet = CAbilitySystemComponent->GetSet<UCAttributeSet>();
**/
OverHeadWidgetComponent = CreateDefaultSubobject<UWidgetComponent>("OverHead Widget Component");
OverHeadWidgetComponent->SetupAttachment(GetRootComponent());
}
void ACCharacter::ServerSideInit()
@@ -31,8 +42,60 @@ void ACCharacter::ClientSideInit()
CAbilitySystemComponent->InitAbilityActorInfo(this, this);
}
bool ACCharacter::IsLocallyControlledByPlayer() const
{
return GetController() && GetController()->IsLocalPlayerController();
}
/**
* this is for the AI controller pawns
* this is only called on the server
* @param NewController
*/
void ACCharacter::PossessedBy(AController* NewController)
{
Super::PossessedBy(NewController);
if (NewController && !NewController->IsPlayerController())
{
ServerSideInit();
}
}
UAbilitySystemComponent* ACCharacter::GetAbilitySystemComponent() const
{
return CAbilitySystemComponent;
}
void ACCharacter::ConfigureOverHeadStatusWidget()
{
if (!OverHeadWidgetComponent) return;
if (IsLocallyControlledByPlayer())
{
OverHeadWidgetComponent->SetHiddenInGame(true);
return;
}
auto OverHeadStatsGauge = Cast<UOverHeadStatsGauge>(OverHeadWidgetComponent->GetUserWidgetObject());
if (OverHeadStatsGauge)
{
OverHeadStatsGauge->ConfigureWithASC(GetAbilitySystemComponent());
OverHeadWidgetComponent->SetHiddenInGame(false);
GetWorldTimerManager().ClearTimer(HeadStatGaugeVisibilityUpdateTimerHandle);
GetWorldTimerManager().SetTimer(
HeadStatGaugeVisibilityUpdateTimerHandle,
this,
&ThisClass::UpdateHeadStatGaugeVisibility,
HeadStatGaugeVisibilityCheckUpdateGap,
true);
}
}
void ACCharacter::UpdateHeadStatGaugeVisibility()
{
auto LocalPlayerPawn = UGameplayStatics::GetPlayerPawn(this, 0);
if (LocalPlayerPawn)
{
const float DistSquared = FVector::DistSquared(GetActorLocation(), LocalPlayerPawn->GetActorLocation());
OverHeadWidgetComponent->SetHiddenInGame(DistSquared > HeadStatGaugeVisibilityRangeSquared);
}
}