// CharacterBase.h// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include"CoreMinimal.h"#include"GameFramework/Character.h"#include"AbilitySystemInterface.h"#include"AbilitySystemComponent.h"#include"Abilities/GameplayAbility.h"#include"CharacterBase.generated.h"classUAttributeSetBase;UCLASS()classABILITYSYSTEM_API ACharacterBase :public ACharacter,public IAbilitySystemInterface
{GENERATED_BODY()public:// Sets default values for this character's propertiesACharacterBase();protected:// Called when the game starts or when spawnedvirtualvoidBeginPlay() override;public:// Called every framevirtualvoidTick(float DeltaTime) override;// Called to bind functionality to inputvirtualvoidSetupPlayerInputComponent(classUInputComponent* PlayerInputComponent) override;UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category ="CharacterBase")
UAbilitySystemComponent* AbilitySystemComp;UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category ="CharacterBase")
UAttributeSetBase * AttributeSetBaseComp;virtual UAbilitySystemComponent*GetAbilitySystemComponent()const;UFUNCTION(BlueprintCallable, Category ="CharacterBase")voidAquireAbility(TSubclassOf<UGameplayAbility> AbilityToAquire);//血量UFUNCTION()voidOnHealthChanged(float Health,float MaxHealth);UFUNCTION(BlueprintImplementableEvent, Category ="CharacterBase", meta =(DisplayName ="OnHealthChanged"))voidBP_OnHealthChanged(float Health,float MaxHealth);//ManaUFUNCTION()voidOnManaChanged(float Mana,float MaxMana);UFUNCTION(BlueprintImplementableEvent, Category ="CharacterBase", meta =(DisplayName ="OnManaChanged"))voidBP_OnManaChanged(float Mana,float MaxMana);// StengthUFUNCTION()voidOnStrengthChanged(float Strength,float MaxStrength);UFUNCTION(BlueprintImplementableEvent, Category ="CharacterBase", meta =(DisplayName ="OnStrengthChanged"))voidBP_OnStrengthChanged(float Strength,float MaxStrength);UFUNCTION(BlueprintImplementableEvent, Category ="CharacterBase", meta =(DisplayName ="Die"))voidBP_Die();UFUNCTION(BlueprintCallable, Category ="CharacterBase")boolIsOtherHostile(ACharacterBase * Other);
uint8 GetTeamID()const;protected:bool bIsDead;
uint8 TeamID;voidAutoDeterminTeamIDbyControllerType();voidDead();};
// CharacterBase.cpp// Fill out your copyright notice in the Description page of Project Settings.#include"CharacterBase.h"# include"AttributeSetBase.h"# include"GameFramework/PlayerController.h"# include"AIController.h"# include"BrainComponent.h"// Sets default values
ACharacterBase::ACharacterBase(){// 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;
AbilitySystemComp = CreateDefaultSubobject<UAbilitySystemComponent>("AbilitySystemComp");
AttributeSetBaseComp = CreateDefaultSubobject<UAttributeSetBase>("AttributeSetBaseComp");
bIsDead =false;
TeamID =255;}// Called when the game starts or when spawnedvoid ACharacterBase::BeginPlay(){
Super::BeginPlay();
AttributeSetBaseComp->OnHealthChange.AddDynamic(this,&ACharacterBase::OnHealthChanged);
AttributeSetBaseComp->OnManaChange.AddDynamic(this,&ACharacterBase::OnManaChanged);
AttributeSetBaseComp->OnStrengthChange.AddDynamic(this,&ACharacterBase::OnStrengthChanged);AutoDeterminTeamIDbyControllerType();}// Called every framevoid ACharacterBase::Tick(float DeltaTime){
Super::Tick(DeltaTime);}// Called to bind functionality to inputvoid ACharacterBase::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent){
Super::SetupPlayerInputComponent(PlayerInputComponent);}
UAbilitySystemComponent * ACharacterBase::GetAbilitySystemComponent()const{return AbilitySystemComp;}void ACharacterBase::AquireAbility(TSubclassOf<UGameplayAbility> AbilityToAquire){if(AbilitySystemComp){if(HasAuthority()&& AbilityToAquire){
AbilitySystemComp->GiveAbility(FGameplayAbilitySpec(AbilityToAquire,1,0));}
AbilitySystemComp->InitAbilityActorInfo(this,this);}}//血量void ACharacterBase::OnHealthChanged(float Health,float MaxHealth){if(Health<=0.0f&&!bIsDead){
bIsDead =true;Dead();BP_Die();}BP_OnHealthChanged(Health, MaxHealth);}// Manavoid ACharacterBase::OnManaChanged(float Mana,float MaxMana){BP_OnManaChanged(Mana, MaxMana);}// Strengthvoid ACharacterBase::OnStrengthChanged(float Strength,float MaxStrength){BP_OnStrengthChanged(Strength, MaxStrength);}bool ACharacterBase::IsOtherHostile(ACharacterBase * Other){return TeamID != Other->GetTeamID();}
uint8 ACharacterBase::GetTeamID()const{return TeamID;}void ACharacterBase::AutoDeterminTeamIDbyControllerType(){if(GetController()&&GetController()->IsPlayerController())
TeamID =0;{}}void ACharacterBase::Dead(){
APlayerController * PC = Cast <APlayerController>(GetController());if(PC){
PC->DisableInput(PC);}
AAIController * AIC = Cast<AAIController>(GetController());if(AIC){
AIC->GetBrainComponent()->StopLogic("Dead");}}