Files
Aura/Source/Aura/Private/Character/AuraCharacterBase.cpp
2024-07-10 18:21:51 +02:00

191 lines
5.6 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "Character/AuraCharacterBase.h"
#include "AbilitySystem/AuraAbilitySystemLibrary.h"
#include "AbilitySystem/AuraAbilitySystemComponent.h"
#include "Components/CapsuleComponent.h"
#include "AbilitySystem/AuraAttributeSet.h"
#include "AbilitySystem/Data/CharacterClassInfo.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Player/AuraPlayerController.h"
#include "Aura/Aura.h"
AAuraCharacterBase::AAuraCharacterBase()
{
PrimaryActorTick.bCanEverTick = true;
bUseControllerRotationPitch = false;
bUseControllerRotationRoll = false;
bUseControllerRotationYaw = false;
GetCharacterMovement()->bUseControllerDesiredRotation = true;
GetCapsuleComponent()->SetCollisionResponseToChannel(ECC_Camera, ECR_Ignore);
GetCapsuleComponent()->SetGenerateOverlapEvents(false);
GetMesh()->SetCollisionResponseToChannel(ECC_Camera, ECR_Ignore);
GetMesh()->SetCollisionResponseToChannel(ECC_Projectile, ECR_Overlap);
GetMesh()->SetGenerateOverlapEvents(true);
Weapon = CreateDefaultSubobject<USkeletalMeshComponent>("Weapon");
Weapon->SetupAttachment(GetMesh(), FName("WeaponHandSocket"));
Weapon->SetCollisionEnabled(ECollisionEnabled::NoCollision);
}
void AAuraCharacterBase::BeginPlay()
{
Super::BeginPlay();
checkf(ClassInfo, TEXT("ClassInfo not set for %s"), *GetNameSafe(this));
}
void AAuraCharacterBase::Restart()
{
Super::Restart();
AAuraPlayerController* AuraPC = GetController<AAuraPlayerController>();
if (IsValid(AuraPC))
{
AuraPC->PawnRestart(this);
}
}
UAbilitySystemComponent* AAuraCharacterBase::GetAbilitySystemComponent() const
{
return AbilitySystemComponent;
}
void AAuraCharacterBase::SetHighlighted_Implementation(bool bNewHighlighted)
{
bHighlighted = bNewHighlighted;
}
void AAuraCharacterBase::GetWorldHoverableInfos_Implementation(FWorldHoverableInfos& Infos)
{
Infos.Location = FVector(0.5, 0.1, 0.0);
Infos.Text = GetClassInfo()->Name;
Infos.bScreenSpace = true;
}
bool AAuraCharacterBase::CanBeHovered_Implementation(AController* HoveringController, const FHitResult& TraceHit)
{
return true;
}
void AAuraCharacterBase::HoveredBegin_Implementation(AController* HoveringController, const FHitResult& TraceHit)
{
SetHighlighted(true);
}
void AAuraCharacterBase::HoveredEnd_Implementation(AController* HoveringController)
{
SetHighlighted(false);
}
void AAuraCharacterBase::InitAbilitySystem()
{
if (UAuraAbilitySystemComponent* ASC = Cast<UAuraAbilitySystemComponent>(AbilitySystemComponent))
{
ASC->AbilitySystemInitDone();
}
AbilitySystemInitialized(AbilitySystemComponent);
OnASCRegistered.Broadcast(AbilitySystemComponent);
// AbilitySystemComponent->OnGameplayEffectAppliedDelegateToSelf.AddUObject(this, &AAuraCharacterBase::OnEffectApplied);
AddCharacterBaseEffects();
AddCharacterAbilities();
ApplyBeginEffects();
}
void AAuraCharacterBase::AbilitySystemInitialized_Implementation(UAbilitySystemComponent* ASC)
{
}
// void AAuraCharacterBase::OnEffectApplied_Implementation(UAbilitySystemComponent* ASC, const FGameplayEffectSpec& EffectSpec, FActiveGameplayEffectHandle EffectHandle)
// {
// }
void AAuraCharacterBase::RegisterGameplayTagEvent(FGameplayTag EventTag, EGameplayTagEventType::Type TagEventType, FTagCountChangeSignature OnTagCountChanged)
{
if (AbilitySystemComponent)
{
AbilitySystemComponent->RegisterGameplayTagEvent(EventTag, TagEventType)
.AddLambda([OnTagCountChanged](const FGameplayTag& CallbackTag, int32 NewCount) {
OnTagCountChanged.ExecuteIfBound(CallbackTag, NewCount);
});
}
}
void AAuraCharacterBase::AddCharacterBaseEffects()
{
const float PlayerLevel = static_cast<float>(ICombatInterface::Execute_GetPlayerLevel(this));
UAuraAbilitySystemComponent* AuraASC = Cast<UAuraAbilitySystemComponent>(AbilitySystemComponent);
for(const TSubclassOf<UGameplayEffect>& EffectClass : ClassInfo->BaseEffects)
{
if (IsValid(EffectClass))
{
FGameplayEffectContextHandle EffectContext = AbilitySystemComponent->MakeEffectContext();
EffectContext.AddSourceObject(AbilitySystemComponent->GetAvatarActor());
FActiveGameplayEffectHandle EffectHandle = AbilitySystemComponent->ApplyGameplayEffectSpecToSelf(*AbilitySystemComponent->MakeOutgoingSpec(EffectClass, PlayerLevel, EffectContext).Data.Get());
if (AuraASC)
AuraASC->BaseEffectsHandles.Add(EffectHandle);
}
}
}
void AAuraCharacterBase::AddCharacterAbilities()
{
if (!HasAuthority()) return;
if (UAuraAbilitySystemComponent* ASC = Cast<UAuraAbilitySystemComponent>(GetAbilitySystemComponent()))
{
ASC->GrantStartupAbilities(ClassInfo->StartupAbilities);
}
}
void AAuraCharacterBase::ApplyBeginEffects()
{
for(const FGameplayEffectParameters& Effect : ClassInfo->OnBeginEffects)
{
UAuraAbilitySystemLibrary::ApplyGameplayEffectWithParameters(Effect, nullptr, AbilitySystemComponent);
}
}
int32 AAuraCharacterBase::GetDeathExperience_Implementation() const
{
return ClassInfo->DeathExperience.GetValueAtLevel(ICombatInterface::Execute_GetPlayerLevel(this));
}
FVector AAuraCharacterBase::GetCombatSocketLocation_Implementation(FGameplayTag SocketTagName) const
{
return GetActorLocation();
}
bool AAuraCharacterBase::IsDead_Implementation() const
{
if (!IsValid(AttributeSet))
return false;
if (UAuraAttributeSet* AS = Cast<UAuraAttributeSet>(AttributeSet))
{
return AS->GetHealth() <= 0.0f;
}
return false;
}
FOnASCRegisteredDelegate& AAuraCharacterBase::GetOnASCRegisteredDelegate()
{
return OnASCRegistered;
}
FOnDeathBroadcastSignature& AAuraCharacterBase::GetOnDeathDelegate()
{
return OnDeath;
}