111 lines
3.0 KiB
C++
111 lines
3.0 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 "AbilitySystem/OLSGlobaAbilitySubsystem.h"
|
|
|
|
#include "Abilities/GameplayAbility.h"
|
|
#include "AbilitySystem/OLSAbilitySystemComponent.h"
|
|
|
|
void FOLSGlobalAppliedAbilityList::AddToASC(TSubclassOf<UGameplayAbility> ability, UOLSAbilitySystemComponent* asc)
|
|
{
|
|
if (FGameplayAbilitySpecHandle* specHandle = Handles.Find(asc))
|
|
{
|
|
RemoveFromASC(asc);
|
|
}
|
|
|
|
UGameplayAbility* abilityCDO = ability->GetDefaultObject<UGameplayAbility>();
|
|
FGameplayAbilitySpec abilitySpec(abilityCDO);
|
|
const FGameplayAbilitySpecHandle abilitySpecHandle = asc->GiveAbility(abilitySpec);
|
|
Handles.Add(asc, abilitySpecHandle);
|
|
}
|
|
|
|
void FOLSGlobalAppliedAbilityList::RemoveFromASC(UOLSAbilitySystemComponent* asc)
|
|
{
|
|
if (FGameplayAbilitySpecHandle* specHandle = Handles.Find(asc))
|
|
{
|
|
asc->ClearAbility(*specHandle);
|
|
Handles.Remove(asc);
|
|
}
|
|
}
|
|
|
|
void FOLSGlobalAppliedAbilityList::RemoveFromAll()
|
|
{
|
|
|
|
for (TTuple<TObjectPtr<UOLSAbilitySystemComponent>, FGameplayAbilitySpecHandle>& handle : Handles)
|
|
{
|
|
if (handle.Key != nullptr)
|
|
{
|
|
handle.Key->ClearAbility(handle.Value);
|
|
}
|
|
}
|
|
|
|
Handles.Empty();
|
|
}
|
|
|
|
void FOLSGlobalAppliedEffectList::AddToASC(const TSubclassOf<UGameplayEffect>& effect, UOLSAbilitySystemComponent* asc)
|
|
{
|
|
if (FActiveGameplayEffectHandle* effectHandle = Handles.Find(asc))
|
|
{
|
|
RemoveFromASC(asc);
|
|
}
|
|
|
|
const UGameplayEffect* gameplayEffectCDO = effect->GetDefaultObject<UGameplayEffect>();
|
|
const FActiveGameplayEffectHandle gameplayEffectHandle = asc->ApplyGameplayEffectToSelf(gameplayEffectCDO, /*Level=*/ 1, asc->MakeEffectContext());
|
|
Handles.Add(asc, gameplayEffectHandle);
|
|
}
|
|
|
|
void FOLSGlobalAppliedEffectList::RemoveFromASC(UOLSAbilitySystemComponent* asc)
|
|
{
|
|
if (FActiveGameplayEffectHandle* effectHandle = Handles.Find(asc))
|
|
{
|
|
asc->RemoveActiveGameplayEffect(*effectHandle);
|
|
Handles.Remove(asc);
|
|
}
|
|
}
|
|
|
|
void FOLSGlobalAppliedEffectList::RemoveFromAll()
|
|
{
|
|
for (TTuple<TObjectPtr<UOLSAbilitySystemComponent>, FActiveGameplayEffectHandle>& handle : Handles)
|
|
{
|
|
if (handle.Key != nullptr)
|
|
{
|
|
handle.Key->RemoveActiveGameplayEffect(handle.Value);
|
|
}
|
|
}
|
|
Handles.Empty();
|
|
}
|
|
|
|
void UOLSGlobaAbilitySubsystem::RegisterASC(UOLSAbilitySystemComponent* asc)
|
|
{
|
|
check(asc);
|
|
|
|
for (TTuple<TSubclassOf<UGameplayAbility>, FOLSGlobalAppliedAbilityList>& appliedAbility : AppliedAbilities)
|
|
{
|
|
appliedAbility.Value.AddToASC(appliedAbility.Key, asc);
|
|
}
|
|
|
|
for (TTuple<TSubclassOf<UGameplayEffect>, FOLSGlobalAppliedEffectList>& appliedEffect : AppliedEffects)
|
|
{
|
|
appliedEffect.Value.AddToASC(appliedEffect.Key, asc);
|
|
}
|
|
|
|
RegisteredASCs.AddUnique(asc);
|
|
}
|
|
|
|
void UOLSGlobaAbilitySubsystem::UnregisterASC(UOLSAbilitySystemComponent* asc)
|
|
{
|
|
check(asc);
|
|
|
|
for (auto& appliedAbility : AppliedAbilities)
|
|
{
|
|
appliedAbility.Value.RemoveFromASC(asc);
|
|
}
|
|
|
|
for (auto& appliedEffect : AppliedEffects)
|
|
{
|
|
appliedEffect.Value.RemoveFromASC(asc);
|
|
}
|
|
|
|
RegisteredASCs.Remove(asc);
|
|
}
|