任务:将界面中的UI血条,金币数量显示与玩家数据进行关联绑定,并编写相关的游戏逻辑
一、将血条与玩家血量关联,并制作与炸弹的互动(碰到炸弹血量减少)
第一步、在角色类(我这里是Man)中添加相关变量
Man.h关键代码如下:
public:
//声明浮点型变量,用来储存角色当前生命值
UPROPERTY(EditAnywhere, BlueprintReadWrite)
float Health;
//声明浮点型变量,用来储存最大的生命值
UPROPERTY(EditAnywhere, BlueprintReadWrite)
float MaxHealth;
//声明函数:接受伤害
UFUNCTION(BlueprintCallable)
void TakeDemage(float Value);
Man.cpp关键代码如下:
//构造函数中初始化血量数据
Aman::Aman()
{
……
MaxHealth = 100;
Health = 100;
……
}
//定义函数:接受伤害
void AMan::TakeDemage(float Value)
{
Health -= Value;
//将玩家当前生命值限定在0到MaxHealth之间
Health = FMath::Clamp(Health, 0.f, MaxHealth);
}
第二步、在Bomb类中重写父类函数OnOverlapBegin()
Bomb.h代码如下:
#pragma once
#include "CoreMinimal.h"
#include "Item.h"
#include "Bomb.generated.h"
/**
*
*/
UCLASS()
class LEARNTEST_API ABomb : public AItem
{
GENERATED_BODY()
public:
ABomb();
//声明浮点型变量:存储炸弹造成的伤害值
UPROPERTY(EditAnywhere, BlueprintReadWrite)
float Demage;
public:
//重写OnOverlapBegin函数,这里注意父类Item类中该函数声明也要加virtual关键字
virtual void OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) override;
};
Bomb.cpp代码如下:
// Fill out your copyright notice in the Description page of Project Settings.
#include "Bomb.h"
#include "Man.h"
ABomb::ABomb()
{
PrimaryActorTick.bCanEverTick = true;
//初始化炸弹伤害值为30
Demage = 30.f;
}
void ABomb::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
Super::OnOverlapBegin(OverlappedComponent,OtherActor,OtherComp,OtherBodyIndex,bFromSweep,SweepResult);
//如果发生重叠,调用Man类中的TakeDemage函数
if (OtherActor)
{
AMan* Man = Cast<AMan>(OtherActor);
if (Man)
{
Man->TakeDemage(Demage);
}
}
}
第三步、将UI控件中的血条值与玩家当前生命值进行绑定
二、将UI中的硬币数量与玩家硬币数量关联,并制作硬币与玩家的互动(拾取硬币,数量增加)
第一步、在角色类(我这里是Man)中添加相关变量
Man.h关键代码如下:
//定义可拾取物品的枚举类型,这里只定义了一种,PT_Coin
enum EPickUpType
{
PT_Coin
};
UCLASS()
class LEARNTEST_API AMan : public ACharacter
{
GENERATED_BODY()
……
public:
//声明整型变量:储存硬币数量
UPROPERTY(BlueprintReadWrite)
int32 NumCoins;
//声明拾取物品函数:需要可拾取物品的类型,以及数量
void PickUp(EPickUpType Type, uint32 Count);
……
};
Man.cpp关键代码如下:
//构造函数
AMan::AMan()
{
……
//初始金币数量为0
NumCoins = 0;
……
}
void AMan::PickUp(EPickUpType Type, uint32 Count)
{
//使用switch函数,根据可拾取物品的类型增加相应的数量
switch(Type)
{
case PT_Coin:
NumCoins += Count;
break;
default:
break;
}
}
第二步、在Coin类中重写父类函数OnOverlapBegin()
Coin.h代码如下:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Item.h"
#include "Coin.generated.h"
/**
*
*/
UCLASS()
class LEARNTEST_API ACoin : public AItem
{
GENERATED_BODY()
public:
ACoin();
//定义整型变量:储存数量
UPROPERTY(EditAnywhere, BlueprintReadWrite)
int32 Count;
//重写OnOverlapBegin函数,这里注意父类Item类中该函数声明也要加virtual关键字
virtual void OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) override;
};
Coin.cpp代码如下:
// Fill out your copyright notice in the Description page of Project Settings.
#include "Coin.h"
#include "Man.h"
//初始化数量为1
ACoin::ACoin()
{
Count = 1;
}
void ACoin::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
//调用父类中包含的方法
Super::OnOverlapBegin(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult);
//调用Man类Pickup函数
if (OtherActor)
{
AMan* Man = Cast<AMan>(OtherActor);
if (Man)
{
Man->PickUp(PT_Coin, Count);
}
}
}
第三步、将UI控件中硬币的数量与玩家当前硬币数量进行绑定