【UE4 C++】角色拾取、替换武器(下)

前言

本篇将告诉你如何替换武器

效果如下:
靠近之后会显示字体,按E装备,
在这里插入图片描述
在这里插入图片描述
靠近下一个继续按E,当前这个装备会销毁,而装备上现在这个。
在这里插入图片描述

角色类

.h文件

  • 添加变量,代表当前手上拿着的武器,设置为protected权限,使得当前武器没有那么容易被修改。
protected:
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly)
		class AWeapon* EquiqedWeapon; 
  • 既然不能那么容易被修改,那么就要有个函数专门去修改他:
    void SetWeapon(class AWeapon* Weapon); 设置当前武器。

  • 声明准备替换的变量,也就是保存我们与道具重叠时候的对象。

UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
	class AItem* ActiveOverlapItem;
  • 拾取武器也不是那么容易的,所以加一个按键之后才能获得当前的武器。
UFUNCTION()
	void OnInteract();

.cpp文件

  • SetupPlayerInputComponent

绑定按键,使得我们按下E才可以获得武器。
找到项目设置中的输入设置,绑定按键:
在这里插入图片描述
然后在代码中将这个按键和我们装备函数绑定起来。
PlayerInputComponent->BindAction("Interact", IE_Pressed, this, &ARole::OnInteract);

  • OnInteract

按下E之后进行武器替换的操作:

AWeapon* Weapon = Cast<AWeapon>(ActiveOverlapItem);
if (Weapon) {
	Weapon->Equip(this);
	ActiveOverlapItem = nullptr;
}

这个ActiveOverlapItem为关键,下面武器函数中只会修改我们Role类中的ActiveOverlapItem变量,也只需要修改这个变量,与道具重叠开始的时候,武器那边就会把ActiveOverlapItem设置好,当我们按下E的时候就会调用这个函数,然后这个函数进行检查,判断是否是武器变量,如果是的,也就是地址不为空的时候,我们就把这个武器装备上,然后把ActiveOverlapItem置为空指针,因为它的使命已经完成了。

  • SetWeapon

添加头文件:#include "Components/TextRenderComponent.h"
设置武器操作:

if (EquiqedWeapon) {
	EquiqedWeapon->Destroy();
}
EquiqedWeapon = Weapon;
Weapon->TextCue->SetVisibility(false);

如果有武器就把手上的武器销毁(这里可以进行拓展,我准备下一篇文章写背包,把这个武器存到背包中去。),然后再把要替换的武器设置当前装备,还有把键上面的字设置为不可见,要不然很滑稽。
这个文字提示,待会会讲。

武器类

.h文件

  • 重写退出重叠函数
virtual void OnOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
	UPrimitiveComponent* OtherComp, int32 OtherBodyIndex) override;
  • 声明拾取武器之后的声音
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Item | Sound")
	class USoundCue* SoundEquiped;
  • 声明文本渲染组件,就是储存提示语的组件。
UPROPERTY(EditAnywhere, BlueprintReadWrite)
	class UTextRenderComponent* TextCue;

.cpp文件

  • 构造函数

添加头文件:#include "Components/TextRenderComponent.h"
创建文本渲染组件实例,并且让他附加在剑上。刚开始的字体显示要为false,设置为不显示。

TextCue = CreateDefaultSubobject<UTextRenderComponent>(TEXT("TextCue"));
TextCue->SetupAttachment(Mesh);
TextCue->SetWorldSize(10.f);
TextCue->SetTextRenderColor(FColor::Black);
TextCue->SetVisibility(false);
  • OnOverlapBegin

这个函数要进行修改一下了,不是重叠就可以装备上了,它的作用转化为了修改role->ActiveOverlapItem变量,如果重叠之后待装备的武器为空,那么就设置为当前这个武器,这个写法比较简单,当有多个物体重叠的时候只取第一个。
这个时候要把提示语设置为显示。

Super::OnOverlapBegin(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult);
if (OtherActor) {
	ARole* role = Cast<ARole>(OtherActor);
	if (role) {
		if (role->ActiveOverlapItem == nullptr) {
			role->ActiveOverlapItem = this;
		}
		TextCue->SetVisibility(true);
	}
}
  • OnOverlapEnd

重叠结束函数,先判断是否是角色重叠结束,如果是,并且角色的待替换武器就是自己的话,那么就让他置为空,因为已经离开了,就不能让这个待替换武器为自己了。离开之后,这个字体就不显示了,减少性能开销。

Super::OnOverlapEnd(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex);
if (OtherActor) {
	ARole* role = Cast<ARole>(OtherActor);
	if (role) {
		if (role->ActiveOverlapItem == this) {
			role->ActiveOverlapItem = nullptr;
		}
		TextCue->SetVisibility(false);
	}
}
  • Equip

添加头文件:

#include "Sound/SoundCue.h"
#include "Kismet/GameplayStatics.h"

装备之后要把球形组件对角色的重叠反应取消掉,文本设置为不可见,如果蓝图中加载了声音,那么就播放声音。

if (owner) {
	Mesh->SetCollisionResponseToAllChannels(ECR_Ignore);
	Mesh->SetSimulatePhysics(false);
	AttachToComponent(owner->GetMesh(), FAttachmentTransformRules::SnapToTargetNotIncludingScale, "WEAPON_R");
	Rotate = false;
	ParticleComp->SetActive(false);
	SpereComp->OnComponentBeginOverlap.RemoveDynamic(this, &AItem::OnOverlapBegin);
↓↓↓//
	SpereComp->OnComponentEndOverlap.RemoveDynamic(this, &AItem::OnOverlapEnd);
	TextCue->SetVisibility(false);
	if (SoundEquiped) {
		UGameplayStatics::PlaySound2D(this, SoundEquiped);
	}
	owner->SetWeapon(this);
}

结束!赶紧运行试试吧!

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值