UE5学习笔记24-添加武器弹药

一、给角色的武器添加弹药

1.创建界面,根据笔记23的界面中添加

2.绑定界面控件

cpp 复制代码
	UPROPERTY(meta = (Bindwidget))
	UTextBlock* WeaponAmmoAmount;

	UPROPERTY(meta = (Bindwidget))
	UTextBlock* CarriedAmmoAmount;

3.添加武器类型枚举

3.1创建武器类型枚举头文件

3.2创建文件时,默认添加的路径是在Intermediate/ProjectFiles文件夹中,通过#include 包含头文件时会找不到对应的头文件,需要更改路径,我是将所有武器类的C++文件放到Weapon文件夹中,所以我将武器类型头文件放到了Weapon文件夹中。

3.3创建枚举

cpp 复制代码
#pragma once

UENUM(BlueprintType)
enum class EWeapoType : uint8
{
	EWT_AssaultRifle UMETA(DisplayName = "Assault Rifle"),

	EWT_MAX UMETA(DisplayName = "DefaultMAX"),
};

3.4创建武器弹药数量,捡起武器的声音

cpp 复制代码
	/**
	*	弹药数量 
	*/

	UPROPERTY(EditAnywhere, ReplicatedUsing = OnRep_Ammo)
	int32 Ammo;//弹药数量

	UFUNCTION()
	void OnRep_Ammo();

	UPROPERTY(EditAnywhere)
	int32 MagCapacity;//最大弹药数量
	
	void SpendRound();

	void SetHUDAmmo();

	UPROPERTY()
	class ABlasterCharacter* BlasterOwnerCharacter; // 当前持有武器的角色

	UPROPERTY()
	class ABlasterPlayerController* BlasterOwnerController; // 当前持有武器的角色控制器

	UPROPERTY()
	EWeapoType WeaponType;

	/**
	*	武器的声音
	*/
	UPROPERTY(EditAnywhere)
	class USoundCue* EquipSound;


bool IsEmpty();
FORCEINLINE EWeapoType GetWeaponType() const { return WeaponType; }
FORCEINLINE int32 GetAmmo() const { return Ammo; }
FORCEINLINE int32 GetMagCapacity() const { return MagCapacity; }
cpp 复制代码
bool AWeapon::IsEmpty()
{
	return Ammo <= 0;
}

void AWeapon::OnRep_Ammo()
{
	//BlasterOwnerCharacter = BlasterOwnerCharacter == nullptr ? Cast<ABlasterCharacter>(GetOwner()) : BlasterOwnerCharacter;
	SetHUDAmmo();
}

void AWeapon::SpendRound()
{
	Ammo = FMath::Clamp(Ammo - 1,0,MagCapacity);
	SetHUDAmmo();
}

void AWeapon::SetHUDAmmo()
{
	BlasterOwnerCharacter = BlasterOwnerCharacter == nullptr ? Cast<ABlasterCharacter>(GetOwner()) : BlasterOwnerCharacter;
	if (BlasterOwnerCharacter)
	{
		BlasterOwnerController = BlasterOwnerController == nullptr ? Cast<ABlasterPlayerController>(BlasterOwnerCharacter->Controller) : BlasterOwnerController;
		if (BlasterOwnerController)
		{
			BlasterOwnerController->SetHUDWeaponAmmo(Ammo);
		}
	}
}

3.5.因为新添加了两个显示弹药数量的地方,所以给PlayerController添加设置界面的函数

cpp 复制代码
	void SetHUDWeaponAmmo(int32 Ammo);
	void SetHUDCarriedAmmo(int32 Ammo);
cpp 复制代码
void ABlasterPlayerController::SetHUDWeaponAmmo(int32 Ammo)
{
	BlasterHUD = BlasterHUD == nullptr ? Cast<AABasterHUD>(GetHUD()) : BlasterHUD;
	bool bHUDValid = BlasterHUD &&
		BlasterHUD->CharacterOverlay &&
		BlasterHUD->CharacterOverlay->WeaponAmmoAmount;
	if (bHUDValid)
	{
		FString AmmoText = FString::Printf(TEXT("%d"), Ammo); // FloorToInt 向下取整
		BlasterHUD->CharacterOverlay->WeaponAmmoAmount->SetText(FText::FromString(AmmoText));
	}
}

void ABlasterPlayerController::SetHUDCarriedAmmo(int32 Ammo)
{
	BlasterHUD = BlasterHUD == nullptr ? Cast<AABasterHUD>(GetHUD()) : BlasterHUD;
	bool bHUDValid = BlasterHUD &&
		BlasterHUD->CharacterOverlay &&
		BlasterHUD->CharacterOverlay->CarriedAmmoAmount;
	if (bHUDValid)
	{
		FString CarriedAmmoText = FString::Printf(TEXT("%d"), Ammo); // FloorToInt 向下取整
		BlasterHUD->CharacterOverlay->CarriedAmmoAmount->SetText(FText::FromString(CarriedAmmoText));
	}
}

3.6添加重新加载弹药功能,当前武器弹药的数量,播放换弹动画的功能

