伤害系统
给敌人创建血条
- 首先添加一个UI界面用来显示敌人血条
- 设置背景图像为黑色半透明
- 填充颜色
- 给敌人类添加两种状态表示血量与最大血量,添加一个
UWidegtComponet组件与UProgressBar组件
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Enemy Stats")
float Health;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Enemy Stats")
float MaxHealth;
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Enemy Stats")
class UWidgetComponent* HealthBarWidgetComponent;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Enemy Stats")
class UProgressBar* HealthBar;
MaxHealth = 100.f;
Health = MaxHealth;
设置WidgetComponent组件
- 头文件:
- #include “Components/WidgetComponent.h”:使用WidgetComponent就要使用
- #include “Blueprint/UserWidget.h”:获取到User内部对象时需要使用
- #include “Components/ProgressBar.h”:获取调用ProgressBar需要使用
- 创建UWidgetComponent组件
HealthBarWidgetComponent = CreateDefaultSubobject<UWidgetComponent>(TEXT("HealthBarWidgetComponent"));
HealthBarWidgetComponent->SetupAttachment(GetRootComponent());
HealthBarWidgetComponent->SetWidgetSpace(EWidgetSpace::Screen);
HealthBarWidgetComponent->SetDrawSize(FVector2D(125.f, 10.f));
HealthBarWidgetComponent->SetWorldLocation(FVector(0.f, 0.f, 50.f));
void ABaseEnemy::BeginPlay()
{
Super::BeginPlay();
ChaseVolume->OnComponentBeginOverlap.AddDynamic(this, &ABaseEnemy::OnChaseVolumeOverlapBegin);
ChaseVolume->OnComponentEndOverlap.AddDynamic(this, &ABaseEnemy::OnChaseVolumeOverlapEnd);
AttackVolume->OnComponentBeginOverlap.AddDynamic(this, &ABaseEnemy::OnAttackVolumeOverlapBegin);
AttackVolume->OnComponentEndOverlap.AddDynamic(this, &ABaseEnemy::OnAttackVolumeOverlapEnd);
HealthBar = Cast<UProgressBar>(HealthBarWidgetComponent->GetUserWidgetObject()->GetWidgetFromName("HealthBar"));
HealthBar->SetPercent(Health / MaxHealth);
AIController = Cast<AAIController>(GetController());
}
- 运行结果
血条进入敌人追逐区显示
- 一开始敌人血条是不显示的,只有主角进入了敌人追逐区才开始显示血条,出了追逐区也不显示
HealthBar = Cast<UProgressBar>(HealthBarWidgetComponent->GetUserWidgetObject()->GetWidgetFromName("HealthBar"));
HealthBar->SetPercent(Health / MaxHealth);
HealthBar->SetVisibility(ESlateVisibility::Hidden);
void ABaseEnemy::OnChaseVolumeOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (OtherActor)
{
AMainPlayer* Player = Cast<AMainPlayer>(OtherActor);
if (Player)
{
HealthBar->SetVisibility(ESlateVisibility::Visible);
MoveToTarget(Player);
}
}
}
void ABaseEnemy::OnChaseVolumeOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
if (OtherActor)
{
AMainPlayer* Player = Cast<AMainPlayer>(OtherActor);
if (Player)
{
if (AIController)
{
HealthBar->SetVisibility(ESlateVisibility::Hidden);
AIController->StopMovement();
}
}
}
}
触发伤害思路
- 我们可以在武器上加一个碰撞器,当这个碰撞器碰撞到敌人的时候就开启伤害,如果没有就躲避的伤害
- 给每把剑的骨骼添加一个Socket
触发伤害需求
- 因为要使用UE中内置的直接伤害附加的功能,类似爆炸物那一节,所以我们要在武器类中新建一个盒子碰撞触发器,新建一个伤害值,新建一个伤害类型,新建一个伤害发起者
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Weapon|Attack")
class UBoxComponent* AttackCollision;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon|Attack")
float Damage;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon|Attack")
TSubclassOf<UDamageType> DamageTyClass;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Weapon|Attack")
class AController* WeaponOwner;
- 新建伤害开始触发事件与结束事件,两个动态切换碰撞函数,我只需要在挥剑的那个瞬间去切换需要在动画的notify中去切换的所以需要添加反射
UFUNCTION()
void OnAttackCollisionOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
UFUNCTION()
void OnAttackCollisionOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
UFUNCTION(BlueprintCallable)
void ActiveAttackCollision();
UFUNCTION(BlueprintCallable)
void DeactiveAttackCollision();
- 初始化,创建BoxComponent需要头文件:#include “Components/BoxComponent.h”
AttackCollision = CreateDefaultSubobject<UBoxComponent>(TEXT("AttackCollision"));
AttackCollision->SetupAttachment(DisplayMesh, "WeaponSocket");
DeactiveAttackCollision();
Damage = 25.f;
void AWeaponItem::BeginPlay()
{
Super::BeginPlay();
AttackCollision->OnComponentBeginOverlap.AddDynamic(this, &AWeaponItem::OnAttackCollisionOverlapBegin);
AttackCollision->OnComponentEndOverlap.AddDynamic(this, &AWeaponItem::OnAttackCollisionOverlapEnd);
}
void AWeaponItem::OnAttackCollisionOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
}
void AWeaponItem::OnAttackCollisionOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
}
void AWeaponItem::ActiveAttackCollision()
{
AttackCollision->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
AttackCollision->SetCollisionObjectType(ECollisionChannel::ECC_WorldDynamic);
AttackCollision->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
AttackCollision->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Overlap);
}
void AWeaponItem::DeactiveAttackCollision()
{
AttackCollision->SetCollisionEnabled(ECollisionEnabled::NoCollision);
}
WeaponItem.cpp
#include "WeaponItem.h"
#include "Components/SkeletalMeshComponent.h"
#include "Characters/Player/MainPlayer.h"
#include "Engine/SkeletalMeshSocket.h"
#include "Kismet/GameplayStatics.h"
#include "Sound/SoundCue.h"
#include "Particles/ParticleSystemComponent.h"
#include "UObject/ConstructorHelpers.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Components/BoxComponent.h"
AWeaponItem::AWeaponItem()
{
if (DisplayMesh)
{
DisplayMesh->DestroyComponent();
}
else
{
}
DisplayMesh=CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("DisplaySkeletalMesh"));
DisplayMesh->SetupAttachment(GetRootComponent());
ActiveDisplayMeshCollision();
AttackCollision = CreateDefaultSubobject<UBoxComponent>(TEXT("AttackCollision"));
AttackCollision->SetupAttachment(DisplayMesh, "WeaponSocket");
DeactiveAttackCollision();
static ConstructorHelpers::FObjectFinder<USoundCue> SoundCueAsset(TEXT("SoundCue'/Game/Assets/Audios/Blade_Cue.Blade_Cue'"));
if (SoundCueAsset.Succeeded())
{
OnEquipSound = SoundCueAsset.Object;
}
bOnEquipParticle = false;
WeaponState = EWeaponState::EWS_CanPickUp;
Damage = 25.f;
}
void AWeaponItem::BeginPlay()
{
Super::BeginPlay();
AttackCollision->OnComponentBeginOverlap.AddDynamic(this, &AWeaponItem::OnAttackCollisionOverlapBegin);
AttackCollision->OnComponentEndOverlap.AddDynamic(this, &AWeaponItem::OnAttackCollisionOverlapEnd);
}
void AWeaponItem::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
Super::OnOverlapBegin(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult);
if (OtherActor && WeaponState == EWeaponState::EWS_CanPickUp)
{
AMainPlayer* Player = Cast<AMainPlayer>(OtherActor);
if (Player)
{
Player->OverlapWeapon = this;
}
}
}
void AWeaponItem::OnOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
Super::OnOverlapEnd(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex);
if (OtherActor)
{
AMainPlayer* Player = Cast<AMainPlayer>(OtherActor);
if (Player && Player->OverlapWeapon == this)
{
Player->OverlapWeapon = nullptr;
}
}
}
void AWeaponItem::Equip(AMainPlayer* Player)
{
if (Player && !Player->GetMovementComponent()->IsFalling())
{
WeaponState = EWeaponState::EWS_Equip;
DeactiveDisplayMeshCollision();
const USkeletalMeshSocket* RightHandSocker = Player->GetMesh()->GetSocketByName(TEXT("RightHandSocket"));
if (RightHandSocker)
{
RightHandSocker->AttachActor(this, Player->GetMesh());
Player->bIsWeapon = true;
Player->EquipWeapon = this;
Player->OverlapWeapon = nullptr;
bRotate = false;
if (OnEquipSound)
{
UGameplayStatics::PlaySound2D(this, OnEquipSound);
}
}
}
}
void AWeaponItem::UnEuip(AMainPlayer* Player)
{
if (Player && !Player->GetMovementComponent()->IsFalling() && !Player->bIsAttacking)
{
WeaponState = EWeaponState::EWS_CanPickUp;
ActiveDisplayMeshCollision();
Player->bIsWeapon = false;
Player->EquipWeapon = nullptr;
if (Player->OverlapWeapon == nullptr)
{
Player->OverlapWeapon = this;
}
DetachFromActor(FDetachmentTransformRules::KeepWorldTransform);
SetActorRotation(FRotator(0.f));
SetActorScale3D(FVector(1.f));
bRotate = true;
}
}
void AWeaponItem::ActiveDisplayMeshCollision()
{
DisplayMesh->SetCollisionEnabled(ECollisionEnabled::PhysicsOnly);
DisplayMesh->SetCollisionObjectType(ECollisionChannel::ECC_WorldStatic);
DisplayMesh->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
DisplayMesh->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Block);
}
void AWeaponItem::DeactiveDisplayMeshCollision()
{
DisplayMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);
}
void AWeaponItem::OnAttackCollisionOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
}
void AWeaponItem::OnAttackCollisionOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
}
void AWeaponItem::ActiveAttackCollision()
{
AttackCollision->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
AttackCollision->SetCollisionObjectType(ECollisionChannel::ECC_WorldDynamic);
AttackCollision->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
AttackCollision->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Overlap);
}
void AWeaponItem::DeactiveAttackCollision()
{
AttackCollision->SetCollisionEnabled(ECollisionEnabled::NoCollision);
}
在蒙太奇中添加攻击通知激活伤害碰撞
- 在主角的蒙太奇中添加四个通知,跟当时添加攻击结束通知差不多
- 然后在动画蓝图中进行调用函数
- 将三把剑的盒子碰撞检测大小调整一下
给敌人添加触发器
- 基本与上面给主角添加伤害需求的思路一致
- 先给敌人添加两个骨骼
触发伤害需求
- 再来就是编辑敌人类的变量需求与基本逻辑了,两个触发盒子因为是两只腿的攻击,新建伤害值,伤害类型
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Attack")
class UBoxComponent* LeftAttackCollision;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Attack")
UBoxComponent* RightAttackCollision;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Attack")
float Damage;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Attack")
TSubclassOf<UDamageType> DamageTyClass;
- 不用说,现在是四个伤害触发事件的新建,四个动态切换碰撞的函数
UFUNCTION()
void OnLeftAttackCollisionOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
UFUNCTION()
void OnLeftAttackCollisionOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
UFUNCTION()
void OnRightAttackCollisionOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
UFUNCTION()
void OnRightAttackCollisionOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
UFUNCTION(BlueprintCallable)
void ActiveLeftAttackCollision();
UFUNCTION(BlueprintCallable)
void DeactiveLeftAttackCollision();
UFUNCTION(BlueprintCallable)
void ActiveRightAttackCollision();
UFUNCTION(BlueprintCallable)
void DeactiveRightAttackCollision();
LeftAttackCollision = CreateDefaultSubobject<UBoxComponent>(TEXT("LeftAttackCollision"));
LeftAttackCollision->SetupAttachment(GetMesh(), "LeftAttackSocket");
DeactiveLeftAttackCollision();
RightAttackCollision = CreateDefaultSubobject<UBoxComponent>(TEXT("RightAttackCollision"));
RightAttackCollision->SetupAttachment(GetMesh(), "RightAttackSocket");
DeactiveRightAttackCollision();
Damage = 10.f;
LeftAttackCollision->OnComponentBeginOverlap.AddDynamic(this, &ABaseEnemy::OnLeftAttackCollisionOverlapBegin);
LeftAttackCollision->OnComponentEndOverlap.AddDynamic(this, &ABaseEnemy::OnLeftAttackCollisionOverlapEnd);
RightAttackCollision->OnComponentBeginOverlap.AddDynamic(this, &ABaseEnemy::OnRightAttackCollisionOverlapBegin);
RightAttackCollision->OnComponentEndOverlap.AddDynamic(this, &ABaseEnemy::OnRightAttackCollisionOverlapEnd);
void ABaseEnemy::ActiveLeftAttackCollision()
{
LeftAttackCollision->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
LeftAttackCollision->SetCollisionObjectType(ECollisionChannel::ECC_WorldDynamic);
LeftAttackCollision->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
LeftAttackCollision->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Overlap);
}
void ABaseEnemy::DeactiveLeftAttackCollision()
{
LeftAttackCollision->SetCollisionEnabled(ECollisionEnabled::NoCollision);
}
void ABaseEnemy::ActiveRightAttackCollision()
{
RightAttackCollision->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
RightAttackCollision->SetCollisionObjectType(ECollisionChannel::ECC_WorldDynamic);
RightAttackCollision->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
RightAttackCollision->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Overlap);
}
void ABaseEnemy::DeactiveRightAttackCollision()
{
RightAttackCollision->SetCollisionEnabled(ECollisionEnabled::NoCollision);
}
BaseEnemy.cpp
#include "BaseEnemy.h"
#include "Components/SphereComponent.h"
#include "Components/SkeletalMeshComponent.h"
#include "Components/CapsuleComponent.h"
#include "AIController.h"
#include "Characters/Player/MainPlayer.h"
#include "Animation/AnimInstance.h"
#include "Kismet/KismetMathLibrary.h"
#include "Kismet/GameplayStatics.h"
#include "Components/WidgetComponent.h"
#include "Blueprint/UserWidget.h"
#include "Components/ProgressBar.h"
#include "Components/BoxComponent.h"
ABaseEnemy::ABaseEnemy()
{
PrimaryActorTick.bCanEverTick = true;
ChaseVolume = CreateDefaultSubobject<USphereComponent>(TEXT("ChaseVolume"));
ChaseVolume->SetupAttachment(GetRootComponent());
ChaseVolume->InitSphereRadius(800.f);
ChaseVolume->SetCollisionObjectType(ECollisionChannel::ECC_WorldDynamic);
ChaseVolume->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
ChaseVolume->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Overlap);
AttackVolume = CreateDefaultSubobject<USphereComponent>(TEXT("AttackVolume"));
AttackVolume->SetupAttachment(GetRootComponent());
AttackVolume->InitSphereRadius(100.f);
AttackVolume->SetCollisionObjectType(ECollisionChannel::ECC_WorldDynamic);
AttackVolume->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
AttackVolume->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Overlap);
HealthBarWidgetComponent = CreateDefaultSubobject<UWidgetComponent>(TEXT("HealthBarWidgetComponent"));
HealthBarWidgetComponent->SetupAttachment(GetRootComponent());
HealthBarWidgetComponent->SetWidgetSpace(EWidgetSpace::Screen);
HealthBarWidgetComponent->SetDrawSize(FVector2D(125.f, 10.f));
HealthBarWidgetComponent->SetWorldLocation(FVector(0.f, 0.f, 50.f));
LeftAttackCollision = CreateDefaultSubobject<UBoxComponent>(TEXT("LeftAttackCollision"));
LeftAttackCollision->SetupAttachment(GetMesh(), "LeftAttackSocket");
DeactiveLeftAttackCollision();
RightAttackCollision = CreateDefaultSubobject<UBoxComponent>(TEXT("RightAttackCollision"));
RightAttackCollision->SetupAttachment(GetMesh(), "RightAttackSocket");
DeactiveRightAttackCollision();
GetMesh()->SetCollisionResponseToChannel(ECollisionChannel::ECC_Camera, ECollisionResponse::ECR_Ignore);
GetCapsuleComponent()->SetCollisionResponseToChannel(ECollisionChannel::ECC_Camera, ECollisionResponse::ECR_Ignore);
AutoPossessAI = EAutoPossessAI::PlacedInWorldOrSpawned;
EnemyMovementStatus = EEnemyMovementStatus::EEMS_Idle;
InterpSpeed = 15.f;
bInterpToPlayer = false;
MaxHealth = 100.f;
Health = MaxHealth;
Damage = 10.f;
}
void ABaseEnemy::BeginPlay()
{
Super::BeginPlay();
ChaseVolume->OnComponentBeginOverlap.AddDynamic(this, &ABaseEnemy::OnChaseVolumeOverlapBegin);
ChaseVolume->OnComponentEndOverlap.AddDynamic(this, &ABaseEnemy::OnChaseVolumeOverlapEnd);
AttackVolume->OnComponentBeginOverlap.AddDynamic(this, &ABaseEnemy::OnAttackVolumeOverlapBegin);
AttackVolume->OnComponentEndOverlap.AddDynamic(this, &ABaseEnemy::OnAttackVolumeOverlapEnd);
LeftAttackCollision->OnComponentBeginOverlap.AddDynamic(this, &ABaseEnemy::OnLeftAttackCollisionOverlapBegin);
LeftAttackCollision->OnComponentEndOverlap.AddDynamic(this, &ABaseEnemy::OnLeftAttackCollisionOverlapEnd);
RightAttackCollision->OnComponentBeginOverlap.AddDynamic(this, &ABaseEnemy::OnRightAttackCollisionOverlapBegin);
RightAttackCollision->OnComponentEndOverlap.AddDynamic(this, &ABaseEnemy::OnRightAttackCollisionOverlapEnd);
HealthBar = Cast<UProgressBar>(HealthBarWidgetComponent->GetUserWidgetObject()->GetWidgetFromName("HealthBar"));
HealthBar->SetPercent(Health / MaxHealth);
HealthBar->SetVisibility(ESlateVisibility::Hidden);
AIController = Cast<AAIController>(GetController());
}
void ABaseEnemy::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (bInterpToPlayer)
{
FRotator LookYaw(0.f, UKismetMathLibrary::FindLookAtRotation(GetActorLocation(), UGameplayStatics::GetPlayerPawn(this, 0)->GetActorLocation()).Yaw, 0.f);
FRotator InterpRotation = FMath::RInterpTo(GetActorRotation(), LookYaw, DeltaTime, InterpSpeed);
SetActorRotation(InterpRotation);
}
}
void ABaseEnemy::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
}
void ABaseEnemy::OnChaseVolumeOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (OtherActor)
{
AMainPlayer* Player = Cast<AMainPlayer>(OtherActor);
if (Player)
{
HealthBar->SetVisibility(ESlateVisibility::Visible);
MoveToTarget(Player);
}
}
}
void ABaseEnemy::OnChaseVolumeOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
if (OtherActor)
{
AMainPlayer* Player = Cast<AMainPlayer>(OtherActor);
if (Player)
{
if (AIController)
{
HealthBar->SetVisibility(ESlateVisibility::Hidden);
AIController->StopMovement();
}
}
}
}
void ABaseEnemy::OnAttackVolumeOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (OtherActor)
{
AMainPlayer* Player = Cast<AMainPlayer>(OtherActor);
if (Player)
{
Player->UpdataAttackTarget();
bAttackVolumeOverlap = true;
AttackBegin();
}
}
}
void ABaseEnemy::OnAttackVolumeOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
if (OtherActor)
{
AMainPlayer* Player = Cast<AMainPlayer>(OtherActor);
if (Player)
{
bAttackVolumeOverlap = false;
if (EnemyMovementStatus!=EEnemyMovementStatus::EEMS_Attacking)
{
MoveToTarget(Player);
}
}
}
}
void ABaseEnemy::MoveToTarget(AMainPlayer* Player)
{
EnemyMovementStatus = EEnemyMovementStatus::EEMS_MoveToTarget;
if (AIController)
{
FAIMoveRequest MoveRequest;
MoveRequest.SetGoalActor(Player);
MoveRequest.SetAcceptanceRadius(10.f);
FNavPathSharedPtr NavPath;
AIController->MoveTo(MoveRequest, &NavPath);
}
}
void ABaseEnemy::AttackBegin()
{
if (AIController)
{
AIController->StopMovement();
}
if (EnemyMovementStatus != EEnemyMovementStatus::EEMS_Attacking)
{
EnemyMovementStatus = EEnemyMovementStatus::EEMS_Attacking;
bInterpToPlayer = true;
UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();
if (AnimInstance && AttackMontage)
{
float PlayRate = FMath::RandRange(0.9f, 1.1f);
FString SectionName = FString::FromInt(FMath::RandRange(1, 3));
AnimInstance->Montage_Play(AttackMontage, PlayRate);
AnimInstance->Montage_JumpToSection(FName(*SectionName), AttackMontage);
}
}
}
void ABaseEnemy::AttackEnd()
{
EnemyMovementStatus = EEnemyMovementStatus::EEMS_Idle;
bInterpToPlayer = false;
if (bAttackVolumeOverlap)
{
AttackBegin();
}
}
void ABaseEnemy::OnLeftAttackCollisionOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
}
void ABaseEnemy::OnLeftAttackCollisionOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
}
void ABaseEnemy::OnRightAttackCollisionOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
}
void ABaseEnemy::OnRightAttackCollisionOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
}
void ABaseEnemy::ActiveLeftAttackCollision()
{
LeftAttackCollision->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
LeftAttackCollision->SetCollisionObjectType(ECollisionChannel::ECC_WorldDynamic);
LeftAttackCollision->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
LeftAttackCollision->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Overlap);
}
void ABaseEnemy::DeactiveLeftAttackCollision()
{
LeftAttackCollision->SetCollisionEnabled(ECollisionEnabled::NoCollision);
}
void ABaseEnemy::ActiveRightAttackCollision()
{
RightAttackCollision->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
RightAttackCollision->SetCollisionObjectType(ECollisionChannel::ECC_WorldDynamic);
RightAttackCollision->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
RightAttackCollision->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Overlap);
}
void ABaseEnemy::DeactiveRightAttackCollision()
{
RightAttackCollision->SetCollisionEnabled(ECollisionEnabled::NoCollision);
}
在蒙太奇中添加攻击通知激活伤害碰撞
- 首先修改攻击触发器的大小
- 在敌人的蒙太奇中添加通知,跟上面主角添加通知差不多
- 编辑动画蓝图即可
设置击中敌人时的特效、音效与伤害
- 给MainPlayer.h与BaseEnemy.h中添加一个粒子系统与声音组件
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Hit Effect")
class UParticleSystem* HitPaticles;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Hit Effect")
class USoundCue* HitSound;
- 在WeaponItem.cpp的攻击开始重叠事件中去添加声音特效与伤害,这个就和之前装备武器与爆炸物传递伤害逻辑差不多
- 正好复习一下,获取插槽的时候必须加const,因为获取插槽的函数返回的是一个const值
void AWeaponItem::OnAttackCollisionOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (OtherActor)
{
ABaseEnemy* BaseEnemy = Cast<ABaseEnemy>(OtherActor);
if (BaseEnemy)
{
if (BaseEnemy->HitPaticles)
{
const USkeletalMeshSocket* WeaponScoket = ((USkeletalMeshComponent*)DisplayMesh)->GetSocketByName("WeaponSocket");
if (WeaponScoket)
{
FVector SocketLocation = WeaponScoket->GetSocketLocation((USkeletalMeshComponent*)DisplayMesh);
UGameplayStatics::SpawnEmitterAtLocation(this, BaseEnemy->HitPaticles, SocketLocation, FRotator(0.f), true);
}
if (BaseEnemy->HitSound)
{
UGameplayStatics::PlaySound2D(this, BaseEnemy->HitSound);
}
if (DamageTyClass)
{
UGameplayStatics::ApplyDamage(BaseEnemy, Damage, WeaponOwner, this, DamageTyClass);
}
}
}
}
}
设置击中玩家时的特效、音效与伤害
- 基本与上面一样,在BaseEnemy.cpp的左右攻击事件中添加粒子音效与伤害
void ABaseEnemy::OnLeftAttackCollisionOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (OtherActor)
{
AMainPlayer* Player = Cast<AMainPlayer>(OtherActor);
if (Player)
{
if (Player->HitPaticles)
{
const USkeletalMeshSocket* AttackScoket = GetMesh()->GetSocketByName("LeftAttackSocket");
if (AttackScoket)
{
FVector SocketLocation = AttackScoket->GetSocketLocation(GetMesh());
UGameplayStatics::SpawnEmitterAtLocation(this, Player->HitPaticles, SocketLocation, FRotator(0.f), true);
}
if (Player->HitSound)
{
UGameplayStatics::PlaySound2D(this, Player->HitSound);
}
if (DamageTyClass)
{
UGameplayStatics::ApplyDamage(Player, Damage, AIController, this, DamageTyClass);
}
}
}
}
}
void ABaseEnemy::OnLeftAttackCollisionOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
}
void ABaseEnemy::OnRightAttackCollisionOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (OtherActor)
{
AMainPlayer* Player = Cast<AMainPlayer>(OtherActor);
if (Player)
{
if (Player->HitPaticles)
{
const USkeletalMeshSocket* AttackScoket = GetMesh()->GetSocketByName("RightAttackSocket");
if (AttackScoket)
{
FVector SocketLocation = AttackScoket->GetSocketLocation(GetMesh());
UGameplayStatics::SpawnEmitterAtLocation(this, Player->HitPaticles, SocketLocation, FRotator(0.f), true);
}
if (Player->HitSound)
{
UGameplayStatics::PlaySound2D(this, Player->HitSound);
}
if (DamageTyClass)
{
UGameplayStatics::ApplyDamage(Player, Damage, AIController, this, DamageTyClass);
}
}
}
}
}
- 将攻击类型添加到敌人上,添加上粒子与音效
敌人受伤死亡分析
- 敌人与主角类中添加死亡函数
- 在敌人类中重写TakeDamage方法接收伤害,注意的是因为MainPlayer的血量UI是写在UI绑定事件蓝图里面在,而敌人血量UI是在C++中编码跟随,所以最后返回写完逻辑返回血量前,要更新一下血条
float TakeDamage(float Damage, struct FDamageEvent const& DamageEvent, AController* EventInstigator, AActor* DamageCauser) override;
void Die();
float ABaseEnemy::TakeDamage(float Damage, FDamageEvent const& DamageEvent, AController* EventInstigator, AActor* DamageCauser)
{
if (Health - Damage <= 0.f)
{
Health = FMath::Clamp(Health - Damage, 0.f, MaxHealth);
Die();
}
else
{
Health -= Damage;
}
HealthBar->SetPercent(Health / MaxHealth);
return Health;
}
按布尔类型播放死亡动画
- 思路:我们还是可以使用notify来通知调用死亡动画,首先在MainPlayer与BaseEnemy中新建一个用于信息通知的函数
UFUNCTION(BlueprintCallable)
void DeathEnd();
- 然后编辑MainPlayer死亡函数的逻辑,首先主角状态设置为死亡,判断是否持剑,持剑就关闭武器所有碰撞
void AMainPlayer::Die()
{
SetMovementStatus(EPlayerMovementStatus::EPMS_Dead);
if (EquipWeapon)
{
EquipWeapon->DeactiveAttackCollision();
EquipWeapon->DeactiveDisplayMeshCollision();
}
}
- 编辑BaseEnemy死亡函数逻辑,首先设置状态为死亡 ,关闭所有的碰撞,因为敌人已死亡更新主角攻击目标
void ABaseEnemy::Die()
{
EnemyMovementStatus = EEnemyMovementStatus::EEMS_Dead;
DeactiveLeftAttackCollision();
DeactiveRightAttackCollision();
ChaseVolume->SetCollisionEnabled(ECollisionEnabled::NoCollision);
AttackVolume->SetCollisionEnabled(ECollisionEnabled::NoCollision);
GetCapsuleComponent()->SetCollisionEnabled(ECollisionEnabled::NoCollision);
Cast<AMainPlayer>(UGameplayStatics::GetPlayerPawn(this, 0))->UpdataAttackTarget();
}
- 编辑他们的动画蓝图,添加Blend Poses by bool播放死亡动画,Enemy状态为死亡时播放死亡动画,否则就正常播放其他动画,记得将死亡的循环播放动画关闭
限制玩家死亡后的一切活动状态
- 思路:我们在MainPlayer与BaseEnemy中新建一个专门用来判断玩家是否存活状态的函数,如果玩家不存活就停止一切活动
FORCEINLINE bool IsAlive() { return MovementStatus != EPlayerMovementStatus::EPMS_Dead; }
FORCEINLINE bool IsAlive() { return EnemyMovementStatus != EEnemyMovementStatus::EEMS_Dead; }
bool HasValidTarget();
bool ABaseEnemy::HasValidTarget()
{
return Cast<AMainPlayer>(UGameplayStatics::GetPlayerPawn(this, 0))->MovementStatus != EPlayerMovementStatus::EPMS_Dead;
}
MainPlayer.cpp
- 然后在MainPlayer中添加判断禁止死亡后的一切活动,也就是都加上IsAlive判断
#include "MainPlayer.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/InputComponent.h"
#include "GameFramework/PlayerController.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GamePlay/WeaponItem.h"
#include "Animation/AnimInstance.h"
#include "Characters/Enemy/BaseEnemy.h"
#include "Kismet/KismetMathLibrary.h"
AMainPlayer::AMainPlayer()
{
PrimaryActorTick.bCanEverTick = true;
SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
SpringArm->SetupAttachment(GetRootComponent());
SpringArm->TargetArmLength = 600.f;
SpringArm->bUsePawnControlRotation = true;
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(SpringArm, NAME_None);
FollowCamera->bUsePawnControlRotation = false;
GetCapsuleComponent()->SetCapsuleSize(35.f, 100.f);
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;
GetCharacterMovement()->bOrientRotationToMovement = true;
GetCharacterMovement()->RotationRate = FRotator(0.f, 500.f, 0.f);
GetCharacterMovement()->JumpZVelocity = 400.f;
GetCharacterMovement()->AirControl = 0.15f;
BaseTurnRate = 21.f;
BaseLookUpRate = 21.f;
MaxHealth = 100.f;
Health = MaxHealth;
MaxStamina = 200.f;
Stamina = MaxStamina;
StaminaConsumeRate = 20.f;
ExhaustedStamina = 0.167f;
Coins = 0;
RunningSpeed = 600.f;
SprintSpeed = 900.f;
MovementStatus = EPlayerMovementStatus::EPMS_Normal;
StaminaStatus = EPlayerStaminaStatus::EPSS_Normal;
bLeftShiftDown = false;
InterpSpeed = 15.f;
bInterpToEnemy = false;
}
void AMainPlayer::BeginPlay()
{
Super::BeginPlay();
}
void AMainPlayer::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (!IsAlive())
{
return;
}
switch (StaminaStatus)
{
case EPlayerStaminaStatus::EPSS_Normal:
if (bLeftShiftDown)
{
if (Stamina - StaminaConsumeRate * DeltaTime <= MaxStamina * ExhaustedStamina)
{
StaminaStatus = EPlayerStaminaStatus::EPSS_Exhausted;
}
Stamina -= StaminaConsumeRate * DeltaTime;
SetMovementStatus(EPlayerMovementStatus::EPMS_Sprinting);
}
else
{
Stamina = FMath::Clamp(Stamina + StaminaConsumeRate * DeltaTime, 0.f, MaxStamina);
SetMovementStatus(EPlayerMovementStatus::EPMS_Normal);
}
break;
case EPlayerStaminaStatus::EPSS_Exhausted:
if (bLeftShiftDown)
{
if (Stamina - StaminaConsumeRate * DeltaTime <= 0.f)
{
LeftShiftUp();
StaminaStatus = EPlayerStaminaStatus::EPSS_ExhaustedRecovering;
SetMovementStatus(EPlayerMovementStatus::EPMS_Normal);
}
else
{
Stamina -= StaminaConsumeRate * DeltaTime;
}
}
else
{
StaminaStatus = EPlayerStaminaStatus::EPSS_ExhaustedRecovering;
Stamina = FMath::Clamp(Stamina + StaminaConsumeRate * DeltaTime, 0.f, MaxStamina);
SetMovementStatus(EPlayerMovementStatus::EPMS_Normal);
}
break;
case EPlayerStaminaStatus::EPSS_ExhaustedRecovering:
if (Stamina + StaminaConsumeRate * DeltaTime >= MaxStamina * ExhaustedStamina)
{
StaminaStatus = EPlayerStaminaStatus::EPSS_Normal;
}
Stamina += StaminaConsumeRate * DeltaTime;
LeftShiftUp();
SetMovementStatus(EPlayerMovementStatus::EPMS_Normal);
break;
default:
break;
}
if (bInterpToEnemy && AttackTarget)
{
FRotator LookAtYaw(0.f, UKismetMathLibrary::FindLookAtRotation(GetActorLocation(), AttackTarget->GetActorLocation()).Yaw, 0.f);
FRotator InterpRotation = FMath::RInterpTo(GetActorRotation(), LookAtYaw, DeltaTime, InterpSpeed);
SetActorRotation(InterpRotation);
}
}
void AMainPlayer::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
check(PlayerInputComponent);
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &AMainPlayer::Jump);
PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
PlayerInputComponent->BindAction("Sprint", IE_Pressed, this, &AMainPlayer::LeftShiftDown);
PlayerInputComponent->BindAction("Sprint", IE_Released, this, &AMainPlayer::LeftShiftUp);
PlayerInputComponent->BindAction("Interact", IE_Pressed, this, &AMainPlayer::InteractKeyDown);
PlayerInputComponent->BindAction("Attack", IE_Pressed, this, &AMainPlayer::AttackKeyDown);
PlayerInputComponent->BindAction("Attack", IE_Released, this, &AMainPlayer::AttackKeyUp);
PlayerInputComponent->BindAxis("MoveForward", this, &AMainPlayer::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &AMainPlayer::MoveRight);
PlayerInputComponent->BindAxis("Turn", this, &AMainPlayer::Turn);
PlayerInputComponent->BindAxis("LookUp", this, &AMainPlayer::LookUp);
PlayerInputComponent->BindAxis("TurnRate", this, &AMainPlayer::TurnRate);
PlayerInputComponent->BindAxis("LookUpRate", this, &AMainPlayer::LookUpRate);
}
void AMainPlayer::Jump()
{
if (IsAlive())
{
Super::Jump();
}
}
void AMainPlayer::MoveForward(float value)
{
if (Controller != nullptr && value != 0.f && !(bIsAttacking) && IsAlive())
{
FRotator Rotation = Controller->GetControlRotation();
FRotator YowRotation = FRotator(0.0f, Rotation.Yaw, 0.0f);
FVector Direction = FRotationMatrix(YowRotation).GetUnitAxis(EAxis::X);
AddMovementInput(Direction, value);
}
}
void AMainPlayer::MoveRight(float value)
{
if (Controller != nullptr && value != 0.f && !(bIsAttacking) && IsAlive())
{
FRotator Rotation = Controller->GetControlRotation();
FRotator YowRotation = FRotator(0.0f, Rotation.Yaw, 0.0f);
FVector Direction = FRotationMatrix(YowRotation).GetUnitAxis(EAxis::Y);
AddMovementInput(Direction, value);
}
}
void AMainPlayer::Turn(float Value)
{
if (Value != 0.f && IsAlive())
{
AddControllerYawInput(Value);
}
}
void AMainPlayer::LookUp(float Value)
{
if (IsAlive())
{
if (GetControlRotation().Pitch < 270.f && GetControlRotation().Pitch >180.f && Value > 0.f)
{
return;
}
else if (GetControlRotation().Pitch < 180.f && GetControlRotation().Pitch >45.f && Value < 0.f)
{
return;
}
AddControllerPitchInput(Value);
}
}
void AMainPlayer::TurnRate(float Rate)
{
float Value = Rate * BaseTurnRate * GetWorld()->GetDeltaSeconds();
if (Value != 0.f && IsAlive())
{
AddControllerYawInput(Value);
}
}
void AMainPlayer::LookUpRate(float Rate)
{
if (IsAlive())
{
float Value = Rate * BaseLookUpRate * GetWorld()->GetDeltaSeconds();
if (GetControlRotation().Pitch < 270.f && GetControlRotation().Pitch >180.f && Value > 0.f)
{
return;
}
else if (GetControlRotation().Pitch < 180.f && GetControlRotation().Pitch >45.f && Value < 0.f)
{
return;
}
AddControllerPitchInput(Value);
}
}
void AMainPlayer::AddHealth(float value)
{
Health = FMath::Clamp(Health + value, 0.f, MaxHealth);
}
void AMainPlayer::AddStamina(float value)
{
Stamina = FMath::Clamp(Stamina + value, 0.f, MaxStamina);
}
void AMainPlayer::AddCoin(float value)
{
Coins += value;
}
float AMainPlayer::TakeDamage(float Damage, FDamageEvent const& DamageEvent, AController* EventInstigator, AActor* DamageCauser)
{
if (Health - Damage <= 0.f)
{
Health = FMath::Clamp(Health - Damage, 0.f, MaxHealth);
}
else
{
Health -= Damage;
}
return Health;
}
void AMainPlayer::SetMovementStatus(EPlayerMovementStatus Status)
{
if (IsAlive())
{
MovementStatus = Status;
switch (MovementStatus)
{
case EPlayerMovementStatus::EPMS_Sprinting:
GetCharacterMovement()->MaxWalkSpeed = SprintSpeed;
break;
default:
GetCharacterMovement()->MaxWalkSpeed = RunningSpeed;
break;
}
}
}
void AMainPlayer::InteractKeyDown()
{
if (OverlapWeapon && IsAlive())
{
if (EquipWeapon)
{
EquipWeapon->UnEuip(this);
OverlapWeapon->Equip(this);
}
else
{
OverlapWeapon->Equip(this);
}
}
else
{
if (EquipWeapon)
{
EquipWeapon->UnEuip(this);
}
}
}
void AMainPlayer::AttackKeyDown()
{
if (IsAlive())
{
bAttackKeyDown = true;
if (bIsWeapon)
{
AttackBegin();
}
}
}
void AMainPlayer::AttackBegin()
{
if (!bIsAttacking && IsAlive())
{
bIsAttacking = true;
bInterpToEnemy = true;
UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();
if (AnimInstance && AttackMontage)
{
float PlayRate = FMath::RandRange(1.25f, 1.75f);
FString SectionName = FString::FromInt(FMath::RandRange(1, 2));
AnimInstance->Montage_Play(AttackMontage, PlayRate);
AnimInstance->Montage_JumpToSection(FName(*SectionName), AttackMontage);
}
}
}
void AMainPlayer::AttackEnd()
{
bIsAttacking = false;
bInterpToEnemy = false;
if (bAttackKeyDown && IsAlive())
{
AttackKeyDown();
}
}
void AMainPlayer::UpdataAttackTarget()
{
TArray<AActor*> OVerlappingActors;
GetOverlappingActors(OVerlappingActors,EnemyFilter);
if (OVerlappingActors.Num() == 0)
{
AttackTarget = nullptr;
return;
}
ABaseEnemy* ClosestDistance = nullptr;
float MinDistance = 1000.f;
FVector Loation = GetActorLocation();
for (auto Actor : OVerlappingActors)
{
ABaseEnemy* Enemy = Cast<ABaseEnemy>(Actor);
if (Enemy && Enemy->EnemyMovementStatus != EEnemyMovementStatus::EEMS_Dead)
{
float DistanceToActor = (Enemy->GetActorLocation() - Loation).Size();
if (DistanceToActor < MinDistance)
{
MinDistance = DistanceToActor;
ClosestDistance = Enemy;
}
}
}
AttackTarget = ClosestDistance;
}
void AMainPlayer::Die()
{
SetMovementStatus(EPlayerMovementStatus::EPMS_Dead);
if (EquipWeapon)
{
EquipWeapon->DeactiveAttackCollision();
EquipWeapon->DeactiveDisplayMeshCollision();
}
}
void AMainPlayer::DeathEnd()
{
}
限制敌人死亡后的一切活动状态
- 基本跟限制玩家差不多,只不过有些位置得多一个玩家是否存活的判断
BaseEnemy.cpp
#include "BaseEnemy.h"
#include "Components/SphereComponent.h"
#include "Components/SkeletalMeshComponent.h"
#include "Components/CapsuleComponent.h"
#include "AIController.h"
#include "Characters/Player/MainPlayer.h"
#include "Animation/AnimInstance.h"
#include "Kismet/KismetMathLibrary.h"
#include "Kismet/GameplayStatics.h"
#include "Components/WidgetComponent.h"
#include "Blueprint/UserWidget.h"
#include "Components/ProgressBar.h"
#include "Components/BoxComponent.h"
#include "Sound/SoundCue.h"
#include "Engine/SkeletalMeshSocket.h"
#include "Characters/Player/MainPlayer.h"
ABaseEnemy::ABaseEnemy()
{
PrimaryActorTick.bCanEverTick = true;
ChaseVolume = CreateDefaultSubobject<USphereComponent>(TEXT("ChaseVolume"));
ChaseVolume->SetupAttachment(GetRootComponent());
ChaseVolume->InitSphereRadius(800.f);
ChaseVolume->SetCollisionObjectType(ECollisionChannel::ECC_WorldDynamic);
ChaseVolume->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
ChaseVolume->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Overlap);
AttackVolume = CreateDefaultSubobject<USphereComponent>(TEXT("AttackVolume"));
AttackVolume->SetupAttachment(GetRootComponent());
AttackVolume->InitSphereRadius(100.f);
AttackVolume->SetCollisionObjectType(ECollisionChannel::ECC_WorldDynamic);
AttackVolume->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
AttackVolume->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Overlap);
HealthBarWidgetComponent = CreateDefaultSubobject<UWidgetComponent>(TEXT("HealthBarWidgetComponent"));
HealthBarWidgetComponent->SetupAttachment(GetRootComponent());
HealthBarWidgetComponent->SetWidgetSpace(EWidgetSpace::Screen);
HealthBarWidgetComponent->SetDrawSize(FVector2D(125.f, 10.f));
HealthBarWidgetComponent->SetWorldLocation(FVector(0.f, 0.f, 50.f));
LeftAttackCollision = CreateDefaultSubobject<UBoxComponent>(TEXT("LeftAttackCollision"));
LeftAttackCollision->SetupAttachment(GetMesh(), "LeftAttackSocket");
DeactiveLeftAttackCollision();
RightAttackCollision = CreateDefaultSubobject<UBoxComponent>(TEXT("RightAttackCollision"));
RightAttackCollision->SetupAttachment(GetMesh(), "RightAttackSocket");
DeactiveRightAttackCollision();
GetMesh()->SetCollisionResponseToChannel(ECollisionChannel::ECC_Camera, ECollisionResponse::ECR_Ignore);
GetCapsuleComponent()->SetCollisionResponseToChannel(ECollisionChannel::ECC_Camera, ECollisionResponse::ECR_Ignore);
AutoPossessAI = EAutoPossessAI::PlacedInWorldOrSpawned;
EnemyMovementStatus = EEnemyMovementStatus::EEMS_Idle;
InterpSpeed = 15.f;
bInterpToPlayer = false;
MaxHealth = 100.f;
Health = MaxHealth;
Damage = 5.f;
}
void ABaseEnemy::BeginPlay()
{
Super::BeginPlay();
ChaseVolume->OnComponentBeginOverlap.AddDynamic(this, &ABaseEnemy::OnChaseVolumeOverlapBegin);
ChaseVolume->OnComponentEndOverlap.AddDynamic(this, &ABaseEnemy::OnChaseVolumeOverlapEnd);
AttackVolume->OnComponentBeginOverlap.AddDynamic(this, &ABaseEnemy::OnAttackVolumeOverlapBegin);
AttackVolume->OnComponentEndOverlap.AddDynamic(this, &ABaseEnemy::OnAttackVolumeOverlapEnd);
LeftAttackCollision->OnComponentBeginOverlap.AddDynamic(this, &ABaseEnemy::OnLeftAttackCollisionOverlapBegin);
LeftAttackCollision->OnComponentEndOverlap.AddDynamic(this, &ABaseEnemy::OnLeftAttackCollisionOverlapEnd);
RightAttackCollision->OnComponentBeginOverlap.AddDynamic(this, &ABaseEnemy::OnRightAttackCollisionOverlapBegin);
RightAttackCollision->OnComponentEndOverlap.AddDynamic(this, &ABaseEnemy::OnRightAttackCollisionOverlapEnd);
HealthBar = Cast<UProgressBar>(HealthBarWidgetComponent->GetUserWidgetObject()->GetWidgetFromName("HealthBar"));
HealthBar->SetPercent(Health / MaxHealth);
HealthBar->SetVisibility(ESlateVisibility::Hidden);
AIController = Cast<AAIController>(GetController());
}
void ABaseEnemy::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (bInterpToPlayer && HasValidTarget() && IsAlive())
{
FRotator LookYaw(0.f, UKismetMathLibrary::FindLookAtRotation(GetActorLocation(), UGameplayStatics::GetPlayerPawn(this, 0)->GetActorLocation()).Yaw, 0.f);
FRotator InterpRotation = FMath::RInterpTo(GetActorRotation(), LookYaw, DeltaTime, InterpSpeed);
SetActorRotation(InterpRotation);
}
}
void ABaseEnemy::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
}
void ABaseEnemy::OnChaseVolumeOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (OtherActor && IsAlive())
{
AMainPlayer* Player = Cast<AMainPlayer>(OtherActor);
if (Player)
{
HealthBar->SetVisibility(ESlateVisibility::Visible);
MoveToTarget(Player);
}
}
}
void ABaseEnemy::OnChaseVolumeOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
if (OtherActor && IsAlive())
{
AMainPlayer* Player = Cast<AMainPlayer>(OtherActor);
if (Player)
{
if (AIController)
{
HealthBar->SetVisibility(ESlateVisibility::Hidden);
AIController->StopMovement();
}
}
}
}
void ABaseEnemy::OnAttackVolumeOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (OtherActor && IsAlive())
{
AMainPlayer* Player = Cast<AMainPlayer>(OtherActor);
if (Player)
{
Player->UpdataAttackTarget();
bAttackVolumeOverlap = true;
AttackBegin();
}
}
}
void ABaseEnemy::OnAttackVolumeOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
if (OtherActor && IsAlive())
{
AMainPlayer* Player = Cast<AMainPlayer>(OtherActor);
if (Player)
{
bAttackVolumeOverlap = false;
if (EnemyMovementStatus!=EEnemyMovementStatus::EEMS_Attacking)
{
MoveToTarget(Player);
}
}
}
}
void ABaseEnemy::MoveToTarget(AMainPlayer* Player)
{
if (IsAlive())
{
EnemyMovementStatus = EEnemyMovementStatus::EEMS_MoveToTarget;
if (AIController)
{
FAIMoveRequest MoveRequest;
MoveRequest.SetGoalActor(Player);
MoveRequest.SetAcceptanceRadius(10.f);
FNavPathSharedPtr NavPath;
AIController->MoveTo(MoveRequest, &NavPath);
}
}
}
void ABaseEnemy::AttackBegin()
{
if (HasValidTarget() && IsAlive())
{
if (AIController)
{
AIController->StopMovement();
}
if (EnemyMovementStatus != EEnemyMovementStatus::EEMS_Attacking)
{
EnemyMovementStatus = EEnemyMovementStatus::EEMS_Attacking;
bInterpToPlayer = true;
UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();
if (AnimInstance && AttackMontage)
{
float PlayRate = FMath::RandRange(0.9f, 1.1f);
FString SectionName = FString::FromInt(FMath::RandRange(1, 3));
AnimInstance->Montage_Play(AttackMontage, PlayRate);
AnimInstance->Montage_JumpToSection(FName(*SectionName), AttackMontage);
}
}
}
}
void ABaseEnemy::AttackEnd()
{
bInterpToPlayer = false;
if (HasValidTarget() && IsAlive())
{
EnemyMovementStatus = EEnemyMovementStatus::EEMS_Idle;
if (bAttackVolumeOverlap && HasValidTarget() && IsAlive())
{
AttackBegin();
}
}
}
void ABaseEnemy::OnLeftAttackCollisionOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (OtherActor && IsAlive())
{
AMainPlayer* Player = Cast<AMainPlayer>(OtherActor);
if (Player)
{
if (Player->HitPaticles)
{
const USkeletalMeshSocket* AttackScoket = GetMesh()->GetSocketByName("LeftAttackSocket");
if (AttackScoket)
{
FVector SocketLocation = AttackScoket->GetSocketLocation(GetMesh());
UGameplayStatics::SpawnEmitterAtLocation(this, Player->HitPaticles, SocketLocation, FRotator(0.f), true);
}
if (Player->HitSound)
{
UGameplayStatics::PlaySound2D(this, Player->HitSound);
}
if (DamageTyClass)
{
UGameplayStatics::ApplyDamage(Player, Damage, AIController, this, DamageTyClass);
}
}
}
}
}
void ABaseEnemy::OnLeftAttackCollisionOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
}
void ABaseEnemy::OnRightAttackCollisionOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (OtherActor && IsAlive())
{
AMainPlayer* Player = Cast<AMainPlayer>(OtherActor);
if (Player)
{
if (Player->HitPaticles)
{
const USkeletalMeshSocket* AttackScoket = GetMesh()->GetSocketByName("RightAttackSocket");
if (AttackScoket)
{
FVector SocketLocation = AttackScoket->GetSocketLocation(GetMesh());
UGameplayStatics::SpawnEmitterAtLocation(this, Player->HitPaticles, SocketLocation, FRotator(0.f), true);
}
if (Player->HitSound)
{
UGameplayStatics::PlaySound2D(this, Player->HitSound);
}
if (DamageTyClass)
{
UGameplayStatics::ApplyDamage(Player, Damage/2, AIController, this, DamageTyClass);
}
}
}
}
}
void ABaseEnemy::OnRightAttackCollisionOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
}
void ABaseEnemy::ActiveLeftAttackCollision()
{
LeftAttackCollision->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
LeftAttackCollision->SetCollisionObjectType(ECollisionChannel::ECC_WorldDynamic);
LeftAttackCollision->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
LeftAttackCollision->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Overlap);
}
void ABaseEnemy::DeactiveLeftAttackCollision()
{
LeftAttackCollision->SetCollisionEnabled(ECollisionEnabled::NoCollision);
}
void ABaseEnemy::ActiveRightAttackCollision()
{
RightAttackCollision->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
RightAttackCollision->SetCollisionObjectType(ECollisionChannel::ECC_WorldDynamic);
RightAttackCollision->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
RightAttackCollision->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Overlap);
}
void ABaseEnemy::DeactiveRightAttackCollision()
{
RightAttackCollision->SetCollisionEnabled(ECollisionEnabled::NoCollision);
}
float ABaseEnemy::TakeDamage(float Damage, FDamageEvent const& DamageEvent, AController* EventInstigator, AActor* DamageCauser)
{
if (Health - Damage <= 0.f)
{
Health = FMath::Clamp(Health - Damage, 0.f, MaxHealth);
Die();
}
else
{
Health -= Damage;
}
HealthBar->SetPercent(Health / MaxHealth);
return Health;
}
void ABaseEnemy::Die()
{
EnemyMovementStatus = EEnemyMovementStatus::EEMS_Dead;
DeactiveLeftAttackCollision();
DeactiveRightAttackCollision();
ChaseVolume->SetCollisionEnabled(ECollisionEnabled::NoCollision);
AttackVolume->SetCollisionEnabled(ECollisionEnabled::NoCollision);
GetCapsuleComponent()->SetCollisionEnabled(ECollisionEnabled::NoCollision);
Cast<AMainPlayer>(UGameplayStatics::GetPlayerPawn(this, 0))->UpdataAttackTarget();
}
void ABaseEnemy::DeathEnd()
{
}
bool ABaseEnemy::HasValidTarget()
{
return Cast<AMainPlayer>(UGameplayStatics::GetPlayerPawn(this, 0))->MovementStatus != EPlayerMovementStatus::EPMS_Dead;
}
主角死亡与敌人死亡后延时销毁
void ABaseEnemy::Die()
{
EnemyMovementStatus = EEnemyMovementStatus::EEMS_Dead;
HealthBar->SetVisibility(ESlateVisibility::Hidden);
DeactiveLeftAttackCollision();
DeactiveRightAttackCollision();
ChaseVolume->SetCollisionEnabled(ECollisionEnabled::NoCollision);
AttackVolume->SetCollisionEnabled(ECollisionEnabled::NoCollision);
GetCapsuleComponent()->SetCollisionEnabled(ECollisionEnabled::NoCollision);
Cast<AMainPlayer>(UGameplayStatics::GetPlayerPawn(this, 0))->UpdataAttackTarget();
}
- 然后去主角的死亡动画中添加DeathEnd的通知,并在动画蓝图中调用
- 编写DeathEnd死亡逻辑,首先关闭动画,添加定时器,玩家死后一秒重启游戏
void AMainPlayer::DeathEnd()
{
GetMesh()->bPauseAnims = true;
GetMesh()->bNoSkeletonUpdate = true;
FTimerHandle DeathTimerHandle;
auto Lambda = []()
{
};
GetWorldTimerManager().SetTimer(DeathTimerHandle, FTimerDelegate::CreateLambda(Lambda), 1.0, false);
}
- 敌人这边也是一样添加通知动画蓝图中调用,然后编写死亡消失逻辑
- 死亡一秒后销毁
void ABaseEnemy::DeathEnd()
{
GetMesh()->bPauseAnims = true;
GetMesh()->bNoSkeletonUpdate = true;
FTimerHandle DeathTimerHandle;
auto Lambda = [this]()
{
Destroy();
};
GetWorldTimerManager().SetTimer(DeathTimerHandle, FTimerDelegate::CreateLambda(Lambda), 1.0, false);
}
玩家死亡重新开始游戏
- 将上面遗留的RestartLevel函数进行新建然后编写,用UGameplayStatics中的方法进行重新开始游戏
void AMainPlayer::DeathEnd()
{
GetMesh()->bPauseAnims = true;
GetMesh()->bNoSkeletonUpdate = true;
FTimerHandle DeathTimerHandle;
auto Lambda = [this]()
{
RestartLevel();
};
GetWorldTimerManager().SetTimer(DeathTimerHandle, FTimerDelegate::CreateLambda(Lambda), 1.0, false);
}
void AMainPlayer::RestartLevel()
{
FString LevelName = UGameplayStatics::GetCurrentLevelName(this);
UGameplayStatics::OpenLevel(this, FName(*LevelName));
}