六一的部落格


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




说明

Homework - Change Weapon Service

  • 和弹药耗尽更换武器性质不同; 使用当时实现的NextWeapon接口
  • NPC攻击时执行: 加入到Attack序列
  • 设置参数, 生成随机数大于该值时不作操作

补充

应该在TickNode最后调用基类TickNode

ShootThemUp: AI/Services/STUFireService.cpp

1// TickNode
2
3Super::TickNode(OwnerComp, NodeMemory, DeltaSeconds);

创建服务类

-
基类 BTService
路径 AI/Services
属性 Public
类名 STUChangeWeaponService

基本函数声明

ShootThemUp: AI/Services/STUChangeWeaponService.h

1// public
2USTUChangeWeaponService();
3
4// protected
5void TickNode(UBehaviorTreeComponent &OwnerComp, uint8 *NodeMemory, float DeltaSeconds);

构造函数实现: 设置服务名称

ShootThemUp: AI/Services/STUChangeWeaponService.cpp

1USTUChangeWeaponService::USTUChangeWeaponService()
2{
3    NodeName = "Change Weapon";
4}

TickNode框架

ShootThemUp: AI/Services/STUChangeWeaponService.cpp

1void USTUChangeWeaponService::TickNode(UBehaviorTreeComponent &OwnerComp, uint8 *NodeMemory, float DeltaSeconds)
2{
3    // ...
4
5    Super::TickNode(OwnerComp, NodeMemory, DeltaSeconds);
6}

添加属性: 更换武器概率

为0时, 不触发该服务

protected

ShootThemUp: AI/Services/STUChangeWeaponService.h

1UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (ClampMin = "0.0", ClampMax = "1.0"))
2float Probability = 0.5f;

实现服务逻辑

ShootThemUp: AI/Services/STUChangeWeaponService.cpp

 1#include "AIController.h"
 2#include "Components/STUWeaponComponent.h"
 3#include "STUUtils.h"
 4
 5// TickNode
 6
 7const auto Controller = OwnerComp.GetAIOwner();
 8if (Controller)
 9{
10    const auto WeaponComponent = STUUtils::GetSTUPlayerComponent<USTUWeaponComponent>(Controller->GetPawn());
11    if (WeaponComponent && Probability > 0 && FMath::FRand() <= Probability)
12    {
13        WeaponComponent->NextWeapon();
14    }
15}

FMath::FRand

返回[0, 1]


测试

  • 为Selector添加STUChangeWeaponService

  • 设置Probability = 0.8, Interval = 1


  • 一切正常


查看

  • 将Selector > STUChangeWeaponService拖动到Attack序列, 设置Interval = 3


  • 勾选发射器和步枪的子弹数 Infinite 选项

    BP_STURifleWeapon BP_STULauncherWeapon

    Details > Default Ammo > Infinite

  • NPC捕获敌人时, 正常随机更换武器进行射击; 触发概率不高

    可以改善的地方: 事件发生时不一定切换成功, 如动画播放中途, 如正在射击


随机更换武器服务



说明

Homework - Change Weapon Service

  • 和弹药耗尽更换武器性质不同; 使用当时实现的NextWeapon接口
  • NPC攻击时执行: 加入到Attack序列
  • 设置参数, 生成随机数大于该值时不作操作

补充

应该在TickNode最后调用基类TickNode

ShootThemUp: AI/Services/STUFireService.cpp

1// TickNode
2
3Super::TickNode(OwnerComp, NodeMemory, DeltaSeconds);

创建服务类

-
基类 BTService
路径 AI/Services
属性 Public
类名 STUChangeWeaponService

基本函数声明

ShootThemUp: AI/Services/STUChangeWeaponService.h

1// public
2USTUChangeWeaponService();
3
4// protected
5void TickNode(UBehaviorTreeComponent &OwnerComp, uint8 *NodeMemory, float DeltaSeconds);

构造函数实现: 设置服务名称

ShootThemUp: AI/Services/STUChangeWeaponService.cpp

1USTUChangeWeaponService::USTUChangeWeaponService()
2{
3    NodeName = "Change Weapon";
4}

TickNode框架

ShootThemUp: AI/Services/STUChangeWeaponService.cpp

1void USTUChangeWeaponService::TickNode(UBehaviorTreeComponent &OwnerComp, uint8 *NodeMemory, float DeltaSeconds)
2{
3    // ...
4
5    Super::TickNode(OwnerComp, NodeMemory, DeltaSeconds);
6}

添加属性: 更换武器概率

为0时, 不触发该服务

protected

ShootThemUp: AI/Services/STUChangeWeaponService.h

1UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (ClampMin = "0.0", ClampMax = "1.0"))
2float Probability = 0.5f;

实现服务逻辑

ShootThemUp: AI/Services/STUChangeWeaponService.cpp

 1#include "AIController.h"
 2#include "Components/STUWeaponComponent.h"
 3#include "STUUtils.h"
 4
 5// TickNode
 6
 7const auto Controller = OwnerComp.GetAIOwner();
 8if (Controller)
 9{
10    const auto WeaponComponent = STUUtils::GetSTUPlayerComponent<USTUWeaponComponent>(Controller->GetPawn());
11    if (WeaponComponent && Probability > 0 && FMath::FRand() <= Probability)
12    {
13        WeaponComponent->NextWeapon();
14    }
15}

FMath::FRand

返回[0, 1]


测试

  • 为Selector添加STUChangeWeaponService

  • 设置Probability = 0.8, Interval = 1


  • 一切正常


查看

  • 将Selector > STUChangeWeaponService拖动到Attack序列, 设置Interval = 3


  • 勾选发射器和步枪的子弹数 Infinite 选项

    BP_STURifleWeapon BP_STULauncherWeapon

    Details > Default Ammo > Infinite

  • NPC捕获敌人时, 正常随机更换武器进行射击; 触发概率不高

    可以改善的地方: 事件发生时不一定切换成功, 如动画播放中途, 如正在射击