22,攻击检测改为C++
回到动画源中进行攻击检测的动画通知改写这里用到了蓝图接口把蓝图接口改为C// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include “CoreMinimal.h”#include “UObject/Interface.h”#include “Combat.generated.h”// This class does not need to be modified.UINTERFACE(MinimalAPI)class UCombat : public UInterface{GENERATED_BODY()};/****/class ROUGHLIKE1_API ICombat{GENERATED_BODY()// Add interface functions to this class. This is the class that will be inherited to implement this interface.public://攻击重置UFUNCTION(BlueprintNativeEvent, Category “Combat”)void INT_ResetAtk();virtual void INT_ResetAtk_Implementation() 0;//攻击检测 UFUNCTION(BlueprintNativeEvent, Category Combat) void INT_AttackDetection(FVector boxPosition, FVector boxExtension); virtual void INT_AttackDetection_Implementation(FVector boxPosition, FVector boxExtension) 0;};再写动画通知类// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include “CoreMinimal.h”#include “Notifies/PaperZDAnimNotify.h”#include “ANZ_Attack.generated.h”/****/UCLASS()class ROUGHLIKE1_API UANZ_Attack : public UPaperZDAnimNotify{GENERATED_BODY()public:UANZ_Attack();//攻击盒大小UPROPERTY(EditAnywhere, BlueprintReadWrite, Category “Attack”)FVector BoxExtent;//攻击位置的Socket名称UPROPERTY(EditAnywhere, BlueprintReadWrite, Category “Attack”)FName socketName;protected://重写动画通知的触发事件virtual void OnReceiveNotify_Implementation(UPaperZDAnimInstance* OwningInstance) const override;};// Fill out your copyright notice in the Description page of Project Settings.#include “AnimNotify/ANZ_Attack.h”#include Interface/Combat.h#include PaperZDAnimInstance.h#include MyPaperZDCharacter.hUANZ_Attack::UANZ_Attack(){BoxExtent FVector(100.0f, 50.0f, 50.0f);socketName TEXT(“Socket_ATK”);}void UANZ_Attack::OnReceiveNotify_Implementation(UPaperZDAnimInstance* OwningInstance) const{if (!OwningInstance){return;}//获取动画实例所属的actorAActor* ownerActor OwningInstance-GetOwningActor();if (!ownerActor){return;}//获取渲染组件用来读取Socket位置UActorComponent* ac ownerActor-GetComponentByClass(USceneComponent::StaticClass());if (!ac){return;}USceneComponent* RenderComp Cast(ac);if (!RenderComp){return;}//获取socket的世界位置FVector socketLocation RenderComp-GetSocketLocation(socketName);if (ownerActor-Implements()){ICombat::Execute_INT_AttackDetection(ownerActor,socketLocation, BoxExtent);}}由于这个蓝图接口是角色类调用的所以在角色类重载virtual void INT_AttackDetection_Implementation(FVector boxPosition, FVector boxExtension) override;由于还用到了其他蓝图接口所以暂时让蓝图实现即// 扩展入口蓝图 / C 均可重写 UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category Combat | Attack) void ProcessHitResult(AActor* Target, float Force); virtual void ProcessHitResult_Implementation(AActor* Target, float Force) {}void AMyPaperZDCharacter::INT_AttackDetection_Implementation( FVector boxPosition, FVector boxExtension){//如果死亡则返回if (IsDead){return;}//构建BoxOverlap参数TArrayTEnumAsByte objectTypes;objectTypes.Add(EObjectTypeQuery::ObjectTypeQuery3); //pawn类型//忽略玩家自身 TArrayAActor* actorsToIgnore; actorsToIgnore.Add(this); //执行盒体检测 TArrayAActor* outActors; bool bHasOverlap UKismetSystemLibrary::BoxOverlapActors( this, boxPosition, boxExtension, objectTypes, nullptr, actorsToIgnore, outActors ); if (!bHasOverlap) { return; } //根据连击数设置推力 float force (comboIndex 0) ? 200.0f : 600.0f; //遍历所有重叠actor for (AActor* target : outActors ) { if (!target) { continue; } bool bIsEnemy target-ActorHasTag(FName(enemy)); if (!bIsEnemy) { continue; } this-ProcessHitResult(target, force); }}在蓝图中暂时保留