当前位置:   article > 正文

UE5 GAS开发P41-43 永久效果,去除永久效果,伤害区域,EnumClass,开始重叠与结束重叠事件

UE5 GAS开发P41-43 永久效果,去除永久效果,伤害区域,EnumClass,开始重叠与结束重叠事件

这一部分学习了怎么创建一个伤害性的地形(火焰地形,毒沼泽等都可以用这个方式创建)

AuraEffectActor.h

  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #pragma once
  3. #include "CoreMinimal.h"
  4. #include "GameplayEffect.h"
  5. #include "AbilitySystem/AuraAbilitySystemComponentBase.h"
  6. #include "Components/SphereComponent.h"
  7. #include "GameFramework/Actor.h"
  8. #include "GameplayEffectTypes.h"
  9. #include "AuraEffectActor.generated.h"
  10. class UGameplayEffect;
  11. class UAbilitySystemComponent;
  12. UENUM(BlueprintType)
  13. enum class EEffectApplicationPolicy : uint8 { ApplyOnOverlap,ApplyOnEndOverlap,DoNotApply };
  14. UENUM(BlueprintType)
  15. enum class EEffectRemovePolicy : uint8 { RemoveOnEndOverlap,DoNotRemove };
  16. UCLASS()
  17. class MYGAS_API AAuraEffectActor : public AActor
  18. {
  19. GENERATED_BODY()
  20. public:
  21. // Sets default values for this actor's properties
  22. AAuraEffectActor();
  23. protected:
  24. // Called when the game starts or when spawned
  25. virtual void BeginPlay() override;
  26. UFUNCTION(BlueprintCallable)
  27. void ApplyEffectToTarget(AActor* TargetActor, TSubclassOf<UGameplayEffect> GameplayEffectClass);
  28. UFUNCTION(BlueprintCallable)
  29. void OnOverlap(AActor* TargetActor);
  30. UFUNCTION(BlueprintCallable)
  31. void OnEndOverlap(AActor* TargetActor);
  32. UPROPERTY(EditAnywhere,BlueprintReadOnly,Category="Applied Effects")
  33. TSubclassOf<UGameplayEffect> InstantGameplayEffectClass;
  34. UPROPERTY(EditAnywhere,BlueprintReadOnly,Category="Applied Effects")
  35. EEffectApplicationPolicy InstantEffectApplicationPolicy = EEffectApplicationPolicy::DoNotApply;
  36. UPROPERTY(EditAnywhere,BlueprintReadOnly,Category="Applied Effects")
  37. TSubclassOf<UGameplayEffect> DurationGameplayEffectClass;
  38. UPROPERTY(EditAnywhere,BlueprintReadOnly,Category="Applied Effects")
  39. EEffectApplicationPolicy DurationEffectApplicationPolicy = EEffectApplicationPolicy::DoNotApply;
  40. UPROPERTY(EditAnywhere,BlueprintReadOnly,Category="Applied Effects")
  41. TSubclassOf<UGameplayEffect> InfiniteGameplayEffectClass;
  42. UPROPERTY(EditAnywhere,BlueprintReadOnly,Category="Applied Effects")
  43. EEffectApplicationPolicy InfiniteEffectApplicationPolicy = EEffectApplicationPolicy::DoNotApply;
  44. UPROPERTY(EditAnywhere,BlueprintReadOnly,Category="Applied Effects")
  45. EEffectRemovePolicy InfiniteEffectRemovePolicy = EEffectRemovePolicy::RemoveOnEndOverlap;
  46. TMap<FActiveGameplayEffectHandle , UAbilitySystemComponent*> ActiveEffectHandles;
  47. private:
  48. };

