完成Bot接入GAS
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -3,19 +3,38 @@
|
||||
|
||||
#include "BotCharacter.h"
|
||||
|
||||
#include "AbilitySystemComponent.h"
|
||||
#include "Abilities/GameplayAbility.h"
|
||||
#include "Components/MainAbilitySystemComponent.h"
|
||||
#include "GameplayEffect.h"
|
||||
#include "MainAttributeSet.h"
|
||||
|
||||
// Sets default values
|
||||
ABotCharacter::ABotCharacter()
|
||||
{
|
||||
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
|
||||
PrimaryActorTick.bCanEverTick = true;
|
||||
|
||||
AbilitySystemComponent = CreateDefaultSubobject<UMainAbilitySystemComponent>(TEXT("MainAbilitySystemComponent"));
|
||||
AbilitySystemComponent->SetIsReplicated(true);
|
||||
AbilitySystemComponent->SetReplicationMode(EGameplayEffectReplicationMode::Minimal);
|
||||
|
||||
AttributeSet = CreateDefaultSubobject<UMainAttributeSet>(TEXT("AttributeSet"));
|
||||
}
|
||||
|
||||
// Called when the game starts or when spawned
|
||||
void ABotCharacter::BeginPlay()
|
||||
{
|
||||
Super::BeginPlay();
|
||||
|
||||
InitAbilityActorInfo();
|
||||
}
|
||||
|
||||
void ABotCharacter::PossessedBy(AController* NewController)
|
||||
{
|
||||
Super::PossessedBy(NewController);
|
||||
|
||||
InitAbilityActorInfo();
|
||||
}
|
||||
|
||||
// Called every frame
|
||||
@@ -32,3 +51,62 @@ void ABotCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputCompon
|
||||
|
||||
}
|
||||
|
||||
UAbilitySystemComponent* ABotCharacter::GetAbilitySystemComponent() const
|
||||
{
|
||||
return AbilitySystemComponent;
|
||||
}
|
||||
|
||||
void ABotCharacter::InitAbilityActorInfo()
|
||||
{
|
||||
if (!AbilitySystemComponent)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
AbilitySystemComponent->InitAbilityActorInfo(this, this);
|
||||
|
||||
if (HasAuthority())
|
||||
{
|
||||
GiveDefaultAbilities();
|
||||
ApplyDefaultAttributeEffect();
|
||||
}
|
||||
}
|
||||
|
||||
void ABotCharacter::GiveDefaultAbilities()
|
||||
{
|
||||
if (bHasGivenDefaultAbilities || !AbilitySystemComponent)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (const TSubclassOf<UGameplayAbility>& AbilityClass : DefaultAbilities)
|
||||
{
|
||||
if (AbilityClass)
|
||||
{
|
||||
AbilitySystemComponent->GiveAbility(FGameplayAbilitySpec(AbilityClass, 1, INDEX_NONE));
|
||||
}
|
||||
}
|
||||
|
||||
bHasGivenDefaultAbilities = true;
|
||||
}
|
||||
|
||||
void ABotCharacter::ApplyDefaultAttributeEffect()
|
||||
{
|
||||
if (bHasAppliedDefaultAttributeEffect || !AbilitySystemComponent || !DefaultAttributeEffect)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
FGameplayEffectContextHandle EffectContextHandle = AbilitySystemComponent->MakeEffectContext();
|
||||
EffectContextHandle.AddSourceObject(this);
|
||||
|
||||
const FGameplayEffectSpecHandle EffectSpecHandle =
|
||||
AbilitySystemComponent->MakeOutgoingSpec(DefaultAttributeEffect, 1.f, EffectContextHandle);
|
||||
|
||||
if (EffectSpecHandle.IsValid())
|
||||
{
|
||||
AbilitySystemComponent->ApplyGameplayEffectSpecToSelf(*EffectSpecHandle.Data.Get());
|
||||
bHasAppliedDefaultAttributeEffect = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,11 +3,17 @@
|
||||
#pragma once
|
||||
|
||||
#include "CoreMinimal.h"
|
||||
#include "AbilitySystemInterface.h"
|
||||
#include "GameFramework/Character.h"
|
||||
#include "BotCharacter.generated.h"
|
||||
|
||||
class UAbilitySystemComponent;
|
||||
class UAttributeSet;
|
||||
class UGameplayAbility;
|
||||
class UGameplayEffect;
|
||||
|
||||
UCLASS()
|
||||
class PVPDEMO_API ABotCharacter : public ACharacter
|
||||
class PVPDEMO_API ABotCharacter : public ACharacter, public IAbilitySystemInterface
|
||||
{
|
||||
GENERATED_BODY()
|
||||
|
||||
@@ -15,10 +21,27 @@ public:
|
||||
// Sets default values for this character's properties
|
||||
ABotCharacter();
|
||||
|
||||
UFUNCTION(BlueprintCallable)
|
||||
virtual UAbilitySystemComponent* GetAbilitySystemComponent() const override;
|
||||
|
||||
virtual void PossessedBy(AController* NewController) override;
|
||||
|
||||
protected:
|
||||
// Called when the game starts or when spawned
|
||||
virtual void BeginPlay() override;
|
||||
|
||||
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="GAS")
|
||||
TObjectPtr<UAbilitySystemComponent> AbilitySystemComponent;
|
||||
|
||||
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="GAS")
|
||||
TObjectPtr<UAttributeSet> AttributeSet;
|
||||
|
||||
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="GAS")
|
||||
TArray<TSubclassOf<UGameplayAbility>> DefaultAbilities;
|
||||
|
||||
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="GAS")
|
||||
TSubclassOf<UGameplayEffect> DefaultAttributeEffect;
|
||||
|
||||
public:
|
||||
// Called every frame
|
||||
virtual void Tick(float DeltaTime) override;
|
||||
@@ -26,4 +49,11 @@ public:
|
||||
// Called to bind functionality to input
|
||||
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
|
||||
|
||||
private:
|
||||
void InitAbilityActorInfo();
|
||||
void GiveDefaultAbilities();
|
||||
void ApplyDefaultAttributeEffect();
|
||||
|
||||
bool bHasGivenDefaultAbilities = false;
|
||||
bool bHasAppliedDefaultAttributeEffect = false;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user