154 lines
4.5 KiB
C++
154 lines
4.5 KiB
C++
// © 2024 Long Ly. All rights reserved. Any unauthorized use, reproduction, or distribution of this trademark is strictly prohibited and may result in legal action.
|
|
|
|
|
|
#include "DataAssets/OLSAbilitySetPrimaryDataAsset.h"
|
|
|
|
#include "ActiveGameplayEffectHandle.h"
|
|
#include "GameplayAbilitySpecHandle.h"
|
|
#include "AbilitySystem/OLSAbilitySystemComponent.h"
|
|
#include "AbilitySystem/Abilities/OLSGameplayAbility.h"
|
|
#include "AbilitySystem/Attributes/OLSAttributeSetBase.h"
|
|
|
|
void FOLSAbilitySet_GrantedHandles::AddAbilitySpecHandle(const FGameplayAbilitySpecHandle& handle)
|
|
{
|
|
if (handle.IsValid())
|
|
{
|
|
AbilitySpecHandles.Add(handle);
|
|
}
|
|
}
|
|
|
|
void FOLSAbilitySet_GrantedHandles::AddGameplayEffectHandle(const FActiveGameplayEffectHandle& handle)
|
|
{
|
|
if (handle.IsValid())
|
|
{
|
|
GameplayEffectHandles.Add(handle);
|
|
}
|
|
}
|
|
|
|
void FOLSAbilitySet_GrantedHandles::AddAttributeSet(UAttributeSet* set)
|
|
{
|
|
GrantedAttributeSets.Add(set);
|
|
}
|
|
|
|
void FOLSAbilitySet_GrantedHandles::TakeFromAbilitySystem(UOLSAbilitySystemComponent* olsASC)
|
|
{
|
|
check(olsASC);
|
|
|
|
if (!olsASC->IsOwnerActorAuthoritative())
|
|
{
|
|
// Must be authoritative to give or take ability sets.
|
|
return;
|
|
}
|
|
|
|
for (const FGameplayAbilitySpecHandle& handle : AbilitySpecHandles)
|
|
{
|
|
if (handle.IsValid())
|
|
{
|
|
olsASC->ClearAbility(handle);
|
|
}
|
|
}
|
|
|
|
for (const FActiveGameplayEffectHandle& handle : GameplayEffectHandles)
|
|
{
|
|
if (handle.IsValid())
|
|
{
|
|
olsASC->RemoveActiveGameplayEffect(handle);
|
|
}
|
|
}
|
|
|
|
for (UAttributeSet* set : GrantedAttributeSets)
|
|
{
|
|
olsASC->RemoveSpawnedAttribute(set);
|
|
}
|
|
|
|
AbilitySpecHandles.Reset();
|
|
GameplayEffectHandles.Reset();
|
|
GrantedAttributeSets.Reset();
|
|
}
|
|
|
|
UOLSAbilitySetPrimaryDataAsset::UOLSAbilitySetPrimaryDataAsset(const FObjectInitializer& objectInitializer) : Super(objectInitializer)
|
|
{
|
|
}
|
|
|
|
void UOLSAbilitySetPrimaryDataAsset::GiveToAbilitySystem(
|
|
UOLSAbilitySystemComponent* olsASC,
|
|
FOLSAbilitySet_GrantedHandles* outGrantedHandlePtrs,
|
|
UObject* sourceObject) const
|
|
{
|
|
check(olsASC);
|
|
|
|
if (!olsASC->IsOwnerActorAuthoritative())
|
|
{
|
|
// Must be authoritative to give or take ability sets.
|
|
return;
|
|
}
|
|
|
|
// Grant the attribute sets.
|
|
for (int32 setIndex = 0; setIndex < GrantedAttributes.Num(); ++setIndex)
|
|
{
|
|
const FOLSAbilitySet_AttributeSet& setToGrant = GrantedAttributes[setIndex];
|
|
|
|
if (!IsValid(setToGrant.AttributeSet))
|
|
{
|
|
//@Todo: Replace this with custom log.
|
|
// UE_LOG(LogLyraAbilitySystem, Error, TEXT("GrantedAttributes[%d] on ability set [%s] is not valid"), SetIndex, *GetNameSafe(this));
|
|
continue;
|
|
}
|
|
|
|
UAttributeSet* newSet = NewObject<UAttributeSet>(olsASC->GetOwner(), setToGrant.AttributeSet);
|
|
olsASC->AddAttributeSetSubobject(newSet);
|
|
|
|
if (outGrantedHandlePtrs)
|
|
{
|
|
outGrantedHandlePtrs->AddAttributeSet(newSet);
|
|
}
|
|
}
|
|
|
|
// Grant the gameplay abilities.
|
|
for (int32 abilityIndex = 0; abilityIndex < GrantedGameplayAbilities.Num(); ++abilityIndex)
|
|
{
|
|
const FOLSAbilitySet_GameplayAbility& AbilityToGrant = GrantedGameplayAbilities[abilityIndex];
|
|
|
|
if (!IsValid(AbilityToGrant.Ability))
|
|
{
|
|
//@Todo: Replace this with custom log.
|
|
// UE_LOG(LogLyraAbilitySystem, Error, TEXT("GrantedGameplayAbilities[%d] on ability set [%s] is not valid."), AbilityIndex, *GetNameSafe(this));
|
|
continue;
|
|
}
|
|
|
|
UOLSGameplayAbility* abilityCDO = AbilityToGrant.Ability->GetDefaultObject<UOLSGameplayAbility>();
|
|
|
|
FGameplayAbilitySpec abilitySpec(abilityCDO, AbilityToGrant.AbilityLevel);
|
|
abilitySpec.SourceObject = sourceObject;
|
|
abilitySpec.GetDynamicSpecSourceTags().AddTag(AbilityToGrant.InputTag);
|
|
|
|
const FGameplayAbilitySpecHandle abilitySpecHandle = olsASC->GiveAbility(abilitySpec);
|
|
|
|
if (outGrantedHandlePtrs)
|
|
{
|
|
outGrantedHandlePtrs->AddAbilitySpecHandle(abilitySpecHandle);
|
|
}
|
|
}
|
|
|
|
// Grant the gameplay effects.
|
|
for (int32 effectIndex = 0; effectIndex < GrantedGameplayEffects.Num(); ++effectIndex)
|
|
{
|
|
const FOLSAbilitySet_GameplayEffect& EffectToGrant = GrantedGameplayEffects[effectIndex];
|
|
|
|
if (!IsValid(EffectToGrant.GameplayEffect))
|
|
{
|
|
//@Todo: Replace this with custom log.
|
|
// UE_LOG(LogLyraAbilitySystem, Error, TEXT("GrantedGameplayEffects[%d] on ability set [%s] is not valid"), EffectIndex, *GetNameSafe(this));
|
|
continue;
|
|
}
|
|
|
|
const UGameplayEffect* gameplayEffect = EffectToGrant.GameplayEffect->GetDefaultObject<UGameplayEffect>();
|
|
const FActiveGameplayEffectHandle gameplayEffectHandle = olsASC->ApplyGameplayEffectToSelf(gameplayEffect, EffectToGrant.EffectLevel, olsASC->MakeEffectContext());
|
|
|
|
if (outGrantedHandlePtrs)
|
|
{
|
|
outGrantedHandlePtrs->AddGameplayEffectHandle(gameplayEffectHandle);
|
|
}
|
|
}
|
|
}
|