六一的部落格


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



Round Time / Rounds Num


说明

  1. 在StartPlay开启回合
  2. 使用定时器更新回合剩余时间, 开启下一回合, 通告游戏结束

修改游戏规则

设置回合数, 为回合指定时长. 目前设定的值是为了快速测试, 回合时长3s-5min

ShootThemUp: STUCoreTypes.h

1UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, meta = (ClampMin = "1", ClampMax = "10"))
2int32 RoundsNum = 4;
3
4UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, meta = (ClampMin = "3", ClampMax = "300"))
5int32 RoundTime = 10; // in seconds

在关卡中添加相关参数和接口

private

ShootThemUp: STUGameModeBase.h


当前回合

1int32 CurrentRound = 1;

当前回合剩余时间

1int32 RoundCountDown = 0;

定时器

1FTimerHandle GameRoundTimerHandle;

接口: 回合开始时调用

1void StartRound();

接口: 定时器回调函数

1void GameTimerUpdate();

实现接口

ShootThemUp: STUGameModeBase.cpp

1DEFINE_LOG_CATEGORY_STATIC(LogSTUGameModeBase, All, All);

实现StartRound

  1. 初始化回合时间
  2. 每秒调用定时器更新剩余时间
1#include "TimerManager.h"
2
3void ASTUGameModeBase::StartRound()
4{
5    RoundCountDown = GameData.RoundTime;
6    GetWorldTimerManager().SetTimer(GameRoundTimerHandle, this, &ASTUGameModeBase::GameTimerUpdate, 1.0f, true);
7}

实现GameTimerUpdate

  1. 显示回合剩余时间, 当前回合 / 总回合
  2. 定时器回调函数可以不知道定时器触发间隔, 通过GetTimerRate获取. 不过GetTimerRate返回float类型变量

    以下代码仅作学习示例
    1const auto TimerRate = GetWorldTimerManager().GetTimerRate(GameRoundTimerHandle);
    2RoundCountDown -= TimerRate;
  3. 回合结束后更新回合数
 1void ASTUGameModeBase::GameTimerUpdate()
 2{
 3    UE_LOG(LogSTUGameModeBase, Display, TEXT("Time: %i / Round: %i/%i"), RoundCountDown, CurrentRound, GameData.RoundsNum);
 4    if (--RoundCountDown == 0)
 5    {
 6        GetWorldTimerManager().ClearTimer(GameRoundTimerHandle);
 7
 8        if (CurrentRound + 1 <= GameData.RoundsNum)
 9        {
10            ++CurrentRound;
11            StartRound();
12        }
13        else
14        {
15            UE_LOG(LogSTUGameModeBase, Display, TEXT("======= GAME OVER ======="));
16        }
17    }
18}

关卡开始时设置相关参数并调用StartRound

1// StartPlay
2
3CurrentRound = 1;
4StartRound();

屏蔽STUBaseWeapon中的日志

ShootThemUp: Weapon/STUBaseWeapon.cpp

1// ChangeClip

查看

BP_STUGameModeBase

设置回合耗时为3s


日志


游戏回合


Round Time / Rounds Num


说明

  1. 在StartPlay开启回合
  2. 使用定时器更新回合剩余时间, 开启下一回合, 通告游戏结束

修改游戏规则

设置回合数, 为回合指定时长. 目前设定的值是为了快速测试, 回合时长3s-5min

ShootThemUp: STUCoreTypes.h

1UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, meta = (ClampMin = "1", ClampMax = "10"))
2int32 RoundsNum = 4;
3
4UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, meta = (ClampMin = "3", ClampMax = "300"))
5int32 RoundTime = 10; // in seconds

在关卡中添加相关参数和接口

private

ShootThemUp: STUGameModeBase.h


当前回合

1int32 CurrentRound = 1;

当前回合剩余时间

1int32 RoundCountDown = 0;

定时器

1FTimerHandle GameRoundTimerHandle;

接口: 回合开始时调用

1void StartRound();

接口: 定时器回调函数

1void GameTimerUpdate();

实现接口

ShootThemUp: STUGameModeBase.cpp

1DEFINE_LOG_CATEGORY_STATIC(LogSTUGameModeBase, All, All);

实现StartRound

  1. 初始化回合时间
  2. 每秒调用定时器更新剩余时间
1#include "TimerManager.h"
2
3void ASTUGameModeBase::StartRound()
4{
5    RoundCountDown = GameData.RoundTime;
6    GetWorldTimerManager().SetTimer(GameRoundTimerHandle, this, &ASTUGameModeBase::GameTimerUpdate, 1.0f, true);
7}

实现GameTimerUpdate

  1. 显示回合剩余时间, 当前回合 / 总回合
  2. 定时器回调函数可以不知道定时器触发间隔, 通过GetTimerRate获取. 不过GetTimerRate返回float类型变量

    以下代码仅作学习示例
    1const auto TimerRate = GetWorldTimerManager().GetTimerRate(GameRoundTimerHandle);
    2RoundCountDown -= TimerRate;
  3. 回合结束后更新回合数
 1void ASTUGameModeBase::GameTimerUpdate()
 2{
 3    UE_LOG(LogSTUGameModeBase, Display, TEXT("Time: %i / Round: %i/%i"), RoundCountDown, CurrentRound, GameData.RoundsNum);
 4    if (--RoundCountDown == 0)
 5    {
 6        GetWorldTimerManager().ClearTimer(GameRoundTimerHandle);
 7
 8        if (CurrentRound + 1 <= GameData.RoundsNum)
 9        {
10            ++CurrentRound;
11            StartRound();
12        }
13        else
14        {
15            UE_LOG(LogSTUGameModeBase, Display, TEXT("======= GAME OVER ======="));
16        }
17    }
18}

关卡开始时设置相关参数并调用StartRound

1// StartPlay
2
3CurrentRound = 1;
4StartRound();

屏蔽STUBaseWeapon中的日志

ShootThemUp: Weapon/STUBaseWeapon.cpp

1// ChangeClip

查看

BP_STUGameModeBase

设置回合耗时为3s


日志