cpp 复制代码
	/* 换弹 */
	UFUNCTION(Server, Reliable)
	void ServerReload();
	/* 换弹 */
	/* 处理换弹 */
	void HandleReload();
	/* 处理换弹 */

	/** 重新加载弹药函数 */
	void Reload();

	/** 处理设置完成战斗状态 */
	UFUNCTION(BlueprintCallable)
	void FinishReloading();

	/** 计算重新加载弹药的数量 */
	int32 AmountToReload();

    //当前装备的武器弹药
    UPROPERTY(ReplicatedUsing = OnRep_CarriedAmmo)
    int32 CarriedAmmo;

    UFUNCTION()
    void OnRep_CarriedAmmo();

	bool CanFire();// 是否可以开火函数

	UPROPERTY(ReplicatedUsing = OnRep_CombatState)
	ECombatState CombatState = ECombatState::ECS_Unoccupied;
	
	UFUNCTION()
	void OnRep_CombatState();

	void InitializeCarriedAmmo();

    TMap<EWeapoType, int32> CarriedAmmoMap;
cpp 复制代码
void UCombatComponent::OnRep_CarriedAmmo()
{
	Controller = Controller == nullptr ? Cast<ABlasterPlayerController>(Character->Controller) : Controller;
	if (Controller)
	{
		Controller->SetHUDCarriedAmmo(CarriedAmmo);
	}
}

bool UCombatComponent::CanFire()
{
	if (EquippedWeapon == nullptr) return false;
	return !EquippedWeapon->IsEmpty() && bCanFire && CombatState == ECombatState::ECS_Unoccupied;
}

void UCombatComponent::OnRep_CombatState()
{
	switch (CombatState)
	{
	case ECombatState::ECS_Reloading:
		HandleReload();
		break;
	case ECombatState::ECS_Unoccupied:
		if (bFireButtonPressed)
		{
			Fire();
		}
		break;
	}
}

void UCombatComponent::FinishReloading()
{
	if (Character == nullptr) return;
	if (Character->HasAuthority())
	{
		CombatState = ECombatState::ECS_Unoccupied;
		UpdateAmmoValues();
	}
	if (bFireButtonPressed)
	{
		Fire();
	}
}

void UCombatComponent::HandleReload()
{
	Character->PlayReloadMontage();
}

void UCombatComponent::Reload()
{
	if (CarriedAmmo > 0 && CombatState != ECombatState::ECS_Reloading)
	{
		ServerReload();
	}
}


void UCombatComponent::ServerReload_Implementation()
{
	if (Character == nullptr || EquippedWeapon == nullptr) return;
	
	
	CombatState = ECombatState::ECS_Reloading;
	HandleReload();
}

int32 UCombatComponent::AmountToReload()
{
	if (EquippedWeapon == nullptr) return 0;
	int32 RoomInMag = EquippedWeapon->GetMagCapacity() - EquippedWeapon->GetAmmo();

	if (CarriedAmmoMap.Contains(EquippedWeapon->GetWeaponType()))
	{
		int32 AmountCarried = CarriedAmmoMap[EquippedWeapon->GetWeaponType()];
		int32 Least = FMath::Min(RoomInMag,AmountCarried);
		return FMath::Clamp(RoomInMag, 0, Least);
	}

	return 0;
}

void UCombatComponent::InitializeCarriedAmmo() beginplay中调用需要检查HasAuthority()
{
	CarriedAmmoMap.Emplace(EWeapoType::EWT_AssaultRifle, StartingARAmmo);
}

3.7.在装备武器时调用

cpp 复制代码
	if (CarriedAmmoMap.Contains(EquippedWeapon->GetWeaponType()))
	{
		CarriedAmmo = CarriedAmmoMap[EquippedWeapon->GetWeaponType()];
	}

	Controller = Controller == nullptr ? Cast<ABlasterPlayerController>(Character->Controller) : Controller;
	if (Controller)
	{
		Controller->SetHUDCarriedAmmo(CarriedAmmo);
	}

	if (EquippedWeapon->EquipSound)
	{
		UGameplayStatics::PlaySoundAtLocation(
			this,
			EquippedWeapon->EquipSound,
			Character->GetActorLocation()
		);
	}

	if (EquippedWeapon->IsEmpty())
	{
		Reload();
	}

3.8.蓝图设置

相关推荐
五味香42 分钟前
Java学习,查找List最大最小值
android·java·开发语言·python·学习·golang·kotlin
小爬菜1 小时前
Django学习笔记(启动项目)-03
前端·笔记·python·学习·django
小爬菜1 小时前
Django学习笔记(bootstrap的运用)-04
笔记·学习·django
叫我龙翔1 小时前
【博客之星】2024年度创作成长总结 - 面朝大海 ,春暖花开!
学习
dal118网工任子仪2 小时前
69,【1】BUUCTF WEB ssrf [De1CTF 2019]SSRF Me
笔记·学习
嵌入式DZC3 小时前
优秀代码段案例__笔记
笔记·算法
猿类崛起@3 小时前
百度千帆大模型实战:AI大模型开发的调用指南
人工智能·学习·百度·大模型·产品经理·大模型学习·大模型教程
Pandaconda3 小时前
【Golang 面试题】每日 3 题(三十九)
开发语言·经验分享·笔记·后端·面试·golang·go
viperrrrrrrrrr73 小时前
大数据学习(40)- Flink执行流
大数据·学习·flink
l1x1n03 小时前
No.35 笔记 | Python学习之旅:基础语法与实践作业总结
笔记·python·学习