AuraEffectActor.cpp

  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #include "Actor/AuraEffectActor.h"
  3. #include "AbilitySystemBlueprintLibrary.h"
  4. #include "AbilitySystemComponent.h"
  5. #include "AbilitySystemInterface.h"
  6. #include "AbilitySystem/AuraAbilitySystemComponentBase.h"
  7. #include "AbilitySystem/AuraAttributeSet.h"
  8. #include "Components/StaticMeshComponent.h"
  9. // Sets default values
  10. AAuraEffectActor::AAuraEffectActor()
  11. {
  12. // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
  13. PrimaryActorTick.bCanEverTick = false;
  14. SetRootComponent(CreateDefaultSubobject<USceneComponent>("SceneRoot"));
  15. }
  16. // Called when the game starts or when spawned
  17. void AAuraEffectActor::BeginPlay()
  18. {
  19. Super::BeginPlay();
  20. }
  21. void AAuraEffectActor::ApplyEffectToTarget(AActor* TargetActor, TSubclassOf<UGameplayEffect> GameplayEffectClass)
  22. {
  23. //这里是使用 UAbilitySystemBlueprintLibrary 内的静态函数,在指定的Target上查找并返回UAbilitySystemComponent
  24. UAbilitySystemComponent* TargetASC = UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(TargetActor);
  25. if(TargetASC==nullptr) return;
  26. check(GameplayEffectClass);
  27. //创建了一个游戏效果的内容句柄,提供游戏效果的信息
  28. FGameplayEffectContextHandle TargetASCContext =TargetASC->MakeEffectContext();
  29. //把自己作为游戏效果的源对象,确保AbilitySystem可以识别到自己
  30. TargetASCContext.AddSourceObject(this);
  31. //这里创建了一个游戏效果规范,GameplayEffectClass 是要应用的游戏效果的类,1.0f 是游戏效果的初始生命周期倍率,TargetASCContext 是游戏效果的上下文. 调用 MakeOutgoingSpec 函数,将会根据提供的参数创建一个游戏效果规范
  32. const FGameplayEffectSpecHandle EffectSpecHandle = TargetASC->MakeOutgoingSpec(GameplayEffectClass,1.0f,TargetASCContext);
  33. const FActiveGameplayEffectHandle ActiveGameplayEffectHandle = TargetASC->ApplyGameplayEffectSpecToSelf(*EffectSpecHandle.Data.Get());
  34. const bool bIsInfinite = EffectSpecHandle.Data.Get()->Def.Get()->DurationPolicy == EGameplayEffectDurationType::Infinite;
  35. if(bIsInfinite)
  36. {
  37. ActiveEffectHandles.Add(ActiveGameplayEffectHandle,TargetASC);
  38. }
  39. }
  40. void AAuraEffectActor::OnOverlap(AActor* TargetActor)
  41. {
  42. if(InstantEffectApplicationPolicy == EEffectApplicationPolicy::ApplyOnOverlap)
  43. {
  44. ApplyEffectToTarget(TargetActor,InstantGameplayEffectClass);
  45. }
  46. if(DurationEffectApplicationPolicy == EEffectApplicationPolicy::ApplyOnOverlap)
  47. {
  48. ApplyEffectToTarget(TargetActor,DurationGameplayEffectClass);
  49. }
  50. if(InfiniteEffectApplicationPolicy == EEffectApplicationPolicy::ApplyOnOverlap)
  51. {
  52. ApplyEffectToTarget(TargetActor,InfiniteGameplayEffectClass);
  53. }
  54. }
  55. void AAuraEffectActor::OnEndOverlap(AActor* TargetActor)
  56. {
  57. if(InstantEffectApplicationPolicy == EEffectApplicationPolicy::ApplyOnOverlap)
  58. {
  59. ApplyEffectToTarget(TargetActor,InstantGameplayEffectClass);
  60. }
  61. if(DurationEffectApplicationPolicy == EEffectApplicationPolicy::ApplyOnOverlap)
  62. {
  63. ApplyEffectToTarget(TargetActor,DurationGameplayEffectClass);
  64. }
  65. if(InfiniteEffectApplicationPolicy == EEffectApplicationPolicy::ApplyOnOverlap)
  66. {
  67. ApplyEffectToTarget(TargetActor,InfiniteGameplayEffectClass);
  68. }
  69. if(InfiniteEffectRemovePolicy == EEffectRemovePolicy::RemoveOnEndOverlap)
  70. {
  71. //获取到目标角色的能力组件
  72. UAbilitySystemComponent* TargetASC = UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(TargetActor);
  73. if(!IsValid(TargetASC)) return;
  74. //创建了一个储存移除游戏效果句柄的数组 HandlesToRemove
  75. TArray<FActiveGameplayEffectHandle> HandlesToRemove;
  76. //遍历 ActiveEffectHandles 数组
  77. for(TTuple<FActiveGameplayEffectHandle, UAbilitySystemComponent*> HandlePair:ActiveEffectHandles)
  78. {
  79. // 当 TargetASC 内有 HandlePair.Value的值的时候
  80. if(TargetASC == HandlePair.Value)
  81. {
  82. // 移除 HandlePair 的效果
  83. TargetASC->RemoveActiveGameplayEffect(HandlePair.Key, 1);
  84. //把这个效果放在 HandlesToRemove 中
  85. HandlesToRemove.Add(HandlePair.Key);
  86. }
  87. }
  88. // 遍历 HandlesToRemove 内的每一个元素
  89. for(auto& Handle : HandlesToRemove)
  90. {
  91. // 在 ActiveEffectHandles 这个数组内移除
  92. ActiveEffectHandles.FindAndRemoveChecked(Handle);
  93. }
  94. }
  95. }

创建GE_FireArea和BP_FireArea

FireArea需要把Applied Effects设置喂Apply Overlay

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/484629
推荐阅读
相关标签
  

闽ICP备14008679号