天天看点

UE5/C++ 基于GAS的Combo连击 4.3 连击数的注册

打开FightComponent.h,添加两个方法GetGamePlayAbility和RegisterCombatAttack

#pragma once

#include "CoreMinimal.h"
#include "../Game/Abilities/MMOARPGAbilitySystemComponent.h"
#include "SimpleCombatType.h"
#include "FightComponent.generated.h"

class UMMOARPGGameplayAbility;
/**
 * 
 */
UCLASS()
class MMOARPGGAME_API UFightComponent : public UActorComponent
{
	GENERATED_BODY()

	UPROPERTY()
	TWeakObjectPtr<UMMOARPGAbilitySystemComponent>AbilitySystemComponent;
    
    //连击检测结构体实例对象
	FSimpleCombatCheck CombatAttack;
public:
	UFightComponent();
    
	FGameplayAbilitySpecHandle AddAbility(TSubclassOf<UGameplayAbility>InNewAbility);
    
    //通过技能名获取指定技能
	UMMOARPGGameplayAbility* GetGamePlayAbility(const FName&InKey);

    //注册连击数
	void RegisterCombatAttack(FSimpleCombatCheck &InCombatCheck,const FName&InKey);

	void NormalAttack(const FName& InKey);

	FSimpleCombatCheck* GetSimpleCombatInfo() { return &CombatAttack; }
protected:
	virtual void BeginPlay()override;

	TMap<FName, FGameplayAbilitySpecHandle>Skills;
};
           

进行实现

GetGamePlayAbility方法,通过技能名返回对应技能

UMMOARPGGameplayAbility* UFightComponent::GetGamePlayAbility(const FName& InKey)
{
	//通过技能名找到对应技能实例Handle
	if (FGameplayAbilitySpecHandle*InHandle=Skills.Find(InKey))
	{
		//判断技能组件是否存在
		if (AbilitySystemComponent.IsValid())
		{
			//通过技能实例Handle找到技能实例
			if (FGameplayAbilitySpec*Spec=AbilitySystemComponent->FindAbilitySpecFromHandle(*InHandle))
			{
				//从技能实例中拿到技能,转成自己的GameplayAbility
				return Cast<UMMOARPGGameplayAbility>(Spec->Ability);
			}
		}
	}
	return nullptr;
}
           

RegisterCombatAttack方法

void UFightComponent::RegisterCombatAttack(FSimpleCombatCheck &InCombatCheck, const FName& InKey)
{
	InCombatCheck.Character = MMOARPGCharacterBase.Get();
	InCombatCheck.CombatKey = InKey;

	if (UMMOARPGGameplayAbility*GameplayAbility=GetGamePlayAbility(InKey))
	{
		//获取技能的Montage动画片段数量,赋值给最大连击数
		InCombatCheck.MaxIndex=GameplayAbility->GetCompositeSectionsNumber();
	}
	else
	{
		//否则默认连击数为4
		InCombatCheck.MaxIndex = 4;
	}
}
           

在FightComponent的BeginPlay中调用,

void UFightComponent::BeginPlay()
{
	Super::BeginPlay();

	//获取到CharacterBase的弱指针
	MMOARPGCharacterBase = Cast<AMMOARPGCharacterBase>(GetOwner());
	if (MMOARPGCharacterBase.IsValid())
	{
		//获取技能系统组件
		AbilitySystemComponent = Cast<UMMOARPGAbilitySystemComponent>(MMOARPGCharacterBase->GetAbilitySystemComponent());

		if (GetWorld())
		{
			//获取到GameState
			if (AMMOARPGGameState*InGameState=GetWorld()->GetGameState<AMMOARPGGameState>())
			{
				//通过GameState拿到技能数据表格
				if (FCharacterSkillTable* InSkillTable = InGameState->GetFCharacterSkillTable(MMOARPGCharacterBase->GetID()))
				{
					//将技能注册结果添加进Map
					Skills.Add(TEXT("NormalAttack"), AddAbility(InSkillTable->NormalAttack));
				}
			}
			//初始化技能信息
			AbilitySystemComponent->InitAbilityActorInfo(MMOARPGCharacterBase.Get(), MMOARPGCharacterBase.Get());
		}
        //注册连击
		RegisterCombatAttack(CombatAttack,TEXT("NormalAttack"));
	}
}
           

继续阅读