Files
Crunch/Source/Crunch/Private/Character/CCharacter.cpp

102 lines
2.7 KiB
C++

// Multiplayer By Caleb
#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;
GetMesh()->SetCollisionEnabled(ECollisionEnabled::NoCollision);
CAbilitySystemComponent = CreateDefaultSubobject<UCAbilitySystemComponent>(TEXT("Ability System Component"));
CAttributeSet = CreateDefaultSubobject<UCAttributeSet>(TEXT("CAttribute Set"));
/**
* This is also valid
* CAttributeSet = CAbilitySystemComponent->GetSet<UCAttributeSet>();
**/
OverHeadWidgetComponent = CreateDefaultSubobject<UWidgetComponent>("OverHead Widget Component");
OverHeadWidgetComponent->SetupAttachment(GetRootComponent());
}
void ACCharacter::ServerSideInit()
{
CAbilitySystemComponent->InitAbilityActorInfo(this, this);
CAbilitySystemComponent->ApplyInitialEffects();
}
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);
}
}