【UE4C++-ActionRougelike-26】动画蓝图
一、眩晕状态
1、动画状态机
在Gideon_AnimBlueprint中的Locomotion状态机中添加以下眩晕相关状态。设置变量bIsStunned来控制眩晕状态执行流程。
2、创建动画实例类
以AnimInstance为父类创建动画实例类MyAnimInstance
//MyAnimInstance.h
UCLASS()
class ACTIONROGUELIKE_API UMyAnimInstance : public UAnimInstance
{
GENERATED_BODY()
protected:
// 表示角色是否处于被击晕状态
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Animation")
bool bIsStunned;
// 动作组件
UPROPERTY(BlueprintReadOnly, Category = "Animation")
UMyActionComponent* ActionComp;
// 重写UAnimInstance的NativeInitializeAnimation方法
void NativeInitializeAnimation() override;
// 重写UAnimInstance的NativeUpdateAnimation方法
void NativeUpdateAnimation(float DeltaSeconds) override;
};
//MyAnimInstance.cpp
void UMyAnimInstance::NativeInitializeAnimation()
{
Super::NativeInitializeAnimation();
// 获取当前动画实例的所有者Actor
AActor* OwningActor = GetOwningActor();
if (OwningActor)
{
// 如果所有者Actor存在,通过类类型UMyActionComponent::StaticClass()获取Actor中的组件,
// 并尝试将其转化为UMyActionComponent类型,然后赋值给ActionComp
ActionComp = Cast<UMyActionComponent>(OwningActor->GetComponentByClass(UMyActionComponent::StaticClass()));
}
}
void UMyAnimInstance::NativeUpdateAnimation(float DeltaSeconds)
{
Super::NativeUpdateAnimation(DeltaSeconds);
// 请求一个名为"Status.Stunned"的游戏标签
static FGameplayTag StunnedTag = FGameplayTag::RequestGameplayTag("Status.Stunned");
if (ActionComp)
{
// 如果ActionComp存在,就检查其ActiveGameplayTags是否包含StunnedTag标签,并将结果赋值给bIsStunned
bIsStunned = ActionComp->ActiveGameplayTags.HasTag(StunnedTag);
}
}
修改Gideon_AnimBlueprint类设置的父类为MyAnimInstance,并设置蓝图中bIsStunned为MyAnimInstance中的属性。
二、漂浮加速动画
1、新建动画混合空间
以Gideon_Skeleton为骨架新建混合空间Sprinting_BS_1D,混合空间设置如下:
2、应用混合空间
2.1 创建加速动画蓝图节点
2.2 应用加速动画蓝图节点
2.3 更新是否加速变量
3、效果演示
三、交互信息
1、添加获取交互信息文本方法
// SInteractionInterface.h
class ACTIONROGUELIKE_API ISInteractionInterface
{
public:
// 获取交互信息文本
UFUNCTION(BlueprintCallable, BlueprintNativeEvent)
FText GetInteractText(APawn* InstigatorPawn);
}
// MyPickup.h
class ACTIONROGUELIKE_API AMyPickup : public AActor, public ISInteractionInterface
{
protected:
// 实现获取交互信息文本方法
FText GetInteractText_Implementation(APawn* InstigatorPawn);
};
// MyPickup.cpp
FText AMyPickup::GetInteractText_Implementation(APawn* InstigatorPawn)
{
return FText::GetEmpty();
}
2、实现接口方法
// MyPickup_Health.h
class ACTIONROGUELIKE_API AMyPickup_Health : public AMyPickup
{
public:
// 实现获取交互信息文本方法
FText GetInteractText_Implementation(APawn* InstigatorPawn) override;
}
// MyPickup_Health.cpp
// 定义一个本地化文本的命名空间为"InteractableActors"
#define LOCTEXT_NAMESPACE "InteractableActors"
FText AMyPickup_Health::GetInteractText_Implementation(APawn* InstigatorPawn)
{
// 如果健康值已满
UMyAttributeComponent* AttributeComp = UMyAttributeComponent::GetAttributes(InstigatorPawn);
if (AttributeComp && AttributeComp->IsFullHealth())
{
// 返回一段本地化文本,文本的key为"HealthPotion_FullHealthWarning",默认值为"Already at full health."
return LOCTEXT("HealthPotion_FullHealthWarning", "Already at full health.");
}
// 返回一段格式化的本地化文本,文本的key为"HealthPotion_InteractMessage",
// 默认值为"Cost {0} Credits. Restores health to maximum.",并用CreditCost替换掉文本中的{0}
return FText::Format(LOCTEXT("HealthPotion_InteractMessage", "Cost {0} Credits. Restores health to maximum."), CreditCost);
}
// 取消之前定义的本地化文本的命名空间
#undef LOCTEXT_NAMESPACE