【OpenHarmony】鸿蒙开发之 behaviortree

71 阅读2分钟

介绍

behaviortree ,是行为树 javascript 版实现,可以运行 node.js 和浏览器中,本库基于  behaviortree 原库 v2.1.0 版本进行验证。

下载安装

ohpm install behaviortree

使用说明

import { BehaviorTree } from "behaviortree";

示例

1.创建一个简单的任务

import { Task, SUCCESS } from "behaviortree";

const myTask = new Task({
  // (optional) this function is called directly before the run method
  // is called. It allows you to setup things before starting to run
  start: function (blackboard) {
    blackboard.isStarted = true;
  },

  // (optional) this function is called directly after the run method
  // is completed with either this.success() or this.fail(). It allows you to clean up
  // things, after you run the task.
  end: function (blackboard) {
    blackboard.isStarted = false;
  },

  // This is the meat of your task. The run method does everything you want it to do.
  run: function (blackboard) {
    return SUCCESS;
  },
});

方法:

  • start - 在调用运行之前调用。但如果任务在以 this.running()结束后恢复,则不会
  • end - 在调用运行后调用。但如果任务以 this.running()结束,则不会
  • run - 包含你希望任务做的事情

2.创建序列

import { Sequence } from "behaviortree";

const mySequence = new Sequence({
  nodes: [
    // here comes in a list of nodes (Tasks, Sequences or Priorities)
    // as objects or as registered strings
  ],
});

DD一下:欢迎大家关注工粽号<程序猿百晓生>,可以了解到以下知识点。

`欢迎大家关注工粽号<程序猿百晓生>,可以了解到以下知识点学习。`
1.OpenHarmony开发基础
2.OpenHarmony北向开发环境搭建
3.鸿蒙南向开发环境的搭建
4.鸿蒙生态应用开发白皮书V2.0 & V3.0
5.鸿蒙开发面试真题(含参考答案) 
6.TypeScript入门学习手册
7.OpenHarmony 经典面试题(含参考答案)
8.OpenHarmony设备开发入门【最新版】
9.沉浸式剖析OpenHarmony源代码
10.系统定制指南
11.【OpenHarmony】Uboot 驱动加载流程
12.OpenHarmony构建系统--GN与子系统、部件、模块详解
13.ohos开机init启动流程
14.鸿蒙版性能优化指南
.......

3.创建优先级选择器

import { Selector } from "behaviortree";

const mySelector = new Selector({
  nodes: [
    // here comes in a list of nodes (Tasks, Sequences or Priorities)
    // as objects or as registered strings
  ],
});

4.创建随机选择器

import { Random } from "behaviortree";

const mySelector = new Random({
  nodes: [
    // here comes in a list of nodes (Tasks, Sequences or Priorities)
    // as objects or as registered strings
  ],
});

5.创建行为树实例

import { BehaviorTree } from "behaviortree";

var bTree = new BehaviorTree({
  tree: mySelector,
  blackboard: {},
});

6.遍历行为树

bTree.step();

接口说明

方法名接口描述
Sequence创建序列
Selector创建优先级选择器
Random创建随机选择器
BehaviorTree创建行为树实例
step遍历行为树