六一的部落格


关关难过关关过,前路漫漫亦灿灿。




说明

拾取补给后开启定时器


实现


添加属性: 拾取后, 给定时长后可再次拾取补给

ShootThemUp: Pickups/STUBasePickup.h

protected

1UPROPERTY(EditAnywhere, BlueprintReadWrite)
2float RespawnTime = 5.0f;

添加函数: 拾取补给后, 关闭碰撞并隐藏补给, 开启定时器; 在定时器中开启碰撞再次显示补给

ShootThemUp: Pickups/STUBasePickup.h

private

1void PickupWasTaken();
2void Respawn();        

ShootThemUp: Pickups/STUBasePickup.cpp

 1#include "TimerManager.h"
 2
 3// BeginPlay
 4check(CollisionComponent);
 5
 6// NotifyActorBeginOverlap
 7
 8// Destroy
 9PickupWasTaken();
10
11void ASTUBasePickup::PickupWasTaken()
12{
13    CollisionComponent->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
14    if (GetRootComponent())
15    {
16        GetRootComponent()->SetVisibility(false, true);
17    }
18
19    FTimerHandle RespawnTimerHandle;
20    GetWorldTimerManager().SetTimer(RespawnTimerHandle, this, &ASTUBasePickup::Respawn, RespawnTime);
21}
22
23void ASTUBasePickup::Respawn()
24{
25    CollisionComponent->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Overlap);
26    if (GetRootComponent())
27    {
28        GetRootComponent()->SetVisibility(true, true);
29    }
30}

隐藏补给

SetVisibility

第一个参数决定当前组件是否被渲染; 第二个参数决定其下级组件的渲染设置是否与该组件一致


查看

运行游戏实例, 按下 ` 打开终端, 输入 show collision


拾取补给后, 碰撞胶囊仍在, 若干秒后补给再次出现, 可反复领取



考虑到补给可能会导致用户数据的修改

实现接口 GivePickupTo

通过Pawn对象获取组件, 修改用户数据

private


基类实现

返回false

ShootThemUp: Pickups/STUBasePickup.h

1virtual bool GivePickupTo(APawn *PlayerPawn);

ShootThemUp: Pickups/STUBasePickup.cpp

1bool ASTUBasePickup::GivePickupTo(APawn *PlayerPawn)
2{
3    return false;
4}

发生重叠事件并导致用户数据修改时, 认为补给被拾取

屏蔽日志打印

ShootThemUp: Pickups/STUBasePickup.cpp

1// NotifyActorBeginOverlap
2
3const auto Pawn = Cast<APawn>(OtherActor);
4if (GivePickupTo(Pawn))
5{
6    PickupWasTaken();
7}

生命补给和武器补给的初步实现

ShootThemUp: Pickups/STUHealthPickup.h

ShootThemUp: Pickups/STUAmmoPickup.h

1virtual bool GivePickupTo(APawn *PlayerPawn) override;

ShootThemUp: Pickups/STUHealthPickup.cpp

1DEFINE_LOG_CATEGORY_STATIC(LogHealthPickup, All, All);
2
3bool ASTUHealthPickup::GivePickupTo(APawn *PlayerPawn)
4{
5    UE_LOG(LogHealthPickup, Display, TEXT("Health was taken"));
6    return true;
7}

ShootThemUp: Pickups/STUAmmoPickup.cpp

1DEFINE_LOG_CATEGORY_STATIC(LogAmmoPickup, All, All);
2
3bool ASTUAmmoPickup::GivePickupTo(APawn *PlayerPawn)
4{
5    UE_LOG(LogAmmoPickup, Display, TEXT("Ammo was taken"));
6    return true;
7}

持续提供补给



说明

拾取补给后开启定时器


实现


添加属性: 拾取后, 给定时长后可再次拾取补给

ShootThemUp: Pickups/STUBasePickup.h

protected

1UPROPERTY(EditAnywhere, BlueprintReadWrite)
2float RespawnTime = 5.0f;

添加函数: 拾取补给后, 关闭碰撞并隐藏补给, 开启定时器; 在定时器中开启碰撞再次显示补给

ShootThemUp: Pickups/STUBasePickup.h

private

1void PickupWasTaken();
2void Respawn();        

ShootThemUp: Pickups/STUBasePickup.cpp

 1#include "TimerManager.h"
 2
 3// BeginPlay
 4check(CollisionComponent);
 5
 6// NotifyActorBeginOverlap
 7
 8// Destroy
 9PickupWasTaken();
10
11void ASTUBasePickup::PickupWasTaken()
12{
13    CollisionComponent->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);
14    if (GetRootComponent())
15    {
16        GetRootComponent()->SetVisibility(false, true);
17    }
18
19    FTimerHandle RespawnTimerHandle;
20    GetWorldTimerManager().SetTimer(RespawnTimerHandle, this, &ASTUBasePickup::Respawn, RespawnTime);
21}
22
23void ASTUBasePickup::Respawn()
24{
25    CollisionComponent->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Overlap);
26    if (GetRootComponent())
27    {
28        GetRootComponent()->SetVisibility(true, true);
29    }
30}

隐藏补给

SetVisibility

第一个参数决定当前组件是否被渲染; 第二个参数决定其下级组件的渲染设置是否与该组件一致


查看

运行游戏实例, 按下 ` 打开终端, 输入 show collision


拾取补给后, 碰撞胶囊仍在, 若干秒后补给再次出现, 可反复领取



考虑到补给可能会导致用户数据的修改

实现接口 GivePickupTo

通过Pawn对象获取组件, 修改用户数据

private


基类实现

返回false

ShootThemUp: Pickups/STUBasePickup.h

1virtual bool GivePickupTo(APawn *PlayerPawn);

ShootThemUp: Pickups/STUBasePickup.cpp

1bool ASTUBasePickup::GivePickupTo(APawn *PlayerPawn)
2{
3    return false;
4}

发生重叠事件并导致用户数据修改时, 认为补给被拾取

屏蔽日志打印

ShootThemUp: Pickups/STUBasePickup.cpp

1// NotifyActorBeginOverlap
2
3const auto Pawn = Cast<APawn>(OtherActor);
4if (GivePickupTo(Pawn))
5{
6    PickupWasTaken();
7}

生命补给和武器补给的初步实现

ShootThemUp: Pickups/STUHealthPickup.h

ShootThemUp: Pickups/STUAmmoPickup.h

1virtual bool GivePickupTo(APawn *PlayerPawn) override;

ShootThemUp: Pickups/STUHealthPickup.cpp

1DEFINE_LOG_CATEGORY_STATIC(LogHealthPickup, All, All);
2
3bool ASTUHealthPickup::GivePickupTo(APawn *PlayerPawn)
4{
5    UE_LOG(LogHealthPickup, Display, TEXT("Health was taken"));
6    return true;
7}

ShootThemUp: Pickups/STUAmmoPickup.cpp

1DEFINE_LOG_CATEGORY_STATIC(LogAmmoPickup, All, All);
2
3bool ASTUAmmoPickup::GivePickupTo(APawn *PlayerPawn)
4{
5    UE_LOG(LogAmmoPickup, Display, TEXT("Ammo was taken"));
6    return true;
7}